nft: Use ARRAY_SIZE() macro in nft_strerror()
Phil Sutter
reviewerPATCH 1/1 nft: Use ARRAY_SIZE() macro in nft_strerror()
Variable 'table' is an array of type struct table_struct, so this is a classical use-case for ARRAY_SIZE() macro.
--- iptables/nft.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/nft.c +1 -1
@@ -2888,7 +2888,7 @@ const char *nft_strerror(int err) { NULL, ENOENT, "No chain/target/match by that name" }, }; -for (i =0 ; i <sizeof (table)/sizeof (struct table_struct ); i++) { +for (i =0 ; i <ARRAY_SIZE (table); i++) {if ((!table[i].fn || table[i].fn == nft_fn) && table[i].err == err)return table[i].message;
Signature · 1 line
2.23.0
Quoted 4 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.Signed-off-by Phil Sutter <[email protected]>
Pablo Neira Ayuso
maintainerQuoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.Signed-off-by Phil Sutter <[email protected]>
BTW, probably good to add the array check?
https://sourceforge.net/p/libhx/libhx/ci/master/tree/include/libHX/defs.h#l152
Phil Sutter
reviewerHi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Phil Sutter <[email protected]>BTW, probably good to add the array check?
https://sourceforge.net/p/libhx/libhx/ci/master/tree/include/libHX/defs.h#l152
Copying from kernel sources, do you think that's fine?
| # ifndef ARRAY_SIZE
| -# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
| +# define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
| +# define __same_type(a, b) \
| + __builtin_types_compatible_p(typeof(a), typeof(b))
| +/* &a[0] degrades to a pointer: a different type from an array */
| +# define __must_be_array(a) \
| + BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| +# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) + __must_be_array(x)
| # endifCheers, Phil
Pablo Neira Ayuso
maintainerQuoted 18 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Phil Sutter <[email protected]>BTW, probably good to add the array check?
https://sourceforge.net/p/libhx/libhx/ci/master/tree/include/libHX/defs.h#l152
Copying from kernel sources, do you think that's fine?
Quoted 10 lines
| # ifndef ARRAY_SIZE
| -# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
| +# define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
| +# define __same_type(a, b) \
| + __builtin_types_compatible_p(typeof(a), typeof(b))
| +/* &a[0] degrades to a pointer: a different type from an array */
| +# define __must_be_array(a) \
| + BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| +# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) + __must_be_array(x)
| # endifAt quick glance I would say that's fine.
Phil Sutter
reviewerHi,
Quoted 30 lines · Pablo Neira Ayuso
Quoted 18 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Phil Sutter <[email protected]>BTW, probably good to add the array check?
https://sourceforge.net/p/libhx/libhx/ci/master/tree/include/libHX/defs.h#l152
Copying from kernel sources, do you think that's fine?
Quoted 10 lines
| # ifndef ARRAY_SIZE | -# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) | +# define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) | +# define __same_type(a, b) \ | + __builtin_types_compatible_p(typeof(a), typeof(b)) | +/* &a[0] degrades to a pointer: a different type from an array */ | +# define __must_be_array(a) \ | + BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | +# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) + __must_be_array(x) | # endif
Quoted 1 line
At quick glance I would say that's fine.
While testing it, I noticed that gcc has a builtin check already:
| ../include/xtables.h:640:36: warning: division 'sizeof (const uint32_t * {aka const unsigned int *}) / sizeof (uint32_t {aka const unsigned int})' does not compute the number of array elements [-Wsizeof-pointer-div]
| 640 | # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
| | ^
| nft.c:914:18: note: in expansion of macro 'ARRAY_SIZE'
| 914 | for (i = 1; i < ARRAY_SIZE(multp); i++) {
| | ^~~~~~~~~~
| nft.c:906:25: note: first 'sizeof' operand was declared here
| 906 | static const uint32_t *multp = mult;
| | ^~~~~AFAICT, the only benefit the above brings is that it causes an error instead of warning. Do you think we still need it? Maybe instead enable -Werror? ;)
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 47 lines · Phil Sutter
Hi,
Quoted 30 lines · Pablo Neira Ayuso
Quoted 18 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Variable 'table' is an array of type struct
table_struct, so this is a classical use-case forARRAY_SIZE()macro.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Phil Sutter <[email protected]>BTW, probably good to add the array check?
https://sourceforge.net/p/libhx/libhx/ci/master/tree/include/libHX/defs.h#l152
Copying from kernel sources, do you think that's fine?
Quoted 10 lines
| # ifndef ARRAY_SIZE | -# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) | +# define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) | +# define __same_type(a, b) \ | + __builtin_types_compatible_p(typeof(a), typeof(b)) | +/* &a[0] degrades to a pointer: a different type from an array */ | +# define __must_be_array(a) \ | + BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | +# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) + __must_be_array(x) | # endifQuoted 1 line
At quick glance I would say that's fine.
While testing it, I noticed that gcc has a builtin check already:
| ../include/xtables.h:640:36: warning: division 'sizeof (const uint32_t * {aka const unsigned int *}) / sizeof (uint32_t {aka const unsigned int})' does not compute the number of array elements [-Wsizeof-pointer-div] | 640 | # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) | | ^ | nft.c:914:18: note: in expansion of macro 'ARRAY_SIZE' | 914 | for (i = 1; i < ARRAY_SIZE(multp); i++) { | | ^~~~~~~~~~ | nft.c:906:25: note: first 'sizeof' operand was declared here | 906 | static const uint32_t *multp = mult; | | ^~~~~
Quoted 3 lines
AFAICT, the only benefit the above brings is that it causes an error instead of warning. Do you think we still need it? Maybe instead enable -Werror? ;)
If gcc is already checking for this. Warning should be fine.
Regarding -Werror, we would at least need to keep the autogenerated C code by bison away from it.
IIRC I enabled this in conntrack-tools long time ago, and I started getting reports on it breaking compilation with new gcc versions that were actually spewing new warnings. That was stopping users to install latest, probably -Werror is too agressive?
Phil Sutter
reviewerHey,
On Thu, Oct 24, 2019 at 11:29:03AM +0200, Pablo Neira Ayuso wrote:
[...]
Quoted 4 lines
If gcc is already checking for this. Warning should be fine.
Regarding -Werror, we would at least need to keep the autogenerated C code by bison away from it.
In nftables there is libparser_la_CFLAGS which holds quite some exclusions already and could be used to pass -Wno-error as well. (Maybe a good idea to add this regardless of whether we set -Werror by default or not.)
Quoted 4 lines
IIRC I enabled this in conntrack-tools long time ago, and I started getting reports on it breaking compilation with new gcc versions that were actually spewing new warnings. That was stopping users to install latest, probably -Werror is too agressive?
Yes, I wasn't completely serious. Breaking users' builds for things they may not be in control of is not the best idea. We could instead add a configure option to enable strict mode, but checking for warnings is something I usually do so probably not that important after all.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 19 lines · Phil Sutter
Hey,
On Thu, Oct 24, 2019 at 11:29:03AM +0200, Pablo Neira Ayuso wrote:
[...]Quoted 4 lines
If gcc is already checking for this. Warning should be fine.
Regarding -Werror, we would at least need to keep the autogenerated C code by bison away from it.
In nftables there is
libparser_la_CFLAGSwhich holds quite some exclusions already and could be used to pass -Wno-error as well. (Maybe a good idea to add this regardless of whether we set -Werror by default or not.)Quoted 4 lines
IIRC I enabled this in conntrack-tools long time ago, and I started getting reports on it breaking compilation with new gcc versions that were actually spewing new warnings. That was stopping users to install latest, probably -Werror is too agressive?
Quoted 4 lines
Yes, I wasn't completely serious. Breaking users' builds for things they may not be in control of is not the best idea. We could instead add a configure option to enable strict mode, but checking for warnings is something I usually do so probably not that important after all.
Indeed. So let's leave things as is then.