[libcamera-devel,v2,7/7] v4l2: v4l2_camera_proxy: Fix segfault on restarting streams

Message ID 20200605090106.15424-8-paul.elder@ideasonboard.com
State Accepted
Headers show
Series
  • Support qv4l2 with v4l2-compat
Related show

Commit Message

Paul Elder June 5, 2020, 9:01 a.m. UTC
The V4L2 compatibility layer keeps track of the index of the next buffer
to be dequeued, to handle VIDIOC_DQBUF. This index is set to 0 on
startup and incremented (modulo #frames), and is otherwise never reset.
This means that if the last handled frame index is not #frames-1, and
the stream is restarted without restarting libcamera and the V4L2
compatilibity layer, the buffer index number won't match with the
libcamera buffer index number, causing a segfault. Fix this by resetting
the current buffer index to zero on VIDIOC_STREAMON.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

---
New in v2
---
 src/v4l2/v4l2_camera_proxy.cpp | 2 ++
 1 file changed, 2 insertions(+)

Comments

Laurent Pinchart June 5, 2020, 10:18 a.m. UTC | #1
Hi Paul,

Thank you for the patch.

On Fri, Jun 05, 2020 at 06:01:06PM +0900, Paul Elder wrote:
> The V4L2 compatibility layer keeps track of the index of the next buffer
> to be dequeued, to handle VIDIOC_DQBUF. This index is set to 0 on
> startup and incremented (modulo #frames), and is otherwise never reset.
> This means that if the last handled frame index is not #frames-1, and
> the stream is restarted without restarting libcamera and the V4L2
> compatilibity layer, the buffer index number won't match with the
> libcamera buffer index number, causing a segfault. Fix this by resetting
> the current buffer index to zero on VIDIOC_STREAMON.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

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

> ---
> New in v2
> ---
>  src/v4l2/v4l2_camera_proxy.cpp | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
> index 7ee4c0cb..059f3cbe 100644
> --- a/src/v4l2/v4l2_camera_proxy.cpp
> +++ b/src/v4l2/v4l2_camera_proxy.cpp
> @@ -465,6 +465,8 @@ int V4L2CameraProxy::vidioc_streamon(int *arg)
>  	if (!validateBufferType(*arg))
>  		return -EINVAL;
>  
> +	currentBuf_ = 0;
> +
>  	return vcam_->streamOn();
>  }
>
Niklas Söderlund June 5, 2020, 6:22 p.m. UTC | #2
Hi Paul,

Thanks for your work.

On 2020-06-05 18:01:06 +0900, Paul Elder wrote:
> The V4L2 compatibility layer keeps track of the index of the next buffer
> to be dequeued, to handle VIDIOC_DQBUF. This index is set to 0 on
> startup and incremented (modulo #frames), and is otherwise never reset.
> This means that if the last handled frame index is not #frames-1, and
> the stream is restarted without restarting libcamera and the V4L2
> compatilibity layer, the buffer index number won't match with the
> libcamera buffer index number, causing a segfault. Fix this by resetting
> the current buffer index to zero on VIDIOC_STREAMON.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

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

> 
> ---
> New in v2
> ---
>  src/v4l2/v4l2_camera_proxy.cpp | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
> index 7ee4c0cb..059f3cbe 100644
> --- a/src/v4l2/v4l2_camera_proxy.cpp
> +++ b/src/v4l2/v4l2_camera_proxy.cpp
> @@ -465,6 +465,8 @@ int V4L2CameraProxy::vidioc_streamon(int *arg)
>  	if (!validateBufferType(*arg))
>  		return -EINVAL;
>  
> +	currentBuf_ = 0;
> +
>  	return vcam_->streamOn();
>  }
>  
> -- 
> 2.20.1
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
index 7ee4c0cb..059f3cbe 100644
--- a/src/v4l2/v4l2_camera_proxy.cpp
+++ b/src/v4l2/v4l2_camera_proxy.cpp
@@ -465,6 +465,8 @@  int V4L2CameraProxy::vidioc_streamon(int *arg)
 	if (!validateBufferType(*arg))
 		return -EINVAL;
 
+	currentBuf_ = 0;
+
 	return vcam_->streamOn();
 }