ID:               30891
 User updated by:  brian_bisaillon at rogers dot com
 Reported By:      brian_bisaillon at rogers dot com
 Status:           Open
 Bug Type:         Documentation problem
 Operating System: SuSE Linux 9.1
 PHP Version:      5.0.2
 New Comment:

Derick,

I'm glad it's not a bug. I will update my code to take into account
that a FALSE integer means that the encryption
identifier is invalid.

Thanks guys!


Previous Comments:
------------------------------------------------------------------------

[2004-11-25 22:57:59] [EMAIL PROTECTED]

There is no bug here, it simply returns false when an invalid
encryption parameter is passed:

[EMAIL PROTECTED]:~$ php -r 'var_dump(mcrypt_generic_init("foo", "key",
"1"));'

Warning: mcrypt_generic_init(): supplied argument is not a valid MCrypt
resource in Command line code on line 1
bool(false)

The documentation is slightly wrong:

 The function returns a negative value on error, -3 when the key length
was incorrect, -4 when there was a memory allocation problem and any
other return value is an unknown error.

It should state that returning bool(false) means that the encryption
identifier is invalid.

------------------------------------------------------------------------

[2004-11-25 22:47:50] [EMAIL PROTECTED]

Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try avoid embedding huge scripts into the report.



------------------------------------------------------------------------

[2004-11-25 05:08:10] brian_bisaillon at rogers dot com

My error checking for -3 worked so it's setting a negative integer when
I supply an invalid $encryptionKey. However, the problem is with
returning negative integers for "unknown errors" such as an invalid
MCrypt resource. In those situations, it doesn't return a negative
integer like it's supposed to.

------------------------------------------------------------------------

[2004-11-25 05:00:28] brian_bisaillon at rogers dot com

Description:
------------
./configure --prefix=/opt/php 
--with-apxs2=/opt/apache/bin/apxs \ 
--with-mod_charset --disable-cgi --enable-magic-quotes 
--with-openssl \ 
--with-libxml-dir=/usr/lib --with-zlib --enable-bcmath 
--with-bz2 \ 
--enable-calendar --with-jpeg-dir=/usr/lib 
--with-tiff-dir=/usr/lib \ 
--with-curl --with-curlwrappers --with-db4 --with-cdb 
--with-inifile \ 
--with-flatfile --enable-dbase --enable-dbx --enable-dio 
--with-fam \ 
--enable-filepro --enable-ftp --with-gd 
--with-png-dir=/usr/lib \ 
--with-xpm-dir=/usr/lib --with-ttf 
--with-freetype-dir=/usr/lib \ 
--with-gettext --with-gmp --with-kerberos --with-ldap 
--with-ldap-sasl \ 
--enable-mbstring --with-mcrypt --with-mhash 
--with-mysql=/opt/mysql \ 
--with-mysqli=/opt/mysql/bin/mysql_config --with-ncurses 
--with-unixODBC \ 
--with-pgsql=/opt/postgresql --with-readline 
--enable-shmop --with-snmp \ 
--enable-ucd-snmp-hack --enable-soap --enable-sockets \ 
--enable-sqlite-utf8 --enable-sysvmsg --enable-sysvsem 
--enable-sysvshm \ 
--with-tidy=/usr --enable-wddx --with-xmlrpc --with-xsl 
--enable-yp \ 
--enable-maintainer-zts --enable-memory-limit 
--enable-zend-multibyte \ 
--with-tsrm-pthreads 

Reproduce code:
---------------
            $returnValue =
@mcrypt_generic_init($this->encryptionDescriptor, $encryptionKey,
$this->SetRandomIv());
            if (0 == intval($returnValue) && -3 !== $returnValue && -4
!== $returnValue) {
                throw new Exception("<h1>\n  Initialization
Failed\n</h1>\n<strong>Fatal:</strong> mcrypt_generic_init(): An
unknown error occurred : phpwebtk.cryptography.Crypt.Exception
<strong>");
            } else if (-3 == $returnValue) {
                throw new Exception("<h1>\n  Initialization
Failed\n</h1>\n<strong>Fatal:</strong> mcrypt_generic_init(): The key
length was incorrect : phpwebtk.cryptography.Crypt.Exception
<strong>");
            } else if (-4 == $returnValue) {
                throw new Exception("<h1>\n  Initialization
Failed\n</h1>\n<strong>Fatal:</strong> mcrypt_generic_init(): There was
a memory allocation problem : phpwebtk.cryptography.Crypt.Exception
<strong>");
            } else {
                $ciphertext =
mcrypt_generic($this->encryptionDescriptor, $plaintext);
                mcrypt_generic_deinit($this->encryptionDescriptor);
                $this->CloseModule();
            }

Expected result:
----------------
I expected the $returnValue to be a negative integer since 
I gave a bogus $this->encryptionDescriptor and according 
to the PHP manual, mcrypt_generic_init is supposed to 
return a negative integer on error. 

Actual result:
--------------
$returnValue was null despite the fact that 
$this->encryptionDescriptor was invalid. It showed the 
warning but I want to throw my own custom exception and 
use the @mcrypt_generic_init() syntax to hide the default 
warning. I cannot do that if a negative integer is not 
being returned upon an error. 


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=30891&edit=1

Reply via email to