[FFmpeg-cvslog] lavd/v4l2: detect device name truncation

Anton Khirnov git at videolan.org
Tue Dec 7 12:18:17 EET 2021


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Thu Nov 25 11:29:07 2021 +0100| [877b6a9e82f73e772b8ac40d4b119f21ee037f0f] | committer: Anton Khirnov

lavd/v4l2: detect device name truncation

Silences the following warning with gcc 10:
src/libavdevice/v4l2.c: In function ‘v4l2_get_device_list’:
src/libavdevice/v4l2.c:1042:64: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 251 [-Wformat-truncation=]
 1042 |         ret = snprintf(device_name, sizeof(device_name), "/dev/%s", entry->d_name);
      |                                                                ^~
src/libavdevice/v4l2.c:1042:15: note: ‘snprintf’ output between 6 and 261 bytes into a destination of size 256
 1042 |         ret = snprintf(device_name, sizeof(device_name), "/dev/%s", entry->d_name);
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Previous patches intending to silence it have proposed increasing the
buffer size, but doing that correctly seems to be tricky. Failing on
truncation is simpler and just as effective (as excessively long device
names are unlikely).

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=877b6a9e82f73e772b8ac40d4b119f21ee037f0f
---

 libavdevice/v4l2.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index fefea54142..2ecfb9fae7 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -1033,13 +1033,19 @@ static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_l
     while ((entry = readdir(dir))) {
         AVDeviceInfo *device = NULL;
         struct v4l2_capability cap;
-        int fd = -1;
+        int fd = -1, size;
         char device_name[256];
 
         if (!v4l2_is_v4l_dev(entry->d_name))
             continue;
 
-        snprintf(device_name, sizeof(device_name), "/dev/%s", entry->d_name);
+        size = snprintf(device_name, sizeof(device_name), "/dev/%s", entry->d_name);
+        if (size >= sizeof(device_name)) {
+            av_log(ctx, AV_LOG_ERROR, "Device name too long.\n");
+            ret = AVERROR(ENOSYS);
+            break;
+        }
+
         if ((fd = device_open(ctx, device_name)) < 0)
             continue;
 



More information about the ffmpeg-cvslog mailing list