From 8dd304035848e780372a5a4bb1afac2b2d20bc43 Mon Sep 17 00:00:00 2001 From: Aliaksey Kandratsenka Date: Sun, 5 Aug 2018 19:52:11 -0700 Subject: [PATCH 18/39] Format and fix out of bound access in CpuProfilerSwitch GCC was giving warning on snprintf and it hinted at base_profile_name and full_profile_name not being long enough. Fix is to ensure base_profile_name is long enough for PATH_MAX and that full_profile_name is enough longer to fit extra chars. --- src/profiler.cc | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/profiler.cc b/src/profiler.cc index f4f5990..3bd0ed9 100644 --- a/src/profiler.cc +++ b/src/profiler.cc @@ -144,34 +144,31 @@ class CpuProfiler { // number is defined in the environment variable CPUPROFILESIGNAL. static void CpuProfilerSwitch(int signal_number) { - bool static started = false; - static unsigned profile_count = 0; - static char base_profile_name[1024] = "\0"; - - if (base_profile_name[0] == '\0') { - if (!GetUniquePathFromEnv("CPUPROFILE", base_profile_name)) { - RAW_LOG(FATAL,"Cpu profiler switch is registered but no CPUPROFILE is defined"); - return; - } - } - if (!started) - { - char full_profile_name[1024]; - - snprintf(full_profile_name, sizeof(full_profile_name), "%s.%u", - base_profile_name, profile_count++); - - if(!ProfilerStart(full_profile_name)) - { - RAW_LOG(FATAL, "Can't turn on cpu profiling for '%s': %s\n", - full_profile_name, strerror(errno)); - } + static unsigned profile_count; + static char base_profile_name[PATH_MAX]; + static bool started = false; + + if (base_profile_name[0] == '\0') { + if (!GetUniquePathFromEnv("CPUPROFILE", base_profile_name)) { + RAW_LOG(FATAL,"Cpu profiler switch is registered but no CPUPROFILE is defined"); + return; } - else - { - ProfilerStop(); + } + + if (!started) { + char full_profile_name[PATH_MAX + 16]; + + snprintf(full_profile_name, sizeof(full_profile_name), "%s.%u", + base_profile_name, profile_count++); + + if(!ProfilerStart(full_profile_name)) { + RAW_LOG(FATAL, "Can't turn on cpu profiling for '%s': %s\n", + full_profile_name, strerror(errno)); } - started = !started; + } else { + ProfilerStop(); + } + started = !started; } // Profile data structure singleton: Constructor will check to see if -- 1.8.3.1