[libcamera-devel,2/2] libcamera: pipeline: simple: Initialize V4L2DeviceFormat before use
diff mbox series

Message ID 20201021024744.19047-2-laurent.pinchart@ideasonboard.com
State Superseded
Delegated to: Laurent Pinchart
Headers show
Series
  • [libcamera-devel,1/2] libcamera: v4l2_videodevice: Check plane count when setting format
Related show

Commit Message

Laurent Pinchart Oct. 21, 2020, 2:47 a.m. UTC
The V4L2DeviceFormat has no default constructor. Zero it before use when
setting formats. Failure to do so leaves the planes uninitialized,
potentially causing memory corruption.

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

Comments

Kieran Bingham Oct. 21, 2020, 10:09 a.m. UTC | #1
Hi Laurent,

On 21/10/2020 03:47, Laurent Pinchart wrote:
> The V4L2DeviceFormat has no default constructor. Zero it before use when
> setting formats. Failure to do so leaves the planes uninitialized,
> potentially causing memory corruption.
> 

Oh dear ;-)

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


Is this missing an explicit setting of the plane count? (based on the
content of the previous patch?)

But I believe this looks correct:

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


> ---
>  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> index b4ee021f6210..b3705abcd626 100644
> --- a/src/libcamera/pipeline/simple/converter.cpp
> +++ b/src/libcamera/pipeline/simple/converter.cpp
> @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
>  	 * Set the format on the input side (V4L2 output) of the converter to
>  	 * enumerate the conversion capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
>  	format.size = { 1, 1 };
>  
> @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  	 * Set the size on the input side (V4L2 output) of the converter to
>  	 * enumerate the scaling capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = V4L2PixelFormat();
>  	format.size = input;
>  
> @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
>  			       StreamConfiguration *cfg)
>  {
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	int ret;
>  
>  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
>
Jacopo Mondi Oct. 21, 2020, 1:13 p.m. UTC | #2
Hi Laurent,

On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
> The V4L2DeviceFormat has no default constructor. Zero it before use when

Isn't it implicitly generated by the compiler ?

> setting formats. Failure to do so leaves the planes uninitialized,
> potentially causing memory corruption.
>

If not, isn't it better to add it in the class ?

Thanks
  j

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> index b4ee021f6210..b3705abcd626 100644
> --- a/src/libcamera/pipeline/simple/converter.cpp
> +++ b/src/libcamera/pipeline/simple/converter.cpp
> @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
>  	 * Set the format on the input side (V4L2 output) of the converter to
>  	 * enumerate the conversion capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
>  	format.size = { 1, 1 };
>
> @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  	 * Set the size on the input side (V4L2 output) of the converter to
>  	 * enumerate the scaling capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = V4L2PixelFormat();
>  	format.size = input;
>
> @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
>  			       StreamConfiguration *cfg)
>  {
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	int ret;
>
>  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Kieran Bingham Oct. 21, 2020, 1:31 p.m. UTC | #3
Hrm,

On 21/10/2020 14:13, Jacopo Mondi wrote:
> Hi Laurent,
> 
> On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
>> The V4L2DeviceFormat has no default constructor. Zero it before use when
> 
> Isn't it implicitly generated by the compiler ?
> 
>> setting formats. Failure to do so leaves the planes uninitialized,
>> potentially causing memory corruption.
>>
> 
> If not, isn't it better to add it in the class ?

Hrm, when I read the patch before, I assumed it was because it was just
a struct or such. Ok - so it's not a complicated class but ...

But indeed, this is marked as a class - so construction surely is suited
to the constructor?

--
Kieran



> 
> Thanks
>   j
> 
>> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> ---
>>  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
>> index b4ee021f6210..b3705abcd626 100644
>> --- a/src/libcamera/pipeline/simple/converter.cpp
>> +++ b/src/libcamera/pipeline/simple/converter.cpp
>> @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
>>  	 * Set the format on the input side (V4L2 output) of the converter to
>>  	 * enumerate the conversion capabilities on its output (V4L2 capture).
>>  	 */
>> -	V4L2DeviceFormat format;
>> +	V4L2DeviceFormat format = {};
>>  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
>>  	format.size = { 1, 1 };
>>
>> @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>>  	 * Set the size on the input side (V4L2 output) of the converter to
>>  	 * enumerate the scaling capabilities on its output (V4L2 capture).
>>  	 */
>> -	V4L2DeviceFormat format;
>> +	V4L2DeviceFormat format = {};
>>  	format.fourcc = V4L2PixelFormat();
>>  	format.size = input;
>>
>> @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>>  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
>>  			       StreamConfiguration *cfg)
>>  {
>> -	V4L2DeviceFormat format;
>> +	V4L2DeviceFormat format = {};
>>  	int ret;
>>
>>  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
>> --
>> Regards,
>>
>> Laurent Pinchart
>>
>> _______________________________________________
>> libcamera-devel mailing list
>> libcamera-devel@lists.libcamera.org
>> https://lists.libcamera.org/listinfo/libcamera-devel
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
>
Niklas Söderlund Oct. 21, 2020, 1:32 p.m. UTC | #4
Hi Laurent,

Thanks for your work.

On 2020-10-21 05:47:44 +0300, Laurent Pinchart wrote:
> The V4L2DeviceFormat has no default constructor. Zero it before use when
> setting formats. Failure to do so leaves the planes uninitialized,
> potentially causing memory corruption.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

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

> ---
>  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> index b4ee021f6210..b3705abcd626 100644
> --- a/src/libcamera/pipeline/simple/converter.cpp
> +++ b/src/libcamera/pipeline/simple/converter.cpp
> @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
>  	 * Set the format on the input side (V4L2 output) of the converter to
>  	 * enumerate the conversion capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
>  	format.size = { 1, 1 };
>  
> @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  	 * Set the size on the input side (V4L2 output) of the converter to
>  	 * enumerate the scaling capabilities on its output (V4L2 capture).
>  	 */
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	format.fourcc = V4L2PixelFormat();
>  	format.size = input;
>  
> @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
>  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
>  			       StreamConfiguration *cfg)
>  {
> -	V4L2DeviceFormat format;
> +	V4L2DeviceFormat format = {};
>  	int ret;
>  
>  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Laurent Pinchart Oct. 21, 2020, 4:30 p.m. UTC | #5
Hi Jacopo,

On Wed, Oct 21, 2020 at 03:13:55PM +0200, Jacopo Mondi wrote:
> On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
> > The V4L2DeviceFormat has no default constructor. Zero it before use when
> 
> Isn't it implicitly generated by the compiler ?

It is, but the implicitly-defined constructor won't help us here:

"If the implicitly-declared default constructor is not defined as
deleted, it is defined (that is, a function body is generated and
compiled) by the compiler if odr-used, and it has the same effect as a
user-defined constructor with empty body and empty initializer list."

(https://en.cppreference.com/w/cpp/language/default_constructor is an
interesting read)

> > setting formats. Failure to do so leaves the planes uninitialized,
> > potentially causing memory corruption.
> 
> If not, isn't it better to add it in the class ?

I had considered that, but wasn't sure, and was hoping nobody would ask
;-)

