Skip to content
Open
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
20 changes: 16 additions & 4 deletions src/main/common/streambuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sbuf_t *sbufInit(sbuf_t *sbuf, uint8_t *ptr, uint8_t *end)
{
sbuf->ptr = ptr;
sbuf->end = end;
sbuf->overrun = false;
return sbuf;
}

Expand Down Expand Up @@ -93,25 +94,35 @@ void sbufWriteStringWithZeroTerminator(sbuf_t *dst, const char *string)
sbufWriteData(dst, string, strlen(string) + 1);
}

uint8_t sbufReadU8(sbuf_t *src)
// Use the raw attribute rather than NOINLINE because NOINLINE expands to
// nothing on non-F7/H7 targets (common.h:29), but LTO is enabled for all
// release targets and these read primitives are inlined into hundreds of
// call sites (e.g. mspFcProcessInCommand), duplicating the overrun check
// at each site and costing ~9 KB of flash on -O2 targets. Keeping them
// out-of-line confines the check to one copy.
__attribute__((noinline)) uint8_t sbufReadU8(sbuf_t *src)
{
if (src->ptr >= src->end) {
src->overrun = true;
return 0;
}
return *src->ptr++;
}

int8_t sbufReadI8(sbuf_t *src)
{
return *src->ptr++;
return (int8_t)sbufReadU8(src);
}

uint16_t sbufReadU16(sbuf_t *src)
__attribute__((noinline)) uint16_t sbufReadU16(sbuf_t *src)
{
uint16_t ret;
ret = sbufReadU8(src);
ret |= sbufReadU8(src) << 8;
return ret;
}

uint32_t sbufReadU32(sbuf_t *src)
__attribute__((noinline)) uint32_t sbufReadU32(sbuf_t *src)
{
uint32_t ret;
ret = sbufReadU8(src);
Expand Down Expand Up @@ -216,4 +227,5 @@ void sbufSwitchToReader(sbuf_t *buf, uint8_t *base)
{
buf->end = buf->ptr;
buf->ptr = base;
buf->overrun = false;
}
1 change: 1 addition & 0 deletions src/main/common/streambuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
typedef struct sbuf_s {
uint8_t *ptr; // data pointer must be first (sbuff_t* is equivalent to uint8_t **)
uint8_t *end;
bool overrun; // sticky: set by an unsafe sbufRead* call that ran past end
} sbuf_t;

sbuf_t *sbufInit(sbuf_t *sbuf, uint8_t *ptr, uint8_t *end);
Expand Down
9 changes: 9 additions & 0 deletions src/main/fc/fc_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,10 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src)
}
}
}

if (src->overrun) {
return MSP_RESULT_ERROR;
}
}
break;

Expand Down Expand Up @@ -3124,6 +3128,11 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src)
for (unsigned ii = 0; ii < MIN(osdCharacterBytes, sizeof(chr.data)); ii++) {
chr.data[ii] = sbufReadU8(src);
}

if (src->overrun) {
return MSP_RESULT_ERROR;
}

displayPort_t *osdDisplayPort = osdGetDisplayPort();
if (osdDisplayPort) {
displayWriteFontCharacter(osdDisplayPort, addr, &chr);
Expand Down
Loading