From 2eabaf0975a7ecfec85b1dda3fe86cdb41fbaa79 Mon Sep 17 00:00:00 2001 From: Carlo Wood Date: Fri, 7 Feb 2025 21:03:37 +0100 Subject: [PATCH] Fix fixes the compile warning, MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ../../../../github/wayfire/wayfire.git/src/debug.cpp: In function ‘std::string read_output(std::string)’: ../../../../github/wayfire/wayfire.git/src/debug.cpp:95:10: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 95 | fgets(buffer, MAX_FUNCTION_NAME, file); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this case there is a potential problem: if gets returns NULL, then nothing was read and the buffer is uninitialized. Not only would this function be returning uninitialized data, an arbitrary amount of memory could be allocated into line (and be returned) when std::string line = buffer; is searching for a terminating zero in the uninitialized buffer. --- src/debug.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/debug.cpp b/src/debug.cpp index 1de7874ac..a0d6dc94b 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -92,9 +92,14 @@ std::string read_output(std::string command) return ""; } - fgets(buffer, MAX_FUNCTION_NAME, file); + char *line_as_c_str = fgets(buffer, MAX_FUNCTION_NAME, file); pclose(file); + if (!line_as_c_str) + { + return {}; + } + std::string line = buffer; if (line.size() && (line.back() == '\n')) {