php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30891 mcrypt_generic_init() not returning negative integer on error
Submitted: 2004-11-25 05:00 UTC Modified: 2010-12-20 11:47 UTC
From: brian_bisaillon at rogers dot com Assigned: vrana (profile)
Status: Closed Package: mcrypt related
PHP Version: 5.0.2 OS: SuSE Linux 9.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: brian_bisaillon at rogers dot com
New email:
PHP Version: OS:

 

 [2004-11-25 05:00 UTC] 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. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-25 05:08 UTC] 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 22:47 UTC] tony2001@php.net
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 22:57 UTC] derick@php.net
There is no bug here, it simply returns false when an invalid encryption parameter is passed:

derick@kossu:~$ 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-26 15:16 UTC] brian_bisaillon at rogers dot com
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!
 [2004-11-26 15:46 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"FALSE is returned if incorrect parameters were passed."
 [2010-12-20 11:47 UTC] jani@php.net
-Package: Tidy +Package: mcrypt related -Assigned To: +Assigned To: vrana
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 23:01:30 2024 UTC