#47752 [Com]: FILTER_VALIDATE_INT doesn't allow "+0" and "-0"

From: Date: Tue, 24 Mar 2009 15:03:53 +0000
Subject: #47752 [Com]: FILTER_VALIDATE_INT doesn't allow "+0" and "-0"
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-135055@lists.php.net to get a copy of this message
ID: 47752 Comment by: peter at lvp-media dot com Reported By: for-bugs at hnw dot jp Status: Feedback Bug Type: Filter related Operating System: * PHP Version: 5.2.9 Assigned To: pajoye New Comment: Hereby a simple bug47752.phpt file. Tested it on a patched- an an unpatched installation, results as expected. Strictly speaking +0 and -0 are valid integers, even though neither of them is positive or negative. Peter Beverloo --TEST-- Bug #47752 (FILTER_VALIDATE_INT doesn't allow "+0" and "-0") --FILE-- <?php $positive = filter_var ('+0', FILTER_VALIDATE_INT); $negative = filter_var ('-0', FILTER_VALIDATE_INT); $zero = filter_var ('0', FILTER_VALIDATE_INT); $octal = filter_var ('-0123', FILTER_VALIDATE_INT); var_dump ($positive); var_dump ($negative); var_dump ($zero); var_dump ($octal); ?> --EXPECT-- int(0) int(0) int(0) bool(false) Previous Comments: ------------------------------------------------------------------------ [2009-03-24 12:44:19] pajoye@php.net As far as I remember it is on purpose. 0 cannot be negative or positive. But if we agree on allowing them again, no problem here. Can you please provide tests as well (phpt)? ------------------------------------------------------------------------ [2009-03-24 11:50:38] php at lvp-media dot com False, this only allows +0 and -0 by checking the lenght of the string, it verifies that the null is the only character in it. ------------------------------------------------------------------------ [2009-03-24 11:49:06] derick@php.net Right, so this patch is not correct as it would allow octal numbers. ------------------------------------------------------------------------ [2009-03-24 11:46:57] php at lvp-media dot com Probably the easiest fix for this would be to change line 88 of logical_filters.c to allow the character "0" as well, seeing values like -0012 and 0012 get returned as false now as well. Con of this is quite obvious as well however, as numbers prefixed by 0 often get interpreted as octal numbers. A better fix would be to add a check for it; --- logical_filters.bak 2009-03-24 12:43:23.000000000 +0100 +++ logical_filters.c 2009-03-24 12:45:09.000000000 +0100 @@ -84,6 +84,12 @@ break; } + /* allow +0 and -0 */ + if ((str + 1) == end && *str == '0') { + *ret = 0; + return 1; + } + /* must start with 1..9*/ if (str < end && *str >= '1' && *str <= '9') { ctx_value = ((*(str++)) - '0'); ------------------------------------------------------------------------ [2009-03-23 05:40:58] for-bugs at hnw dot jp Description: ------------ FILTER_VALIDATE_INT doesn't allow "+0" and "-0", while "0", "+1", and "-1" is valid. Reproduce code: --------------- <?php var_dump(intval("+0")); var_dump(filter_var("+0", FILTER_VALIDATE_INT)); var_dump(intval("-0")); var_dump(filter_var("-0", FILTER_VALIDATE_INT)); Expected result: ---------------- int(0) int(0) int(0) int(0) Actual result: -------------- int(0) bool(false) int(0) bool(false) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47752&edit=1

« previous php.bugs (#135055) next »