Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1337)

Side by Side Diff: media/filters/h264_parser.cc

Issue 1865203002: Avoid integer overflow errors when parsing Exp-Golomb codes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: static_assert Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/filters/h264_parser.h"
6
7 #include <limits>
8
5 #include "base/logging.h" 9 #include "base/logging.h"
6 #include "base/macros.h" 10 #include "base/macros.h"
7 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h" 12 #include "base/stl_util.h"
9
10 #include "media/base/decrypt_config.h" 13 #include "media/base/decrypt_config.h"
11 #include "media/filters/h264_parser.h"
12 14
13 namespace media { 15 namespace media {
14 16
15 bool H264SliceHeader::IsPSlice() const { 17 bool H264SliceHeader::IsPSlice() const {
16 return (slice_type % 5 == kPSlice); 18 return (slice_type % 5 == kPSlice);
17 } 19 }
18 20
19 bool H264SliceHeader::IsBSlice() const { 21 bool H264SliceHeader::IsBSlice() const {
20 return (slice_type % 5 == kBSlice); 22 return (slice_type % 5 == kBSlice);
21 } 23 }
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 // Count the number of contiguous zero bits. 313 // Count the number of contiguous zero bits.
312 do { 314 do {
313 READ_BITS_OR_RETURN(1, &bit); 315 READ_BITS_OR_RETURN(1, &bit);
314 num_bits++; 316 num_bits++;
315 } while (bit == 0); 317 } while (bit == 0);
316 318
317 if (num_bits > 31) 319 if (num_bits > 31)
318 return kInvalidStream; 320 return kInvalidStream;
319 321
320 // Calculate exp-Golomb code value of size num_bits. 322 // Calculate exp-Golomb code value of size num_bits.
323 // Special case for |num_bits| == 31 to avoid integer overflow. The only
324 // valid representation as an int is 2^31 - 1, so the remaining bits must
325 // be 0 or else the number is too large.
326 if (num_bits == 31) {
327 // TODO(jrummell): This file should be converted to int32_t as a lot of
DaleCurtis 2016/04/07 19:02:17 I don't think this is necessary, but up to you. We
jrummell 2016/04/07 20:42:47 happy to remove my name from a TODO.
328 // places assume that the sizeof(int) is 32 bits.
329 static_assert(sizeof(int) == sizeof(int32_t), "int must be 32 bits");
330
331 *val = std::numeric_limits<int>::max();
332 READ_BITS_OR_RETURN(num_bits, &rest);
333 return (rest == 0) ? kOk : kInvalidStream;
334 }
335
321 *val = (1 << num_bits) - 1; 336 *val = (1 << num_bits) - 1;
DaleCurtis 2016/04/07 02:08:23 Is it sufficient to just have (1u << num_bits)
sandersd (OOO until July 31) 2016/04/07 17:56:16 It is correct to restrict to num_bits < 32, use en
DaleCurtis 2016/04/07 19:02:17 dalecurtis@xorax /tmp $ cat test.cc #include <stdi
jrummell 2016/04/07 20:42:47 Done.
322
323 if (num_bits > 0) { 337 if (num_bits > 0) {
324 READ_BITS_OR_RETURN(num_bits, &rest); 338 READ_BITS_OR_RETURN(num_bits, &rest);
325 *val += rest; 339 *val += rest;
326 } 340 }
327 341
328 return kOk; 342 return kOk;
329 } 343 }
330 344
331 H264Parser::Result H264Parser::ReadSE(int* val) { 345 H264Parser::Result H264Parser::ReadSE(int* val) {
332 int ue; 346 int ue;
(...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 1352
1339 default: 1353 default:
1340 DVLOG(4) << "Unsupported SEI message"; 1354 DVLOG(4) << "Unsupported SEI message";
1341 break; 1355 break;
1342 } 1356 }
1343 1357
1344 return kOk; 1358 return kOk;
1345 } 1359 }
1346 1360
1347 } // namespace media 1361 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698