Skip to content

Commit c8dc68b

Browse files
committed
vcap/v4l2: ensure that buffer count doesn't ovrflw
Merely a theoretic case - this might (in default configuration) if driver wants to use > 30 frames (while 3 are requested).
1 parent 35196ff commit c8dc68b

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/v4l2_common.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* @author Martin Piatka <piatka@cesnet.cz>
55
* @author Martin Pulec <martin.pulec@cesnet.cz>
66
*/
7-
/*
8-
* Copyright (c) 2012-2025 CESNET
7+
/*
8+
* Copyright (c) 2012-2026 CESNET, zájmové sdružení právnických osob
99
* All rights reserved.
1010
*
1111
* Redistribution and use in source and binary forms, with or without
@@ -40,15 +40,21 @@
4040
#ifndef V4L2_COMMON_4B01337E_4F0D_48EC_91A7_D220AD919D3B
4141
#define V4L2_COMMON_4B01337E_4F0D_48EC_91A7_D220AD919D3B
4242

43+
#include <assert.h>
44+
#include <errno.h>
4345
#include <linux/videodev2.h>
4446
#include <stdint.h>
4547
#include <sys/ioctl.h>
4648
#include <sys/mman.h>
4749

48-
#include "types.h"
50+
#include "debug.h"
4951
#include "compat/strings.h" // for strerror_s
52+
#include "types.h"
5053

51-
#define V4L2_PROBE_MAX 64
54+
enum {
55+
V4L2_PROBE_MAX = 64,
56+
V4L2_CAP_MAX_BUF_COUNT = 30,
57+
};
5258

5359
static struct {
5460
enum v4l2_field v4l_f;
@@ -136,6 +142,8 @@ static _Bool set_v4l2_buffers(int fd, struct v4l2_requestbuffers *reqbuf, struct
136142
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Not enough buffer memory\n");
137143
return 0;
138144
}
145+
assert(reqbuf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
146+
reqbuf->count < V4L2_CAP_MAX_BUF_COUNT);
139147

140148
for (unsigned int i = 0; i < reqbuf->count; i++) {
141149
struct v4l2_buffer buf;

src/video_capture/v4l2.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060

6161
enum {
6262
DEFAULT_BUF_COUNT = 3,
63-
MAX_BUF_COUNT = 30,
6463
};
6564
#define MOD_NAME "[V4L cap.] "
6665

@@ -89,7 +88,7 @@ struct vidcap_v4l2_state {
8988
struct video_desc desc;
9089

9190
int fd;
92-
struct v4l2_buffer_data buffers[MAX_BUF_COUNT];
91+
struct v4l2_buffer_data buffers[V4L2_CAP_MAX_BUF_COUNT];
9392

9493
_Bool permissive; ///< do not fail if parameters (size, FPS...) not set exactly
9594
#ifdef HAVE_LIBV4LCONVERT
@@ -552,7 +551,7 @@ parse_fmt(char *fmt, struct parsed_opts *opts)
552551
: atoi(strchr(item, '/') + 1);
553552
} else if (IS_KEY_PREFIX(item, "buffers")) {
554553
opts->buffer_count = atoi(strchr(item, '=') + 1);
555-
assert(opts->buffer_count <= MAX_BUF_COUNT);
554+
assert(opts->buffer_count <= V4L2_CAP_MAX_BUF_COUNT);
556555
} else if (IS_KEY_PREFIX(item, "convert")) {
557556
#ifdef HAVE_LIBV4LCONVERT
558557
const char *codec = strchr(item, '=') + 1;

0 commit comments

Comments
 (0)