From adf4ed960e9200710340c2f9db713b6cdaea1cd9 Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 13 Apr 2018 10:31:21 +0100 Subject: [PATCH] Handle null and empty string calls to retro_opendir with a consistent null return. --- libretro-common/file/retro_dirent.c | 14 ++++++++------ libretro-common/include/retro_dirent.h | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/libretro-common/file/retro_dirent.c b/libretro-common/file/retro_dirent.c index 648739a93c..dc3fa5dde0 100644 --- a/libretro-common/file/retro_dirent.c +++ b/libretro-common/file/retro_dirent.c @@ -106,14 +106,16 @@ struct RDIR *retro_opendir(const char *name) wchar_t *path_wide = NULL; unsigned path_len; #endif - struct RDIR *rdir = (struct RDIR*)calloc(1, sizeof(*rdir)); + struct RDIR *rdir; - if (!rdir||!name) - { - if (rdir) - free(rdir); + /*Reject null or empty string paths*/ + if (!name||(*name==0)) + return NULL; + + /*Allocate RDIR struct. Tidied later with retro_closedir*/ + rdir = (struct RDIR*)calloc(1, sizeof(*rdir)); + if (!rdir) return NULL; - } #if defined(_WIN32) (void)path_wide; diff --git a/libretro-common/include/retro_dirent.h b/libretro-common/include/retro_dirent.h index 090076ec64..51b7eac700 100644 --- a/libretro-common/include/retro_dirent.h +++ b/libretro-common/include/retro_dirent.h @@ -32,6 +32,16 @@ RETRO_BEGIN_DECLS typedef struct RDIR RDIR; +/** + * + * retro_opendir: + * @name : path to the directory to open. + * + * Opens a directory for reading. Tidy up with retro_closedir. + * + * Returns: RDIR pointer on success, NULL if name is not a + * valid directory, null itself or the empty string. + */ struct RDIR *retro_opendir(const char *name); int retro_readdir(struct RDIR *rdir);