Skip to content

Commit 80d4d40

Browse files
committed
FPM: change uses of sprintf into snprintf
1 parent 1bd33b7 commit 80d4d40

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

sapi/fpm/fpm/fpm_conf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ int fpm_conf_write_pid(void)
12621262
return -1;
12631263
}
12641264

1265-
len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
1265+
len = snprintf(buf, sizeof(buf), "%d", (int) fpm_globals.parent_pid);
12661266

12671267
if (len != write(fd, buf, len)) {
12681268
zlog(ZLOG_SYSERROR, "Unable to write to the PID file.");

sapi/fpm/fpm/fpm_env.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ int setenv(char *name, char *value, int clobber) /* {{{ */
3232
return 0;
3333
}
3434

35-
if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0) {
35+
size_t length = strlen(name) + strlen(value) + 2;
36+
if ((cp = malloc(length)) == 0) {
3637
return 1;
3738
}
38-
sprintf(cp, "%s=%s", name, value);
39+
snprintf(cp, length, "%s=%s", name, value);
3940
return putenv(cp);
4041
}
4142
/* }}} */

sapi/fpm/fpm/fpm_php.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp) /* {{{ */
165165
char max_workers[10 + 1]; /* 4294967295 */
166166
int len;
167167

168-
len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm_max_children);
168+
len = snprintf(max_workers, sizeof(max_workers), "%u", (unsigned int) wp->config->pm_max_children);
169169

170170
fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
171171
fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, max_workers, len);

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4343
static int routemax = -1;
4444
#endif
4545

46-
static inline void fpm_sockets_get_env_name(char *envname, unsigned idx) /* {{{ */
46+
static inline void fpm_sockets_get_env_name(char *envname, size_t envname_length, unsigned idx) /* {{{ */
4747
{
4848
if (!idx) {
4949
strcpy(envname, "FPM_SOCKETS");
5050
} else {
51-
sprintf(envname, "FPM_SOCKETS_%d", idx);
51+
snprintf(envname, envname_length, "FPM_SOCKETS_%d", idx);
5252
}
5353
}
5454
/* }}} */
@@ -70,7 +70,7 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
7070
} else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
7171
char fd[32];
7272
char *tmpenv_value;
73-
sprintf(fd, "%d", ls->sock);
73+
snprintf(fd, sizeof(fd), "%d", ls->sock);
7474

7575
socket_set_buf = (i % FPM_ENV_SOCKET_SET_SIZE == 0 && i) ? 1 : 0;
7676
tmpenv_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + socket_set_buf + 1);
@@ -104,10 +104,10 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
104104

105105
if (env_value) {
106106
for (i = 0; i < socket_set_count; i++) {
107-
fpm_sockets_get_env_name(envname, i);
107+
fpm_sockets_get_env_name(envname, sizeof(envname), i);
108108
setenv(envname, env_value + socket_set[i], 1);
109109
}
110-
fpm_sockets_get_env_name(envname, socket_set_count);
110+
fpm_sockets_get_env_name(envname, sizeof(envname), socket_set_count);
111111
unsetenv(envname);
112112
free(env_value);
113113
}
@@ -143,7 +143,8 @@ static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int typ
143143
case FPM_AF_INET : {
144144
key = alloca(INET6_ADDRSTRLEN+10);
145145
inet_ntop(sa->sa_family, fpm_get_in_addr(sa), key, INET6_ADDRSTRLEN);
146-
sprintf(key+strlen(key), ":%d", fpm_get_in_port(sa));
146+
size_t key_length = strlen(key);
147+
snprintf(key + key_length, INET6_ADDRSTRLEN + 10 - key_length, ":%d", fpm_get_in_port(sa));
147148
break;
148149
}
149150

@@ -455,7 +456,7 @@ int fpm_sockets_init_main(void)
455456

456457
/* import inherited sockets */
457458
for (i = 0; i < FPM_ENV_SOCKET_SET_MAX; i++) {
458-
fpm_sockets_get_env_name(envname, i);
459+
fpm_sockets_get_env_name(envname, sizeof(envname), i);
459460
inherited = getenv(envname);
460461
if (!inherited) {
461462
break;

sapi/fpm/fpm/fpm_trace_pread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int fpm_trace_ready(pid_t pid) /* {{{ */
3333
{
3434
char buf[128];
3535

36-
sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
36+
snprintf(buf, sizeof(buf), "/proc/%d/" PROC_MEM_FILE, (int) pid);
3737
mem_file = open(buf, O_RDONLY);
3838
if (0 > mem_file) {
3939
zlog(ZLOG_SYSERROR, "failed to open %s", buf);

sapi/fpm/fpm/fpm_unix.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,15 @@ int fpm_unix_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
533533
return -1;
534534
}
535535

536-
new_con = malloc(strlen(con) + strlen(wp->config->apparmor_hat) + 3); // // + 0 Byte
536+
size_t new_con_length = strlen(con) + strlen(wp->config->apparmor_hat) + 3; // // + 0 Byte
537+
new_con = malloc(new_con_length);
537538
if (!new_con) {
538539
zlog(ZLOG_SYSERROR, "[pool %s] failed to allocate memory for apparmor hat change.", wp->config->name);
539540
free(con);
540541
return -1;
541542
}
542543

543-
if (0 > sprintf(new_con, "%s//%s", con, wp->config->apparmor_hat)) {
544+
if (0 > snprintf(new_con, new_con_length, "%s//%s", con, wp->config->apparmor_hat)) {
544545
zlog(ZLOG_SYSERROR, "[pool %s] failed to construct apparmor confinement.", wp->config->name);
545546
free(con);
546547
free(new_con);

0 commit comments

Comments
 (0)