[libbluray-devel] bits: check for errors in bs_init()

hpi1 git at videolan.org
Mon Aug 8 18:26:55 CEST 2016


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Mon Aug  8 17:09:32 2016 +0300| [6679b6a176aa9d05dbac8b3ba5a4b62b73057a9b] | committer: hpi1

bits: check for errors in bs_init()

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

 src/libbluray/bdj/bdjo_parse.c    |  5 ++++-
 src/libbluray/bdnav/bdid_parse.c  |  5 ++++-
 src/libbluray/bdnav/clpi_parse.c  |  6 +++++-
 src/libbluray/bdnav/index_parse.c | 10 +++++++---
 src/libbluray/bdnav/mpls_parse.c  |  7 +++++--
 src/libbluray/bdnav/sound_parse.c |  5 ++++-
 src/libbluray/hdmv/mobj_parse.c   |  5 ++++-
 src/util/bits.c                   | 29 +++++++++++++++++++++--------
 src/util/bits.h                   |  2 +-
 9 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/src/libbluray/bdj/bdjo_parse.c b/src/libbluray/bdj/bdjo_parse.c
index 254b1e1..35f4b69 100644
--- a/src/libbluray/bdj/bdjo_parse.c
+++ b/src/libbluray/bdj/bdjo_parse.c
@@ -507,7 +507,10 @@ static BDJO *_bdjo_parse(BD_FILE_H *fp)
     BITSTREAM   bs;
     BDJO       *p;
 
