Skip to content

Commit ee92090

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 cbed97c commit ee92090

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

driver/cam_hal.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static cam_obj_t *cam_obj = NULL;
4646
static const uint8_t JPEG_SOI_MARKER[] = {0xFF, 0xD8, 0xFF}; /* SOI = FF D8 FF */
4747
#define JPEG_SOI_MARKER_LEN (3)
4848
static const uint16_t JPEG_EOI_MARKER = 0xD9FF; /* EOI = FF D9 */
49+
#define JPEG_EOI_MARKER_LEN (2)
4950

5051
static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
5152
{
@@ -66,10 +67,14 @@ static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
6667

6768
static int cam_verify_jpeg_eoi(const uint8_t *inbuf, uint32_t length)
6869
{
70+
if (length < JPEG_EOI_MARKER_LEN) {
71+
return -1;
72+
}
73+
6974
int offset = -1;
70-
uint8_t *dptr = (uint8_t *)inbuf + length - 2;
75+
uint8_t *dptr = (uint8_t *)inbuf + length - JPEG_EOI_MARKER_LEN;
7176
while (dptr > inbuf) {
72-
if (memcmp(dptr, &JPEG_EOI_MARKER, 2) == 0) {
77+
if (memcmp(dptr, &JPEG_EOI_MARKER, JPEG_EOI_MARKER_LEN) == 0) {
7378
offset = dptr - inbuf;
7479
//ESP_LOGW(TAG, "EOI: %d", length - (offset + 2));
7580
return offset;

0 commit comments

Comments
 (0)