]> granicus.if.org Git - php/commitdiff
Use strpprintf
authorXinchen Hui <laruence@php.net>
Fri, 9 May 2014 16:43:02 +0000 (00:43 +0800)
committerXinchen Hui <laruence@php.net>
Fri, 9 May 2014 16:43:02 +0000 (00:43 +0800)
ext/pdo/pdo_dbh.c
ext/standard/fsock.c
ext/standard/math.c
ext/standard/uniqid.c

index da39634c79cf8b488d9ca3aa8e7ba9638fee50f3..9f286a5ca679f870f350ffbd784a05da4c0c9a21 100644 (file)
@@ -104,7 +104,7 @@ PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC) /* {{{
        const char *msg = "<<Unknown>>";
        char *supp = NULL;
        long native_code = 0;
-       char *message = NULL;
+       zend_string *message = NULL;
        zval info;
 
        if (dbh == NULL || dbh->error_mode == PDO_ERRMODE_SILENT) {
@@ -141,20 +141,20 @@ PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC) /* {{{
        }
 
        if (supp) {
-               spprintf(&message, 0, "SQLSTATE[%s]: %s: %ld %s", *pdo_err, msg, native_code, supp);
+               message = strpprintf(0, "SQLSTATE[%s]: %s: %ld %s", *pdo_err, msg, native_code, supp);
        } else {
-               spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
+               message = strpprintf(0, "SQLSTATE[%s]: %s", *pdo_err, msg);
        }
 
        if (dbh->error_mode == PDO_ERRMODE_WARNING) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message->val);
        } else if (EG(exception) == NULL) {
                zval ex;
                zend_class_entry *def_ex = php_pdo_get_exception_base(1 TSRMLS_CC), *pdo_ex = php_pdo_get_exception();
 
                object_init_ex(&ex, pdo_ex);
 
-               zend_update_property_string(def_ex, &ex, "message", sizeof("message") - 1, message TSRMLS_CC);
+               zend_update_property_str(def_ex, &ex, "message", sizeof("message") - 1, message TSRMLS_CC);
                zend_update_property_string(def_ex, &ex, "code", sizeof("code") - 1, *pdo_err TSRMLS_CC);
                
                if (!Z_ISUNDEF(info)) {
@@ -169,7 +169,7 @@ PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC) /* {{{
        }
 
        if (message) {
-               efree(message);
+               STR_RELEASE(message);
        }
 
        if (supp) {
index 8e164bab38f929772aae483dfcf5aacd74e81770..5eacc2fe46ed203dc1f1843b85f216fe921c2f99 100644 (file)
@@ -126,6 +126,7 @@ PHP_FUNCTION(fsockopen)
        php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
 /* }}} */
+
 /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
    Open persistent Internet or Unix domain socket connection */
 PHP_FUNCTION(pfsockopen)
index 71a2abc6d385c9b6c225662f9adc6aff9558eaf4..9e98fd35ff7b881f2386bf178156ad31c37bea07 100644 (file)
@@ -1063,11 +1063,11 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
                size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len)
 {
        zend_string *res;
-       char *tmpbuf;
+       zend_string *tmpbuf;
        char *s, *t;  /* source, target */
        char *dp;
        int integral;
-       int tmplen, reslen = 0;
+       int reslen = 0;
        int count = 0;
        int is_negative=0;
 
@@ -1078,28 +1078,26 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
 
        dec = MAX(0, dec);
        d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);
-       tmplen = spprintf(&tmpbuf, 0, "%.*F", dec, d);
+       tmpbuf = strpprintf(0, "%.*F", dec, d);
        if (tmpbuf == NULL) {
                return NULL;
-       } else if (!isdigit((int)tmpbuf[0])) {
-               res = STR_INIT(tmpbuf, tmplen, 0);
-               efree(tmpbuf);
-               return res;
+       } else if (!isdigit((int)tmpbuf->val[0])) {
+               return tmpbuf;
        }
 
        /* find decimal point, if expected */
        if (dec) {
-               dp = strpbrk(tmpbuf, ".,");
+               dp = strpbrk(tmpbuf->val, ".,");
        } else {
                dp = NULL;
        }
 
        /* calculate the length of the return buffer */
        if (dp) {
-               integral = dp - tmpbuf;
+               integral = dp - tmpbuf->val;
        } else {
                /* no decimal point was found */
-               integral = tmplen;
+               integral = tmpbuf->len;
        }
 
        /* allow for thousand separators */
@@ -1123,7 +1121,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
        }
        res = STR_ALLOC(reslen, 0);
 
-       s = tmpbuf + tmplen - 1;
+       s = tmpbuf->val + tmpbuf->len - 1;
        t = res->val + reslen;
        *t-- = '\0';
 
@@ -1156,9 +1154,9 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
 
        /* copy the numbers before the decimal point, adding thousand
         * separator every three digits */
-       while(s >= tmpbuf) {
+       while (s >= tmpbuf->val) {
                *t-- = *s--;
-               if (thousand_sep && (++count%3)==0 && s>=tmpbuf) {
+               if (thousand_sep && (++count%3)==0 && s>=tmpbuf->val) {
                        t -= thousand_sep_len;
                        memcpy(t + 1, thousand_sep, thousand_sep_len);
                }
@@ -1170,7 +1168,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
        }
 
        res->len = reslen;
-       efree(tmpbuf);
+       STR_RELEASE(tmpbuf);
        return res;
 }
 
index 610fb2809c100546c255d586de59062a490c2aef..ddc87f51a8fbbe506cc024c702082100f3dacd7a 100644 (file)
@@ -49,7 +49,7 @@ PHP_FUNCTION(uniqid)
 #else
        zend_bool more_entropy = 0;
 #endif
-       char *uniqid;
+       zend_string *uniqid;
        int sec, usec, prefix_len = 0;
        struct timeval tv;
 
@@ -76,14 +76,12 @@ PHP_FUNCTION(uniqid)
         * digits for usecs.
         */
        if (more_entropy) {
-               spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
+               uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
        } else {
-               spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
+               uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
        }
 
-       // TODO: avoid reallocation ???
-       RETVAL_STRING(uniqid);
-       efree(uniqid);
+       RETURN_STR(uniqid);
 }
 #endif
 /* }}} */