qemu_set_log_filename: filename argument may be NULL
PATCH 1/1 qemu_set_log_filename: filename argument may be NULL
From: Salvador Fandino <[email protected]>
NULL is a valid log filename used to indicate we want to use stderr but qemu_set_log_filename (which is called by bsd-user/main.c) was not handling it correctly.
--- util/log.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
util/log.c +15 -12
@@ -151,22 +151,25 @@ void qemu_log_needs_buffers(void)*/ void qemu_set_log_filename (const char *filename,Error **errp) { -char *pidstr;g_free (logfilename); logfilename =NULL ; - pidstr =strstr (filename,"%" ); -if (pidstr) { -/* We only accept one %d, no other format strings */ -if (pidstr[1 ] !='d' ||strchr (pidstr +2 ,'%' )) { -error_setg (errp,"Bad logfile format: %s" , filename); -return ; - }else { - logfilename =g_strdup_printf (filename,getpid ()); - } - }else { - logfilename =g_strdup (filename); +if (filename) { +char *pidstr =strstr (filename,"%" ); +if (pidstr) { +/* We only accept one %d, no other format strings */ +if (pidstr[1 ] !='d' ||strchr (pidstr +2 ,'%' )) { +error_setg (errp,"Bad logfile format: %s" , filename); +return ; + }else { + logfilename =g_strdup_printf (filename,getpid ()); + } + }else { + logfilename =g_strdup (filename); + } } +/* else, let logfilename be NULL indicating we want to use stderr */ +qemu_log_close ();qemu_set_log (qemu_loglevel); }
Signature · 1 line
2.24.1
On Wed, Jan 22, 2020 at 10:08:12PM +0100, [email protected] wrote:
Existing callers like vl.c:main() do:
if (log_file) {
qemu_set_log_filename(log_file, &error_fatal);
}Please fix up existing callers. They won't need to check for NULL anymore before calling qemu_set_log_filename().
Quoted 1 line
+ /* else, let logfilename be NULL indicating we want to use stderr */
Please update the doc comment instead. That way callers know that passing NULL is allowed without reading the implementation.
PATCH 1/1 qemu_set_log_filename: filename argument may be NULL
From: Salvador Fandino <[email protected]>
NULL is a valid log filename used to indicate we want to use stderr but qemu_set_log_filename (which is called by bsd-user/main.c) was not handling it correctly.
That also made redundant a couple of NULL checks in calling code which have been removed.
--- trace/control.c | 4 +--- util/log.c | 28 ++++++++++++++++------------ vl.c | 5 +---- 3 files changed, 18 insertions(+), 19 deletions(-)
trace/control.c +1 -3
@@ -229,9 +229,7 @@ void trace_init_file(const char *file)/* If both the simple and the log backends are enabled, "--trace file" * only applies to the simple backend; use "-D" for the log backend. */ -if (file) { -qemu_set_log_filename (file, &error_fatal); - } +qemu_set_log_filename (file, &error_fatal);#else if (file) { fprintf(stderr, "error: --trace file=...: "
util/log.c +16 -12
@@ -148,25 +148,29 @@ void qemu_log_needs_buffers(void)* Allow the user to include %d in their logfile which will be * substituted with the current PID. This is useful for debugging many * nested linux-user tasks but will result in lots of logs. +* +* filename may be NULL. In that case, log output is sent to stderr */ void qemu_set_log_filename (const char *filename,Error **errp) { -char *pidstr;g_free (logfilename); logfilename =NULL ; - pidstr =strstr (filename,"%" ); -if (pidstr) { -/* We only accept one %d, no other format strings */ -if (pidstr[1 ] !='d' ||strchr (pidstr +2 ,'%' )) { -error_setg (errp,"Bad logfile format: %s" , filename); -return ; - }else { - logfilename =g_strdup_printf (filename,getpid ()); - } - }else { - logfilename =g_strdup (filename); +if (filename) { +char *pidstr =strstr (filename,"%" ); +if (pidstr) { +/* We only accept one %d, no other format strings */ +if (pidstr[1 ] !='d' ||strchr (pidstr +2 ,'%' )) { +error_setg (errp,"Bad logfile format: %s" , filename); +return ; + }else { + logfilename =g_strdup_printf (filename,getpid ()); + } + }else { + logfilename =g_strdup (filename); + } } +qemu_log_close ();qemu_set_log (qemu_loglevel); }
vl.c +1 -4
@@ -3886,10 +3886,7 @@ int main(int argc, char **argv, char **envp)/* Open the logfile at this point and set the log mask if necessary. */ -if (log_file) { -qemu_set_log_filename (log_file, &error_fatal); - } - +qemu_set_log_filename (log_file, &error_fatal);if (log_mask) {int mask; mask =qemu_str_to_log_mask (log_mask);
Signature · 1 line
2.24.1
Quoted 9 lines · On Thu, Jan 23, 2020 at 08:36:26PM +0100, [email protected] wrote:
From: Salvador Fandino <[email protected]>
NULL is a valid log filename used to indicate we want to use stderr but
qemu_set_log_filename(which is called bybsd-user/main.c) was not handling it correctly.That also made redundant a couple of NULL checks in calling code which have been removed.
Quoted 6 lines
Signed-off-by Salvador Fandino <[email protected]>--- trace/control.c | 4 +--- util/log.c | 28 ++++++++++++++++------------ vl.c | 5 +---- 3 files changed, 18 insertions(+), 19 deletions(-)
Stefan Hajnoczi
AppliedQuoted 9 lines · On Thu, Jan 23, 2020 at 08:36:26PM +0100, [email protected] wrote:
From: Salvador Fandino <[email protected]>
NULL is a valid log filename used to indicate we want to use stderr but
qemu_set_log_filename(which is called bybsd-user/main.c) was not handling it correctly.That also made redundant a couple of NULL checks in calling code which have been removed.
Quoted 6 lines
Signed-off-by Salvador Fandino <[email protected]>--- trace/control.c | 4 +--- util/log.c | 28 ++++++++++++++++------------ vl.c | 5 +---- 3 files changed, 18 insertions(+), 19 deletions(-)
Thanks, applied to my tracing tree:
https://github.com/stefanha/qemu/commits/tracing
Stefan