[libcamera-devel] hal: Fix comparison of unsigned integer < 0

Message ID 20190815122332.13717-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit a8b472598ebc7874651a92f6342c714c37b0f083
Headers show
Series
  • [libcamera-devel] hal: Fix comparison of unsigned integer < 0
Related show

Commit Message

Laurent Pinchart Aug. 15, 2019, 12:23 p.m. UTC
The CameraHalManager::getCameraInfo() validates the camera id it
receives from the camera service, and in doing so generates a compiler
error as the id is an unsigned integer and can never be negative:

../src/android/camera_hal_manager.cpp: In member function ‘CameraProxy* CameraHalManager::open(unsigned int, const hw_module_t*)’:
../src/android/camera_hal_manager.cpp:89:9: error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
  if (id < 0 || id >= numCameras()) {

Fix it by removing the unneeded comparison.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/android/camera_hal_manager.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jacopo Mondi Aug. 15, 2019, 12:35 p.m. UTC | #1
Hi Laurent,
   thanks for the fix

On Thu, Aug 15, 2019 at 03:23:32PM +0300, Laurent Pinchart wrote:
> The CameraHalManager::getCameraInfo() validates the camera id it
> receives from the camera service, and in doing so generates a compiler
> error as the id is an unsigned integer and can never be negative:
>
> ../src/android/camera_hal_manager.cpp: In member function ‘CameraProxy* CameraHalManager::open(unsigned int, const hw_module_t*)’:
> ../src/android/camera_hal_manager.cpp:89:9: error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
>   if (id < 0 || id >= numCameras()) {
>
> Fix it by removing the unneeded comparison.
>

If relevant please add which compiler reports the error, as on clang-8 it
is not reported. FYI with gcc-9.1 I see it as well.

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

> ---
>  src/android/camera_hal_manager.cpp | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
> index 1e66f63240da..37ba01355258 100644
> --- a/src/android/camera_hal_manager.cpp
> +++ b/src/android/camera_hal_manager.cpp
> @@ -86,7 +86,7 @@ void CameraHalManager::run()
>  CameraProxy *CameraHalManager::open(unsigned int id,
>  				    const hw_module_t *hardwareModule)
>  {
> -	if (id < 0 || id >= numCameras()) {
> +	if (id >= numCameras()) {
>  		LOG(HAL, Error) << "Invalid camera id '" << id << "'";
>  		return nullptr;
>  	}
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Niklas Söderlund Aug. 17, 2019, 1:19 p.m. UTC | #2
Hi Laurent,

Thanks for your work.

On 2019-08-15 15:23:32 +0300, Laurent Pinchart wrote:
> The CameraHalManager::getCameraInfo() validates the camera id it
> receives from the camera service, and in doing so generates a compiler
> error as the id is an unsigned integer and can never be negative:
> 
> ../src/android/camera_hal_manager.cpp: In member function ‘CameraProxy* CameraHalManager::open(unsigned int, const hw_module_t*)’:
> ../src/android/camera_hal_manager.cpp:89:9: error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
>   if (id < 0 || id >= numCameras()) {
> 
> Fix it by removing the unneeded comparison.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  src/android/camera_hal_manager.cpp | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
> index 1e66f63240da..37ba01355258 100644
> --- a/src/android/camera_hal_manager.cpp
> +++ b/src/android/camera_hal_manager.cpp
> @@ -86,7 +86,7 @@ void CameraHalManager::run()
>  CameraProxy *CameraHalManager::open(unsigned int id,
>  				    const hw_module_t *hardwareModule)
>  {
> -	if (id < 0 || id >= numCameras()) {
> +	if (id >= numCameras()) {
>  		LOG(HAL, Error) << "Invalid camera id '" << id << "'";
>  		return nullptr;
>  	}
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
index 1e66f63240da..37ba01355258 100644
--- a/src/android/camera_hal_manager.cpp
+++ b/src/android/camera_hal_manager.cpp
@@ -86,7 +86,7 @@  void CameraHalManager::run()
 CameraProxy *CameraHalManager::open(unsigned int id,
 				    const hw_module_t *hardwareModule)
 {
-	if (id < 0 || id >= numCameras()) {
+	if (id >= numCameras()) {
 		LOG(HAL, Error) << "Invalid camera id '" << id << "'";
 		return nullptr;
 	}