[libbluray-devel] Check allocs, improve readability

hpi1 git at videolan.org
Sun Aug 7 16:13:48 CEST 2016


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Sat Aug  6 22:35:10 2016 +0300| [0295c7163d9b2d4fcb2093e1d73ae420df2a3d2d] | committer: hpi1

Check allocs, improve readability

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

 src/file/dir_win32.c | 57 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 18 deletions(-)

diff --git a/src/file/dir_win32.c b/src/file/dir_win32.c
index 9e3190a..13fd3ef 100644
--- a/src/file/dir_win32.c
+++ b/src/file/dir_win32.c
@@ -72,37 +72,58 @@ static int _dir_read_win32(BD_DIR_H *dir, BD_DIRENT *entry)
     return 0;
 }
 
+static dir_data_t *_open_impl(const char *dirname)
+{
+    dir_data_t *priv;
+    char *filespec;
+    wchar_t wfilespec[MAX_PATH];
+    int result;
+
+    filespec = str_printf("%s/*", dirname);
+    if (!filespec) {
+        return NULL;
+    }
+
+    result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filespec, -1, wfilespec, MAX_PATH);
+    X_FREE(filespec);
+    if (!result) {
+        return NULL;
+    }
+
+    priv = calloc(1, sizeof(dir_data_t));
+    if (!priv) {
+        return NULL;
+    }
+
+    priv->handle = _wfindfirst(wfilespec, &priv->info);
+    if (priv->handle == -1) {
+        X_FREE(priv);
+    }
+
+    return priv;
+}
+
 static BD_DIR_H *_dir_open_win32(const char* dirname)
 {
     BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H));
+    dir_data_t *priv = NULL;
 
     BD_DEBUG(DBG_DIR, "Opening WIN32 dir %s... (%p)\n", dirname, (void*)dir);
+
     if (!dir) {
         return NULL;
     }
-    dir->close = _dir_close_win32;
-    dir->read = _dir_read_win32;
 
-    char       *filespec = str_printf("%s/*", dirname);
-    dir_data_t *priv     = calloc(1, sizeof(dir_data_t));
+    priv = _open_impl(dirname);
+    if (priv) {
+        dir->close = _dir_close_win32;
+        dir->read = _dir_read_win32;
+        dir->internal = priv;
 
-    dir->internal = priv;
-
-    wchar_t wfilespec[MAX_PATH];
-    if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filespec, -1, wfilespec, MAX_PATH))
-        priv->handle = _wfindfirst(wfilespec, &priv->info);
-    else
-        priv->handle = -1;
-
-    X_FREE(filespec);
-
-    if (priv->handle != -1) {
         return dir;
     }
 
-    BD_DEBUG(DBG_DIR, "Error opening dir! (%p)\n", (void*)dir);
-
-    X_FREE(dir->internal);
+    BD_DEBUG(DBG_DIR, "Error opening dir %s\n", dirname);
     X_FREE(dir);
 
     return NULL;



More information about the libbluray-devel mailing list