wtv: Readd casts for shifting by sector size

Message ID 1382080049-25545-1-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö Oct. 18, 2013, 7:07 a.m.
Contrary to common intuition, a 32 bit integer left shifted
by a 64 bit integer does not promote the 32 bit integer to
64 bit before shifting.

Based on a patch by Carl-Eugen Hoyos.

This reverts commits 979e9e8f3671d49463fd309dcd3d9c445a4c63de
and 86f042dcabde2a5386dbd95ab0451b274987d253 and adds a few other
casts that were missing originally.
---
 libavformat/wtv.c |   16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

Comments

Luca Barbato Oct. 18, 2013, 11:32 a.m. | #1
On 18/10/13 09:07, Martin Storsjö wrote:
> Contrary to common intuition, a 32 bit integer left shifted
> by a 64 bit integer does not promote the 32 bit integer to
> 64 bit before shifting.
> 
> Based on a patch by Carl-Eugen Hoyos.
> 
> This reverts commits 979e9e8f3671d49463fd309dcd3d9c445a4c63de
> and 86f042dcabde2a5386dbd95ab0451b274987d253 and adds a few other
> casts that were missing originally.
> ---
>  libavformat/wtv.c |   16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/libavformat/wtv.c b/libavformat/wtv.c
> index 5099739..97b73f3 100644
> --- a/libavformat/wtv.c
> +++ b/libavformat/wtv.c
> @@ -48,7 +48,7 @@
>   *
>   */
>  
> -#define WTV_SECTOR_BITS    INT64_C(12)
> +#define WTV_SECTOR_BITS    12
>  #define WTV_SECTOR_SIZE    (1 << WTV_SECTOR_BITS)
>  #define WTV_BIGSECTOR_BITS 18
>  
> @@ -94,7 +94,7 @@ static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
>              int i = wf->position >> wf->sector_bits;
>              if (i >= wf->nb_sectors ||
>                  (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
> -                avio_seek(pb, wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
> +                avio_seek(pb, (int64_t)wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
>                  wf->error = 1;
>                  break;
>              }
> @@ -119,7 +119,7 @@ static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
>          offset = wf->length;
>  
>      wf->error = offset < 0 || offset >= wf->length ||
> -                avio_seek(pb, (wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
> +                avio_seek(pb, ((int64_t)wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
>                                + (offset & ((1 << wf->sector_bits) - 1)), SEEK_SET) < 0;

Might be good having a function to seek by sector directly so we would
avoid the problem in the future.

Maybe seek_by_sector(pb, sector, offset) would do.

lu

Patch

diff --git a/libavformat/wtv.c b/libavformat/wtv.c
index 5099739..97b73f3 100644
--- a/libavformat/wtv.c
+++ b/libavformat/wtv.c
@@ -48,7 +48,7 @@ 
  *
  */
 
-#define WTV_SECTOR_BITS    INT64_C(12)
+#define WTV_SECTOR_BITS    12
 #define WTV_SECTOR_SIZE    (1 << WTV_SECTOR_BITS)
 #define WTV_BIGSECTOR_BITS 18
 
@@ -94,7 +94,7 @@  static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
             int i = wf->position >> wf->sector_bits;
             if (i >= wf->nb_sectors ||
                 (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
-                avio_seek(pb, wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
+                avio_seek(pb, (int64_t)wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
                 wf->error = 1;
                 break;
             }
@@ -119,7 +119,7 @@  static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
         offset = wf->length;
 
     wf->error = offset < 0 || offset >= wf->length ||
-                avio_seek(pb, (wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
+                avio_seek(pb, ((int64_t)wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
                               + (offset & ((1 << wf->sector_bits) - 1)), SEEK_SET) < 0;
     wf->position = offset;
     return offset;
@@ -155,7 +155,7 @@  static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int
     WtvFile *wf;
     uint8_t *buffer;
 
-    if (avio_seek(s->pb, first_sector << WTV_SECTOR_BITS, SEEK_SET) < 0)
+    if (avio_seek(s->pb, (int64_t)first_sector << WTV_SECTOR_BITS, SEEK_SET) < 0)
         return NULL;
 
     wf = av_mallocz(sizeof(WtvFile));
@@ -184,14 +184,14 @@  static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int
         int nb_sectors1 = read_ints(s->pb, sectors1, WTV_SECTOR_SIZE / 4);
         int i;
 
-        wf->sectors = av_malloc(nb_sectors1 << WTV_SECTOR_BITS);
+        wf->sectors = av_malloc((int64_t)nb_sectors1 << WTV_SECTOR_BITS);
         if (!wf->sectors) {
             av_free(wf);
             return NULL;
         }
         wf->nb_sectors = 0;
         for (i = 0; i < nb_sectors1; i++) {
-            if (avio_seek(s->pb, sectors1[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)
+            if (avio_seek(s->pb, (int64_t)sectors1[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)
                 break;
             wf->nb_sectors += read_ints(s->pb, wf->sectors + i * WTV_SECTOR_SIZE / 4, WTV_SECTOR_SIZE / 4);
         }
@@ -218,7 +218,7 @@  static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int
 
     /* seek to initial sector */
     wf->position = 0;
-    if (avio_seek(s->pb, wf->sectors[0] << WTV_SECTOR_BITS, SEEK_SET) < 0) {
+    if (avio_seek(s->pb, (int64_t)wf->sectors[0] << WTV_SECTOR_BITS, SEEK_SET) < 0) {
         av_free(wf->sectors);
         av_free(wf);
         return NULL;
@@ -963,7 +963,7 @@  static int read_header(AVFormatContext *s)
     avio_skip(s->pb, 4);
     root_sector = avio_rl32(s->pb);
 
-    avio_seek(s->pb, root_sector << WTV_SECTOR_BITS, SEEK_SET);
+    avio_seek(s->pb, (int64_t)root_sector << WTV_SECTOR_BITS, SEEK_SET);
     root_size = avio_read(s->pb, root, root_size);
     if (root_size < 0)
         return AVERROR_INVALIDDATA;