Skip to content

Commit 5b868dc

Browse files
committed
Fix using of va_list. It is necessary to exec va_copy() every times
1 parent c2e8782 commit 5b868dc

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

utils/logger.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* logger.c: - log events into log file or stderr.
44
*
5-
* Portions Copyright (c) 2017-2017, Postgres Professional
5+
* Copyright (c) 2017-2017, Postgres Professional
66
*
77
*-------------------------------------------------------------------------
88
*/
@@ -103,12 +103,22 @@ write_elevel(FILE *stream, int elevel)
103103
static void
104104
elog_internal(int elevel, const char *fmt, va_list args)
105105
{
106-
bool wrote_to_file = false;
106+
bool wrote_to_file = false,
107+
write_to_error_log,
108+
write_to_stderr;
109+
va_list error_args,
110+
std_args;
107111

108112
/* There is no need to lock if this is elog() from upper elog() */
109113
if (!logging_to_file)
110114
pthread_mutex_lock(&log_file_mutex);
111115

116+
write_to_error_log =
117+
elevel >= ERROR && error_log_filename && !logging_to_file;
118+
/* We need copy args only if we need write to error log file */
119+
if (write_to_error_log)
120+
va_copy(error_args, args);
121+
112122
/*
113123
* Write message to log file.
114124
* Do not write to file if this error was raised during write previous
@@ -131,12 +141,21 @@ elog_internal(int elevel, const char *fmt, va_list args)
131141
wrote_to_file = true;
132142
}
133143

144+
/*
145+
* Write to stderr if the message was not written to log file.
146+
* Write to stderr if the message level is greater than WARNING anyway.
147+
*/
148+
write_to_stderr = !wrote_to_file || elevel >= ERROR;
149+
/* We need copy args only if we need write to stderr */
150+
if (write_to_stderr)
151+
va_copy(std_args, error_args);
152+
134153
/*
135154
* Write error message to error log file.
136155
* Do not write to file if this error was raised during write previous
137156
* message.
138157
*/
139-
if (elevel >= ERROR && error_log_filename && !logging_to_file)
158+
if (write_to_error_log)
140159
{
141160
logging_to_file = true;
142161

@@ -145,25 +164,23 @@ elog_internal(int elevel, const char *fmt, va_list args)
145164

146165
write_elevel(error_log_file, elevel);
147166

148-
vfprintf(error_log_file, fmt, args);
167+
vfprintf(error_log_file, fmt, error_args);
149168
fputc('\n', error_log_file);
150169
fflush(error_log_file);
151170

152171
logging_to_file = false;
172+
va_end(error_args);
153173
}
154174

155-
/*
156-
* Write to stderr if the message was not written to log file.
157-
* Write to stderr if the message level is greater than WARNING anyway.
158-
*/
159-
if (!wrote_to_file ||
160-
elevel >= ERROR)
175+
if (write_to_stderr)
161176
{
162177
write_elevel(stderr, elevel);
163178

164-
vfprintf(stderr, fmt, args);
179+
vfprintf(stderr, fmt, std_args);
165180
fputc('\n', stderr);
166181
fflush(stderr);
182+
183+
va_end(std_args);
167184
}
168185

169186
if (!logging_to_file)

0 commit comments

Comments
 (0)