econsystems / opencv_v4l2

This project contains sample OpenCV application code as well as V4L2 helper library to access camera devices in OpenCV. This code helps to achieve high framerates from cameras in OpenCV. This project gives better results than the VideoCapture class in OpenCV. This source code is only compatible in Linux.
BSD 3-Clause "New" or "Revised" License
39 stars 22 forks source link

Calloc is used improperly #9

Closed Steve132 closed 2 years ago

Steve132 commented 2 years ago

Describe the bug

https://github.com/econsystems/opencv_v4l2/blob/979264c3e1cdab1480975fd62e2050db0c85ba23/lib/src/v4l2_helper.c#L210

sizeof(*buffers) is NOT the number of buffers in C.
it's a compile time constant that's always sizeof(struct buffer); https://godbolt.org/z/1oGzKdWEa

Therefore this code is bugged. It only happens to work because sizeof(struct buffer) > NUM_BUFFS on most platforms. But it is incorrect.

Steve132 commented 2 years ago

This was my bad, I misunderstood what was happening here, exactly. This isn't trying to allocate a dynamic array it's trying to allocate exactly one struct buffer for each buffer, in this case one. So sizeof(struct buffer) is actually correct here.