android: Protect against null callbacks

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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Umang Jain <email@uajain.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2020-09-08 10:48:46 +03:00
parent 219cbfe76b
commit de20029a58
2 changed files with 7 additions and 2 deletions

View file

@ -32,7 +32,7 @@ static int hal_get_camera_info(int id, struct camera_info *info)
return cameraManager.getCameraInfo(id, info); return cameraManager.getCameraInfo(id, info);
} }
static int hal_set_callbacks([[maybe_unused]] const camera_module_callbacks_t *callbacks) static int hal_set_callbacks(const camera_module_callbacks_t *callbacks)
{ {
cameraManager.setCallbacks(callbacks); cameraManager.setCallbacks(callbacks);

View file

@ -29,7 +29,7 @@ LOG_DECLARE_CATEGORY(HAL);
*/ */
CameraHalManager::CameraHalManager() CameraHalManager::CameraHalManager()
: cameraManager_(nullptr), numInternalCameras_(0), : cameraManager_(nullptr), callbacks_(nullptr), numInternalCameras_(0),
nextExternalCameraId_(firstExternalCameraId_) nextExternalCameraId_(firstExternalCameraId_)
{ {
} }
@ -70,6 +70,11 @@ CameraDevice *CameraHalManager::open(unsigned int id,
{ {
MutexLocker locker(mutex_); MutexLocker locker(mutex_);
if (!callbacks_) {
LOG(HAL, Error) << "Can't open camera before callbacks are set";
return nullptr;
}
CameraDevice *camera = cameraDeviceFromHalId(id); CameraDevice *camera = cameraDeviceFromHalId(id);
if (!camera) { if (!camera) {
LOG(HAL, Error) << "Invalid camera id '" << id << "'"; LOG(HAL, Error) << "Invalid camera id '" << id << "'";