From 826f07783fedf3743b54ccc6ee7e85679471b960 Mon Sep 17 00:00:00 2001 From: Daniel Xu Date: Mon, 9 Nov 2015 21:35:54 +0000 Subject: [PATCH] TS-4003 CID 1338381 & 1022062: in traffic_cop Fix unchecked `rename()` return for renaming an inaccessible traffic.out file Refactor a bit of _correct_ code to please Coverity --- cmd/traffic_cop/traffic_cop.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/traffic_cop/traffic_cop.cc b/cmd/traffic_cop/traffic_cop.cc index 133a979d867..6590046d62f 100644 --- a/cmd/traffic_cop/traffic_cop.cc +++ b/cmd/traffic_cop/traffic_cop.cc @@ -747,15 +747,16 @@ spawn_manager() char old_log_file[PATH_NAME_MAX]; snprintf(old_log_file, sizeof(old_log_file), "%s.old", log_file); // coverity[toctou] - rename(log_file, old_log_file); - cop_log(COP_WARNING, "rename %s to %s as it is not accessible.\n", log_file, old_log_file); + if (rename(log_file, old_log_file) != 0) + cop_log(COP_WARNING, "unable to rename() \"%s\" to \"%s\" [%d '%s']\n", log_file, old_log_file, errno, strerror(errno)); + else + cop_log(COP_WARNING, "rename %s to %s as it is not accessible.\n", log_file, old_log_file); } // Bind stdout and stderr of traffic_manager to traffic.out - int max_opts_len = OPTIONS_LEN_MAX - strlen(manager_options); - char tm_opt_buf[max_opts_len]; - int cx = snprintf(tm_opt_buf, max_opts_len, " --%s %s --%s %s", TM_OPT_BIND_STDOUT, log_file, TM_OPT_BIND_STDERR, log_file); - if (cx >= 0 && cx < max_opts_len) + char tm_opt_buf[OPTIONS_LEN_MAX]; + int cx = snprintf(tm_opt_buf, OPTIONS_LEN_MAX, " --%s %s --%s %s", TM_OPT_BIND_STDOUT, log_file, TM_OPT_BIND_STDERR, log_file); + if (cx >= 0 && cx < OPTIONS_LEN_MAX && (strlen(manager_options) + strlen(tm_opt_buf) < OPTIONS_LEN_MAX)) strcat(manager_options, tm_opt_buf); else cop_log(COP_WARNING, "bind_stdout and bind_stderr flags are too long, not binding anything\n");