From 50b4ab1b6c8756caf2b857ab36670d3e2f7df63e Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Jul 2020 19:14:16 -0400 Subject: [PATCH] fix: correct leak in error handling --- runtime/src/main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/runtime/src/main.c b/runtime/src/main.c index d3b28d5..6f71f00 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -103,21 +103,28 @@ runtime_allocate_available_cores() static inline float runtime_get_processor_speed_MHz(void) { + float return_value; + FILE *cmd = popen("grep '^cpu MHz' /proc/cpuinfo | head -n 1 | awk '{print $4}'", "r"); - if (cmd == NULL) return -1; + if (cmd == NULL) goto err; float processor_speed_MHz; size_t n; char buff[16]; - if ((n = fread(buff, 1, sizeof(buff) - 1, cmd)) <= 0) return -1; + if ((n = fread(buff, 1, sizeof(buff) - 1, cmd)) <= 0) goto err; buff[n] = '\0'; - if (sscanf(buff, "%f", &processor_speed_MHz) != 1) return -1; + if (sscanf(buff, "%f", &processor_speed_MHz) != 1) goto err; - pclose(cmd); + return_value = processor_speed_MHz; - return processor_speed_MHz; +done: + pclose(cmd); + return return_value; +err: + return_value = -1; + goto done; } #ifdef DEBUG