Skip to content

Commit 9043ec5

Browse files
author
Julien Pauli
committed
Fix #69031 - FPM Long messages into stdout/stderr are truncated incorrectly
1 parent 99e5d6c commit 9043ec5

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg)
141141
} else { /* error or pipe is closed */
142142

143143
if (res < 0) { /* error */
144-
zlog(ZLOG_SYSERROR, "unable to read what child say");
144+
zlog(ZLOG_SYSERROR, "unable to read what child says");
145145
}
146146

147147
fpm_event_del(event);

sapi/fpm/fpm/zlog.c

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,30 @@ int zlog_set_level(int new_value) /* {{{ */
101101
void vzlog(const char *function, int line, int flags, const char *fmt, va_list args) /* {{{ */
102102
{
103103
struct timeval tv;
104-
char buf[MAX_LINE_LENGTH];
105-
const size_t buf_size = MAX_LINE_LENGTH;
106-
size_t len = 0;
107-
int truncated = 0;
104+
char tmp_buf[MAX_LINE_LENGTH];
105+
int tmp_buf_size = MAX_LINE_LENGTH;
106+
int tmp_buf_len = 0;
107+
108+
char *final_buf;
109+
int final_buf_len;
110+
111+
char *fmt_buf;
112+
int fmt_buf_len;
113+
108114
int saved_errno;
109115

110116
if (external_logger) {
111117
va_list ap;
112118
va_copy(ap, args);
113-
len = vsnprintf(buf, buf_size, fmt, ap);
119+
fmt_buf_len = vasprintf(&fmt_buf, fmt, ap);
114120
va_end(ap);
115-
if (len >= buf_size) {
116-
memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
117-
len = buf_size - 1;
121+
if (fmt_buf_len < 0) {
122+
external_logger(flags & ZLOG_LEVEL_MASK, "Can't allocate memory for zlog()", strlen("Can't allocate memory for zlog()"));
123+
} else {
124+
external_logger(flags & ZLOG_LEVEL_MASK, fmt_buf, fmt_buf_len);
125+
fmt_buf_len = 0;
126+
free(fmt_buf);
118127
}
119-
external_logger(flags & ZLOG_LEVEL_MASK, buf, len);
120-
len = 0;
121-
memset(buf, '\0', buf_size);
122128
}
123129

124130
if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
@@ -128,70 +134,61 @@ void vzlog(const char *function, int line, int flags, const char *fmt, va_list a
128134
saved_errno = errno;
129135
#ifdef HAVE_SYSLOG_H
130136
if (zlog_fd == ZLOG_SYSLOG /* && !fpm_globals.is_child */) {
131-
len = 0;
132137
if (zlog_level == ZLOG_DEBUG) {
133-
len += snprintf(buf, buf_size, "[%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
138+
tmp_buf_len += snprintf(tmp_buf, tmp_buf_size, "[%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
134139
} else {
135-
len += snprintf(buf, buf_size, "[%s] ", level_names[flags & ZLOG_LEVEL_MASK]);
140+
tmp_buf_len += snprintf(tmp_buf, tmp_buf_size, "[%s] ", level_names[flags & ZLOG_LEVEL_MASK]);
136141
}
137142
} else
138143
#endif
139144
{
140145
if (!fpm_globals.is_child) {
141146
gettimeofday(&tv, 0);
142-
len = zlog_print_time(&tv, buf, buf_size);
147+
tmp_buf_len = zlog_print_time(&tv, tmp_buf, tmp_buf_size);
143148
}
144149
if (zlog_level == ZLOG_DEBUG) {
145150
if (!fpm_globals.is_child) {
146-
len += snprintf(buf + len, buf_size - len, "%s: pid %d, %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], getpid(), function, line);
151+
tmp_buf_len += snprintf(tmp_buf + tmp_buf_len, tmp_buf_size - tmp_buf_len, "%s: pid %d, %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], getpid(), function, line);
147152
} else {
148-
len += snprintf(buf + len, buf_size - len, "%s: %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
153+
tmp_buf_len += snprintf(tmp_buf + tmp_buf_len, tmp_buf_size - tmp_buf_len, "%s: %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
149154
}
150155
} else {
151-
len += snprintf(buf + len, buf_size - len, "%s: ", level_names[flags & ZLOG_LEVEL_MASK]);
152-
}
153-
}
154-
155-
if (len > buf_size - 1) {
156-
truncated = 1;
157-
}
158-
159-
if (!truncated) {
160-
len += vsnprintf(buf + len, buf_size - len, fmt, args);
161-
if (len >= buf_size) {
162-
truncated = 1;
156+
tmp_buf_len += snprintf(tmp_buf + tmp_buf_len, tmp_buf_size - tmp_buf_len, "%s: ", level_names[flags & ZLOG_LEVEL_MASK]);
163157
}
164158
}
165159

166-
if (!truncated) {
167-
if (flags & ZLOG_HAVE_ERRNO) {
168-
len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
169-
if (len >= buf_size) {
170-
truncated = 1;
171-
}
172-
}
160+
if (flags & ZLOG_HAVE_ERRNO) {
161+
tmp_buf_len += snprintf(tmp_buf + tmp_buf_len, tmp_buf_size - tmp_buf_len, ": %s (%d)", strerror(saved_errno), saved_errno);
173162
}
174163

175-
if (truncated) {
176-
memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
177-
len = buf_size - 1;
164+
fmt_buf_len = vasprintf(&fmt_buf, fmt, args);
165+
if (fmt_buf_len < 0) {
166+
final_buf = strndup("Can't allocate memory for zlog()", strlen("Can't allocate memory for zlog()"));
167+
final_buf_len = strlen("Can't allocate memory for zlog()");
168+
} else {
169+
final_buf_len = tmp_buf_len + fmt_buf_len + 2;
170+
171+
final_buf = calloc(1, final_buf_len);
172+
memcpy(final_buf, tmp_buf, tmp_buf_len);
173+
memcpy(final_buf + tmp_buf_len, fmt_buf, fmt_buf_len);
174+
free(fmt_buf);
178175
}
179176

180177
#ifdef HAVE_SYSLOG_H
181178
if (zlog_fd == ZLOG_SYSLOG) {
182-
buf[len] = '\0';
183-
php_syslog(syslog_priorities[zlog_level], "%s", buf);
184-
buf[len++] = '\n';
179+
php_syslog(syslog_priorities[zlog_level], "%s", final_buf);
185180
} else
186181
#endif
187182
{
188-
buf[len++] = '\n';
189-
write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
183+
final_buf[final_buf_len - 2] = '\n';
184+
write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, final_buf, final_buf_len);
190185
}
191186

192187
if (zlog_fd != STDERR_FILENO && zlog_fd != -1 && !launched && (flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE) {
193-
write(STDERR_FILENO, buf, len);
188+
write(STDERR_FILENO, final_buf, final_buf_len);
194189
}
190+
191+
free(final_buf);
195192
}
196193
/* }}} */
197194

0 commit comments

Comments
 (0)