From patchwork Thu Nov 25 15:04:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 31661 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp847702iob; Thu, 25 Nov 2021 07:05:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZ2hCg43yFT+VwvYzSvBqYnwvQ45+5B6Io/QM9yzZF+3KMr7r2eTsD2QcK4qiufHZScRmX X-Received: by 2002:a50:ec16:: with SMTP id g22mr38839114edr.214.1637852721498; Thu, 25 Nov 2021 07:05:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637852721; cv=none; d=google.com; s=arc-20160816; b=ztiMdUoaffmcW0TKIbMXFN+WmaAKsNa80jcELc4wOxw2XWJHsjITwOzSqlLuTt/Wut 08y6XTGvizUduV150rfaqJCfW8l84yyO2ZYNNRJed94sxs8QhUgXIBjLOICSW4D/f9PL RrvtHHltUOr3+ZZNwEzICRpqmpy6GWLsAdP9UXFp8xDV3d5gQWlNiuFjNnMnwdei9QXh QsS1DLKUn3lEncLhI6SIY3LbHkc9uHsokHfH6x8xXGrtHH65b8z8ZKdat5/c3tAimnw0 DH6sIwmAuwwbdyJUpLiUQoRAeso0KmYOlDMzm5aRL/YCZXN6u5ClsS7xhgMPKpJ3hlHn prBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=X++LqHiOen1tIS+J31eEy8rQ8fMhT6ghNAXdYFYtZ3s=; b=Ns6lASFNNXBTOrgIoE34Q/pBMGAb/eGX//CE04S2WRDhRe7TT7z2l/6SPcDrSlsSTK dBCpP1kpLBAbvPhYUs6qTk/3C6ZJp9oh/KwcnpCIhyX8dFvqLUPfNyhJq+YBTC8yhTW6 eSvK0r4B1OCu2LhzzgPCyIhO/6eJHRjgczlAIEgcgIuDxDInbaSVq9bR6MBrzkDLE9Nw 0G4Librpt2DFtaqgfmgnI6JyL0nkFLWoJzvgXCcJBGFOqe+Vm5Q5ZFXam7CnjkNCe0lq dWgPdhnCu0W86lE0LYGr1xx5am8y72uYUu+CZa4oiKymzf0GvVExsu8Ujen+ylG7yWbv 0Wdw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id sh31si9829964ejc.257.2021.11.25.07.05.20; Thu, 25 Nov 2021 07:05:21 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CB46268AE6D; Thu, 25 Nov 2021 17:05:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1B11F689981 for ; Thu, 25 Nov 2021 17:05:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6D13E24017C for ; Thu, 25 Nov 2021 16:05:07 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id reNyTQ1C3sG5 for ; Thu, 25 Nov 2021 16:05:07 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 7D0AF24017A for ; Thu, 25 Nov 2021 16:05:06 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id B27193A0743; Thu, 25 Nov 2021 16:05:05 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Nov 2021 16:04:55 +0100 Message-Id: <20211125150500.25040-4-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211125150500.25040-1-anton@khirnov.net> References: <20211125150500.25040-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/9] lavd/v4l2: detect device name truncation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 4Dd/77iCoAXZ 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). --- libavdevice/v4l2.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index c286b31c69..80efc88203 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); + goto fail; + } + if ((fd = device_open(ctx, device_name)) < 0) continue;