]> granicus.if.org Git - php/commitdiff
Refactor php_addslashes using zend_string
authorXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 04:07:09 +0000 (12:07 +0800)
committerXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 04:07:09 +0000 (12:07 +0800)
ext/pcre/php_pcre.c
ext/phar/func_interceptors.c
ext/standard/php_string.h
ext/standard/streamsfuncs.c
ext/standard/string.c

index 7a74082b46bccf2bfaec9af0ba9e560973f07b4d..d3bdf87bcab53dda29d663db0aab05d4772413a6 100644 (file)
@@ -881,14 +881,13 @@ static int preg_do_eval(char *eval_str, int eval_str_len, char *subject,
        zval             retval;                        /* Return value from evaluation */
        char            *eval_str_end,          /* End of eval string */
                                *match,                         /* Current match for a backref */
-                               *esc_match,                     /* Quote-escaped match */
                                *walk,                          /* Used to walk the code string */
                                *segment,                       /* Start of segment to append while walking */
                                 walk_last;                     /* Last walked character */
        int                      match_len;                     /* Length of the match */
-       int                      esc_match_len;         /* Length of the quote-escaped match */
        int                      result_len;            /* Length of the result of the evaluation */
        int                      backref;                       /* Current backref */
+       zend_string *esc_match;                 /* Quote-escaped match */
        char        *compiled_string_description;
        smart_str    code = {0};
        
@@ -914,22 +913,19 @@ static int preg_do_eval(char *eval_str, int eval_str_len, char *subject,
                                        match = subject + offsets[backref<<1];
                                        match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
                                        if (match_len) {
-                                               esc_match = php_addslashes(match, match_len, &esc_match_len, 0 TSRMLS_CC);
+                                               esc_match = php_addslashes(match, match_len, 0 TSRMLS_CC);
                                        } else {
-                                               esc_match = match;
-                                               esc_match_len = 0;
+                                               esc_match = STR_INIT(match, match_len, 0);
                                        }
                                } else {
-                                       esc_match = "";
-                                       esc_match_len = 0;
+                                       esc_match = STR_EMPTY_ALLOC();
                                }
-                               smart_str_appendl(&code, esc_match, esc_match_len);
+                               smart_str_appendl(&code, esc_match->val, esc_match->len);
 
                                segment = walk;
 
                                /* Clean up and reassign */
-                               if (esc_match_len)
-                                       efree(esc_match);
+                               STR_RELEASE(esc_match);
                                continue;
                        }
                }
index c0b03e5838b36a95a06c5f4abd4f795a0fc662ea..5080ddf7c802edbad03eedf76732015440797d41 100644 (file)
@@ -203,14 +203,8 @@ phar_it:
 
                        /* uses mmap if possible */
                        if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {
-#if PHP_API_VERSION < 20100412
-                               if (PG(magic_quotes_runtime)) {
-                                       int newlen;
-                                       contents = php_addslashes(contents, len, &newlen, 1 TSRMLS_CC); /* 1 = free source string */
-                                       len = newlen;
-                               }
-#endif
                                RETVAL_STRINGL(contents, len, 0);
+                               efree(contents);
                        } else if (len == 0) {
                                RETVAL_EMPTY_STRING();
                        } else {
index 2076eff272d77e342f96ffeeb3c32a7a009e486f..0f4240ea57dd0a134a2812e668b1d6717eecb236 100644 (file)
@@ -121,7 +121,7 @@ PHPAPI struct lconv *localeconv_r(struct lconv *out);
 PHPAPI char *php_strtoupper(char *s, size_t len);
 PHPAPI char *php_strtolower(char *s, size_t len);
 PHPAPI char *php_strtr(char *str, int len, char *str_from, char *str_to, int trlen);
-PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free TSRMLS_DC);
+PHPAPI zend_string *php_addslashes(char *str, int length, int should_free TSRMLS_DC);
 PHPAPI zend_string *php_addcslashes(const char *str, int length, int freeit, char *what, int wlength TSRMLS_DC);
 PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC);
 PHPAPI void php_stripcslashes(char *str, int *len);
