Re: [PHP4BETA] cvs: /php4/ext/standard string.c php3_string.h
| From: | Jakub Skopal | Date: | Sun, 05 Sep 1999 11:06:27 +0000 |
| Subject: | Re: [PHP4BETA] cvs: /php4/ext/standard string.c php3_string.h | ||
| References: | 1 2 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-4109@lists.php.net to get a copy of this message | ||
Hi,
05.09.1999 v 12:38:20 (+0200), Sascha Schumann napsal:
> > @@ -1217,13 +1217,18 @@
> > Escape single quote, double quotes and backslash characters in a string with
> > backslashes */
> > PHP_FUNCTION(addslashes)
>
> Don't forget to change the prototype.
ok.
> > PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free)
> > {
> > /* maximum string length, worst case situation */
> > - char *new_str = (char *) emalloc((length?length:(length=strlen(str)))*2+1);
> > + char *new_str;
> > char *source,*target;
> > char *end;
> > char c;
> > + int newlen;
> > PLS_FETCH();
> > +
> > + if (!length) {
> > + length = strlen(str);
> > + }
>
> I disagree with this change. php_addslashes is reserved for
> binary strings where you don't need to have a NUL at the end.
the length is calculated from strlen() only when length=0 is
passed and it is the VERY SAME behaviour as the old
php_addslashes worked!!! (see that
emalloc((length?length:(length=strlen(str))).... stuff (!!))
> In the case a non-NUL terminated string has a length of 0,
> php_addslashes will segfault. Please revert this change.
should I revert it or just make it work when length=0?
> > +
> > + newlen = length+1;
> > + for (source=str,end=source+length; (c = *source) || source<end; source++) {
> > + switch(c) {
> > + case '\0': newlen+=3; break;
> > + case '\'':
> > + case '\"':
> > + case '\\': newlen++; break;
> > + }
> > + }
>
> You can rereduce the complexity of O(2n) to O(n) again by
> simply remalloc'ing the string at the end and thus avoiding
> wasting any memory.
ok. I'll do that.
> > + new_str = (char *)emalloc(newlen);
> > +
> > for (source=str,end=source+length,target=new_str; (c = *source) || source<end;
> > source++) {
> > switch(c) {
> > case '\0':
> > - *target++ = '\\';
> > + *target++ = '\\'; // what if somebody escapes
> > "foo\0"."12bar"?
> > + *target++ = '0'; // we have to add \000 instead of just \0.
> > + *target++ = '0';
> > *target++ = '0';
>
> I don't understand that change. "\000" is NUL and "0" twice.
> Does that make sense? The old behaviour has worked for
> everyone for years.
"\000" isn't null and 00 twice, it's NUL alone.
imagine, somebody has string "foo\0"."12bar", (66 6f 6f 00
31 32 62 61 72 in hex) and he addslashes(), the old version
would rewrite it to "foo\012bar", which isn't right, since
after stripslash()ing, it's (66 6f 6f 0a 62 61 72). I'll
rewrite it so it adds all three 0's only when a valid octal
digit follows...
Jakub
--
Jakub "RiffRaff" Skopal
mailto:j@kubs.cz