php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81546 md2 parameter 2 expects bool but treats truthy as true
Submitted: 2021-10-20 10:22 UTC Modified: 2021-10-20 10:25 UTC
From: devstemail at gmail dot com Assigned:
Status: Not a bug Package: hash related
PHP Version: 8.1.0RC4 OS: Centos 8
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: devstemail at gmail dot com
New email:
PHP Version: OS:

 

 [2021-10-20 10:22 UTC] devstemail at gmail dot com
Description:
------------
$binary treats truthy values (e.g. non-empty string) as true

```
$binary = 'hello';
echo md5( 'abc', $binary );
```

outputs a binary md5, instead of a hex md5, even though we didn't pass true
It should only output a binary md5 if `$binary === true`, not if `$binary == true`

As the documentation states:
>If the optional binary is set to **true**, then the md5 digest is instead returned in raw binary format with a length of 16.

# Solution
Basically what we need here is 2 things:
1) a notice, like we already have for the 1st arg:
`md5() expects parameter 2 to be bool, string given`

2) strict checking `$binary === true`, not `$binary == true`


Test script:
---------------
$binary = 'hello';
echo md5( 'abc', $binary );

Expected result:
----------------
1) a notice, like we already have for the 1st arg:
`md5() expects parameter 2 to be bool, string given`

2) strict checking `$binary === true`, not `$binary == true`
so we would get a hex md5 not a binary in this example

Actual result:
--------------
binary md5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-20 10:25 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-10-20 10:25 UTC] nikic@php.net
md5() uses standard bool parameter semantics. Use declare(strict_types=1) for strict type checks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC