diff options
author | Alexander Larsson <alexl@redhat.com> | 2007-10-16 07:39:11 +0000 |
---|---|---|
committer | Alexander Larsson <alexl@redhat.com> | 2007-10-16 07:39:11 +0000 |
commit | c567c1efcf1b44d6666d67a68213a8a9ba1e202b (patch) | |
tree | 8ee3540f1b43f24540fc07923e5c7cd5dd65ac70 /xdg-user-dir-lookup.c | |
parent | d3dafaef278dcfccff58efb760c3aec147d468e2 (diff) |
2007-10-16 Alexander Larsson <alexl@redhat.com>
* xdg-user-dir-lookup.c:
Handle out of memory and compile with C++ compiler. (#12738)
Patch from Christian Persch
Diffstat (limited to 'xdg-user-dir-lookup.c')
-rw-r--r-- | xdg-user-dir-lookup.c | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/xdg-user-dir-lookup.c b/xdg-user-dir-lookup.c index 6863772..38fb04c 100644 --- a/xdg-user-dir-lookup.c +++ b/xdg-user-dir-lookup.c @@ -42,7 +42,8 @@ * type the value returned is @fallback. * * The return value is newly allocated and must be freed with - * free(). The return value is never NULL if @fallback != NULL. + * free(). The return value is never NULL if @fallback != NULL, unless + * out of memory. **/ static char * xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback) @@ -63,13 +64,19 @@ xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback) config_home = getenv ("XDG_CONFIG_HOME"); if (config_home == NULL || config_home[0] == 0) { - config_file = malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1); + config_file = (char*) malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1); + if (config_file == NULL) + goto error; + strcpy (config_file, home_dir); strcat (config_file, "/.config/user-dirs.dirs"); } else { - config_file = malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1); + config_file = (char*) malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1); + if (config_file == NULL) + goto error; + strcpy (config_file, config_home); strcat (config_file, "/user-dirs.dirs"); } @@ -126,13 +133,19 @@ xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback) if (relative) { - user_dir = malloc (strlen (home_dir) + 1 + strlen (p) + 1); + user_dir = (char*) malloc (strlen (home_dir) + 1 + strlen (p) + 1); + if (user_dir == NULL) + goto error2; + strcpy (user_dir, home_dir); strcat (user_dir, "/"); } else { - user_dir = malloc (strlen (p) + 1); + user_dir = (char*) malloc (strlen (p) + 1); + if (user_dir == NULL) + goto error2; + *user_dir = 0; } @@ -144,7 +157,8 @@ xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback) *d++ = *p++; } *d = 0; - } + } +error2: fclose (file); if (user_dir) @@ -164,7 +178,8 @@ xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback) * Looks up a XDG user directory of the specified type. * Example of types are "DESKTOP" and "DOWNLOAD". * - * The return value is always != NULL, and if a directory + * The return value is always != NULL (unless out of memory), + * and if a directory * for the type is not specified by the user the default * is the home directory. Except for DESKTOP which defaults * to ~/Desktop. @@ -189,7 +204,10 @@ xdg_user_dir_lookup (const char *type) /* Special case desktop for historical compatibility */ if (strcmp (type, "DESKTOP") == 0) { - user_dir = malloc (strlen (home_dir) + strlen ("/Desktop") + 1); + user_dir = (char*) malloc (strlen (home_dir) + strlen ("/Desktop") + 1); + if (user_dir == NULL) + return NULL; + strcpy (user_dir, home_dir); strcat (user_dir, "/Desktop"); return user_dir; |