{"id":9530,"url":"https://patchwork.libcamera.org/api/1.1/patches/9530/?format=json","web_url":"https://patchwork.libcamera.org/patch/9530/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200908075424.14101-1-laurent.pinchart@ideasonboard.com>","date":"2020-09-08T07:54:24","name":"[libcamera-devel] android: Protect against null callbacks","commit_ref":"de20029a582a71a87d99388a62fb63c86e85028a","pull_url":null,"state":"accepted","archived":false,"hash":"412e61a25192a41f0717dae0f214d4a6f2bbee6c","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9530/mbox/","series":[{"id":1273,"url":"https://patchwork.libcamera.org/api/1.1/series/1273/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1273","date":"2020-09-08T07:54:24","name":"[libcamera-devel] android: Protect against null callbacks","version":1,"mbox":"https://patchwork.libcamera.org/series/1273/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9530/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9530/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 469C1BDB1D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Sep 2020 07:54:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 135C762BBA;\n\tTue,  8 Sep 2020 09:54:56 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D329F60534\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Sep 2020 09:54:54 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5621239;\n\tTue,  8 Sep 2020 09:54:53 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"d5ke+veZ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1599551694;\n\tbh=DeETnSJKCwRHsa2r3Nv54I3xOIUmCszwDKnaOPsyXMI=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=d5ke+veZb/Fq/npUHnoPWK8X2P7N7FPMn675T2TPJySb2YeJVBy3EnVDRIJjrrg4y\n\tHK8fsbf38BFT1uUQariVmpevdPVht2caiAb8aqZgJj66OXHANjlnCyOsLjIHCtaXK2\n\t1GnCIs2nxa3z0DRA+LgDnpxxwYTIXX533WwbMtsc=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  8 Sep 2020 10:54:24 +0300","Message-Id":"<20200908075424.14101-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH] android: Protect against null callbacks","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"According to the Android camera HAL C interface documentation, the\ncamera service is supposed to set callbacks after initializing the HAL\nand calling get_number_of_cameras(), before any other calls to the\nmodule. We rely on this behaviour and use callbacks unconditionally,\nwhich would lead to a crash if the camera service behaved incorrectly.\n\nWhile the camera service isn't supposed to behave incorrectly,\ngracefully handling the error when opening cameras isn't costly, and\nprovides better diagnostic than a crash.\n\nWhile at it, removed an unneeded [[maybe_unused]] attribute.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/android/camera3_hal.cpp        | 2 +-\n src/android/camera_hal_manager.cpp | 7 ++++++-\n 2 files changed, 7 insertions(+), 2 deletions(-)","diff":"diff --git a/src/android/camera3_hal.cpp b/src/android/camera3_hal.cpp\nindex 67a497b8c829..d6e04af21470 100644\n--- a/src/android/camera3_hal.cpp\n+++ b/src/android/camera3_hal.cpp\n@@ -32,7 +32,7 @@ static int hal_get_camera_info(int id, struct camera_info *info)\n \treturn cameraManager.getCameraInfo(id, info);\n }\n \n-static int hal_set_callbacks([[maybe_unused]] const camera_module_callbacks_t *callbacks)\n+static int hal_set_callbacks(const camera_module_callbacks_t *callbacks)\n {\n \tcameraManager.setCallbacks(callbacks);\n \ndiff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\nindex a1805ebccf24..05b474010b1d 100644\n--- a/src/android/camera_hal_manager.cpp\n+++ b/src/android/camera_hal_manager.cpp\n@@ -29,7 +29,7 @@ LOG_DECLARE_CATEGORY(HAL);\n  */\n \n CameraHalManager::CameraHalManager()\n-\t: cameraManager_(nullptr), numInternalCameras_(0),\n+\t: cameraManager_(nullptr), callbacks_(nullptr), numInternalCameras_(0),\n \t  nextExternalCameraId_(firstExternalCameraId_)\n {\n }\n@@ -70,6 +70,11 @@ CameraDevice *CameraHalManager::open(unsigned int id,\n {\n \tMutexLocker locker(mutex_);\n \n+\tif (!callbacks_) {\n+\t\tLOG(HAL, Error) << \"Can't open camera before callbacks are set\";\n+\t\treturn nullptr;\n+\t}\n+\n \tCameraDevice *camera = cameraDeviceFromHalId(id);\n \tif (!camera) {\n \t\tLOG(HAL, Error) << \"Invalid camera id '\" << id << \"'\";\n","prefixes":["libcamera-devel"]}