Skip to content

Commit

Permalink
Fix fixes the compile warning,
Browse files Browse the repository at this point in the history
../../../../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.
  • Loading branch information
CarloWood authored and ammen99 committed Feb 14, 2025
1 parent d1faa1b commit 2eabaf0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
{
Expand Down

0 comments on commit 2eabaf0

Please sign in to comment.