From aa4c72d4ed1ed1bb48cab99970abc65fda7f73ee Mon Sep 17 00:00:00 2001 From: Mark Kim Date: Sat, 5 Feb 2022 12:12:37 -0500 Subject: [PATCH] macOS path handling is more robust When looking up paths of fonts, preferences, etc., macOS searches two places, the system paths and the user paths under $HOME. This change handles rare scenario where HOME is not set. If HOME is not set, now the application just searches the system path twice instead of searching the system path followed by going into an undefined behavior (possibly segfault.) --- src/macos.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/macos.c b/src/macos.c index 923dc3472..00d34bc0e 100644 --- a/src/macos.c +++ b/src/macos.c @@ -1,7 +1,7 @@ /* macos.c - Copyright (c) 2021 + Copyright (c) 2021-2022 http://www.tuxpaint.org/ This program is free software; you can redistribute it and/or modify @@ -35,11 +35,11 @@ const char *apple_fontsPath(void) static char *p = NULL; if(!p) { - const char *home = getenv("HOME"); + const char *home = getenv("HOME") ? getenv("HOME") : ""; p = malloc(strlen(home) + strlen(MACOS_FONTS_PATH) + 1); - if(p) sprintf(p, MACOS_FONTS_PATH, getenv("HOME")); + if(p) sprintf(p, MACOS_FONTS_PATH, home); else perror("apple_fontsPath"); } @@ -52,11 +52,11 @@ const char *apple_preferencesPath(void) static char *p = NULL; if(!p) { - const char *home = getenv("HOME"); + const char *home = getenv("HOME") ? getenv("HOME") : ""; p = malloc(strlen(home) + strlen(MACOS_PREFERENCES_PATH) + 1); - if(p) sprintf(p, MACOS_PREFERENCES_PATH, getenv("HOME")); + if(p) sprintf(p, MACOS_PREFERENCES_PATH, home); else perror("apple_preferencesPath"); } @@ -75,11 +75,11 @@ const char *apple_picturesPath(void) static char *p = NULL; if(!p) { - const char *home = getenv("HOME"); + const char *home = getenv("HOME") ? getenv("HOME") : ""; p = malloc(strlen(home) + strlen(MACOS_PICTURES_PATH) + 1); - if(p) sprintf(p, MACOS_PICTURES_PATH, getenv("HOME")); + if(p) sprintf(p, MACOS_PICTURES_PATH, home); else perror("apple_picturesPath"); }