Skip to content

Commit b57d24d

Browse files
committed
fix(cam_hal): guard cam_verify_jpeg_eoi() against buffer-underflow
If DMA returns a frame shorter than two bytes, the previous code did: dptr = inbuf + length - 2; which under-flows the pointer and produces undefined behaviour. Behaviour for valid frames (length ≥ 2) is unchanged; damaged or empty buffers are now discarded safely.
1 parent 23c4fdd commit b57d24d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

driver/cam_hal.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
6767

6868
static int cam_verify_jpeg_eoi(const uint8_t *inbuf, uint32_t length)
6969
{
70+
if (length < sizeof(JPEG_EOI_MARKER)) {
71+
return -1;
72+
}
73+
7074
int offset = -1;
71-
uint8_t *dptr = (uint8_t *)inbuf + length - 2;
75+
uint8_t *dptr = (uint8_t *)inbuf + length - sizeof(JPEG_EOI_MARKER);
7276
while (dptr > inbuf) {
73-
if (memcmp(dptr, &JPEG_EOI_MARKER, 2) == 0) {
77+
if (memcmp(dptr, &JPEG_EOI_MARKER, sizeof(JPEG_EOI_MARKER)) == 0) {
7478
offset = dptr - inbuf;
7579
//ESP_LOGW(TAG, "EOI: %d", length - (offset + 2));
7680
return offset;

0 commit comments

Comments
 (0)