Skip to content

Commit d9000b3

Browse files
authored
tree: replace some unnecessary uses of spprintf (#19354)
1 parent 0596135 commit d9000b3

File tree

6 files changed

+34
-38
lines changed

6 files changed

+34
-38
lines changed

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,10 @@ static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
526526
ExecStatusType status;
527527

528528
switch (ori) {
529-
case PDO_FETCH_ORI_NEXT: spprintf(&ori_str, 0, "NEXT"); break;
530-
case PDO_FETCH_ORI_PRIOR: spprintf(&ori_str, 0, "BACKWARD"); break;
531-
case PDO_FETCH_ORI_FIRST: spprintf(&ori_str, 0, "FIRST"); break;
532-
case PDO_FETCH_ORI_LAST: spprintf(&ori_str, 0, "LAST"); break;
529+
case PDO_FETCH_ORI_NEXT: ori_str = "NEXT"; break;
530+
case PDO_FETCH_ORI_PRIOR: ori_str = "BACKWARD"; break;
531+
case PDO_FETCH_ORI_FIRST: ori_str = "FIRST"; break;
532+
case PDO_FETCH_ORI_LAST: ori_str = "LAST"; break;
533533
case PDO_FETCH_ORI_ABS: spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
534534
case PDO_FETCH_ORI_REL: spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
535535
default:
@@ -542,7 +542,9 @@ static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
542542
}
543543

544544
spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
545-
efree(ori_str);
545+
if (ori == PDO_FETCH_ORI_ABS || ori == PDO_FETCH_ORI_REL) {
546+
efree(ori_str);
547+
}
546548
S->result = PQexec(S->H->server, q);
547549
efree(q);
548550
status = PQresultStatus(S->result);

ext/standard/http_fopen_wrapper.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,9 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
638638

639639
/* protocol version we are speaking */
640640
if (context && (tmpzval = php_stream_context_get_option(context, "http", "protocol_version")) != NULL) {
641-
char *protocol_version;
642-
spprintf(&protocol_version, 0, "%.1F", zval_get_double(tmpzval));
643-
644641
smart_str_appends(&req_buf, " HTTP/");
645-
smart_str_appends(&req_buf, protocol_version);
642+
smart_str_append_printf(&req_buf, "%.1F", zval_get_double(tmpzval));
646643
smart_str_appends(&req_buf, "\r\n");
647-
efree(protocol_version);
648644
} else {
649645
smart_str_appends(&req_buf, " HTTP/1.1\r\n");
650646
}

ext/sysvmsg/sysvmsg.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,12 @@ PHP_FUNCTION(msg_send)
383383
message_len = spprintf(&p, 0, ZEND_LONG_FMT, Z_LVAL_P(message));
384384
break;
385385
case IS_FALSE:
386-
message_len = spprintf(&p, 0, "0");
386+
p = "0";
387+
message_len = 1;
387388
break;
388389
case IS_TRUE:
389-
message_len = spprintf(&p, 0, "1");
390+
p = "1";
391+
message_len = 1;
390392
break;
391393
case IS_DOUBLE:
392394
message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
@@ -400,7 +402,7 @@ PHP_FUNCTION(msg_send)
400402
messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
401403
memcpy(messagebuffer->mtext, p, message_len + 1);
402404

403-
if (Z_TYPE_P(message) != IS_STRING) {
405+
if (Z_TYPE_P(message) == IS_LONG || Z_TYPE_P(message) == IS_DOUBLE) {
404406
efree(p);
405407
}
406408
}

main/main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
10491049
const char *space = "";
10501050
const char *class_name = "";
10511051
const char *function;
1052-
int origin_len;
1052+
size_t origin_len;
10531053
char *origin;
10541054
zend_string *message;
10551055
int is_function = 0;
@@ -1116,9 +1116,10 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
11161116

11171117
/* if we still have memory then format the origin */
11181118
if (is_function) {
1119-
origin_len = (int)spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
1119+
origin_len = spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
11201120
} else {
1121-
origin_len = (int)spprintf(&origin, 0, "%s", function);
1121+
origin_len = strlen(function);
1122+
origin = estrndup(function, origin_len);
11221123
}
11231124

11241125
if (PG(html_errors)) {
@@ -1135,14 +1136,14 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
11351136

11361137
/* no docref given but function is known (the default) */
11371138
if (!docref && is_function) {
1138-
int doclen;
1139+
size_t doclen;
11391140
while (*function == '_') {
11401141
function++;
11411142
}
11421143
if (space[0] == '\0') {
1143-
doclen = (int)spprintf(&docref_buf, 0, "function.%s", function);
1144+
doclen = spprintf(&docref_buf, 0, "function.%s", function);
11441145
} else {
1145-
doclen = (int)spprintf(&docref_buf, 0, "%s.%s", class_name, function);
1146+
doclen = spprintf(&docref_buf, 0, "%s.%s", class_name, function);
11461147
}
11471148
while((p = strchr(docref_buf, '_')) != NULL) {
11481149
*p = '-';

sapi/fpm/fpm/fpm_conf.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,46 +1383,41 @@ static void fpm_conf_cleanup(int which, void *arg) /* {{{ */
13831383

13841384
static void fpm_conf_ini_parser_include(char *inc, void *arg) /* {{{ */
13851385
{
1386-
char *filename;
13871386
int *error = (int *)arg;
13881387
php_glob_t g;
13891388
size_t i;
13901389

13911390
if (!inc || !arg) return;
13921391
if (*error) return; /* We got already an error. Switch to the end. */
1393-
spprintf(&filename, 0, "%s", ini_filename);
1392+
1393+
const char *filename = ini_filename;
13941394

13951395
{
13961396
g.gl_offs = 0;
13971397
if ((i = php_glob(inc, PHP_GLOB_ERR | PHP_GLOB_MARK, NULL, &g)) != 0) {
13981398
#ifdef PHP_GLOB_NOMATCH
13991399
if (i == PHP_GLOB_NOMATCH) {
14001400
zlog(ZLOG_WARNING, "Nothing matches the include pattern '%s' from %s at line %d.", inc, filename, ini_lineno);
1401-
efree(filename);
14021401
return;
14031402
}
14041403
#endif /* PHP_GLOB_NOMATCH */
14051404
zlog(ZLOG_ERROR, "Unable to globalize '%s' (ret=%zd) from %s at line %d.", inc, i, filename, ini_lineno);
14061405
*error = 1;
1407-
efree(filename);
14081406
return;
14091407
}
14101408

14111409
for (i = 0; i < g.gl_pathc; i++) {
1412-
int len = strlen(g.gl_pathv[i]);
1410+
size_t len = strlen(g.gl_pathv[i]);
14131411
if (len < 1) continue;
14141412
if (g.gl_pathv[i][len - 1] == '/') continue; /* don't parse directories */
14151413
if (0 > fpm_conf_load_ini_file(g.gl_pathv[i])) {
14161414
zlog(ZLOG_ERROR, "Unable to include %s from %s at line %d", g.gl_pathv[i], filename, ini_lineno);
14171415
*error = 1;
1418-
efree(filename);
14191416
return;
14201417
}
14211418
}
14221419
php_globfree(&g);
14231420
}
1424-
1425-
efree(filename);
14261421
}
14271422
/* }}} */
14281423

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static inline int phpdbg_call_register(phpdbg_param_t *stack) /* {{{ */
111111
array_init(&params);
112112

113113
while (next) {
114-
char *buffered = NULL;
114+
zend_string *buffered = NULL;
115115

116116
switch (next->type) {
117117
case OP_PARAM:
@@ -125,28 +125,28 @@ static inline int phpdbg_call_register(phpdbg_param_t *stack) /* {{{ */
125125
break;
126126

127127
case METHOD_PARAM:
128-
spprintf(&buffered, 0, "%s::%s", next->method.class, next->method.name);
129-
add_next_index_string(&params, buffered);
128+
buffered = strpprintf(0, "%s::%s", next->method.class, next->method.name);
129+
add_next_index_str(&params, buffered);
130130
break;
131131

132132
case NUMERIC_METHOD_PARAM:
133-
spprintf(&buffered, 0, "%s::%s#"ZEND_LONG_FMT, next->method.class, next->method.name, next->num);
134-
add_next_index_string(&params, buffered);
133+
buffered = strpprintf(0, "%s::%s#"ZEND_LONG_FMT, next->method.class, next->method.name, next->num);
134+
add_next_index_str(&params, buffered);
135135
break;
136136

137137
case NUMERIC_FUNCTION_PARAM:
138-
spprintf(&buffered, 0, "%s#"ZEND_LONG_FMT, next->str, next->num);
139-
add_next_index_string(&params, buffered);
138+
buffered = strpprintf(0, "%s#"ZEND_LONG_FMT, next->str, next->num);
139+
add_next_index_str(&params, buffered);
140140
break;
141141

142142
case FILE_PARAM:
143-
spprintf(&buffered, 0, "%s:"ZEND_ULONG_FMT, next->file.name, next->file.line);
144-
add_next_index_string(&params, buffered);
143+
buffered = strpprintf(0, "%s:"ZEND_ULONG_FMT, next->file.name, next->file.line);
144+
add_next_index_str(&params, buffered);
145145
break;
146146

147147
case NUMERIC_FILE_PARAM:
148-
spprintf(&buffered, 0, "%s:#"ZEND_ULONG_FMT, next->file.name, next->file.line);
149-
add_next_index_string(&params, buffered);
148+
buffered = strpprintf(0, "%s:#"ZEND_ULONG_FMT, next->file.name, next->file.line);
149+
add_next_index_str(&params, buffered);
150150
break;
151151

152152
default: {

0 commit comments

Comments
 (0)