It could make sense to replace the planes array and planesCount field
with an std::vector. A bit of a shame to have to use dynamic allocation
when we know there will never be more than 3 planes though. I wish there
was a dynamic array container, with fixed memory preallocation and
dynamic size up to the number of elements.

Any preference between using a vector, and keeping it as-is with a
constructor that sets numPlanes to 0 ?

> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> > index b4ee021f6210..b3705abcd626 100644
> > --- a/src/libcamera/pipeline/simple/converter.cpp
> > +++ b/src/libcamera/pipeline/simple/converter.cpp
> > @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
> >  	 * Set the format on the input side (V4L2 output) of the converter to
> >  	 * enumerate the conversion capabilities on its output (V4L2 capture).
> >  	 */
> > -	V4L2DeviceFormat format;
> > +	V4L2DeviceFormat format = {};
> >  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
> >  	format.size = { 1, 1 };
> >
> > @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> >  	 * Set the size on the input side (V4L2 output) of the converter to
> >  	 * enumerate the scaling capabilities on its output (V4L2 capture).
> >  	 */
> > -	V4L2DeviceFormat format;
> > +	V4L2DeviceFormat format = {};
> >  	format.fourcc = V4L2PixelFormat();
> >  	format.size = input;
> >
> > @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> >  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
> >  			       StreamConfiguration *cfg)
> >  {
> > -	V4L2DeviceFormat format;
> > +	V4L2DeviceFormat format = {};
> >  	int ret;
> >
> >  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
Laurent Pinchart Oct. 21, 2020, 4:36 p.m. UTC | #6
On Wed, Oct 21, 2020 at 07:30:53PM +0300, Laurent Pinchart wrote:
> Hi Jacopo,
> 
> On Wed, Oct 21, 2020 at 03:13:55PM +0200, Jacopo Mondi wrote:
> > On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
> > > The V4L2DeviceFormat has no default constructor. Zero it before use when
> > 
> > Isn't it implicitly generated by the compiler ?
> 
> It is, but the implicitly-defined constructor won't help us here:
> 
> "If the implicitly-declared default constructor is not defined as
> deleted, it is defined (that is, a function body is generated and
> compiled) by the compiler if odr-used, and it has the same effect as a
> user-defined constructor with empty body and empty initializer list."
> 
> (https://en.cppreference.com/w/cpp/language/default_constructor is an
> interesting read)
> 
> > > setting formats. Failure to do so leaves the planes uninitialized,
> > > potentially causing memory corruption.
> > 
> > If not, isn't it better to add it in the class ?
> 
> I had considered that, but wasn't sure, and was hoping nobody would ask
> ;-)
> 
> It could make sense to replace the planes array and planesCount field
> with an std::vector. A bit of a shame to have to use dynamic allocation
> when we know there will never be more than 3 planes though. I wish there
> was a dynamic array container, with fixed memory preallocation and
> dynamic size up to the number of elements.
> 
> Any preference between using a vector, and keeping it as-is with a
> constructor that sets numPlanes to 0 ?