index 68567ea0d521138647592c21e9697bfb29111328..b5608f450d2e21911e849348299aea70248b4513 100644 (file)
@@ -136,10 +136,10 @@ PHP_FUNCTION(stream_socket_client)
 
        if (stream == NULL) {
                /* host might contain binary characters */
-               char *quoted_host = php_addslashes(host, host_len, NULL, 0 TSRMLS_CC);
+               zend_string *quoted_host = php_addslashes(host, host_len, 0 TSRMLS_CC);
 
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host, errstr == NULL ? "Unknown error" : errstr);
-               efree(quoted_host);
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host->val, errstr == NULL ? "Unknown error" : errstr);
+               STR_RELEASE(quoted_host);
        }
 
        if (hashkey) {
index f16e38921892530d2a680fd5c43ccd6a06e31721..90391ab14edc131948b200310f99c7abef46ce57 100644 (file)
@@ -922,12 +922,12 @@ PHP_FUNCTION(wordwrap)
                /* Multiple character line break or forced cut */
                if (linelength > 0) {
                        chk = (int)(textlen/linelength + 1);
-                       newtext = STR_ALLOC(chk * breakcharlen + textlen + 1, 0);
+                       newtext = STR_ALLOC(chk * breakcharlen + textlen, 0);
                        alloced = textlen + chk * breakcharlen + 1;
                } else {
                        chk = textlen;
                        alloced = textlen * (breakcharlen + 1) + 1;
-                       newtext = STR_ALLOC(textlen * (breakcharlen + 1) + 1, 0);
+                       newtext = STR_ALLOC(textlen * (breakcharlen + 1), 0);
                }
 
                /* now keep track of the actual new text length */
@@ -3305,8 +3305,8 @@ PHP_FUNCTION(addcslashes)
    Escapes single quote, double quotes and backslash characters in a string with backslashes */
 PHP_FUNCTION(addslashes)
 {
-       char *str, *new_str;
-       int  str_len, new_len;
+       char *str;
+       int  str_len;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
                return;
@@ -3316,13 +3316,7 @@ PHP_FUNCTION(addslashes)
                RETURN_EMPTY_STRING();
        }
 
-//???  RETURN_STRING(php_addslashes(str,
-//???                               str_len,
-//???                               &Z_STRLEN_P(return_value), 0
-//???                               TSRMLS_CC), 0);
-       new_str = php_addslashes(str, str_len, &new_len, 0 TSRMLS_CC);
-       RETVAL_STRINGL(new_str, new_len);
-       efree(new_str);
+       RETURN_STR(php_addslashes(str, str_len, 0 TSRMLS_CC));
 }
 /* }}} */
 
@@ -3449,7 +3443,7 @@ PHPAPI zend_string *php_addcslashes(const char *str, int length, int should_free
        char *end;
        char c;
        int  newlen;
-       zend_string *new_str = STR_ALLOC(4 * (length? length : (length = strlen(str))) + 1, 0);
+       zend_string *new_str = STR_ALLOC(4 * (length? length : (length = strlen(str))), 0);
 
        if (!wlength) {
                wlength = strlen(what);
@@ -3492,25 +3486,21 @@ PHPAPI zend_string *php_addcslashes(const char *str, int length, int should_free
 
 /* {{{ php_addslashes
  */
-PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free TSRMLS_DC)
+PHPAPI zend_string *php_addslashes(char *str, int length, int should_free TSRMLS_DC)
 {
        /* maximum string length, worst case situation */
-       char *new_str;
        char *source, *target;
        char *end;
-       int local_new_length;
+       zend_string *new_str;
 
-       if (!new_length) {
-               new_length = &local_new_length;
-       }
        if (!str) {
-               *new_length = 0;
-               return str;
+               return STR_EMPTY_ALLOC();
        }
-       new_str = (char *) safe_emalloc(2, (length ? length : (length = strlen(str))), 1);
+
+       new_str = STR_ALLOC(2 * (length ? length : (length = strlen(str))), 0);
        source = str;
        end = source + length;
-       target = new_str;
+       target = new_str->val;
 
        while (source < end) {
                switch (*source) {
@@ -3532,11 +3522,11 @@ PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_f
        }
 
        *target = 0;
-       *new_length = target - new_str;
        if (should_free) {
 //???          STR_FREE(str);
        }
-       new_str = (char *) erealloc(new_str, *new_length + 1);
+       new_str = STR_REALLOC(new_str, target - new_str->val, 0);
+
        return new_str;
 }
 /* }}} */