From:             ikindred at cox dot net
Operating system: FreeBSD 4.9
PHP version:      4.3.4
PHP Bug Type:     Documentation problem
Bug description:  strncmp using the length of the shorter string

Description:
------------
http://www.php.net/manual/en/function.strncmp.php says:

"If any of the strings is shorter than len, then the length of that string
will be used for the comparison."

strncmp() does not behave in this manner.  Not in PHP.  Not in C.

On reflection, I guess that PHP cannot just call the C library strncmp
function, as C strings are terminated by \x00 while PHP strings are not so
terminated and can therefore contain one or more \x00's.  So, maybe, just
maybe, this is not a documentation problem, but an actual PHP bug.

There have been many times when I wished that len would fallback to the
length of the shorter string... so perhaps this is not a documentation
bug, but rather PHP bug????

For example, this usually happens when I am writing something like:

// note the tedious to write, performance degrading call to strlen
("staticPrefix")
if (strncmp ($dynamicString, "staticPrefix", strlen ("staticPrefix")) ==
0)
{
  print "dynamicString starts with staticPrefix\n";
}
else
{
  print "dynmaicString does not start with staticPrefix\n";
}

If you decide this is a PHP bug (and not a documentation bug), then be
careful, because "fixing" PHP will cause strncmp to behave significantly
differently from its C language namesake and may break existing code.

One solution might be to make $len optional.  When $len is omitted, then
the length of the shorter string is used.  This should not break code and
is also not flagrantly inconsistent with the C language strncmp.  At
present (PHP 4.3.4), omitting $len causes "Warning: Wrong parameter count
for strncmp" and NULL is returned.

But even this has a major problem - I was wrong - it will break code: if
either string (even/especially the dynamic one) is null or "", then
strncmp will say the strings are equal!!!  This is not what I/we want.

You can remedy this problem by only falling back to the length of the
second string, or the first, as long as you are consistent.  But I'd go
with the second - that will look prettier if you have a whole bunch of
strncmps one line after another with the same $dynamicString but different
"staticPrefixes" of different lengths.

Have fun deciding what to do!  strncmp ($dynamic, "static") would
definitely be useful, but it is assymetrical and un-C-like.  But it would
be useful.  I'd say, go for it!  The documentation will need to be changed
anyways, so I'll leave this bug here as a documentation bug.  If you agree
that it's worth changing strncmp as I have described, once you fixed the
docs, reclassify/forward this bug on.

Thanks!

Reproduce code:
---------------
<?php

print "a  aa: ". strncmp ("a" , "aa", 100). "\n";
print "aa aa: ". strncmp ("aa", "aa", 100). "\n";
print "aa a : ". strncmp ("aa", "a" , 100). "\n";

?>


Expected result:
----------------
If strncmp behaved as described, I would expect the following (as len
would "fallback" to 1 in the first and third cases):

a  aa: 0
aa aa: 0
aa a : 0


Actual result:
--------------
The actual result:

a  aa: -1
aa aa: 0
aa a : 1

(This is what strncmp does in C.)

-- 
Edit bug report at http://bugs.php.net/?id=26940&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=26940&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=26940&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=26940&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=26940&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=26940&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=26940&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=26940&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=26940&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=26940&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=26940&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=26940&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=26940&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26940&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=26940&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=26940&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=26940&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=26940&r=float

Reply via email to