Skip to content

Commit

Permalink
libebml2: fix size_t vs filepos_t comparisons
Browse files Browse the repository at this point in the history
Regression from edeaa81.
  • Loading branch information
robUx4 committed Dec 27, 2024
1 parent 3bb49f2 commit dd369b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion libebml2/ebmlbinary.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ static err_t ReadData(ebml_binary *Element, stream *Input, const ebml_parser_con
goto failed;
}

if (Element->Base.DataSize > (filepos_t)SIZE_MAX || !ArrayResize(&Element->Data,(size_t)Element->Base.DataSize,0))
#if MAX_FILEPOS >= SIZE_MAX
if ((filepos_t)Element->Base.DataSize > (filepos_t)SIZE_MAX
#else
if ((size_t)Element->Base.DataSize > (size_t)SIZE_MAX
#endif
|| !ArrayResize(&Element->Data,(size_t)Element->Base.DataSize,0))
{
Result = ERR_OUT_OF_MEMORY;
goto failed;
Expand Down
15 changes: 13 additions & 2 deletions libebml2/ebmlmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,12 @@ static err_t ReadData(ebml_master *Element, stream *Input, const ebml_parser_con
// read the rest of the element in memory to avoid reading it a second time later
ArrayInit(&CrcBuffer);
filepos_t element_size = EBML_ElementPositionEnd((ebml_element*)Element) - EBML_ElementPositionEnd(SubElement);
if (element_size < (filepos_t)SIZE_MAX && ArrayResize(&CrcBuffer, (size_t)element_size, 0))
#if MAX_FILEPOS >= SIZE_MAX
if ((filepos_t)element_size < (filepos_t)SIZE_MAX
#else
if ((size_t)element_size < (size_t)SIZE_MAX
#endif
&& ArrayResize(&CrcBuffer, (size_t)element_size, 0))
{
CRCData = ARRAYBEGIN(CrcBuffer,uint8_t);
CRCDataSize = ARRAYCOUNT(CrcBuffer,uint8_t);
Expand Down Expand Up @@ -499,7 +504,13 @@ static err_t RenderData(ebml_master *Element, stream *Output, bool_t bForceWitho
array TmpBuf;
bool_t IsMemory = Node_IsPartOf(Output,MEMSTREAM_CLASS);
ArrayInit(&TmpBuf);
if (!IsMemory && ((Element->Base.DataSize - CRC_EBML_SIZE) > (filepos_t)SIZE_MAX || !ArrayResize(&TmpBuf, (size_t)Element->Base.DataSize - CRC_EBML_SIZE, 0)))
if (!IsMemory &&
#if MAX_FILEPOS >= SIZE_MAX
((filepos_t)(Element->Base.DataSize - CRC_EBML_SIZE) > (filepos_t)SIZE_MAX
#else
((size_t)(Element->Base.DataSize - CRC_EBML_SIZE) > (size_t)SIZE_MAX
#endif
|| !ArrayResize(&TmpBuf, (size_t)Element->Base.DataSize - CRC_EBML_SIZE, 0)))
Err = ERR_OUT_OF_MEMORY;
else
{
Expand Down
6 changes: 5 additions & 1 deletion libebml2/ebmlstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ static err_t ReadData(ebml_string *Element, stream *Input, const ebml_parser_con
Result = ERR_READ;
goto failed;
}
if (Element->Base.DataSize > (filepos_t)SIZE_MAX - 1)
#if MAX_FILEPOS >= SIZE_MAX
if ((filepos_t)Element->Base.DataSize > (filepos_t)(SIZE_MAX - 1))
#else
if ((size_t)Element->Base.DataSize > (size_t)(SIZE_MAX - 1))
#endif
{
Result = ERR_OUT_OF_MEMORY;
goto failed;
Expand Down

0 comments on commit dd369b3

Please sign in to comment.