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

Message ID 20190629113922.17119-1-niklas.soderlund@ragnatech.se
State Accepted
Commit 0116a940ba0232b625ab84bb90bb1bc3ddd47658
Headers show
Series
  • [libcamera-devel,v3] libcamera: v4l2_device: Fix variable-sized object initialization
Related show

Commit Message

Niklas Söderlund June 29, 2019, 11:39 a.m. UTC
Compiling with clang renders errors as a variable-sized arrays are not
allowed to be initialized. Solve this by using memset() for v4l2Ctrls
which is the only one of the two arrays that needs to be zeroed.

    ../../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>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/v4l2_device.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Jacopo Mondi June 30, 2019, 9:07 a.m. UTC | #1
Hi Niklas,
  thanks for the patch

On Sat, Jun 29, 2019 at 01:39:22PM +0200, Niklas Söderlund wrote:
> Compiling with clang renders errors as a variable-sized arrays are not
> allowed to be initialized. Solve this by using memset() for v4l2Ctrls
> which is the only one of the two arrays that needs to be zeroed.
>
>     ../../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>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j
> ---
>  src/libcamera/v4l2_device.cpp | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> index f1821a7ba162e709..06939dead705459f 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -152,8 +152,10 @@ 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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
> +
>  	for (unsigned int i = 0; i < count; ++i) {
>  		const V4L2Control *ctrl = ctrls->getByIndex(i);
>  		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> @@ -224,8 +226,9 @@ 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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
>
>  	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
Niklas Söderlund June 30, 2019, 11:52 a.m. UTC | #2
Hi, 

This patch was pushed with Jacopo's tag, thanks!

On 2019-06-30 11:07:29 +0200, Jacopo Mondi wrote:
> Hi Niklas,
>   thanks for the patch
> 
> On Sat, Jun 29, 2019 at 01:39:22PM +0200, Niklas Söderlund wrote:
> > Compiling with clang renders errors as a variable-sized arrays are not
> > allowed to be initialized. Solve this by using memset() for v4l2Ctrls
> > which is the only one of the two arrays that needs to be zeroed.
> >
> >     ../../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>
> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
> 
> Thanks
>   j
> > ---
> >  src/libcamera/v4l2_device.cpp | 11 +++++++----
> >  1 file changed, 7 insertions(+), 4 deletions(-)
> >
> > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> > index f1821a7ba162e709..06939dead705459f 100644
> > --- a/src/libcamera/v4l2_device.cpp
> > +++ b/src/libcamera/v4l2_device.cpp
> > @@ -152,8 +152,10 @@ 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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
> > +
> >  	for (unsigned int i = 0; i < count; ++i) {
> >  		const V4L2Control *ctrl = ctrls->getByIndex(i);
> >  		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> > @@ -224,8 +226,9 @@ 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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
> >
> >  	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

Patch

diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index f1821a7ba162e709..06939dead705459f 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -152,8 +152,10 @@  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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
+
 	for (unsigned int i = 0; i < count; ++i) {
 		const V4L2Control *ctrl = ctrls->getByIndex(i);
 		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
@@ -224,8 +226,9 @@  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(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
 
 	for (unsigned int i = 0; i < count; ++i) {
 		const V4L2Control *ctrl = ctrls->getByIndex(i);