Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libebml2: fix size_t vs filepos_t comparisons #169

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion corec/corec/helpers/parser/parser2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ NOINLINE bool_t DataToString(tchar_t* Value, size_t ValueLen, const void* Data,
break;

case TYPE_SIZE:
#if defined(SIZE_MAX) && SIZE_MAX > 0xFFFFFFFF
#if SIZE_MAX > INT32_MAX
Int64ToString(Value,ValueLen,*(size_t*)Data,0);
#else
IntToString(Value,ValueLen,*(size_t*)Data,0);
Expand Down
8 changes: 2 additions & 6 deletions corec/corec/portab.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ typedef uint64_t uint_fast64_t;
#define TPRIx64 PRIx64
#endif

#define MAX_INT64 LL(0x7fffffffffffffff)

#define MAX_TICK INT_MAX

typedef int_fast32_t err_t;
typedef int_fast32_t bool_t;
typedef int_fast32_t tick_t;
Expand Down Expand Up @@ -498,10 +494,10 @@ void * __alloca(size_t size);

#ifdef CONFIG_FILEPOS_64
typedef int_fast64_t filepos_t;
#define MAX_FILEPOS MAX_INT64
#define MAX_FILEPOS INT_FAST64_MAX
#else
typedef int_fast32_t filepos_t;
#define MAX_FILEPOS INT_MAX
#define MAX_FILEPOS INT_FAST32_MAX
#endif

#define INVALID_FILEPOS_T ((filepos_t)-1)
Expand Down
4 changes: 2 additions & 2 deletions corec/corec/str/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static int get_integers(int64_t IntValue)
{
int Result = 0;
int64_t TmpVal = 1;
while (IntValue >= TmpVal && TmpVal < MAX_INT64/10)
while (IntValue >= TmpVal && TmpVal < INT64_MAX/10)
{
TmpVal *= 10;
Result++;
Expand All @@ -197,7 +197,7 @@ static int get_decimals(int Decimals, int64_t IntValue)
{
int Result = 0;
int64_t TmpVal = IntValue+1;
while (TmpVal < MAX_INT64/10)
while (TmpVal < INT64_MAX/10)
{
TmpVal *= 10;
Result++;
Expand Down
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
2 changes: 1 addition & 1 deletion libmatroska2/matroska2/matroska.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

MATROSKA_DLL err_t MATROSKA_Init(parsercontext *p);

#define INVALID_TIMESTAMP_T MAX_INT64
#define INVALID_TIMESTAMP_T INT64_MAX
typedef int64_t mkv_timestamp_t; // in nanoseconds

typedef struct matroska_block matroska_block;
Expand Down
Loading