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

fixed -Wformat-overflow compiler warnings / added missing newline to log messages #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

firewave
Copy link
Contributor

@firewave firewave commented Dec 2, 2023

Some examples for reference:

Src/OSD/SDL/Main.cpp:1086:30: warning: ‘ (Paused)’ directive writing 9 bytes into a region of size between 1 and 128 [-Wformat-overflow=]
 1086 |         sprintf(titleStr, "%s (Paused)", baseTitleStr);
      |                              ^~~~~~~~~
Src/OSD/SDL/Main.cpp:1086:16: note: ‘sprintf’ output between 10 and 137 bytes into a destination of size 128
 1086 |         sprintf(titleStr, "%s (Paused)", baseTitleStr);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Src/OSD/Logger.cpp:382:29: warning: ‘%s’ directive writing up to 4095 bytes into a region of size 4088 [-Wformat-overflow=]
  382 |   sprintf(string2, "[Error] %s\n", string1);
      |                             ^~     ~~~~~~~
Src/OSD/Logger.cpp:382:10: note: ‘sprintf’ output between 10 and 4105 bytes into a destination of size 4096
  382 |   sprintf(string2, "[Error] %s\n", string1);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@firewave firewave changed the title fixed some -Wformat-overflow Clang compiler warnings / added missing newline to log messages fixed -Wformat-overflow Clang compiler warnings / added missing newline to log messages Dec 2, 2023
@firewave firewave force-pushed the clang-format-overflow branch from 8a00f76 to 5858095 Compare December 2, 2023 18:22
@firewave firewave marked this pull request as draft December 2, 2023 18:45
@firewave firewave force-pushed the clang-format-overflow branch from 5858095 to c2f5ed5 Compare December 2, 2023 18:45
@firewave firewave changed the title fixed -Wformat-overflow Clang compiler warnings / added missing newline to log messages fixed -Wformat-overflow compiler warnings / added missing newline to log messages Dec 2, 2023
@firewave firewave marked this pull request as ready for review December 2, 2023 18:46
Copy link
Owner

@trzy trzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the attached comments. I don't think the buffer lengths should be touched in Logger.cpp, there is no need for it. Rather, use the safe forms of vsprintf and sprintf (vsnprintf and snprintf). Also, the \n in debug logging was intentionally omitted. It's not a typo.

@@ -228,10 +228,10 @@ void CFileLogger::DebugLog(const char *fmt, va_list vl)
}

char string1[4096];
char string2[4096];
char string2[4105]; // add 8 for the prefix and 1 for newline
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not needed. 4096 was chosen somewhat arbitrarily. We don't actually check the size of the resulting output. If you want to fix this, you should instead prefer to use vsnprintf() and pass 4096 as the "n" parameter. Likewise, sprintf -> snprintf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I moved back to the original buffer length and use *snprintf() now.


vsprintf(string1, fmt, vl);
sprintf(string2, "[Debug] %s", string1);
sprintf(string2, "[Debug] %s\n", string1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This newline is actually incorrect. DebugLog is special and you'll notice wherever it is used in Supermodel, a \n is manually included. The intent behind this was to allow formatting debug output more freely with multiple calls to generate a single line if needed. However, now that the [Debug] tag has been added, it may be worthwhile to automatically include a \n but if this is done, all places where DebugLog is used must be updated to drop their \n.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


vsprintf(string1, fmt, vl);
sprintf(string2, "[Debug] %s", string1);
sprintf(string2, "[Debug] %s\n", string1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -917,7 +917,7 @@ int Supermodel(const Game &game, ROMSet *rom_set, IEmulator *Model3, CInputs *In

// Set the video mode
char baseTitleStr[128];
char titleStr[128];
char titleStr[256];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, I suppose. Does anything actually exceed 128 bytes? We should probably also move to using snprintf() for safety here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it appends up to 128 bytes from baseTitleStr in that string it needs to be at least 128 + n.

Src/OSD/SDL/Main.cpp:1086:30: warning: ‘ (Paused)’ directive writing 9 bytes into a region of size between 1 and 128 [-Wformat-overflow=]
 1086 |         sprintf(titleStr, "%s (Paused)", baseTitleStr);
      |                              ^~~~~~~~~
Src/OSD/SDL/Main.cpp:1086:16: note: ‘sprintf’ output between 10 and 137 bytes into a destination of size 128
 1086 |         sprintf(titleStr, "%s (Paused)", baseTitleStr);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@trzy
Copy link
Owner

trzy commented Mar 10, 2024

Will you be able to make the suggested changes?

@firewave
Copy link
Contributor Author

Will you be able to make the suggested changes?

Yes. Hopefully I finally get to it this week.

@firewave firewave force-pushed the clang-format-overflow branch from c2f5ed5 to 640aeda Compare March 30, 2024 20:13
@firewave firewave marked this pull request as draft March 30, 2024 20:22
@firewave firewave force-pushed the clang-format-overflow branch 4 times, most recently from 4042cdd to afe8ddf Compare March 30, 2024 20:33
@firewave
Copy link
Contributor Author

There are additional warnings now but I cannot change that file because there are some issues with the line endings.

Src/Debugger/CPU/68KDebug.cpp:1353:56: note: ‘sprintf’ output between 15 and 64 bytes into a destination of size 50
 1353 |                                                 sprintf(moveStr, "movem.%c %s,[AM2]", sizeC, regList);
      |                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@firewave firewave force-pushed the clang-format-overflow branch from afe8ddf to 075bfa2 Compare March 30, 2024 20:34
@firewave firewave marked this pull request as ready for review March 30, 2024 20:35
@firewave firewave requested a review from trzy March 30, 2024 20:35
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.

2 participants