By the way, we also have the option of using a default member
initializer
(https://en.cppreference.com/w/cpp/language/data_members#Member_initialization):

diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index 40ed87e17cfa..12eaf45a31ec 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -160,7 +160,7 @@ public:
 		uint32_t size;
 		uint32_t bpl;
 	} planes[3];
-	unsigned int planesCount;
+	unsigned int planesCount = 0;
 
 	const std::string toString() const;
 };

Tomi recently pointed this out. I don't like it much when there are
explicit constructors (as initialization is then split between the
header file and the cpp file), but for structures with no explicitly
defined constructor, I don't object.

> > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > ---
> > >  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
> > >  1 file changed, 3 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> > > index b4ee021f6210..b3705abcd626 100644
> > > --- a/src/libcamera/pipeline/simple/converter.cpp
> > > +++ b/src/libcamera/pipeline/simple/converter.cpp
> > > @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
> > >  	 * Set the format on the input side (V4L2 output) of the converter to
> > >  	 * enumerate the conversion capabilities on its output (V4L2 capture).
> > >  	 */
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
> > >  	format.size = { 1, 1 };
> > >
> > > @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > >  	 * Set the size on the input side (V4L2 output) of the converter to
> > >  	 * enumerate the scaling capabilities on its output (V4L2 capture).
> > >  	 */
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	format.fourcc = V4L2PixelFormat();
> > >  	format.size = input;
> > >
> > > @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > >  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
> > >  			       StreamConfiguration *cfg)
> > >  {
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	int ret;
> > >
> > >  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
Jacopo Mondi Oct. 21, 2020, 4:40 p.m. UTC | #7
Hi Laurent,

On Wed, Oct 21, 2020 at 07:30:53PM +0300, Laurent Pinchart wrote:
> Hi Jacopo,
>
> On Wed, Oct 21, 2020 at 03:13:55PM +0200, Jacopo Mondi wrote:
> > On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
> > > The V4L2DeviceFormat has no default constructor. Zero it before use when
> >
> > Isn't it implicitly generated by the compiler ?
>
> It is, but the implicitly-defined constructor won't help us here:
>
> "If the implicitly-declared default constructor is not defined as
> deleted, it is defined (that is, a function body is generated and
> compiled) by the compiler if odr-used, and it has the same effect as a
> user-defined constructor with empty body and empty initializer list."
>
> (https://en.cppreference.com/w/cpp/language/default_constructor is an
> interesting read)
>

Ah, thanks for the pointer!

> > > setting formats. Failure to do so leaves the planes uninitialized,
> > > potentially causing memory corruption.
> >
> > If not, isn't it better to add it in the class ?
>
> I had considered that, but wasn't sure, and was hoping nobody would ask
> ;-)

