We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4664 - TTF_RenderUNICODE_Blended segmentation fault
Summary: TTF_RenderUNICODE_Blended segmentation fault
Status: RESOLVED INVALID
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: 2.0.9
Hardware: x86_64 Windows 10
: P2 blocker
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-11 14:19 UTC by Davide Prade
Modified: 2019-06-17 13:35 UTC (History)
1 user (show)

See Also:


Attachments
Font used (837.25 KB, application/x-font-ttf)
2019-06-11 14:51 UTC, Davide Prade
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Davide Prade 2019-06-11 14:19:54 UTC
The following code generates a segmentation fault on the marked line.
I compile on MSYS2 MinGW 64 bit:
 - mingw64/mingw-w64-x86_64-SDL2 2.0.9-1
 - mingw64/mingw-w64-x86_64-SDL2_ttf


---------------------------
Thread 1 received signal SIGSEGV, Segmentation fault.
0x000000006c7c6100 in ?? () from SDL2.dll
(gdb) bt
#0  0x000000006c7c6100 in ?? () from SDL2.dll
#1  0x000000006c7c9d03 in ?? () from SDL2.dll
#2  0x0000000071003922 in ?? ()
   from SDL2_ttf.dll
#3  0x0000000071003e24 in ?? ()
   from SDL2_ttf.dll
#4  0x0000000000450e61 in main (argc=1, argv=0x5c74390)
    at test.cpp:100
---------------------------


---------------------------
    if (TTF_Init() != 0) {
        fprintf(stderr, "Couldn't initialize TTF library");
    } else {
        TTF_Font *font = TTF_OpenFont("arialbd.ttf", 26);
        if (font != NULL) {
            SDL_Color col = {255, 255, 255, 255};

            const Uint16 txt1[] = {'P', 'u', 's', 'h', ' ', 'U', 'p', ' ', '1'};
            SDL_Surface *surf1 = TTF_RenderUNICODE_Blended(font, txt1, col);
            SDL_FreeSurface(surf1);
            surf1 = NULL;

            const Uint16 txt2[] = {'P', 'u', 's', 'h', ' ', 'U', 'p', ' ', '2'};
/*SEGFAULT*/SDL_Surface *surf2 = TTF_RenderUNICODE_Blended(font, txt2, col);
            SDL_FreeSurface(surf2);
            surf2 = NULL;

            TTF_CloseFont(font);
            font = NULL;
        } else {
            fprintf(stderr, "Couldn't open font");
        }
    }
---------------------------
Comment 1 Sylvain 2019-06-11 14:24:34 UTC
you would need to check with latest SDL2_ttf code because it has changed
Comment 2 Davide Prade 2019-06-11 14:29:19 UTC
I am using the last available on MSYS2.

$ pacman -Ss SDL2_ttf
mingw64/mingw-w64-x86_64-SDL2_ttf 2.0.15-1 [installed]
    A library that allows you to use TrueType fonts in your SDL applications (Version 2) (mingw-w64)



Commenting on calls to "SDL_FreeSurface" works.
Comment 3 Sylvain 2019-06-11 14:37:04 UTC
This isn't the latest head source.

Can you attach your font so that I can try ?
Comment 4 Davide Prade 2019-06-11 14:51:31 UTC
Created attachment 3816 [details]
Font used
Comment 5 Sylvain 2019-06-11 15:03:25 UTC
It doesn't crash with latest, but I guess you need to null-terminated your strings
Comment 6 Davide Prade 2019-06-11 15:05:43 UTC
The crash happens also with null terminated strings and also with other fonts.

  const Uint16 txt1[] = {'P', 'u', 's', 'h', ' ', 'U', 'p', ' ', '1', '\0'};
  const Uint16 txt2[] = {'P', 'u', 's', 'h', ' ', 'U', 'p', ' ', '2', '\0'};
Comment 7 Sylvain 2019-06-11 15:24:31 UTC
I am a little bit sceptical. The test-case was clearly broken.
Did you recompile it ?

I suggest you try with the latest SDL_ttf source, from mercurial. (you'll need probably also head SDL sources).
Comment 8 Davide Prade 2019-06-12 07:16:53 UTC
I have created a minimal project and it works. Probably there are some kind of issues with linked libraries in the original project.
Do you have any idea how to find the problem with the linked libraries?
Comment 9 Sylvain 2019-06-12 07:33:33 UTC
Ok, thant for the feedback, so I close both tickets. (bug 4664 and bug 4663)

You can use valgrind to catch issues !
make sure to clean all project and recompile
Comment 10 Sylvain 2019-06-12 07:34:12 UTC
Marked as resolved
Comment 11 Davide Prade 2019-06-17 10:12:14 UTC
I reopen the issue because I isolate the problem around "SDL_PixelFormat", more precisely in the "SDL_FreeFormat" function that doesn't set "formats" pointer (SDL_pixels.c) to NULL, but I don't know why.

If I redefine "TTF_Render*_Blended" functions the problem is resolved.

---------------------------
inline SDL_Surface *TTF_RenderText_Blended_WRAPPER(TTF_Font *font, const char *text, SDL_Color fg) {
    SDL_Surface *surf = TTF_RenderText_Blended(font, text, fg);
    if (surf->format->refcount == 1) {
        surf->format->refcount++;
    }
    return surf;
}

inline SDL_Surface *TTF_RenderUTF8_Blended_WRAPPER(TTF_Font *font, const char *text, SDL_Color fg) {
    SDL_Surface *surf = TTF_RenderUTF8_Blended(font, text, fg);
    if (surf->form  at->refcount == 1) {
        surf->format->refcount++;
    }
    return surf;
}

inline SDL_Surface *TTF_RenderUNICODE_Blended_WRAPPER(TTF_Font *font, const Uint16 *text, SDL_Color fg) {
    SDL_Surface *surf = TTF_RenderUNICODE_Blended(font, text, fg);
    if (surf->format->refcount == 1) {
        surf->format->refcount++;
    }
    return surf;
}

#define TTF_RenderText_Blended TTF_RenderText_Blended_WRAPPER
#define TTF_RenderUTF8_Blended TTF_RenderUTF8_Blended_WRAPPER
#define TTF_RenderUNICODE_Blended TTF_RenderUNICODE_Blended_WRAPPER
---------------------------



I don't understand why the GDB debugger doesn't go into the "SDL_FreeFormat" function, and seems that manual modification in the same function is completely ignored. Changes to the other functions are taken into account instead (for example "SDL_AllocFormat").
Comment 12 Davide Prade 2019-06-17 13:35:26 UTC
I resolved the issue: it was just a difficult to find linker problem (a CMake WIN32/MINGW IF statement in a dynamic sub-library).