Skip to content

Commit

Permalink
Fixed bug 3093 - Memory Leak in TTF_RenderUTF8_Blended_Wrapped
Browse files Browse the repository at this point in the history
Pankaj

I have found that in function "SDL_Surface *TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font, const char *text, SDL_Color fg, Uint32 wrapLength)"
It is allocating "str" then allocating "strLines". If "strLines" fails to allocate, error for out of memory is set and returned NULL. But memory allocated for "str" is not freed. There should be code for freeing the memory allocated to "str" in do-while loop where NULL has returned for allocating "strLines"
  • Loading branch information
slouken committed Sep 10, 2017
1 parent 88bf957 commit 90a83a6
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions SDL_ttf.c
Expand Up @@ -1931,7 +1931,7 @@ SDL_Surface *TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,
FT_UInt prev_index = 0;
const int lineSpace = 2;
int line, numLines, rowSize;
char *str, **strLines;
char *str, **strLines, **newLines;
size_t textlen;

TTF_CHECKPOINTER(text, NULL);
Expand Down Expand Up @@ -1964,11 +1964,14 @@ SDL_Surface *TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,
tok = str;
end = str + str_len;
do {
strLines = (char **)SDL_realloc(strLines, (numLines+1)*sizeof(*strLines));
if (!strLines) {
newLines = (char **)SDL_realloc(strLines, (numLines+1)*sizeof(*strLines));
if (!newLines) {
TTF_SetError("Out of memory");
SDL_free(strLines);
SDL_stack_free(str);
return(NULL);
}
strLines = newLines;
strLines[numLines++] = tok;

/* Look for the end of the line */
Expand Down Expand Up @@ -2064,6 +2067,10 @@ SDL_Surface *TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,
if (error) {
TTF_SetFTError("Couldn't find glyph", error);
SDL_FreeSurface(textbuf);
if (strLines) {
SDL_free(strLines);
SDL_stack_free(str);
}
return NULL;
}
glyph = font->current;
Expand Down

0 comments on commit 90a83a6

Please sign in to comment.