Fix binary msg read edge cases

This commit is contained in:
Allen Hill
2026-05-15 21:39:01 +00:00
parent b510eaaef8
commit e5797b598a
2 changed files with 14 additions and 1 deletions
+11
View File
@@ -1168,6 +1168,7 @@ uint8_t AVCLAN_parseframe(const uint8_t *bytes, uint8_t len,
enum : uint8_t { enum : uint8_t {
TOO_SHORT = 0x01, TOO_SHORT = 0x01,
MISMATCH_LENGTH, MISMATCH_LENGTH,
LENGTH_TOO_BIG,
} errno; } errno;
uint8_t val; uint8_t val;
} err = {0}; } err = {0};
@@ -1186,6 +1187,12 @@ uint8_t AVCLAN_parseframe(const uint8_t *bytes, uint8_t len,
frame->control = *bytes++; frame->control = *bytes++;
frame->length = *bytes++; frame->length = *bytes++;
if (frame->length > MAXMSGLEN) {
err.errno = LENGTH_TOO_BIG;
err.val = frame->length;
goto handle_err;
}
if ((bytes + frame->length) <= last) { if ((bytes + frame->length) <= last) {
memcpy(frame->data, bytes, frame->length); memcpy(frame->data, bytes, frame->length);
} else { } else {
@@ -1203,6 +1210,10 @@ uint8_t AVCLAN_parseframe(const uint8_t *bytes, uint8_t len,
case MISMATCH_LENGTH: case MISMATCH_LENGTH:
RS232_Print("frame->length is longer than remaining data"); RS232_Print("frame->length is longer than remaining data");
break; break;
case LENGTH_TOO_BIG:
RS232_Print("frame->length exceeds MAXMSGLEN: 0x");
RS232_PrintHex8(err.val);
break;
default: break; default: break;
} }
RS232_Print("\n"); RS232_Print("\n");
+3 -1
View File
@@ -104,7 +104,9 @@ int main() {
uint8_t readBinary = 0; uint8_t readBinary = 0;
uint8_t muteBus = 0; uint8_t muteBus = 0;
uint8_t data_tmp[MAXMSGLEN]; // Binary-mode REPL includes the full wire preamble (broadcast + 2*addr +
// control + length), so size for the worst case.
uint8_t data_tmp[MAXMSGLEN + sizeof(AVCLAN_frame_t)];
uint8_t seqLen = 0; // current length written to data_tmp uint8_t seqLen = 0; // current length written to data_tmp
uint8_t err = 0; uint8_t err = 0;