Skip to content

Commit 26d423c

Browse files
Wonsik Kimandi34
authored andcommitted
DO NOT MERGE - stagefright: fix integer overflow error
Bug: 30103394 Change-Id: If449d3e30a0bf2ebea5317f41813bfed094f7408 (cherry picked from commit 2c74a3c)
1 parent 47301ba commit 26d423c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

media/libstagefright/SampleTable.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
#define __STDINT_MACROS
18+
#define __STDINT_LIMITS
19+
1720
#define LOG_TAG "SampleTable"
1821
//#define LOG_NDEBUG 0
1922
#include <utils/Log.h>
@@ -27,11 +30,6 @@
2730
#include <media/stagefright/DataSource.h>
2831
#include <media/stagefright/Utils.h>
2932

30-
/* TODO: remove after being merged into other branches */
31-
#ifndef UINT32_MAX
32-
#define UINT32_MAX (4294967295U)
33-
#endif
34-
3533
namespace android {
3634

3735
// static
@@ -45,6 +43,8 @@ const uint32_t SampleTable::kSampleSizeTypeCompact = FOURCC('s', 't', 'z', '2');
4543

4644
////////////////////////////////////////////////////////////////////////////////
4745

46+
const off64_t kMaxOffset = INT64_MAX;
47+
4848
struct SampleTable::CompositionDeltaLookup {
4949
CompositionDeltaLookup();
5050

@@ -233,11 +233,11 @@ status_t SampleTable::setSampleToChunkParams(
233233

234234
mNumSampleToChunkOffsets = U32_AT(&header[4]);
235235

236-
if (data_size < 8 + mNumSampleToChunkOffsets * 12) {
236+
if ((data_size - 8) / sizeof(SampleToChunkEntry) < mNumSampleToChunkOffsets) {
237237
return ERROR_MALFORMED;
238238
}
239239

240-
if ((uint64_t)SIZE_MAX / sizeof(SampleToChunkEntry) <=
240+
if ((uint64_t)kMaxTotalSize / sizeof(SampleToChunkEntry) <=
241241
(uint64_t)mNumSampleToChunkOffsets) {
242242
ALOGE("Sample-to-chunk table size too large.");
243243
return ERROR_OUT_OF_RANGE;
@@ -269,16 +269,19 @@ status_t SampleTable::setSampleToChunkParams(
269269
return OK;
270270
}
271271

272-
if ((off64_t)(SIZE_MAX - 8 -
272+
if ((off64_t)(kMaxOffset - 8 -
273273
((mNumSampleToChunkOffsets - 1) * sizeof(SampleToChunkEntry)))
274274
< mSampleToChunkOffset) {
275275
return ERROR_MALFORMED;
276276
}
277277

278278
for (uint32_t i = 0; i < mNumSampleToChunkOffsets; ++i) {
279-
uint8_t buffer[12];
279+
uint8_t buffer[sizeof(SampleToChunkEntry)];
280+
280281
if (mDataSource->readAt(
281-
mSampleToChunkOffset + 8 + i * 12, buffer, sizeof(buffer))
282+
mSampleToChunkOffset + 8 + i * sizeof(SampleToChunkEntry),
283+
buffer,
284+
sizeof(buffer))
282285
!= (ssize_t)sizeof(buffer)) {
283286
return ERROR_IO;
284287
}
@@ -378,8 +381,7 @@ status_t SampleTable::setTimeToSampleParams(
378381
}
379382

380383
mTimeToSampleCount = U32_AT(&header[4]);
381-
if ((uint64_t)mTimeToSampleCount >
382-
(uint64_t)UINT32_MAX / (2 * sizeof(uint32_t))) {
384+
if (mTimeToSampleCount > UINT32_MAX / (2 * sizeof(uint32_t))) {
383385
// Choose this bound because
384386
// 1) 2 * sizeof(uint32_t) is the amount of memory needed for one
385387
// time-to-sample entry in the time-to-sample table.
@@ -455,7 +457,7 @@ status_t SampleTable::setCompositionTimeToSampleParams(
455457

456458
mNumCompositionTimeDeltaEntries = numEntries;
457459
uint64_t allocSize = (uint64_t)numEntries * 2 * sizeof(uint32_t);
458-
if (allocSize > SIZE_MAX) {
460+
if (allocSize > kMaxTotalSize) {
459461
ALOGE("Composition-time-to-sample table size too large.");
460462
return ERROR_OUT_OF_RANGE;
461463
}
@@ -522,7 +524,7 @@ status_t SampleTable::setSyncSampleParams(off64_t data_offset, size_t data_size)
522524
}
523525

524526
uint64_t allocSize = (uint64_t)mNumSyncSamples * sizeof(uint32_t);
525-
if (allocSize > SIZE_MAX) {
527+
if (allocSize > kMaxTotalSize) {
526528
ALOGE("Sync sample table size too large.");
527529
return ERROR_OUT_OF_RANGE;
528530
}

0 commit comments

Comments
 (0)