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 1531

Summary: File is not getting Close in TTF_OpenFontIndexRW.
Product: SDL_ttf Reporter: Nitz <nitin.j4>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.11   
Hardware: x86   
OS: Linux   

Description Nitz 2012-07-02 00:11:04 UTC
Hi Mr.Sam,
Actually i am using SDL_ttf for some application. While going through the code i found that:

In TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long nFontIndex ) function when if( ! TTF_initialized ) condition gets true, code just set error and return NULL but file is still open, there is no handling to close the file in this condition which causes crash in the code.

Inside this if( ! TTF_initialized )condition there should be some function call also for closing the file i.e SDL_RWclose.

Earlier the code is in SDL_ttf-2.0.11 ver.

TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long nFontIndex )
{
        TTF_Font* font;
        FT_Error error;
        FT_Face face;
        FT_Fixed scale;
        FT_Stream stream;
        int position;

        if ( ! TTF_initialized ) {
                TTF_SetError( "Library not initialized" );
                return NULL;
        }
      //Some code 
}

Fix should be Like this

TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long nFontIndex )
{
        TTF_Font* font;
        FT_Error error;
        FT_Face face;
        FT_Fixed scale;
        FT_Stream stream;
        int position;

        if ( ! TTF_initialized ) {
                SDL_RWclose(src);
                TTF_SetError( "Library not initialized" );
                return NULL;
        }
      //Some code 
}

Thanks for your support.

Best Regards,
NITZ
Comment 1 Sam Lantinga 2012-07-02 05:25:58 UTC
Fixed, thanks!
http://hg.libsdl.org/SDL_ttf/rev/ef14fbc2201c
Comment 2 Nitz 2012-07-05 23:39:01 UTC
(In reply to comment #1)
> Fixed, thanks!
> http://hg.libsdl.org/SDL_ttf/rev/ef14fbc2201c

Mr.Sam kindly apply patch here also:
 /* Check to make sure we can seek in this stream */
        position = SDL_RWtell(src);
        if ( position < 0 ) {
                TTF_SetError( "Can't seek in stream" );
                if ( freesrc ) {
                        SDL_RWclose( src );
                }
                return NULL;
        }

Thanks and Regards
Nitz
Comment 3 Sam Lantinga 2012-07-07 06:01:13 UTC
Whoops, good catch.  Fixed, thanks!
Comment 4 Nitz 2013-04-16 05:46:26 UTC
Hi Mr.Sam
The same patch should be applied here also, like this

/* Check to make sure we can seek in this stream */
   position = SDL_RWtell(src);
   if ( position < 0 ) {
   TTF_SetError( "Can't seek in stream" );
     if ( freesrc ) {
       SDL_RWclose( src );
     }

   return NULL;
   }
 

This is not applied yet, I think it was missed :)
Comment 5 Sam Lantinga 2013-04-17 04:44:11 UTC
It looks like it's fixed to me, did I miss something?
http://hg.libsdl.org/SDL_ttf/rev/e716077c97c5
Comment 6 Sam Lantinga 2017-09-10 05:41:50 UTC
I double checked, and this is fixed.