From patchwork Tue Apr 4 16:11:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 18522 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 8886CC0F2A for ; Tue, 4 Apr 2023 16:11:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF0FA62724; Tue, 4 Apr 2023 18:11:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1680624683; bh=QcSH2iUvuO32L5ZbL6Mv7CNgoHp9OFYOONs6/dH1BPg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=LhgqvoCY9n7bqXNsY54fb8gXSbaaVDahrqAUVDbN/z/2F2U3Yd47Tfy6w1dYLY6um jeVBir9J9ijjtZmZP2jTqursBPxlSjsSzdlHvdLBnXVE1Vc6de3n1aFBU+GeaxKemF pu44A0KV7r+7V7RSX1I7ZWIHkn0PE7UDeoE2YsvnQN8hsGBPzEsDmriSiKAKcbmDdJ 9EZNzMt63rQw3gSYpJ9PorKshE14gIPmxHok6HLyQ5PEw8QTpOFW8uFyoqKmvgCVjZ Tnm3B3xrSFpcK3Z+bQ3yl1Awh6ACXAj51by25RDeldIhHzB7LrYMh2Kdf8mpg3zUbq MWarEawZQUdxw== Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 33694626DB for ; Tue, 4 Apr 2023 18:11:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="iMSCtHLW"; dkim-atps=neutral Received: by mail-wr1-x42b.google.com with SMTP id i9so33401024wrp.3 for ; Tue, 04 Apr 2023 09:11:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; t=1680624682; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=WldeUi+pF2aTTNBdp8g3pArwejx+IeB0Qyypmeuvzbc=; b=iMSCtHLWd3untLOLr5dHQuYTPTXzr7OyqocIoMZTGy+Gm4wT3SeewY8Wdy+mggRze6 oFBAeZF/j+Oa9Uwhv4fUVVtxL275+XkixhMOwcnEs+7f7/jgJRL4/jvz0Mibjy5xUYls fgqmR9wUjKi6F2GDbgSPg6A2avFrjTE2MuJALmNFLjcxgamMuuEbWE+cbxgPBWZEGRgZ ramzV1d/cXmRIamW09jbzX+LYyNHCS/2prQlD201LJO7sA/H3BKrRN8HJpwFC9CeYAb/ RSVXw5L+Uj9NJkeDRBn6C7s/sZV9FiXUk8C0/qoFug/RoZdjlM2UawZKQRV/DPV3Byxa RVlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680624682; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WldeUi+pF2aTTNBdp8g3pArwejx+IeB0Qyypmeuvzbc=; b=LPVddROjS8ttNxVuMI1X1FCiiwD0L7yC8XlrPRKMaMKQjfQFK1IKhJMHNiphps5L3i HiabZDowb813ACbqcjg/RYMC5zmfGtbwQQta4CuRZjtrbGwmxg5m/r+gYWD36vdZWIXM VU0xCLF5pFM/Az/vWHCLPPeDji3I+O/uYFwmz6YYSmK0+umDVqd/GZDGFhnD40lFLtcZ ZA9DRZ/emMovXkARPjUBvJsZGdJToOQ8rNHFtt2QldhWiucPLwY8vAVEtZ5yGvYlYeDS E3HiQVQ0DbPnXfB6esBFpj4YWYq8Q7ezAo6abAo4Dex/HGSV7mnISCbS0wbV6QTh1Kdr vhEg== X-Gm-Message-State: AAQBX9e6Euu3GzFnChET/Uve65sDnerBNmBQwecj29LBDal/oFuTM4V9 O8Zs6bVYi7Qvzl4nX3pp+CPjFg== X-Google-Smtp-Source: AKy350YB4jsiThSNVibgmfJoZSvmbnpWamfhEwWH9xTzI5TTK3TW2CkSIoOkE4SATM2dtsCt8lH6rg== X-Received: by 2002:adf:e8cb:0:b0:2da:45f4:c053 with SMTP id k11-20020adfe8cb000000b002da45f4c053mr1885307wrn.52.1680624681754; Tue, 04 Apr 2023 09:11:21 -0700 (PDT) Received: from [192.168.1.20] ([2a01:cb19:85e6:1900:2bf7:7388:731d:c4e1]) by smtp.gmail.com with ESMTPSA id m2-20020a5d4a02000000b002d75ef32032sm12625130wrq.68.2023.04.04.09.11.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:11:21 -0700 (PDT) Date: Tue, 04 Apr 2023 18:11:16 +0200 MIME-Version: 1.0 Message-Id: <20230404-guard-idmap-v1-1-b75c2d924caf@baylibre.com> X-B4-Tracking: v=1; b=H4sIACNMLGQC/x2NQQqDMBBFryKzbmiS6qK9SuliTEYziEmYwVIQ7 97o8j3+4++gJEwKr24HoS8rl9zA3ToICfNMhmNj8NY/bG97M28osckVq3HxOUzeWYsBoRUjKpl RMId0NutSpJJoKrTcg/I5qUIT/67D9+c4/hu8HUOAAAAA To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.13-dev-00303 Subject: [libcamera-devel] [PATCH] libcamera: controls: guard ControlInfoMap against nullptr idmap_ 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: , X-Patchwork-Original-From: Mattijs Korpershoek via libcamera-devel From: Mattijs Korpershoek Reply-To: Mattijs Korpershoek Cc: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" It's possible to construct a Camera with an unsafe controlInfo_. This is the case in the Simple pipeline, where the camera controls are not populated. With Simple, if we attempt to set a Control, we end up with a segfault because the default constructor for ControlInfoMap doesn't intialized idmap_ which is initialized at class declaration time as const ControlIdMap *idmap_ = nullptr; Add some safeguards in ControlInfoMap to handle this case. Link: http://codepad.org/CiLLcPNW Link: https://lists.libcamera.org/pipermail/libcamera-devel/2023-April/037439.html Suggested-by: Jacopo Mondi Signed-off-by: Mattijs Korpershoek Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- I have build-tested this against master and functionally tested on a v0.0.4 android integration branch. I've tested that i'm able to do a camera preview even when the ScalerCrop control is unavailble. Note that Jacopo already discussed alternative implementation by making idmap_ an instance instead of a pointer, but that seemed not a good idea either: https://lists.libcamera.org/pipermail/libcamera-devel/2023-April/037439.html --- src/libcamera/controls.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) --- base-commit: ac7511dc4c594f567ddff27ccc02c30bf6c00bfd change-id: 20230404-guard-idmap-1d95f2100aca Best regards, diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index 6dbf9b348709..b808116c01e5 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -677,6 +677,9 @@ ControlInfoMap::ControlInfoMap(Map &&info, const ControlIdMap &idmap) bool ControlInfoMap::validate() { + if (!idmap_) + return false; + for (const auto &ctrl : *this) { const ControlId *id = ctrl.first; auto it = idmap_->find(id->id()); @@ -719,6 +722,8 @@ bool ControlInfoMap::validate() */ ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id) { + ASSERT(idmap_); + return at(idmap_->at(id)); } @@ -729,6 +734,8 @@ ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id) */ const ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id) const { + ASSERT(idmap_); + return at(idmap_->at(id)); } @@ -739,6 +746,9 @@ const ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id) const */ ControlInfoMap::size_type ControlInfoMap::count(unsigned int id) const { + if (!idmap_) + return 0; + /* * The ControlInfoMap and its idmap have a 1:1 mapping between their * entries, we can thus just count the matching entries in idmap to @@ -755,6 +765,9 @@ ControlInfoMap::size_type ControlInfoMap::count(unsigned int id) const */ ControlInfoMap::iterator ControlInfoMap::find(unsigned int id) { + if (!idmap_) + return end(); + auto iter = idmap_->find(id); if (iter == idmap_->end()) return end(); @@ -770,6 +783,9 @@ ControlInfoMap::iterator ControlInfoMap::find(unsigned int id) */ ControlInfoMap::const_iterator ControlInfoMap::find(unsigned int id) const { + if (!idmap_) + return end(); + auto iter = idmap_->find(id); if (iter == idmap_->end()) return end();