I'm sorry :)

But once fixed in the constructor, it's fixed for good, isn't it ?

>
> It could make sense to replace the planes array and planesCount field
> with an std::vector. A bit of a shame to have to use dynamic allocation
> when we know there will never be more than 3 planes though. I wish there
> was a dynamic array container, with fixed memory preallocation and
> dynamic size up to the number of elements.

Can't we use std::array<3> in that case ? Although std::array<> is
horrible to use :(

>
> Any preference between using a vector, and keeping it as-is with a
> constructor that sets numPlanes to 0 ?

To me a constructor sounds quite right, but other options are fine
too.

Thanks
  j

>
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > ---
> > >  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
> > >  1 file changed, 3 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> > > index b4ee021f6210..b3705abcd626 100644
> > > --- a/src/libcamera/pipeline/simple/converter.cpp
> > > +++ b/src/libcamera/pipeline/simple/converter.cpp
> > > @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
> > >  	 * Set the format on the input side (V4L2 output) of the converter to
> > >  	 * enumerate the conversion capabilities on its output (V4L2 capture).
> > >  	 */
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
> > >  	format.size = { 1, 1 };
> > >
> > > @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > >  	 * Set the size on the input side (V4L2 output) of the converter to
> > >  	 * enumerate the scaling capabilities on its output (V4L2 capture).
> > >  	 */
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	format.fourcc = V4L2PixelFormat();
> > >  	format.size = input;
> > >
> > > @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > >  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
> > >  			       StreamConfiguration *cfg)
> > >  {
> > > -	V4L2DeviceFormat format;
> > > +	V4L2DeviceFormat format = {};
> > >  	int ret;
> > >
> > >  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);
>
> --
> Regards,
>
> Laurent Pinchart
Laurent Pinchart Oct. 21, 2020, 6:37 p.m. UTC | #8
Hi Jacopo,

On Wed, Oct 21, 2020 at 06:40:44PM +0200, Jacopo Mondi wrote:
> On Wed, Oct 21, 2020 at 07:30:53PM +0300, Laurent Pinchart wrote:
> > On Wed, Oct 21, 2020 at 03:13:55PM +0200, Jacopo Mondi wrote:
> > > On Wed, Oct 21, 2020 at 05:47:44AM +0300, Laurent Pinchart wrote:
> > > > The V4L2DeviceFormat has no default constructor. Zero it before use when
> > >
> > > Isn't it implicitly generated by the compiler ?
> >
> > It is, but the implicitly-defined constructor won't help us here:
> >
> > "If the implicitly-declared default constructor is not defined as
> > deleted, it is defined (that is, a function body is generated and
> > compiled) by the compiler if odr-used, and it has the same effect as a
> > user-defined constructor with empty body and empty initializer list."
> >
> > (https://en.cppreference.com/w/cpp/language/default_constructor is an
> > interesting read)
> 
> Ah, thanks for the pointer!
> 
> > > > setting formats. Failure to do so leaves the planes uninitialized,
> > > > potentially causing memory corruption.
> > >
> > > If not, isn't it better to add it in the class ?
> >
> > I had considered that, but wasn't sure, and was hoping nobody would ask
> > ;-)
> 
> I'm sorry :)
> 
> But once fixed in the constructor, it's fixed for good, isn't it ?

