From de20029a582a71a87d99388a62fb63c86e85028a Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 8 Sep 2020 10:48:46 +0300 Subject: android: Protect against null callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to the Android camera HAL C interface documentation, the camera service is supposed to set callbacks after initializing the HAL and calling get_number_of_cameras(), before any other calls to the module. We rely on this behaviour and use callbacks unconditionally, which would lead to a crash if the camera service behaved incorrectly. While the camera service isn't supposed to behave incorrectly, gracefully handling the error when opening cameras isn't costly, and provides better diagnostic than a crash. While at it, removed an unneeded [[maybe_unused]] attribute. Reported-by: Coverity CID=298638 Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Umang Jain Reviewed-by: Kieran Bingham --- src/android/camera_hal_manager.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/android/camera_hal_manager.cpp') diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp index a1805ebc..05b47401 100644 --- a/src/android/camera_hal_manager.cpp +++ b/src/android/camera_hal_manager.cpp @@ -29,7 +29,7 @@ LOG_DECLARE_CATEGORY(HAL); */ CameraHalManager::CameraHalManager() - : cameraManager_(nullptr), numInternalCameras_(0), + : cameraManager_(nullptr), callbacks_(nullptr), numInternalCameras_(0), nextExternalCameraId_(firstExternalCameraId_) { } @@ -70,6 +70,11 @@ CameraDevice *CameraHalManager::open(unsigned int id, { MutexLocker locker(mutex_); + if (!callbacks_) { + LOG(HAL, Error) << "Can't open camera before callbacks are set"; + return nullptr; + } + CameraDevice *camera = cameraDeviceFromHalId(id); if (!camera) { LOG(HAL, Error) << "Invalid camera id '" << id << "'"; -- cgit v1.2.1