From patchwork Wed Apr 5 08:14:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 18524 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 992A1C32A3 for ; Wed, 5 Apr 2023 08:14:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EB11B62767; Wed, 5 Apr 2023 10:14:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1680682482; bh=FtnHyAOR90JiVUYsn1W0VqIDMDythWC2N1zlHI/d0o4=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QD1xt+moa+Vx2BwTYwHJEP9bvxIi4uZm9w2I+B+Ddg5y2ueJ4iFcWKlK2+jGWwxZK 0L5BG6ggqBJb+LtGlU/EbLbzqF/8eujLw9JihJGOcFXFas033vJz/xnj0wXyp2dKmR 451gr5p09juRXtsDr3puAD3d9nOAQ0EBsNpjGthA56L2/Peo3qHefSJWclWrHvgGcU kyXI721+XFjIH4Nu0tR//Y3D6DxbiwxfYEPRtgcdDCfycuT2rCi4NJdGS6MK2tY+0R jFQKn8DuroVxs2iVmkW9AgcYgo0SUzBElWNy0zGkEwKPBl3lHpIHiYPv8vaJkpF6aA 6RfA/nS3J1/Wg== Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D68F061EC4 for ; Wed, 5 Apr 2023 10:14:40 +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="Ka9sOf2f"; dkim-atps=neutral Received: by mail-wm1-x32a.google.com with SMTP id s13so20363158wmr.4 for ; Wed, 05 Apr 2023 01:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; t=1680682480; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=mygre3XJMw6fTJh+BG3yPWhv4WZFhNQpRIGnoctMzA4=; b=Ka9sOf2fNHXmKMRwNp+aalSOPAogxJD2LgxrnThy4GFLIHs5/hLt0ukYiNP4l05bzj a6XgNiCGFjONFHrczCorpaZrHbzg+TmgGNS2weXmWabC26GyVaGGqG1dow4Qkg/7bx1S +CrU5vNBbFO/FDIxHzQ9ZHWOvGO0Bzvj4RZ6ZWRXuQEL1ZWGyVEMIa+tyvC08THDmiRf rG50zP873LHWyM+oSgoLshr3XdRnFk9K8ImcyZQILHJ9d7ow6jyPWw5SmWFl5yP8uXiN zipIWiuw87fNzI6Wegi0hchc6MPF+n1rRwpSHJFQtCaGjTjOpQG/haNqSgUzkDX98boq bTOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680682480; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mygre3XJMw6fTJh+BG3yPWhv4WZFhNQpRIGnoctMzA4=; b=pkooAF4qjd8BarictdlLoNY+lfxhoVghyhypTyU28xGI2v9iEnHecl7adP03G/0bGB Z/dcW5BPZMjRoIMY6Lna7VCS01Hh8/6P6p79mSW+ga5c1BU9azV3oabRE5PhlvnNRxnu rCfNqHxcjHh9zAX582YOsa2Xcqg1ctwi7gdVXBPxAQ2UYCRe97Lx0L3oKhWyczCcxA/v fAonF/DhsaG1mlnyBxNENWgCzRuRufEKVJCsxAGRqSi/mfDbZhFFovhrVtM2C3iZKRDb ajRUvr+m3VYdLW0fHvzfqgGR5CLTcmtgmS9bpofyM+KOwuxdcqIXKt/ZFdeqkFyudqdF 7K/g== X-Gm-Message-State: AAQBX9de/ClqBN7DhTOM98pQqxb2kvcCPLepvm2V/kpzXC+I6xJns+Gh RbsHQ4PHL/V96jmhvLknhikdzA== X-Google-Smtp-Source: AKy350Y+wojnIYEGcoI1WQQxFnIpsBbqcE3YafpBm9e6SACIJpDEIktlT0DDKYrCSKnQNNuypVvLYw== X-Received: by 2002:a7b:c395:0:b0:3ee:9c0e:c78f with SMTP id s21-20020a7bc395000000b003ee9c0ec78fmr3676459wmj.5.1680682480491; Wed, 05 Apr 2023 01:14:40 -0700 (PDT) Received: from [192.168.1.20] ([2a01:cb19:85e6:1900:2bf7:7388:731d:c4e1]) by smtp.gmail.com with ESMTPSA id p11-20020a05600c1d8b00b003ef36ef3833sm1403957wms.8.2023.04.05.01.14.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 01:14:40 -0700 (PDT) Date: Wed, 05 Apr 2023 10:14:30 +0200 MIME-Version: 1.0 Message-Id: <20230404-guard-idmap-v2-1-444d135b3895@baylibre.com> References: <20230404-guard-idmap-v2-0-444d135b3895@baylibre.com> In-Reply-To: <20230404-guard-idmap-v2-0-444d135b3895@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.13-dev-00303 Subject: [libcamera-devel] [PATCH v2 1/2] 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: 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 --- src/libcamera/controls.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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(); From patchwork Wed Apr 5 08:14:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 18525 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 5A2CDC32A4 for ; Wed, 5 Apr 2023 08:14:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 690886276A; Wed, 5 Apr 2023 10:14:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1680682483; bh=msB3Wzrrhi3I9TAVWNTF1/dDu3KNso0+MWxlawnagD8=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=aKhz3CaLEACJwTtj8Jf/TDh43cTUvR76wtIr2zTAa/CZqMRLOLTU28JzPymTNFOUJ ewKEVfTn0lMt/e+oTC3/pbCrKF2+cxrkWBdoV9RIIxBu2KO7mTBV3digUvQeX2upSV bIIxirWO9ei4Ui4xDv9DP3XgxEfTacu3VahOCPNvGsWBBbUltZrtEfS5s2SeeT53MH ViddCztmxzTZO+Bn5s7QPSY9drnnA000JwTpDuZwd0qqcyehVFyLUGGJlUizHU17fP CEleczhazf+XY06InrSvLssEvpp5hZ41Vm8YI//FYW6NLemrVnHFUIME1/VYc5sG/k 3tzRzAvEqcRRg== Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4496861EC4 for ; Wed, 5 Apr 2023 10:14:41 +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="qjLfmr/y"; dkim-atps=neutral Received: by mail-wm1-x32a.google.com with SMTP id s13so20363187wmr.4 for ; Wed, 05 Apr 2023 01:14:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; t=1680682481; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=hz8Bv7ipSekhM/wCfh/FxRtDIkLeLdvvGon+FKEuBKA=; b=qjLfmr/yNcsqTn2gfjzQ4SuPT2j26jTyK/u4L07DY9cNkuQhLGtlseARHnEIw0Am+J UCHzVTgP//7C5/WZQwrAXyaJrXKxwyNgObzcFPDdkUTgcYMYSwIavl8SIjPSftfdnzIB u8u10ByiCub5vaoIvQA+4Gt+juueZ3isILICwr4y3Lxk7jEChl/2tOk0FYQkicPn91ZS hf5qD190ZqdbQ9JKn2lDLyg6+bKWUhFG5gw4k/DHArM0qsvinqKeVY67eMjpCfRmtVDM rjcA7zU5M6khsPsyg+ysAzgGfwyXoHS5Q5+9+l2EAow6+cnvXew6f1OiQ8PpLQooQX3/ AerQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680682481; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hz8Bv7ipSekhM/wCfh/FxRtDIkLeLdvvGon+FKEuBKA=; b=fyRUHVirAwnUkvPnUNrBVYntQt4/6zWVh72C86IYwQnAJFulyUCK+FgWqm9jYPhpD6 RYFKdPvK1W04QCgj6NN7AsJNeXToZg7wW121oVQ+j5TttYxE1gzB/c5lyPXFtt+N6swX uo8WTrgTeRhoC4OwZoHaWUEu/Hb92FGQVfWZCY+xpxkZCDepA83p7GkfMxdsuX4lzeoD DICb93gKuXanFsqETHLeYF8GwUvJ4zSqHNE9w1CRL5z1SuBASOrFzs3dj0BuYwIi6EO0 MrH7dOiBnSogneD7Sk4UuE3YIPDeAFT5+T+10952GppGqptGRL/mFaZ6vn7pyfhK1OQh Am6A== X-Gm-Message-State: AAQBX9dStPje9BdIA9Uu6E8nRt8ZI+IVJ1Ni+mxEFbesXmrO2ypVU3kB kgGUai9xAIypEgkTmO5lSuRzoQ== X-Google-Smtp-Source: AKy350Z6abUXzjqsdWErg/b5QcUM5EeUHM9NYaTt4Asps7lrG8FuTxqoVkkRZnP4dRDpPUgD/hC0xg== X-Received: by 2002:a05:600c:2117:b0:3ee:672d:caa5 with SMTP id u23-20020a05600c211700b003ee672dcaa5mr3750029wml.17.1680682481091; Wed, 05 Apr 2023 01:14:41 -0700 (PDT) Received: from [192.168.1.20] ([2a01:cb19:85e6:1900:2bf7:7388:731d:c4e1]) by smtp.gmail.com with ESMTPSA id p11-20020a05600c1d8b00b003ef36ef3833sm1403957wms.8.2023.04.05.01.14.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 01:14:40 -0700 (PDT) Date: Wed, 05 Apr 2023 10:14:31 +0200 MIME-Version: 1.0 Message-Id: <20230404-guard-idmap-v2-2-444d135b3895@baylibre.com> References: <20230404-guard-idmap-v2-0-444d135b3895@baylibre.com> In-Reply-To: <20230404-guard-idmap-v2-0-444d135b3895@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.13-dev-00303 Subject: [libcamera-devel] [PATCH v2 2/2] test: controls: control_info_map: Test default constructor 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" ControlInfoMap can be default-constructed. In that case, some of its members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault. Add a test with a default constructed ControlInfoMap to cover this. Signed-off-by: Mattijs Korpershoek Reviewed-by: Jacopo Mondi Reviewed-by: Umang Jain Reviewed-by: Laurent Pinchart --- test/controls/control_info_map.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp index db95945a1580..29b33515e48c 100644 --- a/test/controls/control_info_map.cpp +++ b/test/controls/control_info_map.cpp @@ -75,6 +75,13 @@ protected: return TestFail; } + /* Test looking up a control on a default-constructed infoMap */ + const ControlInfoMap emptyInfoMap; + if (emptyInfoMap.find(12345) != emptyInfoMap.end()) { + cerr << "find() on empty ControlInfoMap failed" << endl; + return TestFail; + } + return TestPass; } };