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

Message ID 20190626173543.2257-1-niklas.soderlund@ragnatech.se
State Superseded
Headers show
Series
  • [libcamera-devel] libcamera: v4l2_device: Fix variable-sized object initialization
Related show

Commit Message

Niklas Söderlund June 26, 2019, 5:35 p.m. UTC
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@ragnatech.se>
---
 src/libcamera/v4l2_device.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Jacopo Mondi June 28, 2019, 6:07 a.m. UTC | #1
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@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?

> +	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);
> --
> 2.22.0
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Laurent Pinchart June 28, 2019, 12:34 p.m. UTC | #2
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@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 ?

> > +	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);
Jacopo Mondi June 28, 2019, 3:09 p.m. UTC | #3
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@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

Patch

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];
+	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);
 		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);