[libbluray-devel] Check malloc/calloc result

hpi1 git at videolan.org
Thu May 7 11:50:37 CEST 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Thu May  7 12:03:01 2015 +0300| [ccbc5864ef9ce7561774b8af2796e2a65ba86edf] | committer: hpi1

Check malloc/calloc result

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=ccbc5864ef9ce7561774b8af2796e2a65ba86edf
---

 src/file/dirs_win32.c |   17 +++++++++++------
 src/file/file_win32.c |   50 ++++++++++++++++++++++++++++++-------------------
 src/util/strutl.c     |   11 ++++++++++-
 3 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/src/file/dirs_win32.c b/src/file/dirs_win32.c
index 60bfbf8..5279ea5 100644
--- a/src/file/dirs_win32.c
+++ b/src/file/dirs_win32.c
@@ -32,7 +32,6 @@
 #include <windows.h>
 #include <shlobj.h>
 #include <limits.h>
-#include <direct.h>
 
 
 char *win32_get_font_dir(const char *font_file)
@@ -45,9 +44,11 @@ char *win32_get_font_dir(const char *font_file)
 
     int   len  = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
     char *path = malloc(len + strlen(font_file) + 2);
-    WideCharToMultiByte(CP_UTF8, 0, wdir, -1, path, len, NULL, NULL);
-    path[len - 1] = '\\';
-    strcpy(path + len, font_file);
+    if (path) {
+        WideCharToMultiByte(CP_UTF8, 0, wdir, -1, path, len, NULL, NULL);
+        path[len - 1] = '\\';
+        strcpy(path + len, font_file);
+    }
     return path;
 }
 
@@ -65,7 +66,9 @@ char *file_get_data_home(void)
                                  NULL, SHGFP_TYPE_CURRENT, wdir)) {
         int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
         char *appdir = malloc(len);
-        WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
+        if (appdir) {
+            WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
+        }
         return appdir;
     }
 
@@ -94,7 +97,9 @@ const char *file_get_config_system(const char *dir)
                     NULL, SHGFP_TYPE_CURRENT, wdir)) {
             int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
             appdir = malloc(len);
-            WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
+            if (appdir) {
+                WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
+            }
             return appdir;
         } else {
             BD_DEBUG(DBG_FILE, "Can't find common configuration directory !\n");
diff --git a/src/file/file_win32.c b/src/file/file_win32.c
index ca81ccd..5eb52d7 100644
--- a/src/file/file_win32.c
+++ b/src/file/file_win32.c
@@ -95,28 +95,40 @@ static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
 
 static BD_FILE_H *_file_open(const char* filename, const char *mode)
 {
-    FILE *fp = NULL;
-
+    BD_FILE_H *file;
+    FILE *fp;
     wchar_t wfilename[MAX_PATH], wmode[8];
-    if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) &&
-        MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8) &&
-        (fp = _wfopen(wfilename, wmode))) {
-
-        BD_FILE_H *file = calloc(1, sizeof(BD_FILE_H));
-        file->internal = fp;
-        file->close    = _file_close;
-        file->seek     = _file_seek;
-        file->read     = _file_read;
-        file->write    = _file_write;
-        file->tell     = _file_tell;
-        //file->eof      = _file_eof;
-
-        BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
-        return file;
+
+    if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) ||
+        !MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8)) {
+
+        BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
+        return NULL;
+    }
+
+    fp = _wfopen(wfilename, wmode);
+    if (!fp) {
+        BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
+        return NULL;
+    }
+
+    file = calloc(1, sizeof(BD_FILE_H));
+    if (!file) {
+        BD_DEBUG(DBG_FILE | DBG_CRIT, "Error opening file %s (out of memory)\n", filename);
+        fclose(fp);
+        return NULL;
     }
 
-    BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
-    return NULL;
+    file->internal = fp;
+    file->close    = _file_close;
+    file->seek     = _file_seek;
+    file->read     = _file_read;
+    file->write    = _file_write;
+    file->tell     = _file_tell;
+    //file->eof      = _file_eof;
+
+    BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
+    return file;
 }
 
 BD_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
diff --git a/src/util/strutl.c b/src/util/strutl.c
index beae4a6..4712373 100644
--- a/src/util/strutl.c
+++ b/src/util/strutl.c
@@ -33,7 +33,16 @@
 
 char *str_dup(const char *str)
 {
-    return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
+    char *dup = NULL;
+
+    if (str) {
+        size_t size = strlen(str) + 1;
+        dup = malloc(size);
+        if (dup) {
+            memcpy(dup, str, size);
+        }
+    }
+    return dup;
 }
 
 char *str_printf(const char *fmt, ...)



More information about the libbluray-devel mailing list