Skip to content

Commit

Permalink
Fix null pointer crash if an error occurs when trying to makea sysem …
Browse files Browse the repository at this point in the history
…font (Fixes #529, thanks igor725)
  • Loading branch information
UnknownShadow200 committed Jun 10, 2019
1 parent f34011b commit ad314a5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Audio.c
Expand Up @@ -585,7 +585,7 @@ static struct SoundOutput monoOutputs[AUDIO_MAX_HANDLES] = { SOUND_INV, SOUND_
static struct SoundOutput stereoOutputs[AUDIO_MAX_HANDLES] = { SOUND_INV, SOUND_INV, SOUND_INV, SOUND_INV, SOUND_INV, SOUND_INV };

CC_NOINLINE static void Sounds_Fail(ReturnCode res) {
Logger_OldWarn(res, "playing sounds");
Logger_SimpleWarn(res, "playing sounds");
Chat_AddRaw("&cDisabling sounds");
Audio_SetSounds(0);
}
Expand Down Expand Up @@ -850,7 +850,7 @@ static void Music_RunLoop(void) {

res = Music_PlayOgg(&stream);
if (res) {
Logger_OldWarn2(res, "playing", &path);
Logger_SimpleWarn2(res, "playing", &path);
stream.Close(&stream); break;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Drawer2D.c
Expand Up @@ -29,12 +29,14 @@ void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, const FontDesc* font, boo
}

void Drawer2D_MakeFont(FontDesc* desc, int size, int style) {
ReturnCode res;
if (Drawer2D_BitmappedText) {
desc->Handle = NULL;
desc->Size = size;
desc->Style = style;
} else {
Font_Make(desc, &Drawer2D_FontName, size, style);
res = Font_Make(desc, &Drawer2D_FontName, size, style);
if (res) Logger_Abort2(res, "Making default font failed");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Launcher.c
Expand Up @@ -75,10 +75,10 @@ bool Launcher_ConnectToServer(const String* hash) {
Launcher_StartFromInfo(&FetchServerTask.Server);
return true;
} else if (FetchServerTask.Base.Res) {
Logger_OldWarn(FetchServerTask.Base.Res, "fetching server info");
Logger_SimpleWarn(FetchServerTask.Base.Res, "fetching server info");
} else if (FetchServerTask.Base.Status != 200) {
/* TODO: Use a better dialog message.. */
Logger_OldWarn(FetchServerTask.Base.Status, "fetching server info");
Logger_SimpleWarn(FetchServerTask.Base.Status, "fetching server info");
} else {
Window_ShowDialog("Failed to connect", "No server has that hash");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Logger.c
Expand Up @@ -52,15 +52,15 @@ void Logger_DialogWarn(const String* msg) {
const char* Logger_DialogTitle = "Error";
Logger_DoWarn Logger_WarnFunc = Logger_DialogWarn;

void Logger_OldWarn(ReturnCode res, const char* place) {
void Logger_SimpleWarn(ReturnCode res, const char* place) {
String msg; char msgBuffer[128];
String_InitArray(msg, msgBuffer);

String_Format2(&msg, "Error %h when %c", &res, place);
Logger_WarnFunc(&msg);
}

void Logger_OldWarn2(ReturnCode res, const char* place, const String* path) {
void Logger_SimpleWarn2(ReturnCode res, const char* place, const String* path) {
String msg; char msgBuffer[256];
String_InitArray(msg, msgBuffer);

Expand Down
4 changes: 2 additions & 2 deletions src/Logger.h
Expand Up @@ -15,9 +15,9 @@ extern const char* Logger_DialogTitle;
void Logger_DialogWarn(const String* msg);

/* Informs the user about a non-fatal error, with a message of form: "Error [result] when [place] */
void Logger_OldWarn(ReturnCode res, const char* place);
void Logger_SimpleWarn(ReturnCode res, const char* place);
/* Informs the user about a non-fatal error, with a message of form: "Error [result] when [place] '[path]' */
void Logger_OldWarn2(ReturnCode res, const char* place, const String* path);
void Logger_SimpleWarn2(ReturnCode res, const char* place, const String* path);
/* Informs the user about a non-fatal error, with a message of either:
"Error [error desc] ([result]) when [place] or "Error [result] when [place] */
void Logger_SysWarn(ReturnCode res, const char* place, Logger_DescribeError describeErr);
Expand Down
19 changes: 13 additions & 6 deletions src/Menus.c
Expand Up @@ -1423,14 +1423,21 @@ static void FontListScreen_EntryClick(void* screen, void* widget) {
}

static void FontListScreen_UpdateEntry(struct ListScreen* s, struct ButtonWidget* button, const String* text) {
FontDesc font = { 0 };
FontDesc font;
ReturnCode res;

if (String_CaselessEqualsConst(text, LIST_SCREEN_EMPTY)) {
ButtonWidget_Set(button, text, &s->font);
} else {
Font_Make(&font, text, 16, FONT_STYLE_NORMAL);
ButtonWidget_Set(button, text, &s->font); return;
}

res = Font_Make(&font, text, 16, FONT_STYLE_NORMAL);
if (!res) {
ButtonWidget_Set(button, text, &font);
Font_Free(&font);
} else {
Logger_SimpleWarn2(res, "making font", text);
ButtonWidget_Set(button, text, &s->font);
}
Font_Free(&font);
}

static void FontListScreen_Init(void* screen) {
Expand All @@ -1446,7 +1453,7 @@ static struct ScreenVTABLE FontListScreen_VTABLE = {
Menu_OnResize, Menu_ContextLost, ListScreen_ContextRecreated,
};
struct Screen* FontListScreen_MakeInstance(void) {
const static String title = String_FromConst("Select a font");
const static String title = String_FromConst("Select a font");
struct ListScreen* s = ListScreen_MakeInstance();

s->titleText = title;
Expand Down
33 changes: 17 additions & 16 deletions src/Platform.c
Expand Up @@ -884,12 +884,13 @@ static void FontData_Close(FT_Stream stream) {
FontData_Free(data);
}

static bool FontData_Init(const String* path, struct FontData* data, FT_Open_Args* args) {
static ReturnCode FontData_Init(const String* path, struct FontData* data, FT_Open_Args* args) {
FileHandle file;
uint32_t size;
ReturnCode res;

if (File_Open(&file, path)) return false;
if (File_Length(file, &size)) { File_Close(file); return false; }
if ((res = File_Open(&file, path))) return res;
if ((res = File_Length(file, &size))) { File_Close(file); return res; }

data->stream.base = NULL;
data->stream.size = size;
Expand Down Expand Up @@ -920,7 +921,7 @@ static bool FontData_Init(const String* path, struct FontData* data, FT_Open_Arg
Mem_Set(data->widths, 0xFF, sizeof(data->widths));
Mem_Set(data->glyphs, 0x00, sizeof(data->glyphs));
Mem_Set(data->shadow_glyphs, 0x00, sizeof(data->shadow_glyphs));
return true;
return 0;
}

void Font_GetNames(StringsBuffer* buffer) {
Expand Down Expand Up @@ -958,29 +959,30 @@ String Font_Lookup(const String* fontName, int style) {
return path.length ? path : Font_LookupOf(fontName, 'R');
}

void Font_Make(FontDesc* desc, const String* fontName, int size, int style) {
ReturnCode Font_Make(FontDesc* desc, const String* fontName, int size, int style) {
struct FontData* data;
String value, path, index;
int faceIndex;
FT_Open_Args args;
FT_Error err;

desc->Size = size;
desc->Style = style;
desc->Size = size;
desc->Style = style;
desc->Handle = NULL;

value = Font_Lookup(fontName, style);
if (!value.length) Logger_Abort("Unknown font");
String_UNSAFE_Separate(&value, ',', &path, &index);
Convert_ParseInt(&index, &faceIndex);

data = (struct FontData*)Mem_Alloc(1, sizeof(struct FontData), "FontData");
if (!FontData_Init(&path, data, &args)) return;
if ((err = FontData_Init(&path, data, &args))) { Mem_Free(data); return err; }
desc->Handle = data;

err = FT_New_Face(ft_lib, &args, faceIndex, &data->face);
if (err) Logger_Abort2(err, "Creating font failed");
err = FT_Set_Char_Size(data->face, size * 64, 0, DPI_DEVICE, 0);
if (err) Logger_Abort2(err, "Resizing font failed");
if ((err = FT_New_Face(ft_lib, &args, faceIndex, &data->face))) return err;
if ((err = FT_Set_Char_Size(data->face, size * 64, 0, DPI_DEVICE, 0))) return err;

return 0;
}

void Font_Free(FontDesc* desc) {
Expand Down Expand Up @@ -1033,7 +1035,7 @@ static int Font_Register(const String* path, int faceIndex) {
FT_Error err;
int flags, count;

if (!FontData_Init(path, &data, &args)) return 0;
if (FontData_Init(path, &data, &args)) return 0;
err = FT_New_Face(ft_lib, &args, faceIndex, &data.face);
if (err) { FontData_Free(&data); return 0; }

Expand Down Expand Up @@ -1308,15 +1310,14 @@ String Font_Lookup(const String* fontName, int style) {
String str = String_FromConst("-----"); return str;
}

void Font_Make(FontDesc* desc, const String* fontName, int size, int style) {
ReturnCode Font_Make(FontDesc* desc, const String* fontName, int size, int style) {
desc->Size = size;
desc->Style = style;
desc->Handle = NULL;
return 0;
}
void Font_Free(FontDesc* desc) {
desc->Size = 0;
desc->Style = 0;
desc->Handle = NULL;
}

int Platform_TextWidth(struct DrawTextArgs* args) { return 0; }
Expand Down
2 changes: 1 addition & 1 deletion src/Platform.h
Expand Up @@ -195,7 +195,7 @@ void Font_GetNames(StringsBuffer* buffer);
/* Finds the path and face number of the given font, with closest matching style */
String Font_Lookup(const String* fontName, int style);
/* Allocates a new font from the given arguments. */
void Font_Make(FontDesc* desc, const String* fontName, int size, int style);
ReturnCode Font_Make(FontDesc* desc, const String* fontName, int size, int style);
/* Frees an allocated font. */
CC_API void Font_Free(FontDesc* desc);
/* Measures width of the given text when drawn with the given font. */
Expand Down

0 comments on commit ad314a5

Please sign in to comment.