Skip to content

Commit cc94f45

Browse files
committed
dr_mp3: Fix an error when padding is malformed.
1 parent 1445333 commit cc94f45

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

dr_mp3.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,8 +3202,17 @@ static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drm
32023202
pTagData += 21;
32033203

32043204
if (pTagData - pFirstFrameData + 14 < firstFrameInfo.frame_bytes) {
3205-
pMP3->delayInPCMFrames = (( (drmp3_uint32)pTagData[0] << 4) | ((drmp3_uint32)pTagData[1] >> 4)) + (528 + 1);
3206-
pMP3->paddingInPCMFrames = ((((drmp3_uint32)pTagData[1] & 0xF) << 8) | ((drmp3_uint32)pTagData[2] )) - (528 + 1);
3205+
int delayInPCMFrames;
3206+
int paddingInPCMFrames;
3207+
3208+
delayInPCMFrames = (( (drmp3_uint32)pTagData[0] << 4) | ((drmp3_uint32)pTagData[1] >> 4)) + (528 + 1);
3209+
paddingInPCMFrames = ((((drmp3_uint32)pTagData[1] & 0xF) << 8) | ((drmp3_uint32)pTagData[2] )) - (528 + 1);
3210+
if (paddingInPCMFrames < 0) {
3211+
paddingInPCMFrames = 0; /* Padding cannot be negative. Probably a malformed file. Ignore. */
3212+
}
3213+
3214+
pMP3->delayInPCMFrames = (drmp3_uint32)delayInPCMFrames;
3215+
pMP3->paddingInPCMFrames = (drmp3_uint32)paddingInPCMFrames;
32073216
}
32083217
}
32093218

@@ -4515,15 +4524,30 @@ DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint
45154524

45164525
DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3)
45174526
{
4527+
drmp3_uint64 totalPCMFrameCount;
4528+
45184529
if (pMP3 == NULL) {
45194530
return 0;
45204531
}
45214532

45224533
if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX) {
4523-
return (drmp3_uint64)pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames - pMP3->delayInPCMFrames;
4534+
totalPCMFrameCount = pMP3->totalPCMFrameCount;
4535+
4536+
if (totalPCMFrameCount >= pMP3->delayInPCMFrames) {
4537+
totalPCMFrameCount -= pMP3->delayInPCMFrames;
4538+
} else {
4539+
/* The delay is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */
4540+
}
4541+
4542+
if (totalPCMFrameCount >= pMP3->paddingInPCMFrames) {
4543+
totalPCMFrameCount -= pMP3->paddingInPCMFrames;
4544+
} else {
4545+
/* The padding is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */
4546+
}
4547+
4548+
return totalPCMFrameCount;
45244549
} else {
45254550
/* Unknown frame count. Need to calculate it. */
4526-
drmp3_uint64 totalPCMFrameCount;
45274551
if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
45284552
return 0;
45294553
}

0 commit comments

Comments
 (0)