Fix undefined behavior in STREAM buffer bounds checking by avoiding out-of-bounds pointer arithmetic

This commit is contained in:
Andrea Mazzoleni 2026-08-17 10:12:03 +02:00
parent 3185e1bf34
commit 608393867a
2 changed files with 4 additions and 3 deletions

View file

@ -277,7 +277,7 @@ int sdeplete(STREAM* s, unsigned char* last)
while (1) {
/* increase the position up to 4 bytes before the end */
if (s->pos + 4 <= s->end)
if (s->end - s->pos >= 4)
s->pos = s->end - 4;
/* insert the last 4 bytes */

View file

@ -152,9 +152,10 @@ uint32_t scrc_stream(STREAM* s);
/**
* Check if the buffer has enough data loaded.
*/
static inline int sptrlookup(STREAM* s, ssize_t size)
static inline int sptrlookup(STREAM* s, size_t size)
{
return s->pos + size <= s->end;
/* compute remaining distance to avoid pointer arithmetic out of buffer bounds */
return size <= (size_t)(s->end - s->pos);
}
/**