Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54600 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42000 invoked from network); 14 Aug 2011 21:52:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2011 21:52:54 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.210.172 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.210.172 mail-iy0-f172.google.com Received: from [209.85.210.172] ([209.85.210.172:65453] helo=mail-iy0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/F5-05730-5B3484E4 for ; Sun, 14 Aug 2011 17:52:53 -0400 Received: by iye7 with SMTP id 7so7278347iye.31 for ; Sun, 14 Aug 2011 14:52:50 -0700 (PDT) Received: by 10.231.52.16 with SMTP id f16mr3808595ibg.31.1313358770403; Sun, 14 Aug 2011 14:52:50 -0700 (PDT) Received: from [192.168.200.5] (c-50-131-46-20.hsd1.ca.comcast.net [50.131.46.20]) by mx.google.com with ESMTPS id g21sm3418012ibl.7.2011.08.14.14.52.49 (version=SSLv3 cipher=OTHER); Sun, 14 Aug 2011 14:52:49 -0700 (PDT) Sender: Rasmus Lerdorf Message-ID: <4E4843B0.70605@php.net> Date: Sun, 14 Aug 2011 14:52:48 -0700 Organization: PHP Development Team User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20110627 Thunderbird/5.0 MIME-Version: 1.0 To: Stas Malyshev CC: Derick Rethans , PHP Internals References: <4E48121A.3090007@sugarcrm.com> <4E48134E.4030708@sugarcrm.com> <4E481695.6040900@php.net> <4E483804.7070009@sugarcrm.com> <4E484037.2080107@php.net> In-Reply-To: <4E484037.2080107@php.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [VOTE]strn(case)cmp supporting a negative length as its third paramter From: rasmus@php.net (Rasmus Lerdorf) On 08/14/2011 02:37 PM, Rasmus Lerdorf wrote: > On 08/14/2011 02:03 PM, Stas Malyshev wrote: >> Hi! >> >> On 8/14/11 11:40 AM, Rasmus Lerdorf wrote: >>> 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. > > 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