Browse code

avc: fix memory errors when encoding invalid h264 codecdata

Signed-off-by: Ronald S. Bultje <rsbultje@gmail.com>

John Brooks authored on 2011/11/10 12:14:19
Showing 1 changed files
... ...
@@ -75,8 +75,11 @@ int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
75 75
 
76 76
     size = 0;
77 77
     nal_start = ff_avc_find_startcode(p, end);
78
-    while (nal_start < end) {
79
-        while(!*(nal_start++));
78
+    for (;;) {
79
+        while (nal_start < end && !*(nal_start++));
80
+        if (nal_start == end)
81
+            break;
82
+
80 83
         nal_end = ff_avc_find_startcode(nal_start, end);
81 84
         avio_wb32(pb, nal_end - nal_start);
82 85
         avio_write(pb, nal_start, nal_end - nal_start);
... ...
@@ -117,22 +120,26 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
117 117
             end = buf + len;
118 118
 
119 119
             /* look for sps and pps */
120
-            while (buf < end) {
121
-                unsigned int size;
120
+            while (end - buf > 4) {
121
+                uint32_t size;
122 122
                 uint8_t nal_type;
123
-                size = AV_RB32(buf);
124
-                nal_type = buf[4] & 0x1f;
123
+                size = FFMIN(AV_RB32(buf), end - buf - 4);
124
+                buf += 4;
125
+                nal_type = buf[0] & 0x1f;
126
+
125 127
                 if (nal_type == 7) { /* SPS */
126
-                    sps = buf + 4;
128
+                    sps = buf;
127 129
                     sps_size = size;
128 130
                 } else if (nal_type == 8) { /* PPS */
129
-                    pps = buf + 4;
131
+                    pps = buf;
130 132
                     pps_size = size;
131 133
                 }
132
-                buf += size + 4;
134
+
135
+                buf += size;
133 136
             }
134
-            assert(sps);
135
-            assert(pps);
137
+
138
+            if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
139
+                return AVERROR_INVALIDDATA;
136 140
 
137 141
             avio_w8(pb, 1); /* version */
138 142
             avio_w8(pb, sps[1]); /* profile */