[libcamera-devel] libcamera: v4l2_videodevice: Fix potential errno overwrite
diff mbox series

Message ID 20220331154526.27882-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit f9e2df951969f10f1ee1e46fb806942a27db8aca
Headers show
Series
  • [libcamera-devel] libcamera: v4l2_videodevice: Fix potential errno overwrite
Related show

Commit Message

Laurent Pinchart March 31, 2022, 3:45 p.m. UTC
The errno variable can potentially be overwritten by operator<<(). Store
it in a local variable before logging the error message.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/v4l2_videodevice.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Kieran Bingham March 31, 2022, 4:08 p.m. UTC | #1
Quoting Laurent Pinchart via libcamera-devel (2022-03-31 16:45:26)
> The errno variable can potentially be overwritten by operator<<(). Store
> it in a local variable before logging the error message.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/libcamera/v4l2_videodevice.cpp | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 009f6d55610f..0830be80c553 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -647,14 +647,17 @@ int V4L2VideoDevice::open()
>   */
>  int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type)
>  {
> +       int ret;
> +
>         UniqueFD newFd = handle.dup();
>         if (!newFd.isValid()) {
> +               ret = -errno;
>                 LOG(V4L2, Error) << "Failed to duplicate file handle: "
> -                                << strerror(errno);
> -               return -errno;
> +                                << strerror(-ret);
> +               return ret;
>         }
>  
> -       int ret = V4L2Device::setFd(std::move(newFd));
> +       ret = V4L2Device::setFd(std::move(newFd));
>         if (ret < 0) {
>                 LOG(V4L2, Error) << "Failed to set file handle: "
>                                  << strerror(-ret);
> -- 
> Regards,
> 
> Laurent Pinchart
>
Umang Jain March 31, 2022, 4:28 p.m. UTC | #2
Hello,

On 3/31/22 21:15, Laurent Pinchart via libcamera-devel wrote:
> The errno variable can potentially be overwritten by operator<<(). Store


Ah good to know!

> it in a local variable before logging the error message.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>


Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>

> ---
>   src/libcamera/v4l2_videodevice.cpp | 9 ++++++---
>   1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 009f6d55610f..0830be80c553 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -647,14 +647,17 @@ int V4L2VideoDevice::open()
>    */
>   int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type)
>   {
> +	int ret;
> +
>   	UniqueFD newFd = handle.dup();
>   	if (!newFd.isValid()) {
> +		ret = -errno;
>   		LOG(V4L2, Error) << "Failed to duplicate file handle: "
> -				 << strerror(errno);
> -		return -errno;
> +				 << strerror(-ret);
> +		return ret;
>   	}
>   
> -	int ret = V4L2Device::setFd(std::move(newFd));
> +	ret = V4L2Device::setFd(std::move(newFd));
>   	if (ret < 0) {
>   		LOG(V4L2, Error) << "Failed to set file handle: "
>   				 << strerror(-ret);

Patch
diff mbox series

diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 009f6d55610f..0830be80c553 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -647,14 +647,17 @@  int V4L2VideoDevice::open()
  */
 int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type)
 {
+	int ret;
+
 	UniqueFD newFd = handle.dup();
 	if (!newFd.isValid()) {
+		ret = -errno;
 		LOG(V4L2, Error) << "Failed to duplicate file handle: "
-				 << strerror(errno);
-		return -errno;
+				 << strerror(-ret);
+		return ret;
 	}
 
-	int ret = V4L2Device::setFd(std::move(newFd));
+	ret = V4L2Device::setFd(std::move(newFd));
 	if (ret < 0) {
 		LOG(V4L2, Error) << "Failed to set file handle: "
 				 << strerror(-ret);