[PHP-DEV] [PATCH] substr() returns false

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default [PHP-DEV] [PATCH] substr() returns false

    --=-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 [url]http://www.perldoc.com/perl5.6/pod/func/substr.html[/url])
    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 <morten-Qg2caEh8@afdelingp.dk>
    [url]http://www.afdelingp.dk/[/url]

    --=-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
    Content-Type: text/plain; charset=us-ascii

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    --=-sQWDp5Weadel0FX8XkrW--
    Morten Poulsen Guest

  2. Similar Questions and Discussions

    1. mail() returns FALSE, no err msg
      Sometimes my mail() function returns FALSE, but there is no error message. Error reporting is set to E_ALL. It seems to be a problem of the bcc:...
    2. #24627 [Bgs]: feof always returns false
      ID: 24627 Updated by: sniper@php.net Reported By: duerra at yahoo dot com Status: Bogus Bug Type: ...
    3. #24627 [Opn->Bgs]: feof always returns false
      ID: 24627 Updated by: elmicha@php.net Reported By: duerra at yahoo dot com -Status: Open +Status: ...
    4. #24627 [Bgs->Opn]: feof always returns false
      ID: 24627 User updated by: duerra at yahoo dot com Reported By: duerra at yahoo dot com -Status: Bogus +Status: ...
    5. #24627 [NEW]: feof always returns false
      From: duerra at yahoo dot com Operating system: XP Pro PHP version: 4.3.3RC1 PHP Bug Type: Filesystem function related Bug...
  3. #2

    Default Re: [PHP-DEV] [PATCH] substr() returns false

    Not sure if/how this should be changed.
    Isn't it a mistake to give a start position at the end or beyond the
    string? In this case, doesn't it make sense to return false?

    Andi

    At 02:39 PM 11/19/2003 +0100, Morten Poulsen wrote:
    >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 [url]http://www.perldoc.com/perl5.6/pod/func/substr.html[/url])
    >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 <morten-Qg2caEh8@afdelingp.dk>
    >[url]http://www.afdelingp.dk/[/url]
    >
    >
    >--
    >PHP Internals - PHP Runtime Development Mailing List
    >To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]

    Andi Gutmans Guest

  4. #3

    Default Re: [PHP-DEV] [PATCH] substr() returns false

    On Wed, 2003-11-19 at 19:30, Andi Gutmans wrote:
    > Isn't it a mistake to give a start position at the end or beyond the
    > string?
    You could easily argue, that beyond the end is an error, but I agree
    with the way Perl allows the start to be at the end. I think of it as
    with (pseudo-) C:

    char str[] = { 'A', 'B', '\0' };
    substr(str, 0) == "AB"
    substr(str, 2) == ""
    substr(str, 3) ERROR
    > In this case, doesn't it make sense to return false?
    If things doesn't make sense, I think there should be some kind of
    warning too (like with array to string cast).

    On Wed, 2003-11-19 at 22:30, Jani Taskinen wrote:
    > IF you change it, it'll break BC.. :)
    True, but if you do it like Perl, it will only break BC for the case
    where start is at the end of string, which is an error now.
    > IMO, it should just give an error in such case, and return FALSE too.
    Yes, with the case where start is beyond the end (which is an error),
    there should be a warning or an error.

    Morten

    --
    Morten Poulsen <morten-Qg2caEh8@afdelingp.dk>
    [url]http://www.afdelingp.dk/[/url]

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]

    Morten Poulsen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139