Browse code

Add missing overflow checks in av_image_fill_pointers() and av_image_fill_linesizes().

Originally committed as revision 25861 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stefano Sabatini authored on 2010/12/03 04:49:55
Showing 1 changed files
... ...
@@ -71,6 +71,8 @@ int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int widt
71 71
         return AVERROR(EINVAL);
72 72
 
73 73
     if (desc->flags & PIX_FMT_BITSTREAM) {
74
+        if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
75
+            return AVERROR(EINVAL);
74 76
         linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
75 77
         return 0;
76 78
     }
... ...
@@ -78,7 +80,10 @@ int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int widt
78 78
     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
79 79
     for (i = 0; i < 4; i++) {
80 80
         int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
81
-        linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
81
+        int shifted_w = ((width + (1 << s) - 1)) >> s;
82
+        if (max_step[i] > INT_MAX / shifted_w)
83
+            return AVERROR(EINVAL);
84
+        linesizes[i] = max_step[i] * shifted_w;
82 85
     }
83 86
 
84 87
     return 0;
... ...
@@ -98,6 +103,8 @@ int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int heigh
98 98
         return AVERROR(EINVAL);
99 99
 
100 100
     data[0] = ptr;
101
+    if (linesizes[0] > (INT_MAX - 1024) / height)
102
+        return AVERROR(EINVAL);
101 103
     size[0] = linesizes[0] * height;
102 104
 
103 105
     if (desc->flags & PIX_FMT_PAL) {
... ...
@@ -114,7 +121,11 @@ int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int heigh
114 114
         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
115 115
         data[i] = data[i-1] + size[i-1];
116 116
         h = (height + (1 << s) - 1) >> s;
117
+        if (linesizes[i] > INT_MAX / h)
118
+            return AVERROR(EINVAL);
117 119
         size[i] = h * linesizes[i];
120
+        if (total_size > INT_MAX - size[i])
121
+            return AVERROR(EINVAL);
118 122
         total_size += size[i];
119 123
     }
120 124