Patch Detail
Show a patch.
GET /api/patches/19964/?format=api
{ "id": 19964, "url": "https://patchwork.libcamera.org/api/patches/19964/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19964/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240429142406.59765-1-pobrn@protonmail.com>", "date": "2024-04-29T14:24:09", "name": "[v3] treewide: Query list of cameras just once", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "0c56d8cd2686d0ab36a347cf1ee78861286a125b", "submitter": { "id": 133, "url": "https://patchwork.libcamera.org/api/people/133/?format=api", "name": "Pőcze Barnabás", "email": "pobrn@protonmail.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19964/mbox/", "series": [ { "id": 4278, "url": "https://patchwork.libcamera.org/api/series/4278/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4278", "date": "2024-04-29T14:24:09", "name": "[v3] treewide: Query list of cameras just once", "version": 3, "mbox": "https://patchwork.libcamera.org/series/4278/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19964/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19964/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id B6AADBE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Apr 2024 14:24:18 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B6BD763415;\n\tMon, 29 Apr 2024 16:24:17 +0200 (CEST)", "from mail-40131.protonmail.ch (mail-40131.protonmail.ch\n\t[185.70.40.131])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 96529633F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Apr 2024 16:24:15 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"N2oZMVzd\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1714400654; x=1714659854;\n\tbh=9h0QJ7cHA/nEDNA8hhvlK76ajxV+OWSVbXwajNzrgWM=;\n\th=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date:\n\tSubject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector;\n\tb=N2oZMVzdsrZ7fyGgQwVjEXxUW9LEN6r2/DygyfFyOSezribH6N3AMeipRPn6+e2oo\n\t585JyTsl6duGZj3MihyzjzXjM1TR9DSc75NILZCHYM17fRtzorYDlSmZFwsUSWb8j2\n\tAlMZfCidS9d6pcrxwq5bv+4ksFdLFZIcNnuxOIh4RhPHcW/qRQ2f6Sn2U6RusWvbB2\n\tSJL5+/X/ihzqYG4DI9538YCc+SEGgeo2Xbce3WFzx/95dwtAX+aKrqbuu08B0V/WkT\n\tZ/gyWVH3FR9U1wh5EUxhFp5/1OQfO+Vzmot5Rdfbe52xlBF8ETIMYavlQaAEipBjqO\n\ttN1DU3lSqmqzg==", "Date": "Mon, 29 Apr 2024 14:24:09 +0000", "To": "libcamera-devel@lists.libcamera.org", "From": "=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>", "Subject": "[PATCH v3] treewide: Query list of cameras just once", "Message-ID": "<20240429142406.59765-1-pobrn@protonmail.com>", "Feedback-ID": "20568564:user:proton", "X-Pm-Message-ID": "900f6db4446b275d500bc26e2477b3811ffe5640", "MIME-Version": "1.0", "Content-Type": "text/plain; charset=utf-8", "Content-Transfer-Encoding": "quoted-printable", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "This is more efficient since only a single vector will be constructed,\nand furthermore, it prevents the TOCTOU issue that might arise when\nthe list of cameras changes between the two queries.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n---\n\nchanges in v3\n * drop std::move() from this change\n * extend note in documentation\n * limit scope of `cameras` in `CameraSession::CameraSession()`\n\nchanges in v2\n * fix code block in documentation\n * add comment noting that the camera may disappear\n\n---\n Documentation/guides/application-developer.rst | 12 +++++++-----\n src/apps/cam/camera_session.cpp | 11 ++++++++---\n src/gstreamer/gstlibcamerasrc.cpp | 5 +++--\n 3 files changed, 18 insertions(+), 10 deletions(-)\n\n\nbase-commit: fb74bb7df66b96dbe28702155cddfc96a1b30f78\n--\n2.44.0", "diff": "diff --git a/Documentation/guides/application-developer.rst b/Documentation/guides/application-developer.rst\nindex 9a9905b1..92e2a373 100644\n--- a/Documentation/guides/application-developer.rst\n+++ b/Documentation/guides/application-developer.rst\n@@ -116,19 +116,21 @@ available.\n\n .. code:: cpp\n\n- if (cm->cameras().empty()) {\n+ auto cameras = cm->cameras();\n+ if (cameras.empty()) {\n std::cout << \"No cameras were identified on the system.\"\n << std::endl;\n cm->stop();\n return EXIT_FAILURE;\n }\n\n- std::string cameraId = cm->cameras()[0]->id();\n- camera = cm->get(cameraId);\n+ std::string cameraId = cameras[0]->id();\n\n+ auto camera = cm->get(cameraId);\n /*\n- * Note that is equivalent to:\n- * camera = cm->cameras()[0];\n+ * Note that `camera` may not compare equal to `cameras[0]`.\n+ * In fact, it might simply be a `nullptr`, as the particular\n+ * device might have disappeared (and reappeared) in the meantime.\n */\n\n Once a camera has been selected an application needs to acquire an exclusive\ndiff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp\nindex 8447f932..334d2ed8 100644\n--- a/src/apps/cam/camera_session.cpp\n+++ b/src/apps/cam/camera_session.cpp\n@@ -39,9 +39,14 @@ CameraSession::CameraSession(CameraManager *cm,\n {\n \tchar *endptr;\n \tunsigned long index = strtoul(cameraId.c_str(), &endptr, 10);\n-\tif (*endptr == '\\0' && index > 0 && index <= cm->cameras().size())\n-\t\tcamera_ = cm->cameras()[index - 1];\n-\telse\n+\n+\tif (*endptr == '\\0' && index > 0) {\n+\t\tauto cameras = cm->cameras();\n+\t\tif (index <= cameras.size())\n+\t\t\tcamera_ = cameras[index - 1];\n+\t}\n+\n+\tif (!camera_)\n \t\tcamera_ = cm->get(cameraId);\n\n \tif (!camera_) {\ndiff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex f015c6d2..4eddfa3e 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -385,13 +385,14 @@ gst_libcamera_src_open(GstLibcameraSrc *self)\n \t\t\treturn false;\n \t\t}\n \t} else {\n-\t\tif (cm->cameras().empty()) {\n+\t\tauto cameras = cm->cameras();\n+\t\tif (cameras.empty()) {\n \t\t\tGST_ELEMENT_ERROR(self, RESOURCE, NOT_FOUND,\n \t\t\t\t\t (\"Could not find any supported camera on this system.\"),\n \t\t\t\t\t (\"libcamera::CameraMananger::cameras() is empty\"));\n \t\t\treturn false;\n \t\t}\n-\t\tcam = cm->cameras()[0];\n+\t\tcam = cameras[0];\n \t}\n\n \tGST_INFO_OBJECT(self, \"Using camera '%s'\", cam->id().c_str());\n", "prefixes": [ "v3" ] }