# [VOTE]strn(case)cmp supporting a negative length as its third paramter

- Thread: https://externals.io/message/54275
- Original archive: https://news-web.php.net/php.internals/54275
- Started by Laruence on 2011-08-01T06:40:34+00:00
- Messages: 33
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/54275.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54275 — Laruence — 2011-08-01T06:40:34+00:00

URL: https://externals.io/message/54275#54275

Hi everyone:

   plz vote for this : "strn(case)cmp supporting a negative length as
its third paramter"

   RFC: https://wiki.php.net/rfc/strncmpnegativelen#vote

   if you have any objection, plz write back.

thanks

-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54291 — Jan Dolecek — 2011-08-02T15:41:20+00:00

URL: https://externals.io/message/54275#54291 · In reply to: #54275

This would make perfect sense with constant strings and constant
length, when using variables this may lead to some magic bugs, which
would be normally reported as E_WARNING. Not sure if it's a good idea
:(

Jan Dolecek
juzna.cz@gmail.com




> Hi everyone:
>
>   plz vote for this : "strn(case)cmp supporting a negative length as
> its third paramter"
>
>   RFC: https://wiki.php.net/rfc/strncmpnegativelen#vote
>
>   if you have any objection, plz write back.
>
> thanks
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/

---

## #54407 — Laruence — 2011-08-05T16:22:12+00:00

URL: https://externals.io/message/54275#54407 · In reply to: #54291

Hi:
 I am not good at english, so I could not explain the idea very clearly
 but I really think it was a good idea,,

 could someone plz make a decision that make this patch applied to svn trunk?

 thanks

2011/8/2 Jan Dolecek <juzna.cz@gmail.com>:
> This would make perfect sense with constant strings and constant
> length, when using variables this may lead to some magic bugs, which
> would be normally reported as E_WARNING. Not sure if it's a good idea
> :(
>
> Jan Dolecek
> juzna.cz@gmail.com
>
>
>
> 
> > […]
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54408 — Laruence — 2011-08-05T16:23:29+00:00

URL: https://externals.io/message/54275#54408 · In reply to: #54407

HI:
   and I don't think this was diffcult to accepted
   it was a little improve, and make a litte change, even no bc break...

thanks

2011/8/6 Laruence <laruence@php.net>:
> Hi:
>  I am not good at english, so I could not explain the idea very clearly
>  but I really think it was a good idea,,
>
>  could someone plz make a decision that make this patch applied to svn trunk?
>
>  thanks
>
> 2011/8/2 Jan Dolecek <juzna.cz@gmail.com>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54568 — Laruence — 2011-08-13T12:17:13+00:00

URL: https://externals.io/message/54275#54568 · In reply to: #54408

Dear all:
    I am going to close strn(case)cmp supporting negative length vote,
since it has been calling for vote near two weeks, and no new feedback
.

    the Voting result is:
     Support :  6  felipe pajoye pierrick gwynne tyrael laruence
     Decline :  3  iliaa rasmus salathe

    it wins 2/3 vote,   so I think this supposed to mean that accept,  right?

    thanks

2011/8/6 Laruence <laruence@php.net>:
> HI:
>   and I don't think this was diffcult to accepted
>   it was a little improve, and make a litte change, even no bc break...
>
> thanks
>
> 2011/8/6 Laruence <laruence@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54574 — Derick Rethans — 2011-08-14T11:24:37+00:00

URL: https://externals.io/message/54275#54574 · In reply to: #54568

> Dear all:
>     I am going to close strn(case)cmp supporting negative length vote,
> since it has been calling for vote near two weeks, and no new feedback
> .
> 
>     the Voting result is:
>      Support :  6  felipe pajoye pierrick gwynne tyrael laruence
>      Decline :  3  iliaa rasmus salathe
> 
>     it wins 2/3 vote,   so I think this supposed to mean that accept,  right?

I voted against too. Also, you started the vote with not even a week 
between RFC announcement and call for voting, so I guess that makes this 
invalid?


Just looked over the RFC, and this whole example is weird:

<?php
var_dump(strncmp("prefix_num", "num", -10));
?>
  
Why does it even find the substring as you can't do "-10" from the end? 
If the number is too high, it should give you a warning.

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

---

## #54576 — Ferenc Kovacs — 2011-08-14T12:56:23+00:00

URL: https://externals.io/message/54275#54576 · In reply to: #54574

> 
>
> > […]
>
> I voted against too. Also, you started the vote with not even a week
> between RFC announcement and call for voting, so I guess that makes this
> invalid?
>
>
> Just looked over the RFC, and this whole example is weird:
>
> <?php
> var_dump(strncmp("prefix_num", "num", -10));
> ?>
>
> Why does it even find the substring as you can't do "-10" from the end?
> If the number is too high, it should give you a warning.

imo the patch is consistent with how substr works:
tyrael@thor:~$ php -d display_errors=1 -d error_reporting=-1 -r 'echo
substr("prefix_num", -100);'
prefix_num

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #54578 — Derick Rethans — 2011-08-14T13:00:00+00:00

URL: https://externals.io/message/54275#54578 · In reply to: #54576

> 
> > […]
> 
> imo the patch is consistent with how substr works:
> tyrael@thor:~$ php -d display_errors=1 -d error_reporting=-1 -r 'echo
> substr("prefix_num", -100);'
> prefix_num

Maybe, but I would classify *that* as a bug as it makes no sense at all.

Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

---

## #54579 — Ferenc Kovacs — 2011-08-14T13:05:01+00:00

URL: https://externals.io/message/54275#54579 · In reply to: #54578

> > […]
>
> Maybe, but I would classify *that* as a bug as it makes no sense at all.
>

I don't mind that, and imho laruence would be happy to change the
patch, after we fix this "bug".
But thats a separate issue, and we should also keep in mind the BC
break for that.
-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #54580 — Laruence — 2011-08-14T13:43:25+00:00

URL: https://externals.io/message/54275#54580 · In reply to: #54578

Hi:
   this just to say that stncasecmp has the same behavior of either
negative or postive length argument.

   why you insist to this point?  as strncmp("aaaa", "bbb",  1000000)
works with no warning,  why negative length need trigger warnings?

thanks

2011/8/14 Derick Rethans <derick@php.net>:
> 
>
> > […]
>
> Maybe, but I would classify *that* as a bug as it makes no sense at all.
>
> Derick
>
> --
> http://derickrethans.nl | http://xdebug.org
> Like Xdebug? Consider a donation: http://xdebug.org/donate.php
> twitter: @derickr and @xdebug



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54582 — Laruence — 2011-08-14T14:02:21+00:00

URL: https://externals.io/message/54275#54582 · In reply to: #54580

Hi:
  I think what I was done is try to describe a image, that we can make
follow codes more simple:

  if (strncmp(substr("num_suffix", 0, -5), "suffix", 5) == 0) {
          echo "they have the same suffix";
  }

  yes,  there is must something not good in my patch,  but I think I
can tweak it as you wish.

  the only thing I want you to accept is : " should this behavior is
more make sense for some occassion?"

thanks

2011/8/14 Laruence <laruence@php.net>:
> Hi:
>   this just to say that stncasecmp has the same behavior of either
> negative or postive length argument.
>
>   why you insist to this point?  as strncmp("aaaa", "bbb",  1000000)
> works with no warning,  why negative length need trigger warnings?
>
> thanks
>
> 2011/8/14 Derick Rethans <derick@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54585 — Laruence — 2011-08-14T14:52:51+00:00

URL: https://externals.io/message/54275#54585 · In reply to: #54582

Hi all internalers:
    Since there comes some new objections,  I think I should open the
RFC voteing again.

    to Derick,  if I extended voting phase for two weeks ,  can we
consider  this voteing valid?

    I think this proposal is not explained clearly before, so I am
going to explain it agian,

    and I am not good at english,  so if I make you confused, sorry in advance.

    -------

    this RFC comes from a Featurn request: https://bugs.php.net/bug.php?id=36944

    when I saw the request,  I totally agree with the reporter,  and I
think this feature can make sense for simplify following situation:

    <?php
           if (strncmp(substr("num_suffix", 0, -5), "suffix", 5) == 0) {
                echo "they have the same suffix";
      }

     and you may wondering does negative length make sense?

     yes,  because substr supports it already:

     <?php
         echo substr("test", 0, -1);

     Now this RFC is  calling for vote again, plz read the RFC, and
vote for it: https://wiki.php.net/rfc/strncmpnegativelen

     if you have any question about this,  plz write back.

thanks.

2011/8/14 Laruence <laruence@php.net>:
> Hi:
>  I think what I was done is try to describe a image, that we can make
> follow codes more simple:
>
>  if (strncmp(substr("num_suffix", 0, -5), "suffix", 5) == 0) {
>          echo "they have the same suffix";
>  }
>
>  yes,  there is must something not good in my patch,  but I think I
> can tweak it as you wish.
>
>  the only thing I want you to accept is : " should this behavior is
> more make sense for some occassion?"
>
> thanks
>
> 2011/8/14 Laruence <laruence@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54586 — Laruence — 2011-08-14T15:01:10+00:00

URL: https://externals.io/message/54275#54586 · In reply to: #54585

Hi:
   sorry, the example is wrong , it should be :

    <?php
          if (strncmp(substr("num_suffix", -5, 5), "suffix", 5) == 0) {
               echo "they have the same suffix";
     }
    ?>

thanks

2011/8/14 Laruence <laruence@php.net>:
> Hi all internalers:
>    Since there comes some new objections,  I think I should open the
> RFC voteing again.
>
>    to Derick,  if I extended voting phase for two weeks ,  can we
> consider  this voteing valid?
>
>    I think this proposal is not explained clearly before, so I am
> going to explain it agian,
>
>    and I am not good at english,  so if I make you confused, sorry in advance.
>
>    -------
>
>    this RFC comes from a Featurn request: https://bugs.php.net/bug.php?id=36944
>
>    when I saw the request,  I totally agree with the reporter,  and I
> think this feature can make sense for simplify following situation:
>
>    <?php
>           if (strncmp(substr("num_suffix", 0, -5), "suffix", 5) == 0) {
>                echo "they have the same suffix";
>      }
>
>     and you may wondering does negative length make sense?
>
>     yes,  because substr supports it already:
>
>     <?php
>         echo substr("test", 0, -1);
>
>     Now this RFC is  calling for vote again, plz read the RFC, and
> vote for it: https://wiki.php.net/rfc/strncmpnegativelen
>
>     if you have any question about this,  plz write back.
>
> thanks.
>
> 2011/8/14 Laruence <laruence@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54588 — Stas Malyshev — 2011-08-14T18:21:14+00:00

URL: https://externals.io/message/54275#54588 · In reply to: #54578

Hi!

> > […]
>
> Maybe, but I would classify *that* as a bug as it makes no sense at all.

It makes some sense, if you take the meaning of argument as "at most 100 
characters". I'm not sure how useful it'd be backwards, but forward 
something like "at most 100 chars" is very useful and warning, on the 
contrary, in such situation would be useless and annoying. Why not make 
backwards case symmetrical?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #54589 — Stas Malyshev — 2011-08-14T18:26:22+00:00

URL: https://externals.io/message/54275#54589 · In reply to: #54588

Hi!

> > […]

Thinking more about it, something like:

if(strncasecmp($filename, ".php", -4) === 0) {
   /* got php file! */
}

seems to me just fine and better than doing the same with substr. I 
would definitely be against the warning in this case.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #54590 — Stas Malyshev — 2011-08-14T18:27:07+00:00

URL: https://externals.io/message/54275#54590 · In reply to: #54568

Hi!


> Dear all:
>      I am going to close strn(case)cmp supporting negative length vote,
> since it has been calling for vote near two weeks, and no new feedback

I think we need some more time to discuss it. Give it a week, then let's 
see.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #54591 — Rasmus Lerdorf — 2011-08-14T18:40:21+00:00

URL: https://externals.io/message/54275#54591 · In reply to: #54589

> Hi!
> 
> > […]
> 
> Thinking more about it, something like:
> 
> if(strncasecmp($filename, ".php", -4) === 0) {
>   /* got php file! */
> }
> 
> seems to me just fine and better than doing the same with substr. I
> would definitely be against the warning in this case.

My main issue with changing strncmp/strncasecmp is that these are
currently exact mappings of the underlying libc functions. For people
familiar with the underlying functionality having a negative length
parameter do something would be confusing and in some cases could mask bugs.

For example, I could imagine people writing code along these lines:

$len = strlen($user_data) - strlen($suffix);
if(!strncmp($user_data, $string, $len)) {
   // do something
}

That is, check to see if the part of a $user_data without a given suffix
matches $string. That should obviously also have a check to make sure
that $user_data is at least as long as $suffix, but if they forget this
check then they get a nice warning telling them that "Length must be
greater than or equal to 0." If we change this to now make negative
lengths work, code like this will do extremely unexpected things.

-Rasmus

---

## #54596 — Stas Malyshev — 2011-08-14T21:03:00+00:00

URL: https://externals.io/message/54275#54596 · In reply to: #54591

Hi!


> My main issue with changing strncmp/strncasecmp is that these are
> currently exact mappings of the underlying libc functions. For people

And why should anybody care? 99% of people using PHP never used a libc 
function and can hardly tell libc from gcc. If we can extend this 
function with useful functionality, nobody cares about what libc does.

> For example, I could imagine people writing code along these lines:
>
> $len = strlen($user_data) - strlen($suffix);
> if(!strncmp($user_data, $string, $len)) {
>     // do something
> }

Warning doesn't fix the bug - and unless you're in 0.0001% of the 
population that actually reads the logs daily and checks every message 
there it would be little to help you. We should have more useful 
functions, not more warnings. Warning won't make this code to work.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #54598 — Rasmus Lerdorf — 2011-08-14T21:37:59+00:00

URL: https://externals.io/message/54275#54598 · In reply to: #54596

> Hi!
> 
> 
> > […]
> 
> And why should anybody care? 99% of people using PHP never used a libc
> function and can hardly tell libc from gcc. If we can extend this
> function with useful functionality, nobody cares about what libc does.
> 
> > […]
> 
> Warning doesn't fix the bug - and unless you're in 0.0001% of the
> population that actually reads the logs daily and checks every message
> there it would be little to help you. We should have more useful
> functions, not more warnings. Warning won't make this code to work.

I agree, however this change would potentially change the return value
of the function. Before it would warn and not match. Even if you never
saw the warning, at least length -1 would not give you a match. Now if
the user data happens to end with the right character we now have a
string match which is not at all what the code was written to do.

-Rasmus

---

## #54600 — Rasmus Lerdorf — 2011-08-14T21:52:48+00:00

URL: https://externals.io/message/54275#54600 · In reply to: #54598

> 
> > […]
> 
> I agree, however this change would potentially change the return value
> of the function. Before it would warn and not match. Even if you never
> saw the warning, at least length -1 would not give you a match. Now if
> the user data happens to end with the right character we now have a
> string match which is not at all what the code was written to do.

Put more succinctly. Subtle BC breaks like this worry me. Any `strncmp()`
call with a computed length where that length may in some cases go
negative will now potentially return a match where it wouldn't before.
This would be very hard to track down. And the reason for introducing
this subtle BC break is so that you can rewrite:

   if (substr("prefix_num", -3) == "num") {
        echo "they have same suffix\n";
   }


into:

   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }

That doesn't seem like a big win to me.

-Rasmus

---

## #54604 — Laruence — 2011-08-15T03:18:33+00:00

URL: https://externals.io/message/54275#54604 · In reply to: #54600

Hi:
   compare to use substr,   strncmp supporting negative lenght can
save a temp variable.

thanks

2011/8/15 Rasmus Lerdorf <rasmus@php.net>:
> 
> > […]
>
> Put more succinctly. Subtle BC breaks like this worry me. Any `strncmp()`
> call with a computed length where that length may in some cases go
> negative will now potentially return a match where it wouldn't before.
> This would be very hard to track down. And the reason for introducing
> this subtle BC break is so that you can rewrite:
>
>   if (substr("prefix_num", -3) == "num") {
>        echo "they have same suffix\n";
>   }
>
>
> into:
>
>   if (strncmp("prefix_num", "num", -3) === 0) {
>        echo "they have same suffix\n";
>   }
>
> That doesn't seem like a big win to me.
>
> -Rasmus
>
> --
> 
> 
>
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54605 — Tjerk Anne Meesters — 2011-08-15T03:48:18+00:00

URL: https://externals.io/message/54275#54605 · In reply to: #54604

Hi,

Doesn't `substr_compare()` accomplish the same thing since 5.1.0?

The only thing I couldn't figure out is how to make it case
insensitive without specifying an explicit value to $length


Best,
  Tjerk


> Hi:
>   compare to use substr,   strncmp supporting negative lenght can
> save a temp variable.
>
> thanks
>
> 2011/8/15 Rasmus Lerdorf <rasmus@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>
> --
> 
> 
>
>



-- 
--
Tjerk

---

## #54606 — Sebastian Krebs — 2011-08-15T04:28:49+00:00

URL: https://externals.io/message/54275#54606 · In reply to: #54600

Am 14.08.2011 23:52, schrieb Rasmus Lerdorf:
> 
> > […]
>
> Put more succinctly. Subtle BC breaks like this worry me. Any `strncmp()`
> call with a computed length where that length may in some cases go
> negative will now potentially return a match where it wouldn't before.
> This would be very hard to track down. And the reason for introducing
> this subtle BC break is so that you can rewrite:
>
>     if (substr("prefix_num", -3) == "num") {
>          echo "they have same suffix\n";
>     }
>
>
> into:
>
>     if (strncmp("prefix_num", "num", -3) === 0) {
>          echo "they have same suffix\n";
>     }
>
> That doesn't seem like a big win to me.

Or just

     if (strncmp(strrev('prefix_num'), 'mun', 3) === 0) {
         // do something
     }


>
> -Rasmus
>


Regards,
Sebastian

---

## #54608 — Laruence — 2011-08-15T05:00:34+00:00

URL: https://externals.io/message/54275#54608 · In reply to: #54605

Hi Tjerk:
   yes,  substr_compare can do it, and it also supports negative
length argument too.

   but I am proposal to improve strn(case)cmp, so they are not  conflict . :)

thanks

2011/8/15 Tjerk Anne Meesters <datibbaw@php.net>:
> Hi,
>
> Doesn't `substr_compare()` accomplish the same thing since 5.1.0?
>
> The only thing I couldn't figure out is how to make it case
> insensitive without specifying an explicit value to $length
>
>
> Best,
>  Tjerk
>
> 
> > […]
>
>
>
> --
> --
> Tjerk
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54612 — Laruence — 2011-08-15T07:05:03+00:00

URL: https://externals.io/message/54275#54612 · In reply to: #54606

Hi:

  I think we are not talking about how to accomplish a function,  we
are talking about how to do it better

  and, in fact, I think strrev is need more cpu time and alloc space,
although they are very tiny :)

thanks

2011/8/15 Sebastian Krebs <sebastian.krebs.berlin@googlemail.com>:
>
>
> Am 14.08.2011 23:52, schrieb Rasmus Lerdorf:
> > […]
>
> Or just
>
>    if (strncmp(strrev('prefix_num'), 'mun', 3) === 0) {
>        // do something
>    }
>
>
> > […]
>
>
> Regards,
> Sebastian
>
> --
> 
> 
>
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

Next page: https://externals.io/message/54275.md?page=2
