[libcamera-devel] [PATCH] libcamera: v4l2_device: Fix variable-sized object initialization

Jacopo Mondi jacopo at jmondi.org
Fri Jun 28 17:09:40 CEST 2019


Hi Laurent,

On Fri, Jun 28, 2019 at 03:34:52PM +0300, Laurent Pinchart wrote:
> Hi Jacopo,
>
> On Fri, Jun 28, 2019 at 08:07:48AM +0200, Jacopo Mondi wrote:
> > Hi Niklas,
> >    sorry, I didn't test on clang. It should become a standard practice
> > since it's the default compiler used for building, ie, Cros..
> >
> > On Wed, Jun 26, 2019 at 07:35:43PM +0200, Nklas Söderlund wrote:
> > > Compiling with clang renders errors as a variable-sized arrays are not
> > > allowed to be initialized, solve this by using memset() instead.
> > >
> > >     ../../src/libcamera/v4l2_device.cpp:155:37: error: variable-sized object may not be initialized
> > >         const V4L2ControlInfo *controlInfo[count] = {};
> > >                                            ^~~~~
> > >     ../../src/libcamera/v4l2_device.cpp:156:36: error: variable-sized object may not be initialized
> > > 	    struct v4l2_ext_control v4l2Ctrls[count] = {};
> > > 					      ^~~~~
> > >     ../../src/libcamera/v4l2_device.cpp:227:37: error: variable-sized object may not be initialized
> > > 	    const V4L2ControlInfo *controlInfo[count] = {};
> > > 					       ^~~~~
> > >     ../../src/libcamera/v4l2_device.cpp:228:36: error: variable-sized object may not be initialized
> > > 	    struct v4l2_ext_control v4l2Ctrls[count] = {};
> > > 					      ^~~~~
> > > Fixes: eb068f4e67eedacd ("libcamera: v4l2_device: Implement get and set controls")
> > > Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
> > > ---
> > >  src/libcamera/v4l2_device.cpp | 13 +++++++++----
> > >  1 file changed, 9 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> > > index 84758a811c271976..d730df78e1ab2478 100644
> > > --- a/src/libcamera/v4l2_device.cpp
> > > +++ b/src/libcamera/v4l2_device.cpp
> > > @@ -152,8 +152,11 @@ int V4L2Device::getControls(V4L2ControlList *ctrls)
> > >  	if (count == 0)
> > >  		return 0;
> > >
> > > -	const V4L2ControlInfo *controlInfo[count] = {};
> > > -	struct v4l2_ext_control v4l2Ctrls[count] = {};
> > > +	const V4L2ControlInfo *controlInfo[count];
> >
> > No need to zero this one.
> >
> > > +	struct v4l2_ext_control v4l2Ctrls[count];
> >
> > We can initialize each entry as we walk them maybe?
> >
> > --- a/src/libcamera/v4l2_device.cpp
> > +++ b/src/libcamera/v4l2_device.cpp
> > @@ -152,8 +152,8 @@ int V4L2Device::getControls(V4L2ControlList *ctrls)
> >         if (count == 0)
> >                 return 0;
> >
> > -       const V4L2ControlInfo *controlInfo[count] = {};
> > -       struct v4l2_ext_control v4l2Ctrls[count] = {};
> > +       const V4L2ControlInfo *controlInfo[count];
> > +       struct v4l2_ext_control v4l2Ctrls[count];
> >         for (unsigned int i = 0; i < count; ++i) {
> >                 const V4L2Control *ctrl = ctrls->getByIndex(i);
> >                 const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> > @@ -164,6 +164,7 @@ int V4L2Device::getControls(V4L2ControlList *ctrls)
> >                 }
> >
> >                 controlInfo[i] = info;
> > +               v4l2Ctrls[i] = {};
> >                 v4l2Ctrls[i].id = info->id();
> >         }
> > With the same to be done for setControl()?
> >
> > What do you think?
>
> Wouldn't a single memset be more efficient ?

Probably yes, as we need to walk all entries anyway.


>
> > > +	memset(controlInfo, 0, count * sizeof(controlInfo[0]));
> > > +	memset(v4l2Ctrls, 0, count * sizeof(v4l2Ctrls[0]));
> > > +
> > >  	for (unsigned int i = 0; i < count; ++i) {
> > >  		const V4L2Control *ctrl = ctrls->getByIndex(i);
> > >  		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> > > @@ -224,8 +227,10 @@ int V4L2Device::setControls(V4L2ControlList *ctrls)
> > >  	if (count == 0)
> > >  		return 0;
> > >
> > > -	const V4L2ControlInfo *controlInfo[count] = {};
> > > -	struct v4l2_ext_control v4l2Ctrls[count] = {};
> > > +	const V4L2ControlInfo *controlInfo[count];
> > > +	struct v4l2_ext_control v4l2Ctrls[count];
> > > +	memset(controlInfo, 0, count * sizeof(controlInfo[0]));
> > > +	memset(v4l2Ctrls, 0, count * sizeof(v4l2Ctrls[0]));
> > >
> > >  	for (unsigned int i = 0; i < count; ++i) {
> > >  		const V4L2Control *ctrl = ctrls->getByIndex(i);
>
> --
> Regards,
>
> Laurent Pinchart
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.libcamera.org/pipermail/libcamera-devel/attachments/20190628/e054bbbe/attachment.sig>


More information about the libcamera-devel mailing list