From patchwork Tue Jun 18 01:06:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 20353 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 815ECBD87C for ; Tue, 18 Jun 2024 01:07:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1D292654A5; Tue, 18 Jun 2024 03:07:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="aK002GfU"; dkim-atps=neutral Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E78C5619F6 for ; Tue, 18 Jun 2024 03:07:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1718672820; x=1718932020; bh=XcquBwmotK+rCmriO0e7Rc0o7fyhxBlQ76xRGLmTduY=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=aK002GfUHxHMcwVRSr3Ep+5HffQ8RI1fHxjyvVR5JRpCTvgFuxwrpU8aCrpN5zuiR vJKP3v6J/1FEJs2iKV6zcYpsj8M53j+TFiBjp+pc8yGXZDu8y3zZz3bq5Aryjswt+3 GhmkcWp9hjvNAqzuXh9lMR8U7bVfe4LVJfUVKb6TZZLS05mgK49I5kf0xs0afreA/I 7U5l9IGxjvn9NntQLKlz/bkfPFupR46412l4gAqxGNWuCGc6ujY1kh6k5xHDq7xQIo ubam9QXhyxSdqw/c0TcGeyxkiCDBLp3hxYfqLrrvNirwdrc+DfUthyiVGLLtd99klj mZJkeWU3bssqA== Date: Tue, 18 Jun 2024 01:06:55 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v1] v4l2: v4l2_compat: Move `open*()` flag check into function Message-ID: <20240618010653.2346969-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: 37fff4d260ace952993c6d7353e5d72553bc313c MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This commit moves the check that determines whether the mode argument of `open*()` exists into a separate function. With that, the check is fixed because previously it failed to account for the fact that `O_TMPFILE` is not a power of two. Furthermore, add `asserts()` in the fortified variants that ensure that no mode is required by the specified flags. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- src/v4l2/v4l2_compat.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/v4l2/v4l2_compat.cpp b/src/v4l2/v4l2_compat.cpp index 8a44403e..5c6f925e 100644 --- a/src/v4l2/v4l2_compat.cpp +++ b/src/v4l2/v4l2_compat.cpp @@ -7,6 +7,7 @@ #include "v4l2_compat_manager.h" +#include #include #include #include @@ -28,12 +29,17 @@ using namespace libcamera; va_end(ap); \ } +static inline bool needs_mode(int flags) +{ + return (flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE); +} + extern "C" { LIBCAMERA_PUBLIC int open(const char *path, int oflag, ...) { mode_t mode = 0; - if (oflag & O_CREAT || oflag & O_TMPFILE) + if (needs_mode(oflag)) extract_va_arg(mode_t, mode, oflag); return V4L2CompatManager::instance()->openat(AT_FDCWD, path, @@ -43,6 +49,7 @@ LIBCAMERA_PUBLIC int open(const char *path, int oflag, ...) /* _FORTIFY_SOURCE redirects open to __open_2 */ LIBCAMERA_PUBLIC int __open_2(const char *path, int oflag) { + assert(!needs_mode(oflag)); return open(path, oflag); } @@ -50,7 +57,7 @@ LIBCAMERA_PUBLIC int __open_2(const char *path, int oflag) LIBCAMERA_PUBLIC int open64(const char *path, int oflag, ...) { mode_t mode = 0; - if (oflag & O_CREAT || oflag & O_TMPFILE) + if (needs_mode(oflag)) extract_va_arg(mode_t, mode, oflag); return V4L2CompatManager::instance()->openat(AT_FDCWD, path, @@ -59,6 +66,7 @@ LIBCAMERA_PUBLIC int open64(const char *path, int oflag, ...) LIBCAMERA_PUBLIC int __open64_2(const char *path, int oflag) { + assert(!needs_mode(oflag)); return open64(path, oflag); } #endif @@ -66,7 +74,7 @@ LIBCAMERA_PUBLIC int __open64_2(const char *path, int oflag) LIBCAMERA_PUBLIC int openat(int dirfd, const char *path, int oflag, ...) { mode_t mode = 0; - if (oflag & O_CREAT || oflag & O_TMPFILE) + if (needs_mode(oflag)) extract_va_arg(mode_t, mode, oflag); return V4L2CompatManager::instance()->openat(dirfd, path, oflag, mode); @@ -74,6 +82,7 @@ LIBCAMERA_PUBLIC int openat(int dirfd, const char *path, int oflag, ...) LIBCAMERA_PUBLIC int __openat_2(int dirfd, const char *path, int oflag) { + assert(!needs_mode(oflag)); return openat(dirfd, path, oflag); } @@ -81,7 +90,7 @@ LIBCAMERA_PUBLIC int __openat_2(int dirfd, const char *path, int oflag) LIBCAMERA_PUBLIC int openat64(int dirfd, const char *path, int oflag, ...) { mode_t mode = 0; - if (oflag & O_CREAT || oflag & O_TMPFILE) + if (needs_mode(oflag)) extract_va_arg(mode_t, mode, oflag); return V4L2CompatManager::instance()->openat(dirfd, path, @@ -90,6 +99,7 @@ LIBCAMERA_PUBLIC int openat64(int dirfd, const char *path, int oflag, ...) LIBCAMERA_PUBLIC int __openat64_2(int dirfd, const char *path, int oflag) { + assert(!needs_mode(oflag)); return openat64(dirfd, path, oflag); } #endif