Crash in SDL_TTF

(I’m using SDL 1.2.7, and SDL_TTF 2.0.6)
I found that one of my programs was crashing when I called
TTF_OpenFont(“font.ttf”,32), instead of returning a NULL value as I
expected. (The file was missing)
Looking at the code, I found that TTF_OpenFont is a wrapper for
TTF_OpenFontIndex, which is a wrapper for TTF_OpenFontIndexRW.
The crash is happening in TTF_OpenFontIndexRW, because it’s getting a
NULL pointer and not checking it.
The source of the null pointer is TTF_OpenFontIndex, which calls
SDL_RWFromFile to allocate a SDL_RWops* structure.
When a non-existent file is specified, SDL_RWFromFile returns NULL,which
is passed into TTF_OpenFontIndexRW which uses it without checking for NULL.
The doc page for TTF_OpenFontIndexRW does say that “NOTE: src is not
checked for NULL, so be careful.”, but it’s not clear (unless you have
read the source) that TTF_OpenFont calls the same function, still
without checking.

I think the problem can be fixed by changing just TTF_OpenFontIndex;

TTF_Font* TTF_OpenFontIndex( const char *file, int ptsize, long index )
{
SDL_RWops *src;
src=SDL_RWFromFile(file, “rb”);
if( !src ) {
TTF_SetError( “Couldn’t open file” );
return NULL;
}
return TTF_OpenFontIndexRW(src, 1, ptsize, index);
}

since TTF_OpenFont calls TTF_OpenFontIndex, fixing TTF_OpenFontIndex
should make them both work correctly.

  • Philip D. Bober

Are you putting the font.ttf file into your project folder? That’s one
thing I usually forget to do before I compile and run.

Philip D. Bober

(I’m using SDL 1.2.7, and SDL_TTF 2.0.6)
I found that one of my programs was crashing when I called
TTF_OpenFont(“font.ttf”,32), instead of returning a NULL value as I
expected. (The file was missing)
Looking at the code, I found that TTF_OpenFont is a wrapper for
TTF_OpenFontIndex, which is a wrapper for TTF_OpenFontIndexRW.
The crash is happening in TTF_OpenFontIndexRW, because it’s getting a
NULL pointer and not checking it.
The source of the null pointer is TTF_OpenFontIndex, which calls
SDL_RWFromFile to allocate a SDL_RWops* structure.
When a non-existent file is specified, SDL_RWFromFile returns
NULL,which
is passed into TTF_OpenFontIndexRW which uses it without checking for
NULL.
The doc page for TTF_OpenFontIndexRW does say that “NOTE: src is not
checked for NULL, so be careful.”, but it’s not clear (unless you have
read the source) that TTF_OpenFont calls the same function, still
without checking.

I think the problem can be fixed by changing just TTF_OpenFontIndex;

TTF_Font* TTF_OpenFontIndex( const char *file, int ptsize, long index
)> -----Original Message-----
From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Sent: Monday, February 23, 2004 6:30 PM
To: sdl at libsdl.org
Subject: [SDL] Crash in SDL_TTF
{
SDL_RWops *src;
src=SDL_RWFromFile(file, “rb”);
if( !src ) {
TTF_SetError( “Couldn’t open file” );
return NULL;
}
return TTF_OpenFontIndexRW(src, 1, ptsize, index);
}

since TTF_OpenFont calls TTF_OpenFontIndex, fixing TTF_OpenFontIndex
should make them both work correctly.

  • Philip D. Bober

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Kevin wrote:

Are you putting the font.ttf file into your project folder? That’s one
thing I usually forget to do before I compile and run.

Yes, I was. I found itquite quickly, and it was easy to fix.
The problem isn’t my program not working, it’s SDL_TFF crashing instead
of returning an error code.

-Philip D. Bober

Kevin wrote:

Are you putting the font.ttf file into your project folder? That’s one
thing I usually forget to do before I compile and run.

Yes, I was. I found itquite quickly, and it was easy to fix.
The problem isn’t my program not working, it’s SDL_TFF crashing instead
of returning an error code.

Yes, this is fixed in CVS… thanks!

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment