[libcamera-devel,v2,2/2] test: controls: control_info_map: Test default constructor
diff mbox series

Message ID 20230404-guard-idmap-v2-2-444d135b3895@baylibre.com
State Accepted
Commit 683c6da83f078d09fc020d2b48a4abe471853b2b
Headers show
Series
  • libcamera: controls: guard ControlInfoMap against nullptr idmap_
Related show

Commit Message

Mattijs Korpershoek April 5, 2023, 8:14 a.m. UTC
ControlInfoMap can be default-constructed. In that case, some of its
members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault.

Add a test with a default constructed ControlInfoMap to cover this.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
 test/controls/control_info_map.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Jacopo Mondi April 5, 2023, 8:21 a.m. UTC | #1
Hi Mattijs,

On Wed, Apr 05, 2023 at 10:14:31AM +0200, Mattijs Korpershoek wrote:
> ControlInfoMap can be default-constructed. In that case, some of its
> members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault.
>
> Add a test with a default constructed ControlInfoMap to cover this.
>
> Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

Thanks
   j

> ---
>  test/controls/control_info_map.cpp | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp
> index db95945a1580..29b33515e48c 100644
> --- a/test/controls/control_info_map.cpp
> +++ b/test/controls/control_info_map.cpp
> @@ -75,6 +75,13 @@ protected:
>  			return TestFail;
>  		}
>
> +		/* Test looking up a control on a default-constructed infoMap */
> +		const ControlInfoMap emptyInfoMap;
> +		if (emptyInfoMap.find(12345) != emptyInfoMap.end()) {
> +			cerr << "find() on empty ControlInfoMap failed" << endl;
> +			return TestFail;
> +		}
> +
>  		return TestPass;
>  	}
>  };
>
> --
> 2.39.2
>
Umang Jain April 17, 2023, 6:22 a.m. UTC | #2
Hi Mattijs

On 4/5/23 1:44 PM, Mattijs Korpershoek via libcamera-devel wrote:
> ControlInfoMap can be default-constructed. In that case, some of its
> members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault.
>
> Add a test with a default constructed ControlInfoMap to cover this.
>
> Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

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

> ---
>   test/controls/control_info_map.cpp | 7 +++++++
>   1 file changed, 7 insertions(+)
>
> diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp
> index db95945a1580..29b33515e48c 100644
> --- a/test/controls/control_info_map.cpp
> +++ b/test/controls/control_info_map.cpp
> @@ -75,6 +75,13 @@ protected:
>   			return TestFail;
>   		}
>   
> +		/* Test looking up a control on a default-constructed infoMap */
> +		const ControlInfoMap emptyInfoMap;
> +		if (emptyInfoMap.find(12345) != emptyInfoMap.end()) {
> +			cerr << "find() on empty ControlInfoMap failed" << endl;
> +			return TestFail;
> +		}
> +
>   		return TestPass;
>   	}
>   };
>
Laurent Pinchart April 20, 2023, 10:22 a.m. UTC | #3
Hello Mattijs,

Thank you for the patch.

On Wed, Apr 05, 2023 at 10:14:31AM +0200, Mattijs Korpershoek wrote:
> ControlInfoMap can be default-constructed. In that case, some of its
> members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault.

Not anymore with patch 1/2 :-) It's however best to add the test first,
to verify it fails, and then the fix, to verify it fixes the problem.
With patch 1/2 and 2/2 swapped, I would phrase the commit message of
this patch as

--------
ControlInfoMap can be default-constructed. In that case, some of its
members (like idmap_) can be a nullptr. Add a test to ensure that the
find() function doesn't segfault.

The test is expected to fail, the issue will be fixed by a subsequent
commit.
--------

> Add a test with a default constructed ControlInfoMap to cover this.
> 
> Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
> ---
>  test/controls/control_info_map.cpp | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp
> index db95945a1580..29b33515e48c 100644
> --- a/test/controls/control_info_map.cpp
> +++ b/test/controls/control_info_map.cpp
> @@ -75,6 +75,13 @@ protected:
>  			return TestFail;
>  		}
>  
> +		/* Test looking up a control on a default-constructed infoMap */

s/infoMap/infoMap./

With this,

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

> +		const ControlInfoMap emptyInfoMap;
> +		if (emptyInfoMap.find(12345) != emptyInfoMap.end()) {
> +			cerr << "find() on empty ControlInfoMap failed" << endl;
> +			return TestFail;
> +		}
> +
>  		return TestPass;
>  	}
>  };
Kieran Bingham April 20, 2023, 10:47 a.m. UTC | #4
Hi Laurent,

Quoting Laurent Pinchart via libcamera-devel (2023-04-20 11:22:30)
> Hello Mattijs,
> 
> Thank you for the patch.
> 
> On Wed, Apr 05, 2023 at 10:14:31AM +0200, Mattijs Korpershoek wrote:
> > ControlInfoMap can be default-constructed. In that case, some of its
> > members (like idmap_) can be a nullptr, and ControlInfoMap.find() will segfault.
> 
> Not anymore with patch 1/2 :-) It's however best to add the test first,
> to verify it fails, and then the fix, to verify it fixes the problem.
> With patch 1/2 and 2/2 swapped, I would phrase the commit message of
> this patch as
> 
> --------
> ControlInfoMap can be default-constructed. In that case, some of its
> members (like idmap_) can be a nullptr. Add a test to ensure that the
> find() function doesn't segfault.
> 
> The test is expected to fail, the issue will be fixed by a subsequent
> commit.
> --------

Sorry I have merged this series already - just about the time you wrote
your review.

--
Kieran


> 
> > Add a test with a default constructed ControlInfoMap to cover this.
> > 
> > Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
> > ---
> >  test/controls/control_info_map.cpp | 7 +++++++
> >  1 file changed, 7 insertions(+)
> > 
> > diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp
> > index db95945a1580..29b33515e48c 100644
> > --- a/test/controls/control_info_map.cpp
> > +++ b/test/controls/control_info_map.cpp
> > @@ -75,6 +75,13 @@ protected:
> >                       return TestFail;
> >               }
> >  
> > +             /* Test looking up a control on a default-constructed infoMap */
> 
> s/infoMap/infoMap./
> 
> With this,
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> > +             const ControlInfoMap emptyInfoMap;
> > +             if (emptyInfoMap.find(12345) != emptyInfoMap.end()) {
> > +                     cerr << "find() on empty ControlInfoMap failed" << endl;
> > +                     return TestFail;
> > +             }
> > +
> >               return TestPass;
> >       }
> >  };
> 
> -- 
> Regards,
> 
> Laurent Pinchart

Patch
diff mbox series

diff --git a/test/controls/control_info_map.cpp b/test/controls/control_info_map.cpp
index db95945a1580..29b33515e48c 100644
--- a/test/controls/control_info_map.cpp
+++ b/test/controls/control_info_map.cpp
@@ -75,6 +75,13 @@  protected:
 			return TestFail;
 		}
 
+		/* Test looking up a control on a default-constructed infoMap */
+		const ControlInfoMap emptyInfoMap;
+		if (emptyInfoMap.find(12345) != emptyInfoMap.end()) {
+			cerr << "find() on empty ControlInfoMap failed" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 };