|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-04-07 15:46 UTC] krtek4+php at gmail dot com
Description: ------------ If you try to apply bin2hex on the result of hex2bin, when the length of the initial data is an odd number, the resulting data are not the same as the original. $ php -v PHP 5.4.1RC1 (cli) (built: Apr 6 2012 13:31:16) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies This is the original Debian package present in sid on the 7th April 2012 Test script: --------------- <?php $data = '12345'; var_dump($data, bin2hex(hex2bin($data))); // outputs : // string(5) "12345" // string(4) "1234" Expected result: ---------------- The output should be the same. (ie 12345 both times) Actual result: -------------- string(5) "12345" string(4) "1234" Patcheshex2bin_padd_odd_new_patch (last revision 2012-04-08 09:18 UTC by theanomaly dot is at gmail dot com)hex2bin_padd_odd (last revision 2012-04-08 04:33 UTC by theanomaly dot is at gmail dot com) hex2bin_pad_odd_length.patch (last revision 2012-04-07 17:02 UTC by krtek4+php at gmail dot com) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 13:00:02 2025 UTC |
@laruence I've replaced the last patch with a better patch because I realized I created a memory leak and that was a poor strategy. I can't understand why there should be any confusion about whether it's an octal value or a hexadecimal value though. Since when should using bin2hex() ever leave us with the expectation that would _ever_ get back an octal value? I might be missing something here, but hex2bin() should always be expecting a hexadecimal value and bin2hex() should always leave us with the expectation of a hexadecimal value. I see nothing wrong with padding the value to an even number otherwise the result is hex2bin() isn't doing what it's supposed to be doing. It makes sense to me that even if the client sends a value of '1' that it's completely expected behavior that '01' and '1' should both be a valid hexadecimal value. To me it just makes no sense to punish the client for forgetting to pad the value by returning false data. At the very least we should be issuing a warning to let the client know they have sent unexpected data and then this can be documented behavior. But why waste time fixing it to issue E_WARNINGs when this patch fixes the issue completely? Besides hex2bin is returning a string. It's not like the user can inadvertently use it as an octal value. var_dump('0123' + '0123'); // int(246) This would be silly not to fix in my opinion. Especially since it's such an easy fix. At least run the patch and let me know which test case you can come up with that would break any of PHP's already existing documented behavior by making this modification?The patch currently in master still leaves a remanent problem. First, var_dump(hex2bin('')) still returns string(0) "". Whether or not this is acceptable is arguable. Since 0x00 is null, but an empty string isn't really null. For example: var_dump(unpack('H*',hex2bin(''))); // will give me string(0) "" Whereas: var_dump(unpack('H*',hex2bin('00'))); // will give me string(2) "" and: var_dump(hex2bin('00')); // will give me string(1) "" # which is what I expect So the warning needs to be amended for an empty string as well as odd-sized hex. Additionally, the function now returns a bool(false), breaking BC. I suggest a second optional argument to return partial binary (potentially broken binary) even on error in the event the user wants to maintain backwards compatibility. I'll be happy to submit another patch for this if there are no objections.