Skip to content

Commit 711b572

Browse files
committed
fix(cam_hal): prevent SOI scan from (1) running on length<3
and (2) over-reading the last 2 bytes Changes: * Store SOI as a 3-byte array (0xFF D8 FF) and use sizeof() everywhere. * Early-exit when length < 3 to avoid over-reading * calculate end index correctly, to avoid over-reading
1 parent 5fe2266 commit 711b572

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

driver/cam_hal.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,21 @@
4242
static const char *TAG = "cam_hal";
4343
static cam_obj_t *cam_obj = NULL;
4444

45-
static const uint32_t JPEG_SOI_MARKER = 0xFFD8FF; // written in little-endian for esp32
46-
static const uint16_t JPEG_EOI_MARKER = 0xD9FF; // written in little-endian for esp32
45+
/* JPEG markers in little-endian order (ESP32). */
46+
static const uint8_t JPEG_SOI_MARKER[] = {0xFF, 0xD8, 0xFF}; /* SOI = FF D8 FF */
47+
static const uint16_t JPEG_EOI_MARKER = 0xD9FF; /* EOI = FF D9 */
4748

4849
static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
4950
{
50-
for (uint32_t i = 0; i < length; i++) {
51-
if (memcmp(&inbuf[i], &JPEG_SOI_MARKER, 3) == 0) {
51+
const size_t soi_len = sizeof(JPEG_SOI_MARKER);
52+
53+
if (length < soi_len) {
54+
ESP_LOGW(TAG, "NO-SOI");
55+
return -1;
56+
}
57+
58+
for (uint32_t i = 0; i <= length - soi_len; i++) {
59+
if (memcmp(&inbuf[i], JPEG_SOI_MARKER, soi_len) == 0) {
5260
//ESP_LOGW(TAG, "SOI: %d", (int) i);
5361
return i;
5462
}
@@ -539,4 +547,4 @@ void cam_give_all(void) {
539547
bool cam_get_available_frames(void)
540548
{
541549
return 0 < uxQueueMessagesWaiting(cam_obj->frame_buffer_queue);
542-
}
550+
}

0 commit comments

Comments
 (0)