Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile warning. #2574

Merged
merged 2 commits into from
Feb 14, 2025
Merged

Conversation

CarloWood
Copy link
Contributor

Fix fixes the compile warning,

../../../../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.

I kinda rewrote this function (while I was at it) to not write first to the stack and then copy that into line. Instead the std::string is created with the same space as the buffer was, and then we write directly into the allocated memory. Afterwards a resize() sets line to the correct size - removing any trailing newline at the same time.

In other words, this code is much faster too (as if that was needed, but alas)!

@soreau
Copy link
Member

soreau commented Feb 7, 2025

I kinda rewrote this function (while I was at it) to not write first to the stack and then copy that into line. Instead the std::string is created with the same space as the buffer was, and then we write directly into the allocated memory. Afterwards a resize() sets line to the correct size - removing any trailing newline at the same time.

In my opinion, this part should go into another commit, since it makes it unclear on how the warning was fixed.

@CarloWood
Copy link
Contributor Author

The warning was fixed by assigning the return of the call to fgets to a variable.

@CarloWood CarloWood closed this Feb 7, 2025
@CarloWood CarloWood force-pushed the PR_02_Compile_warning_fgets branch from b77fd77 to e12d076 Compare February 7, 2025 19:56
@soreau soreau reopened this Feb 7, 2025
../../../../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.
It was just because of the compiler warning fix that
I was like: lets beautify this code a bit.

Now it turned into a PR "test", adding a second commit
to the same PR (because otherwise this obviously won't apply).
@CarloWood CarloWood force-pushed the PR_02_Compile_warning_fgets branch from bcd9ebc to 41bfd6b Compare February 14, 2025 02:06
@ammen99
Copy link
Member

ammen99 commented Feb 14, 2025

thanks :)

@ammen99 ammen99 merged commit a18e686 into WayfireWM:master Feb 14, 2025
4 checks passed
@CarloWood CarloWood deleted the PR_02_Compile_warning_fgets branch February 14, 2025 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants