-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Conversation
-Wformat-overflow
Clang compiler warnings / added missing newline to log messages
8a00f76
to
5858095
Compare
5858095
to
c2f5ed5
Compare
-Wformat-overflow
Clang compiler warnings / added missing newline to log messages-Wformat-overflow
compiler warnings / added missing newline to log messages
There was a problem hiding this 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.
Src/OSD/Logger.cpp
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Src/OSD/Logger.cpp
Outdated
|
||
vsprintf(string1, fmt, vl); | ||
sprintf(string2, "[Debug] %s", string1); | ||
sprintf(string2, "[Debug] %s\n", string1); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Src/OSD/Logger.cpp
Outdated
|
||
vsprintf(string1, fmt, vl); | ||
sprintf(string2, "[Debug] %s", string1); | ||
sprintf(string2, "[Debug] %s\n", string1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Will you be able to make the suggested changes? |
Yes. Hopefully I finally get to it this week. |
c2f5ed5
to
640aeda
Compare
4042cdd
to
afe8ddf
Compare
There are additional warnings now but I cannot change that file because there are some issues with the line endings.
|
afe8ddf
to
075bfa2
Compare
Some examples for reference: