[libbluray-devel] Check calloc result

hpi1 git at videolan.org
Thu Jun 4 14:45:43 CEST 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Thu Jun  4 14:29:44 2015 +0300| [3c43b555423cd6b5f9714746cfa52a65c935cc56] | committer: hpi1

Check calloc result

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

 src/libbluray/bdj/bdj.c                |   13 ++++++++++++-
 src/libbluray/bdnav/index_parse.c      |    8 ++++++++
 src/libbluray/bdnav/meta_parse.c       |    4 ++++
 src/libbluray/bdnav/sound_parse.c      |   33 ++++++++++++++++++++++++--------
 src/libbluray/bluray.c                 |    4 +++-
 src/libbluray/decoders/textst_render.c |    4 ++++
 6 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/src/libbluray/bdj/bdj.c b/src/libbluray/bdj/bdj.c
index ef9452c..6747972 100644
--- a/src/libbluray/bdj/bdj.c
+++ b/src/libbluray/bdj/bdj.c
@@ -464,6 +464,11 @@ static int _create_jvm(void *jvm_lib, const char *java_home, const char *jar_fil
     }
 
     JavaVMOption* option = calloc(1, sizeof(JavaVMOption) * 20);
+    if (!option) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        return 0;
+    }
+
     int n = 0;
     JavaVMInitArgs args;
     option[n++].optionString = str_dup   ("-Dawt.toolkit=java.awt.BDToolkit");
@@ -538,16 +543,22 @@ BDJAVA* bdj_open(const char *path, struct bluray *bd,
         return 0;
     }
 
+    BDJAVA* bdjava = calloc(1, sizeof(BDJAVA));
+    if (!bdjava) {
+        dl_dlclose(jvm_lib);
+        return NULL;
+    }
+
     JNIEnv* env = NULL;
     JavaVM *jvm = NULL;
     if (!_find_jvm(jvm_lib, &env, &jvm) &&
         !_create_jvm(jvm_lib, java_home, jar_file, &env, &jvm)) {
 
+        X_FREE(bdjava);
         dl_dlclose(jvm_lib);
         return NULL;
     }
 
-    BDJAVA* bdjava = calloc(1, sizeof(BDJAVA));
     bdjava->h_libjvm = jvm_lib;
     bdjava->jvm = jvm;
 
diff --git a/src/libbluray/bdnav/index_parse.c b/src/libbluray/bdnav/index_parse.c
index 6c07ba1..64dc5e3 100644
--- a/src/libbluray/bdnav/index_parse.c
+++ b/src/libbluray/bdnav/index_parse.c
@@ -103,8 +103,16 @@ static int _parse_index(BITSTREAM *bs, INDX_ROOT *index)
     }
 
     index->num_titles = bs_read(bs, 16);
+    if (!index->num_titles) {
+        BD_DEBUG(DBG_CRIT, "empty index\n");
+        return 0;
+    }
 
     index->titles = calloc(index->num_titles, sizeof(INDX_TITLE));
