Skip to content

Commit

Permalink
Avoid signed integer (POC) overflow in decoder_core.c
Browse files Browse the repository at this point in the history
Addresses: cisco#3745
  • Loading branch information
tyan0 committed Jul 12, 2024
1 parent 478e5ab commit 49e1455
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions codec/decoder/core/src/decoder_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,16 +1079,34 @@ int32_t ParseSliceHeaderSyntaxs (PWelsDecoderContext pCtx, PBitStringAux pBs, co
pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb = 0;
pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb = 0;
}
/* We shoud not expect that overflow in signed integer returns wrapped-
around result. C/C++ standard states the result is undefined. */
int32_t pocMsb;
if (pocLsb < pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb
&& pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb - pocLsb >= iMaxPocLsb / 2)
pocMsb = pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb + iMaxPocLsb;
{
if (INT32_MAX - iMaxPocLsb < pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb)
/* Will overflow. Emulate wraparound result. */
pocMsb = (pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb - INT32_MAX - 1) + (INT32_MIN + iMaxPocLsb);
else
pocMsb = pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb + iMaxPocLsb;
}
else if (pocLsb > pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb
&& pocLsb - pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb > iMaxPocLsb / 2)
pocMsb = pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb - iMaxPocLsb;
{
if (INT32_MIN + iMaxPocLsb > pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb)
/* Will overflow. Emulate wraparound result. */
pocMsb = (pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb - INT32_MIN) + (INT32_MAX - iMaxPocLsb + 1);
else
pocMsb = pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb - iMaxPocLsb;
}
else
pocMsb = pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb;
pSliceHead->iPicOrderCntLsb = pocMsb + pocLsb;
if (INT32_MAX - pocLsb < pocMsb)
/* Will overflow. Emulate wraparound result. */
pSliceHead->iPicOrderCntLsb = (pocMsb - INT32_MAX - 1) + (INT32_MIN + pocLsb);
else
pSliceHead->iPicOrderCntLsb = pocMsb + pocLsb;

if (pPps->bPicOrderPresentFlag && !pSliceHead->bFieldPicFlag) {
pSliceHead->iPicOrderCntLsb += pSliceHead->iDeltaPicOrderCntBottom;
Expand Down
2 changes: 1 addition & 1 deletion codec/decoder/plus/src/welsDecoderExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ void CWelsDecoder::ReleaseBufferedReadyPictureReorder (PWelsDecoderContext pCtx,
int32_t iLastPOC = pCtx != NULL ? pCtx->pSliceHeader->iPicOrderCntLsb : m_sPictInfoList[m_iLastBufferedIdx].iPOC;
int32_t iLastSeqNum = pCtx != NULL ? pCtx->iSeqNum : m_sPictInfoList[m_iLastBufferedIdx].iSeqNum;
isReady = (m_sReoderingStatus.iLastWrittenPOC > IMinInt32
&& m_sReoderingStatus.iMinPOC - m_sReoderingStatus.iLastWrittenPOC <= 1)
&& m_sReoderingStatus.iMinPOC <= m_sReoderingStatus.iLastWrittenPOC + 1)
|| m_sReoderingStatus.iMinPOC < iLastPOC
|| m_sReoderingStatus.iMinSeqNum - iLastSeqNum < 0;
}
Expand Down

0 comments on commit 49e1455

Please sign in to comment.