-    bs_init(&bs, fp);
+    if (bs_init(&bs, fp) < 0) {
+        BD_DEBUG(DBG_BDJ, "?????.bdjo: read error\n");
+        return NULL;
+    }
 
     p = calloc(1, sizeof(BDJO));
     if (!p) {
diff --git a/src/libbluray/bdnav/bdid_parse.c b/src/libbluray/bdnav/bdid_parse.c
index 512cd8e..2ba47fc 100644
--- a/src/libbluray/bdnav/bdid_parse.c
+++ b/src/libbluray/bdnav/bdid_parse.c
@@ -67,7 +67,10 @@ static BDID_DATA *_bdid_parse(BD_FILE_H *fp)
     uint32_t   data_start, extension_data_start;
     uint8_t    tmp[16];
 
-    bs_init(&bs, fp);
+    if (bs_init(&bs, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "id.bdmv: read error\n");
+        return NULL;
+    }
 
     if (!_parse_header(&bs, &data_start, &extension_data_start)) {
         BD_DEBUG(DBG_NAV | DBG_CRIT, "id.bdmv: invalid header\n");
diff --git a/src/libbluray/bdnav/clpi_parse.c b/src/libbluray/bdnav/clpi_parse.c
index 394347e..4838261 100644
--- a/src/libbluray/bdnav/clpi_parse.c
+++ b/src/libbluray/bdnav/clpi_parse.c
@@ -680,13 +680,17 @@ _clpi_parse(BD_FILE_H *fp)
     BITSTREAM  bits;
     CLPI_CL   *cl;
 
+    if (bs_init(&bits, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "?????.clpi: read error\n");
+        return NULL;
+    }
+
     cl = calloc(1, sizeof(CLPI_CL));
     if (cl == NULL) {
         BD_DEBUG(DBG_CRIT, "out of memory\n");
         return NULL;
     }
 
-    bs_init(&bits, fp);
     if (!_parse_header(&bits, cl)) {
         clpi_free(cl);
         return NULL;
diff --git a/src/libbluray/bdnav/index_parse.c b/src/libbluray/bdnav/index_parse.c
index 64dc5e3..4585a13 100644
--- a/src/libbluray/bdnav/index_parse.c
+++ b/src/libbluray/bdnav/index_parse.c
@@ -194,16 +194,20 @@ static int _parse_header(BITSTREAM *bs, int *index_start, int *extension_data_st
 static INDX_ROOT *_indx_parse(BD_FILE_H *fp)
 {
     BITSTREAM  bs;
-    INDX_ROOT *index = calloc(1, sizeof(INDX_ROOT));
+    INDX_ROOT *index;
     int        indexes_start, extension_data_start;
 
+    if (bs_init(&bs, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "index.bdmv: read error\n");
+        return NULL;
+    }
+
+    index = calloc(1, sizeof(INDX_ROOT));
     if (!index) {
         BD_DEBUG(DBG_CRIT, "out of memory\n");
         return NULL;
     }
 
-    bs_init(&bs, fp);
-
     if (!_parse_header(&bs, &indexes_start, &extension_data_start) ||
         !_parse_app_info(&bs, &index->app_info)) {
 
diff --git a/src/libbluray/bdnav/mpls_parse.c b/src/libbluray/bdnav/mpls_parse.c
index e196118..62dc243 100644
--- a/src/libbluray/bdnav/mpls_parse.c
+++ b/src/libbluray/bdnav/mpls_parse.c
@@ -914,14 +914,17 @@ _mpls_parse(BD_FILE_H *fp)
     BITSTREAM  bits;
     MPLS_PL   *pl = NULL;
 
+    if (bs_init(&bits, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "?????.mpls: read error\n");
+        return NULL;
+    }
+
     pl = calloc(1, sizeof(MPLS_PL));
     if (pl == NULL) {
         BD_DEBUG(DBG_CRIT, "out of memory\n");
         return NULL;
     }
 
-    bs_init(&bits, fp);
-
     if (!_parse_header(&bits, pl)) {
         _clean_playlist(pl);
         return NULL;
diff --git a/src/libbluray/bdnav/sound_parse.c b/src/libbluray/bdnav/sound_parse.c
index 7c267da..1df8ec2 100644
--- a/src/libbluray/bdnav/sound_parse.c
+++ b/src/libbluray/bdnav/sound_parse.c
@@ -149,7 +149,10 @@ static SOUND_DATA *_sound_parse(BD_FILE_H *fp)
     uint32_t      data_start, extension_data_start;
     uint32_t     *data_offsets = NULL;
 
-    bs_init(&bs, fp);
+    if (bs_init(&bs, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "sound.bdmv: read error\n");
+        goto error;
+    }
 
     if (!_bclk_parse_header(&bs, &data_start, &extension_data_start)) {
         BD_DEBUG(DBG_NAV | DBG_CRIT, "invalid header\n");
diff --git a/src/libbluray/hdmv/mobj_parse.c b/src/libbluray/hdmv/mobj_parse.c
index 97bc3ff..c7b5413 100644
--- a/src/libbluray/hdmv/mobj_parse.c
+++ b/src/libbluray/hdmv/mobj_parse.c
@@ -136,7 +136,10 @@ static MOBJ_OBJECTS *_mobj_parse(BD_FILE_H *fp)
     uint32_t      data_len;
     int           extension_data_start, i;
 
-    bs_init(&bs, fp);
+    if (bs_init(&bs, fp) < 0) {
+        BD_DEBUG(DBG_NAV, "MovieObject.bdmv: read error\n");
+        goto error;;
+    }
 
     if (!_mobj_parse_header(&bs, &extension_data_start)) {
         BD_DEBUG(DBG_NAV | DBG_CRIT, "MovieObject.bdmv: invalid header\n");
diff --git a/src/util/bits.c b/src/util/bits.c
index 850031f..c595934 100644
--- a/src/util/bits.c
+++ b/src/util/bits.c
@@ -40,19 +40,32 @@ void bb_init( BITBUFFER *bb, const uint8_t *p_data, size_t i_data )
     bb->i_left  = 8;
 }
 
-void bs_init( BITSTREAM *bs, BD_FILE_H *fp )
+static int _bs_read( BITSTREAM *bs)
+{
+    int result = 0;
+    int64_t got;
+
+    got = file_read(bs->fp, bs->buf, BF_BUF_SIZE);
+    if (got <= 0 || got > BF_BUF_SIZE) {
+        BD_DEBUG(DBG_FILE, "_bs_read(): read error\n");
+        got = 0;
+        result = -1;
+    }
+
+    bs->size = (size_t)got;
+    bb_init(&bs->bb, bs->buf, bs->size);
+
+    return result;
+}
+
+int bs_init( BITSTREAM *bs, BD_FILE_H *fp )
 {
     int64_t size = file_size(fp);;
     bs->fp = fp;
     bs->pos = 0;
     bs->end = (size < 0) ? 0 : size;
-    bs->size = file_read(bs->fp, bs->buf, BF_BUF_SIZE);
-    if (bs->size == 0 || bs->size > BF_BUF_SIZE) {
-        bs->size = 0;
-        bs->end = 0;
-        BD_DEBUG(DBG_FILE|DBG_CRIT, "bs_init(): read error!\n");
-    }
-    bb_init(&bs->bb, bs->buf, bs->size);
+
+    return _bs_read(bs);
 }
 
 void bb_seek( BITBUFFER *bb, int64_t off, int whence)
diff --git a/src/util/bits.h b/src/util/bits.h
index dc7e9d8..66ab3cf 100644
--- a/src/util/bits.h
+++ b/src/util/bits.h
@@ -55,7 +55,7 @@ typedef struct {
 } BITSTREAM;
 
 BD_PRIVATE void bb_init( BITBUFFER *bb, const uint8_t *p_data, size_t i_data );
-BD_PRIVATE void bs_init( BITSTREAM *bs, BD_FILE_H *fp );
+BD_PRIVATE int  bs_init( BITSTREAM *bs, BD_FILE_H *fp );
 BD_PRIVATE void bb_seek( BITBUFFER *bb, int64_t off, int whence);
 BD_PRIVATE void bs_seek( BITSTREAM *bs, int64_t off, int whence);
 //BD_PRIVATE void bb_seek_byte( BITBUFFER *bb, int64_t off);



More information about the libbluray-devel mailing list