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.
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 @@
// 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";
+ 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;
- }
- }
- 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));
- }
+ 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