SDL: SDL_Log causes exception with "%z" size_t length modifier
SDL_Log exhibits questionable behavior with the ‘z’ length modifier introduced in c99. See the following wiki for more information:
https://en.cppreference.com/w/c/io/fprintf
OS: Win10 Platform: x64
Example 1: %zu causes memory exception/crash:
#include <SDL.h>
int main(int argc, char** argv)
{
size_t objectCount = 64;
const char* levelName = "test";
SDL_Log("Initializing");
if (SDL_Init(SDL_INIT_EVERYTHING))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to init SDL");
return -1;
}
SDL_Log("Loaded %zu objects for level \"%s\"", objectCount, levelName);
SDL_Log("Quiting");
SDL_Quit();
return 0;
}
Console Output:
INFO: Initializing
C:\Users\spaxt\Desktop\SDL_Log__LengthModifierIssue\x64\Release\SDL_Log__LengthModifierIssue.exe (process 26256) exited with code -1073741819.
Press any key to close this window . . .
Example 2: %uz causes the ‘z’ to not be processed as a format specifier:
#include <SDL.h>
int main(int argc, char** argv)
{
size_t objectCount = 64;
const char* levelName = "test";
SDL_Log("Initializing");
if (SDL_Init(SDL_INIT_EVERYTHING))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to init SDL");
return -1;
}
SDL_Log("Loaded %uz objects for level \"%s\"", objectCount, levelName);
SDL_Log("Quiting");
SDL_Quit();
return 0;
}
Console Output:
INFO: Initializing
INFO: Loaded 64z objects for level "test"
INFO: Quiting
C:\Users\spaxt\Desktop\SDL_Log__LengthModifierIssue\x64\Release\SDL_Log__LengthModifierIssue.exe (process 24588) exited with code 0.
Press any key to close this window . . .
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (6 by maintainers)
Commits related to this issue
- Implemented size_t format specifiers for SDL_snprintf() and SDL_sscanf() Fixes https://github.com/libsdl-org/SDL/issues/6264 — committed to PJB3005/SDL by slouken 2 years ago
- Implemented size_t format specifiers for SDL_snprintf() and SDL_sscanf() Fixes https://github.com/libsdl-org/SDL/issues/6264 (cherry picked from commit 216e3f10bb18022049235c7c82c38d5b4d8dbd4a) — committed to sezero/SDL by slouken 2 years ago
Great! I just saw my original issue wasn’t as clear as I thought it was this morning after 2 cups of coffee. Sorry for confusion, glad you got a fix!