+    if (!index->titles) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        return 0;
+    }
 
     for (i = 0; i < index->num_titles; i++) {
 
diff --git a/src/libbluray/bdnav/meta_parse.c b/src/libbluray/bdnav/meta_parse.c
index 50b8c75..d7a7c6c 100644
--- a/src/libbluray/bdnav/meta_parse.c
+++ b/src/libbluray/bdnav/meta_parse.c
@@ -145,6 +145,10 @@ META_ROOT *meta_parse(BD_DISC *disc)
 {
 #ifdef HAVE_LIBXML2
     META_ROOT *root = calloc(1, sizeof(META_ROOT));
+    if (!root) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        return NULL;
+    }
     root->dl_count = 0;
 
     xmlDocPtr doc;
diff --git a/src/libbluray/bdnav/sound_parse.c b/src/libbluray/bdnav/sound_parse.c
index c1cbcfb..677eb2a 100644
--- a/src/libbluray/bdnav/sound_parse.c
+++ b/src/libbluray/bdnav/sound_parse.c
@@ -103,7 +103,15 @@ static int _sound_read_samples(BITSTREAM *bs, SOUND_OBJECT *obj)
     uint32_t n;
     uint32_t num_samples = obj->num_frames * obj->num_channels;
 
+    if (!num_samples) {
+        return 1;
+    }
+
     obj->samples = calloc(num_samples, sizeof(uint16_t));
+    if (!obj->samples) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        return 0;
+    }
 
     for (n = 0; n < num_samples; n++) {
         obj->samples[n] = bs_read(bs, 16);
@@ -116,13 +124,14 @@ void sound_free(SOUND_DATA **p)
 {
     if (p && *p) {
 
-        unsigned i;
-        for (i = 0 ; i < (*p)->num_sounds; i++) {
-            X_FREE((*p)->sounds[i].samples);
-        }
-
-        X_FREE((*p)->sounds);
+        if ((*p)->sounds) {
+            unsigned i;
+            for (i = 0 ; i < (*p)->num_sounds; i++) {
+                X_FREE((*p)->sounds[i].samples);
+            }
 
+            X_FREE((*p)->sounds);
+        }
         X_FREE(*p);
     }
 }
@@ -150,21 +159,29 @@ static SOUND_DATA *_sound_parse(BD_FILE_H *fp)
     bs_skip(&bs, 8); /* reserved */
     num_sounds = bs_read(&bs, 8);
 
-    if (data_len < 1) {
+    if (data_len < 1 || num_sounds < 1) {
         BD_DEBUG(DBG_NAV | DBG_CRIT, "empty database\n");
         goto error;
     }
 
     data_offsets = calloc(num_sounds, sizeof(uint32_t));
     data = calloc(1, sizeof(SOUND_DATA));
+    if (!data_offsets | !data) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        goto error;
+    }
     data->num_sounds = num_sounds;
     data->sounds = calloc(num_sounds, sizeof(SOUND_OBJECT));
+    if (!data->sounds) {
+        BD_DEBUG(DBG_CRIT, "out of memory\n");
+        goto error;
+    }
 
     /* parse headers */
 
     for (i = 0; i < data->num_sounds; i++) {
         if (!_sound_parse_index(&bs, data_offsets + i, &data->sounds[i])) {
-            BD_DEBUG(DBG_NAV | DBG_CRIT, "error parsing sound %d attribues\n", i);
+            BD_DEBUG(DBG_NAV | DBG_CRIT, "error parsing sound %d attributes\n", i);
             goto error;
         }
     }
diff --git a/src/libbluray/bluray.c b/src/libbluray/bluray.c
index 98781e2..8a61e94 100644
--- a/src/libbluray/bluray.c
+++ b/src/libbluray/bluray.c
@@ -202,7 +202,9 @@ static void _init_event_queue(BLURAY *bd)
 {
     if (!bd->event_queue) {
         bd->event_queue = calloc(1, sizeof(struct bd_event_queue_s));
-        bd_mutex_init(&bd->event_queue->mutex);
+        if (bd->event_queue) {
+            bd_mutex_init(&bd->event_queue->mutex);
+        }
     } else {
         bd_mutex_lock(&bd->event_queue->mutex);
         bd->event_queue->in  = 0;
diff --git a/src/libbluray/decoders/textst_render.c b/src/libbluray/decoders/textst_render.c
index 8d1527e..0e87d4b 100644
--- a/src/libbluray/decoders/textst_render.c
+++ b/src/libbluray/decoders/textst_render.c
@@ -74,6 +74,10 @@ TEXTST_RENDER *textst_render_init(void)
 #ifdef HAVE_FT2
     TEXTST_RENDER *p = calloc(1, sizeof(TEXTST_RENDER));
 
+    if (!p) {
+        return NULL;
+    }
+
     if (!FT_Init_FreeType(&p->ft_lib)) {
         return p;
     }



More information about the libbluray-devel mailing list