Yes, which is why it's probably best. I'll work on it.

> > It could make sense to replace the planes array and planesCount field
> > with an std::vector. A bit of a shame to have to use dynamic allocation
> > when we know there will never be more than 3 planes though. I wish there
> > was a dynamic array container, with fixed memory preallocation and
> > dynamic size up to the number of elements.
> 
> Can't we use std::array<3> in that case ? Although std::array<> is
> horrible to use :(

We can use std::array<>, but it won't initialize the contents of the
array by default. Furthermore, arrays have a fixed size, they don't
embed and additional field that reports the number of elements actually
used.

> > Any preference between using a vector, and keeping it as-is with a
> > constructor that sets numPlanes to 0 ?
> 
> To me a constructor sounds quite right, but other options are fine
> too.
> 
> > > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > > ---
> > > >  src/libcamera/pipeline/simple/converter.cpp | 6 +++---
> > > >  1 file changed, 3 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
> > > > index b4ee021f6210..b3705abcd626 100644
> > > > --- a/src/libcamera/pipeline/simple/converter.cpp
> > > > +++ b/src/libcamera/pipeline/simple/converter.cpp
> > > > @@ -72,7 +72,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
> > > >  	 * Set the format on the input side (V4L2 output) of the converter to
> > > >  	 * enumerate the conversion capabilities on its output (V4L2 capture).
> > > >  	 */
> > > > -	V4L2DeviceFormat format;
> > > > +	V4L2DeviceFormat format = {};
> > > >  	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
> > > >  	format.size = { 1, 1 };
> > > >
> > > > @@ -103,7 +103,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > > >  	 * Set the size on the input side (V4L2 output) of the converter to
> > > >  	 * enumerate the scaling capabilities on its output (V4L2 capture).
> > > >  	 */
> > > > -	V4L2DeviceFormat format;
> > > > +	V4L2DeviceFormat format = {};
> > > >  	format.fourcc = V4L2PixelFormat();
> > > >  	format.size = input;
> > > >
> > > > @@ -142,7 +142,7 @@ SizeRange SimpleConverter::sizes(const Size &input)
> > > >  int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
> > > >  			       StreamConfiguration *cfg)
> > > >  {
> > > > -	V4L2DeviceFormat format;
> > > > +	V4L2DeviceFormat format = {};
> > > >  	int ret;
> > > >
> > > >  	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp
index b4ee021f6210..b3705abcd626 100644
--- a/src/libcamera/pipeline/simple/converter.cpp
+++ b/src/libcamera/pipeline/simple/converter.cpp
@@ -72,7 +72,7 @@  std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
 	 * Set the format on the input side (V4L2 output) of the converter to
 	 * enumerate the conversion capabilities on its output (V4L2 capture).
 	 */
-	V4L2DeviceFormat format;
+	V4L2DeviceFormat format = {};
 	format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
 	format.size = { 1, 1 };
 
@@ -103,7 +103,7 @@  SizeRange SimpleConverter::sizes(const Size &input)
 	 * Set the size on the input side (V4L2 output) of the converter to
 	 * enumerate the scaling capabilities on its output (V4L2 capture).
 	 */
-	V4L2DeviceFormat format;
+	V4L2DeviceFormat format = {};
 	format.fourcc = V4L2PixelFormat();
 	format.size = input;
 
@@ -142,7 +142,7 @@  SizeRange SimpleConverter::sizes(const Size &input)
 int SimpleConverter::configure(PixelFormat inputFormat, const Size &inputSize,
 			       StreamConfiguration *cfg)
 {
-	V4L2DeviceFormat format;
+	V4L2DeviceFormat format = {};
 	int ret;
 
 	V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputFormat);