[libcamera-devel,1/1] qcam: Fix crash when switching from non existant camera
diff mbox series

Message ID 20220907163504.209658-1-utkarsh02t@gmail.com
State Accepted
Commit 5a9fd9a95cce4ab17c9cf5d9e0c33a008255bedb
Headers show
Series
  • [libcamera-devel,1/1] qcam: Fix crash when switching from non existant camera
Related show

Commit Message

Utkarsh Tiwari Sept. 7, 2022, 4:35 p.m. UTC
When switching to different camera we try to release the camera
previously used. But if that camera has been unplugged, then its
instance would have been destroyed. Accessing it leads to seg fault.

Fix by checking camera_ to see if it exists.

Bug: https://bugs.libcamera.org/show_bug.cgi?id=147
Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>
---
 src/qcam/main_window.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Laurent Pinchart Sept. 7, 2022, 8:13 p.m. UTC | #1
Hi Utkarsh,

Thank you for the patch.

On Wed, Sep 07, 2022 at 10:05:04PM +0530, Utkarsh Tiwari via libcamera-devel wrote:
> When switching to different camera we try to release the camera
> previously used. But if that camera has been unplugged, then its
> instance would have been destroyed. Accessing it leads to seg fault.
> 
> Fix by checking camera_ to see if it exists.
> 
> Bug: https://bugs.libcamera.org/show_bug.cgi?id=147
> Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>

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

> ---
>  src/qcam/main_window.cpp | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> index 6fe36168..e0e5092e 100644
> --- a/src/qcam/main_window.cpp
> +++ b/src/qcam/main_window.cpp
> @@ -284,7 +284,9 @@ void MainWindow::switchCamera()
>  	 */
>  	startStopAction_->setChecked(false);
>  
> -	camera_->release();
> +	if (camera_)
> +		camera_->release();
> +
>  	camera_ = cam;
>  
>  	startStopAction_->setChecked(true);
Kieran Bingham Sept. 7, 2022, 9:39 p.m. UTC | #2
Quoting Laurent Pinchart via libcamera-devel (2022-09-07 21:13:36)
> Hi Utkarsh,
> 
> Thank you for the patch.
> 
> On Wed, Sep 07, 2022 at 10:05:04PM +0530, Utkarsh Tiwari via libcamera-devel wrote:
> > When switching to different camera we try to release the camera
> > previously used. But if that camera has been unplugged, then its
> > instance would have been destroyed. Accessing it leads to seg fault.
> > 
> > Fix by checking camera_ to see if it exists.
> > 
> > Bug: https://bugs.libcamera.org/show_bug.cgi?id=147
> > Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

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

> > ---
> >  src/qcam/main_window.cpp | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> > index 6fe36168..e0e5092e 100644
> > --- a/src/qcam/main_window.cpp
> > +++ b/src/qcam/main_window.cpp
> > @@ -284,7 +284,9 @@ void MainWindow::switchCamera()
> >        */
> >       startStopAction_->setChecked(false);
> >  
> > -     camera_->release();
> > +     if (camera_)
> > +             camera_->release();
> > +
> >       camera_ = cam;
> >  
> >       startStopAction_->setChecked(true);
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Nicolas Dufresne via libcamera-devel Sept. 8, 2022, 5:05 a.m. UTC | #3
On Wed, Sep 07, 2022 at 10:05:04PM +0530, Utkarsh Tiwari via libcamera-devel wrote:
> When switching to different camera we try to release the camera
> previously used. But if that camera has been unplugged, then its
> instance would have been destroyed. Accessing it leads to seg fault.
> 
> Fix by checking camera_ to see if it exists.
> 
> Bug: https://bugs.libcamera.org/show_bug.cgi?id=147
> Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>

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

> ---
>  src/qcam/main_window.cpp | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> index 6fe36168..e0e5092e 100644
> --- a/src/qcam/main_window.cpp
> +++ b/src/qcam/main_window.cpp
> @@ -284,7 +284,9 @@ void MainWindow::switchCamera()
>  	 */
>  	startStopAction_->setChecked(false);
>  
> -	camera_->release();
> +	if (camera_)
> +		camera_->release();
> +
>  	camera_ = cam;
>  
>  	startStopAction_->setChecked(true);
> -- 
> 2.34.1
>

Patch
diff mbox series

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index 6fe36168..e0e5092e 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -284,7 +284,9 @@  void MainWindow::switchCamera()
 	 */
 	startStopAction_->setChecked(false);
 
-	camera_->release();
+	if (camera_)
+		camera_->release();
+
 	camera_ = cam;
 
 	startStopAction_->setChecked(true);