Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:5552 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58911 invoked by uid 1010); 19 Nov 2003 13:40:26 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 58877 invoked from network); 19 Nov 2003 13:40:26 -0000 Received: from unknown (HELO mail-in1.inet.tele.dk) (194.182.148.158) by pb1.pair.com with SMTP; 19 Nov 2003 13:40:26 -0000 Received: from mopo.tv2i.dk (pc.tv2.dk [193.88.88.10]) by mail-in1.inet.tele.dk (Postfix) with ESMTP id BFC7666E9; Wed, 19 Nov 2003 14:40:25 +0100 (CET) To: internals@lists.php.net Content-Type: multipart/mixed; boundary="=-sQWDp5Weadel0FX8XkrW" Message-ID: <1069249148.1962.52.camel@mopo.tv2i.dk> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Wed, 19 Nov 2003 14:39:09 +0100 Subject: [PATCH] substr() returns false From: morten-Qg2caEh8@afdelingp.dk (Morten Poulsen) --=-sQWDp5Weadel0FX8XkrW Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi, Even though this is documented, it is strange behaviour: If the from parameter to substr() is at, or past, the end of the input string, the function returns false. The function is documented as string substr ( string string, int start [, int length]) IMHO substr() should either 1) be changed to do like Perl, which returns an error only if start is _beyond_ the end (not _at_): (from http://www.perldoc.com/perl5.6/pod/func/substr.html) my $null = substr $name, 6, 2; # returns '' (no warning) my $oops = substr $name, 7; # returns undef, with warning 2) be changed to return an empty string, if start is either at or beyond the end of the input string. I have attached patches for both solutions. If you don't change it to be like Perl, I think it should be noted in the documentation (as with chop()). If, on the other hand, it _is_ changed to behave like Perl, I think it should be emphasized that you are not guaranteed that the result is a string, because this will cause (eg. has caused us) problems when using the result as part of the $data array to PEAR DB::execute(). Happy hacking, Morten -- Morten Poulsen http://www.afdelingp.dk/ --=-sQWDp5Weadel0FX8XkrW Content-Disposition: attachment; filename=php-4.3.4-substr-1.diff Content-Type: text/plain; name=php-4.3.4-substr-1.diff; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit --- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003 +++ php-4.3.4/ext/standard/string.c Wed Nov 19 14:18:08 2003 @@ -1679,7 +1679,11 @@ } } - if (f >= Z_STRLEN_PP(str)) { + /* if "from" position is beyond the end of the string, emit a warning + * and return false + */ + if (f > Z_STRLEN_PP(str)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second argument has to be less than or equal the length of the first argument"); RETURN_FALSE; } --=-sQWDp5Weadel0FX8XkrW Content-Disposition: attachment; filename=php-4.3.4-substr-2.diff Content-Type: text/plain; name=php-4.3.4-substr-2.diff; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit --- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003 +++ php-4.3.4/ext/standard/string.c Wed Nov 19 12:14:41 2003 @@ -1679,8 +1679,11 @@ } } + /* if "from" position is past the end of the string, return an empty + * string + */ if (f >= Z_STRLEN_PP(str)) { - RETURN_FALSE; + RETURN_STRING("", 1); } if ((f + l) > Z_STRLEN_PP(str)) { --=-sQWDp5Weadel0FX8XkrW--