Skip to content

Commit ca880d2

Browse files
committed
sapi/fpm: remove use of variable-length arrays
According to @cmb69, PHP does not require VLA support (#10304 (comment)). VLAs are a bad idea for several reasons, so let's get rid of them. Two of the VLAs were probably unintended; unlike C++, C doesn't have the concept of "constant expressions", so an array with a "const" length is technically still a VLA. This is fixed by removing the "const" variable, and using sizeof() instead.
1 parent 2f65da0 commit ca880d2

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

sapi/fpm/fpm/fpm_php_trace.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *
3939
int callers_limit = child->wp->config->request_slowlog_trace_depth;
4040
pid_t pid = child->pid;
4141
struct timeval tv;
42-
static const int buf_size = 1024;
43-
char buf[buf_size];
42+
char buf[1024];
4443
long execute_data;
4544
long path_translated;
4645
long l;
4746

4847
gettimeofday(&tv, 0);
4948

50-
zlog_print_time(&tv, buf, buf_size);
49+
zlog_print_time(&tv, buf, sizeof(buf));
5150

5251
fprintf(slowlog, "\n%s [pool %s] pid %d\n", buf, child->wp->config->name, (int) pid);
5352

@@ -57,7 +56,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *
5756

5857
path_translated = l;
5958

60-
if (0 > fpm_trace_get_strz(buf, buf_size, path_translated)) {
59+
if (0 > fpm_trace_get_strz(buf, sizeof(buf), path_translated)) {
6160
return -1;
6261
}
6362

@@ -103,7 +102,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *
103102
ZEND_UNREACHABLE();
104103
}
105104
} else {
106-
if (0 > fpm_trace_get_strz(buf, buf_size, function_name + offsetof(zend_string, val))) {
105+
if (0 > fpm_trace_get_strz(buf, sizeof(buf), function_name + offsetof(zend_string, val))) {
107106
return -1;
108107
}
109108

@@ -149,7 +148,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *
149148

150149
file_name = l;
151150

152-
if (0 > fpm_trace_get_strz(buf, buf_size, file_name + offsetof(zend_string, val))) {
151+
if (0 > fpm_trace_get_strz(buf, sizeof(buf), file_name + offsetof(zend_string, val))) {
153152
return -1;
154153
}
155154

sapi/fpm/fpm/fpm_status.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ int fpm_status_export_to_zval(zval *status)
6161

6262
/* copy the scoreboard not to bother other processes */
6363
scoreboard = *scoreboard_p;
64-
struct fpm_scoreboard_proc_s procs[scoreboard.nprocs];
64+
struct fpm_scoreboard_proc_s *procs = safe_emalloc(scoreboard.nprocs, sizeof(*procs), 0);
65+
if (procs == NULL) {
66+
return FAILURE;
67+
}
6568

6669
struct fpm_scoreboard_proc_s *proc_p;
6770
for(i=0; i<scoreboard.nprocs; i++) {
@@ -129,6 +132,7 @@ int fpm_status_export_to_zval(zval *status)
129132
add_assoc_long(&fpm_proc_stat, "last-request-memory", procs[i].request_stage == FPM_REQUEST_ACCEPTING ? procs[i].memory : 0);
130133
add_next_index_zval(&fpm_proc_stats, &fpm_proc_stat);
131134
}
135+
efree(procs);
132136
add_assoc_zval(status, "procs", &fpm_proc_stats);
133137
return 0;
134138
}

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ int fpm_stdio_flush_child(void)
168168

169169
static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
170170
{
171-
static const int max_buf_size = 1024;
172171
int fd = ev->fd;
173-
char buf[max_buf_size];
172+
char buf[1024];
174173
struct fpm_child_s *child;
175174
int is_stdout;
176175
struct fpm_event_s *event;
@@ -219,7 +218,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
219218

220219
while (1) {
221220
stdio_read:
222-
in_buf = read(fd, buf, max_buf_size - 1);
221+
in_buf = read(fd, buf, sizeof(buf) - 1);
223222
if (in_buf <= 0) { /* no data */
224223
if (in_buf == 0 || !PHP_IS_TRANSIENT_ERROR(errno)) {
225224
/* pipe is closed or error */

0 commit comments

Comments
 (0)