Skip to content

Commit 98c6ee4

Browse files
Avoid using unsafe sprintf()
1 parent b7d6618 commit 98c6ee4

File tree

3 files changed

+22
-42
lines changed

3 files changed

+22
-42
lines changed

Zend/zend_alloc.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,24 +379,6 @@ static const uint32_t bin_pages[] = {
379379
ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y)
380380
};
381381

382-
#if ZEND_DEBUG
383-
ZEND_COLD void zend_debug_alloc_output(char *format, ...)
384-
{
385-
char output_buf[256];
386-
va_list args;
387-
388-
va_start(args, format);
389-
vsprintf(output_buf, format, args);
390-
va_end(args);
391-
392-
#ifdef ZEND_WIN32
393-
OutputDebugString(output_buf);
394-
#else
395-
fprintf(stderr, "%s", output_buf);
396-
#endif
397-
}
398-
#endif
399-
400382
static ZEND_COLD ZEND_NORETURN void zend_mm_panic(const char *message)
401383
{
402384
fprintf(stderr, "%s\n", message);

ext/pdo_firebird/firebird_statement.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zv
8787
struct tm t;
8888
ISC_TIME time;
8989
char timeBuf[80] = {0};
90-
char timeTzBuf[124] = {0};
9190
if (fb_decode_time_tz(S->H->isc_status, timeTz, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
9291
return 1;
9392
}
@@ -100,8 +99,8 @@ static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zv
10099
return 1;
101100
}
102101

103-
size_t time_tz_len = sprintf(timeTzBuf, "%s %s", timeBuf, timeZoneBuffer);
104-
ZVAL_STRINGL(result, timeTzBuf, time_tz_len);
102+
zend_string *time_tz_str = zend_strpprintf(0, "%s %s", timeBuf, timeZoneBuffer);
103+
ZVAL_NEW_STR(result, time_tz_str);
105104
return 0;
106105
}
107106

@@ -115,7 +114,6 @@ static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ*
115114
struct tm t;
116115
ISC_TIMESTAMP ts;
117116
char timestampBuf[80] = {0};
118-
char timestampTzBuf[124] = {0};
119117
if (fb_decode_timestamp_tz(S->H->isc_status, timestampTz, &year, &month, &day, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
120118
return 1;
121119
}
@@ -130,8 +128,8 @@ static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ*
130128
return 1;
131129
}
132130

133-
size_t timestamp_tz_len = sprintf(timestampTzBuf, "%s %s", timestampBuf, timeZoneBuffer);
134-
ZVAL_STRINGL(result, timestampTzBuf, timestamp_tz_len);
131+
zend_string *timestamp_tz_str = zend_strpprintf(0, "%s %s", timestampBuf, timeZoneBuffer);
132+
ZVAL_NEW_STR(result, timestamp_tz_str);
135133
return 0;
136134
}
137135

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "fpm_env.h"
2828
#include "fpm_cleanup.h"
2929
#include "fpm_scoreboard.h"
30+
#include "zend_smart_string.h"
3031

3132
struct listening_socket_s {
3233
int refcount;
@@ -60,38 +61,36 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
6061
unsigned socket_set[FPM_ENV_SOCKET_SET_MAX];
6162
unsigned socket_set_buf = 0;
6263
char envname[32];
63-
char *env_value = 0;
64-
int p = 0;
64+
smart_string env_str = {0};
6565
struct listening_socket_s *ls = sockets_list.data;
6666

6767
for (i = 0; i < sockets_list.used; i++, ls++) {
6868
if (which != FPM_CLEANUP_PARENT_EXEC) {
6969
close(ls->sock);
7070
} else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
7171
char fd[32];
72-
char *tmpenv_value;
7372
snprintf(fd, sizeof(fd), "%d", ls->sock);
7473

7574
socket_set_buf = (i % FPM_ENV_SOCKET_SET_SIZE == 0 && i) ? 1 : 0;
76-
tmpenv_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + socket_set_buf + 1);
77-
if (!tmpenv_value) {
78-
zlog(ZLOG_SYSERROR, "failure to inherit data on parent exec for socket `%s` due to memory allocation failure", ls->key);
79-
free(ls->key);
80-
break;
81-
}
82-
83-
env_value = tmpenv_value;
8475

8576
if (i % FPM_ENV_SOCKET_SET_SIZE == 0) {
86-
socket_set[socket_set_count] = p + socket_set_buf;
77+
socket_set[socket_set_count] = env_str.len + socket_set_buf;
8778
socket_set_count++;
8879
if (i) {
89-
*(env_value + p + 1) = 0;
80+
smart_string_appendc(&env_str, '\0');
9081
}
9182
}
9283

93-
p += sprintf(env_value + p + socket_set_buf, "%s%s=%s", (p && !socket_set_buf) ? "," : "", ls->key, fd);
94-
p += socket_set_buf;
84+
if (env_str.len && !socket_set_buf) {
85+
smart_string_appendc(&env_str, ',');
86+
}
87+
smart_string_appends(&env_str, ls->key);
88+
smart_string_appendc(&env_str, '=');
89+
smart_string_appends(&env_str, fd);
90+
91+
if (socket_set_buf) {
92+
smart_string_appendc(&env_str, '\0');
93+
}
9594
}
9695

9796
if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
@@ -102,14 +101,15 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
102101
free(ls->key);
103102
}
104103

105-
if (env_value) {
104+
if (env_str.c) {
105+
smart_string_0(&env_str);
106106
for (i = 0; i < socket_set_count; i++) {
107107
fpm_sockets_get_env_name(envname, sizeof(envname), i);
108-
setenv(envname, env_value + socket_set[i], 1);
108+
setenv(envname, env_str.c + socket_set[i], 1);
109109
}
110110
fpm_sockets_get_env_name(envname, sizeof(envname), socket_set_count);
111111
unsetenv(envname);
112-
free(env_value);
112+
smart_string_free(&env_str);
113113
}
114114

115115
fpm_array_free(&sockets_list);

0 commit comments

Comments
 (0)