From 4c5a1c2f3b52b4543f62d16b324f7b7f01b36ab5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 21 Feb 2023 13:15:30 +0100 Subject: [PATCH] sapi/fpm: remove use of variable-length arrays According to @cmb69, PHP does not require VLA support (https://github.com/php/php-src/pull/10304#discussion_r1069343092). 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. --- sapi/fpm/fpm/fpm_php_trace.c | 11 +++++------ sapi/fpm/fpm/fpm_stdio.c | 5 ++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/sapi/fpm/fpm/fpm_php_trace.c b/sapi/fpm/fpm/fpm_php_trace.c index 0e1d8e3f6cee0..b1535b26e3ef8 100644 --- a/sapi/fpm/fpm/fpm_php_trace.c +++ b/sapi/fpm/fpm/fpm_php_trace.c @@ -39,15 +39,14 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ * int callers_limit = child->wp->config->request_slowlog_trace_depth; pid_t pid = child->pid; struct timeval tv; - static const int buf_size = 1024; - char buf[buf_size]; + char buf[1024]; long execute_data; long path_translated; long l; gettimeofday(&tv, 0); - zlog_print_time(&tv, buf, buf_size); + zlog_print_time(&tv, buf, sizeof(buf)); fprintf(slowlog, "\n%s [pool %s] pid %d\n", buf, child->wp->config->name, (int) pid); @@ -57,7 +56,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ * path_translated = l; - if (0 > fpm_trace_get_strz(buf, buf_size, path_translated)) { + if (0 > fpm_trace_get_strz(buf, sizeof(buf), path_translated)) { return -1; } @@ -103,7 +102,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ * ZEND_UNREACHABLE(); } } else { - if (0 > fpm_trace_get_strz(buf, buf_size, function_name + offsetof(zend_string, val))) { + if (0 > fpm_trace_get_strz(buf, sizeof(buf), function_name + offsetof(zend_string, val))) { return -1; } @@ -149,7 +148,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ * file_name = l; - if (0 > fpm_trace_get_strz(buf, buf_size, file_name + offsetof(zend_string, val))) { + if (0 > fpm_trace_get_strz(buf, sizeof(buf), file_name + offsetof(zend_string, val))) { return -1; } diff --git a/sapi/fpm/fpm/fpm_stdio.c b/sapi/fpm/fpm/fpm_stdio.c index 46cf4aaa2851d..a6c0793d9347e 100644 --- a/sapi/fpm/fpm/fpm_stdio.c +++ b/sapi/fpm/fpm/fpm_stdio.c @@ -168,9 +168,8 @@ int fpm_stdio_flush_child(void) static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) /* {{{ */ { - static const int max_buf_size = 1024; int fd = ev->fd; - char buf[max_buf_size]; + char buf[1024]; struct fpm_child_s *child; int is_stdout; struct fpm_event_s *event; @@ -219,7 +218,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) while (1) { stdio_read: - in_buf = read(fd, buf, max_buf_size - 1); + in_buf = read(fd, buf, sizeof(buf) - 1); if (in_buf <= 0) { /* no data */ if (in_buf == 0 || !PHP_IS_TRANSIENT_ERROR(errno)) { /* pipe is closed or error */