Skip to content

Commit db1989a

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 5fe2266 commit db1989a

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
@@ -59,10 +59,14 @@ static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
5959

6060
static int cam_verify_jpeg_eoi(const uint8_t *inbuf, uint32_t length)
6161
{
62+
if (length < sizeof(JPEG_EOI_MARKER)) {
63+
return -1;
64+
}
65+
6266
int offset = -1;
63-
uint8_t *dptr = (uint8_t *)inbuf + length - 2;
67+
uint8_t *dptr = (uint8_t *)inbuf + length - sizeof(JPEG_EOI_MARKER);
6468
while (dptr > inbuf) {
65-
if (memcmp(dptr, &JPEG_EOI_MARKER, 2) == 0) {
69+
if (memcmp(dptr, &JPEG_EOI_MARKER, sizeof(JPEG_EOI_MARKER)) == 0) {
6670
offset = dptr - inbuf;
6771
//ESP_LOGW(TAG, "EOI: %d", length - (offset + 2));
6872
return offset;

0 commit comments

Comments
 (0)