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

Most upvoted comments

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!