Warn about explicit usage of sizeof(void)
Christopher Li
maintainerPATCH 1/1 Warn about explicit usage of sizeof(void)
Quoted 11 lines · Christopher Li
Quoted 6 lines · Tommy Thorn
You can't have one without the other as you will break identities like
(
uintptr_t) (x + k) === (uintptr_t) x + sizeof (typeof (x)) * kwhich could appear in a macro.
Right. That is exactly the place I actuall want to know. We should consider fix that in the source to have proper type. I expect there is not much place in the kernel use that.
I think you do have a point that from the "spase as a compiler" point of view, there is need to maintain the compatibility with gcc. And I do care about the current patch breaking the assumption of using void_ctype.bit_size.
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without warnings. It will also make is_byte_type() continue to work as it was before.
Here is my test script:
void *p;
int i = sizeof(void);
int j = sizeof(*p);
void* foo(void)
{
return p + 1;
}I run the kernel compile with C=2, guess what. None of the current kernel code use sizeof(void) explicitly. So that is good news.
Every one happy?
Chris
============================================================
Warn about explicit usage of sizeof(void)
sizeof(void) still evaluate as 1 after the warning. void_ctype.bit_size remain zero so is_byte_type() will continue to work.
evaluate.c +7 -1
@@ -584,7 +584,7 @@ static struct symbol *evaluate_ptr_add(s }/* Get the size of whatever the pointer points to */ - multiply =bits_to_bytes (base->bit_size); + multiply = (base == &void_ctype) ?1 :bits_to_bytes (base->bit_size);if (ctype == &null_ctype) ctype = &ptr_ctype;
@@ -2049,8 +2049,14 @@ static struct symbol *evaluate_sizeof(streturn NULL ; size = type->bit_size; + +if (type->ctype.base_type == &void_ctype) { +warning (expr->pos,"expression using sizeof(void)" ); + size = bits_in_char; + }if ((size <0 ) || (size & (bits_in_char -1 )))expression_error (expr,"cannot size expression" ); + expr->type =EXPR_VALUE ; expr->value =bits_to_bytes (size); expr->taint =0 ;
symbol.c +1 -1
@@ -834,7 +834,7 @@ static const struct ctype_declare {struct symbol *base_type; } ctype_declaration[] = { { &bool_ctype,SYM_BASETYPE ,MOD_UNSIGNED , &bits_in_bool, &max_int_alignment, &int_type }, - { &void_ctype, SYM_BASETYPE, 0, &bits_in_char, NULL, NULL }, + { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL }, { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL }, { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
{ &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
Quoted 5 lines · Christopher Li
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without warnings. It will also make
is_byte_type()continue to work as it was before.
Quoted 7 lines
Here is my test script:
void *p;
int i = sizeof(void);
int j =sizeof(*p);
I can't test it right now, but does it give a warning for both sizeof's above? If just first results in a warning, then I think that quite reasonable.
Tommy
PATCH 1/1 Warn about explicit usage of sizeof(void) and sizeof(function)
Quoted 10 lines · Tommy Thorn
Quoted 9 lines · Christopher Li
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without warnings. It will also make
is_byte_type()continue to work as it was before.Here is my test script:
void *p;
Quoted 7 lines
Quoted 3 lines
int i = sizeof(void);
int j =sizeof(*p);I can't test it right now, but does it give a warning for both sizeof's above? If just first results in a warning, then I think that quite reasonable.
Both trigger the warning. I'm not sure this is a problem, as there are no such usage cases in the kernel.
I added (hopefully the right way) handling of (sizeof(function)) to the patch. function++ was already prohibited.
Running the test on the kernel right now.
--
From: Christopher Li <[email protected]>
sizeof(void) and sizeof(function) still evaluate as 1 after the warning. void_ctype.bit_size remain zero so is_byte_type() will continue to work.
--- evaluate.c | 13 ++++++++++++- symbol.c | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-)
evaluate.c +12 -1
@@ -579,7 +579,7 @@ static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *i }/* Get the size of whatever the pointer points to */ - multiply =bits_to_bytes (base->bit_size); + multiply = (base == &void_ctype) ?1 :bits_to_bytes (base->bit_size);if (ctype == &null_ctype) ctype = &ptr_ctype;
@@ -2044,8 +2044,19 @@ static struct symbol *evaluate_sizeof(struct expression *expr)return NULL ; size = type->bit_size; + +if (type->ctype.base_type == &void_ctype) { +warning (expr->pos,"expression using sizeof(void)" ); + size = bits_in_char; + } + +if (is_function (type->ctype.base_type)) { +warning (expr->pos,"expression using sizeof on a function" ); + size = bits_in_char; + }if ((size <0 ) || (size & (bits_in_char -1 )))expression_error (expr,"cannot size expression" ); + expr->type =EXPR_VALUE ; expr->value =bits_to_bytes (size); expr->taint =0 ;
symbol.c +1 -1
@@ -834,7 +834,7 @@ static const struct ctype_declare {struct symbol *base_type; } ctype_declaration[] = { { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type }, - { &void_ctype, SYM_BASETYPE, 0, &bits_in_char, NULL, NULL }, + { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL }, { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL }, { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL }, { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
Christopher Li
maintainerRe: [PATCH] Warn about explicit usage of sizeof(void) and sizeof(function)
Hi Alexey,
Quoted 2 lines · Alexey Zaytsev
I added (hopefully the right way) handling of (sizeof(function)) to the patch. function++ was already prohibited.
Can you send me an incremental patch for the sizeof(function) change? BTW, is it one of the gcc special treatment as well?
Thanks
Chris
Quoted 95 lines · Alexey Zaytsev
Quoted 8 lines · Tommy Thorn
Quoted 7 lines · Christopher Li
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without warnings. It will also make
is_byte_type()continue to work as it was before.Here is my test script:
I added (hopefully the right way) handling of (sizeof(function)) to the patch. function++ was already prohibited.
Quoted 9 lines
Quoted 5 lines
void *p;
int i = sizeof(void);
int j =sizeof(*p);I can't test it right now, but does it give a warning for both sizeof's above? If just first results in a warning, then I think that quite reasonable.
Both trigger the warning. I'm not sure this is a problem, as there are no such usage cases in the kernel.
Running the test on the kernel right now.
--
From: Christopher Li <[email protected]>
sizeof(void) and sizeof(function) still evaluate as 1 after the warning. void_ctype.bit_size remain zero so
is_byte_type()will continue to work.Signed-off-by Christopher Li <[email protected]>[sizeof(function) added by Alexey Zaytsev]Signed-off-by Alexey Zaytsev <[email protected]>--- evaluate.c | 13 ++++++++++++- symbol.c | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-)evaluate.c +12
-1@@ -579,7 +579,7 @@ static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *i }/* Get the size of whatever the pointer points to */ - multiply =bits_to_bytes (base->bit_size); + multiply = (base == &void_ctype) ?1 :bits_to_bytes (base->bit_size);if (ctype == &null_ctype) ctype = &ptr_ctype;@@ -2044,8 +2044,19 @@ static struct symbol *evaluate_sizeof(struct expression *expr)return NULL ; size = type->bit_size; + +if (type->ctype.base_type == &void_ctype) { +warning (expr->pos,"expression using sizeof(void)" ); + size = bits_in_char; + } + +if (is_function (type->ctype.base_type)) { +warning (expr->pos,"expression using sizeof on a function" ); + size = bits_in_char; + }if ((size <0 ) || (size & (bits_in_char -1 )))expression_error (expr,"cannot size expression" ); + expr->type =EXPR_VALUE ; expr->value =bits_to_bytes (size); expr->taint =0 ;symbol.c +1
-1@@ -834,7 +834,7 @@ static const struct ctype_declare {struct symbol *base_type; } ctype_declaration[] = { { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type }, - { &void_ctype, SYM_BASETYPE, 0, &bits_in_char, NULL, NULL }, + { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL }, { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL }, { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL }, { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
PATCH 1/1 Also warn about sizeof(function)
---
Quoted 8 lines
Hi Alexey,
Quoted 2 lines · Alexey Zaytsev
I added (hopefully the right way) handling of (sizeof(function)) to the patch. function++ was already prohibited.
Can you send me an incremental patch for the sizeof(function) change?
Sure. Btw, your patches come demaged lately. Could you please use git-send-email to send them in the future?
Quoted 1 line
BTW, is it one of the gcc special treatment as well?
It was just mentioned in the same option's description:
http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gcc/pointer-arith.html
evaluate.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
evaluate.c +6 -0
@@ -2049,6 +2049,12 @@ static struct symbol *evaluate_sizeof(struct expression *expr)warning (expr->pos,"expression using sizeof(void)" ); size = bits_in_char; } + +if (is_function (type->ctype.base_type)) { +warning (expr->pos,"expression using sizeof on a function" ); + size = bits_in_char; + } +if ((size <0 ) || (size & (bits_in_char -1 )))expression_error (expr,"cannot size expression" );
Christopher Li
maintainerRe: [PATCH] Also warn about sizeof(function)
Thanks, apply and pushed.
Quoted 7 lines · Alexey Zaytsev
Signed-off-by Alexey Zaytsev <[email protected]>---
Quoted 2 lines
Hi Alexey,
Sure. Btw, your patches come demaged lately. Could you please use git-send-email to send them in the future?
Sorry about that. I will use attachment to send them next time. I am still learning my way to use git properly.
Chris
PATCH 1/1 Null ctype should have ptr_ctype as its base type.
In reply to Alexey ZaytsevAmirite?
---
Quoted 19 lines · Alexey Zaytsev
Quoted 10 lines · Tommy Thorn
Quoted 9 lines · Christopher Li
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without warnings. It will also make
is_byte_type()continue to work as it was before.Here is my test script:
void *p;
Quoted 7 lines
Quoted 3 lines
int i = sizeof(void);
int j =sizeof(*p);I can't test it right now, but does it give a warning for both sizeof's above? If just first results in a warning, then I think that quite reasonable.
Quoted 8 lines
Both trigger the warning. I'm not sure this is a problem, as there are no such usage cases in the kernel.
I added (hopefully the right way) handling of (sizeof(function)) to the patch. function++ was already prohibited.
Running the test on the kernel right now.
And it found something.
2158a2159
Quoted 1 line
fs/compat_ioctl.c:787:10: warning: expression using sizeof on a function
9524a9526,9543
Quoted 10 lines
drivers/media/video/gspca/sonixb.c:494:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:494:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:494:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:495:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:495:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:495:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:496:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:498:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:498:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:498:1: warning: expression using sizeof(void)
Quoted 8 lines
drivers/media/video/gspca/sonixb.c:500:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:500:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:502:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:502:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:502:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:504:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:504:1: warning: expression using sizeof(void)drivers/media/video/gspca/sonixb.c:504:1: warning: expression using sizeof(void)
15293a15313,15325
Quoted 5 lines
net/netfilter/nf_conntrack_sip.c:282:21: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:282:21: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:287:23: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:287:23: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:288:29: warning: expression using sizeof(void)
Quoted 8 lines
net/netfilter/nf_conntrack_sip.c:586:23: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:586:23: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:587:25: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:588:29: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:589:25: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:590:29: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:591:22: warning: expression using sizeof(void)net/netfilter/nf_conntrack_sip.c:591:22: warning: expression using sizeof(void)
The first one is
if (copy_in_user(&sgio->status, sgio32->status,
(4 * sizeof(unsigned char)) +
(2 * sizeof(unsigned (short))) + <------- oops
(3 * sizeof(int))))
return -EFAULT;a bug. So, if anyone ever missed his sgio.info, now we know why. Patch sent.
drivers/media/video/gspca/sonixb.c looks like
SENS(initHv7131, NULL, hv7131_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ, 0),
#define SENS(bridge_1, bridge_3, sensor, sensor_1, \
sensor_3, _flags, _ctrl_dis, _sensor_addr) \
{ \
.bridge_init = { bridge_1, bridge_3 }, \
.bridge_init_size = { sizeof(bridge_1), sizeof(bridge_3) }, \So, we are getting a sizeof(NULL), or a sizeof((void *)0). It triggers here because we are getting a null_ctype, and it's base_type points to void_ctype. I'm not sure if this patch is correct, but it seems to do the trick, all void warnings are gone. Christopher?
symbol.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
symbol.c +1 -1
@@ -861,7 +861,7 @@ static const struct ctype_declare { { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype }, { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype }, - { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype }, + { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &ptr_ctype }, { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype }, { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype }, { NULL, }
Christopher Li
maintainerRe: [PATCH] Null ctype should have ptr_ctype as its base type.
Quoted 1 line · Alexey Zaytsev
So, we are getting a sizeof(NULL), or a
sizeof((void *)0). It triggers here
That is my fault. My test for void type should have been more careful.
Quoted 3 lines
because we are getting a
null_ctype, and it'sbase_typepoints tovoid_ctype. I'm not sure if this patch is correct, but it seems to do the trick, all void warnings are gone. Christopher?
No, that is not the right way to fix it. Now you declare NULL as pointer to a pointer type "(void**) 0".
Thanks for finding it out. Does my patch work for you?
Chris
From a7b5de90ea3ea8485882ba348b045279709bd6d9 Mon Sep 17 00:00:00 2001
From: Christopher Li <[email protected]>
Date: Sun, 28 Dec 2008 12:31:50 -0800
Subject: [PATCH] Correct testing for void type.
The last sizeof(void) warning commit too eager to
declare type is void. It end up sizeof(NULL) == 1
as well.
--- evaluate.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-)
evaluate.c +9 -2
@@ -337,6 +337,13 @@ static inline int is_byte_type(struct symbol *type)return type->bit_size == bits_in_char && type->type !=SYM_BITFIELD ; } +static inline int is_void_type (struct symbol *type) +{ +if (type->type ==SYM_NODE ) + type = type->ctype.base_type; +return type == &void_ctype; +} +enum {TYPE_NUM =1 ,TYPE_BITFIELD =2 ,
@@ -584,7 +591,7 @@ static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *i }/* Get the size of whatever the pointer points to */ - multiply = (base == &void_ctype) ?1 :bits_to_bytes (base->bit_size); + multiply =is_void_type (base) ?1 :bits_to_bytes (base->bit_size);if (ctype == &null_ctype) ctype = &ptr_ctype;
@@ -2050,7 +2057,7 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = type->bit_size; -if (type->ctype.base_type == &void_ctype) { +if (size <0 &&is_void_type (type)) {warning (expr->pos,"expression using sizeof(void)" ); size = bits_in_char; }
Signature · 1 line
1.6.0.6
Re: [PATCH] Null ctype should have ptr_ctype as its base type.
Quoted 7 lines · Christopher Li
Quoted 1 line · Alexey Zaytsev
So, we are getting a sizeof(NULL), or a
sizeof((void *)0). It triggers hereThat is my fault. My test for void type should have been more careful.
Quoted 8 lines
Quoted 3 lines
because we are getting a
null_ctype, and it'sbase_typepoints tovoid_ctype. I'm not sure if this patch is correct, but it seems to do the trick, all void warnings are gone. Christopher?No, that is not the right way to fix it. Now you declare NULL as pointer to a pointer type "(void**) 0".
Thanks for finding it out. Does my patch work for you?
Yes, it works, thank you.
There is one problem left:
11091a11093,11130
Quoted 3 lines
drivers/net/wireless/wavelan_cs.c:362:16: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:379:17: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:385:21: error: subtraction of different types can't work (different base types)
[...]
It looks like:
... mmroff(0, mmr_fee_status) ..
#define mmroff(p,f) (unsigned short)((void *)(&((mmr_t *)((void
*)0 + (p)))->f) - (void *)0)
mmr_fee_status being an element in struct mmr.
Here we end up substracting null from non-null void * pointer. Looks quite pointless, but I think sparse should be able to cope with this?
Christopher Li
maintainerRe: [PATCH] Null ctype should have ptr_ctype as its base type.
Quoted 4 lines · Alexey Zaytsev
11091a11093,11130
Quoted 3 lines
drivers/net/wireless/wavelan_cs.c:362:16: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:379:17: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:385:21: error: subtraction of different types can't work (different base types)
Quoted 8 lines
[...]
It looks like:
...
mmroff(0, mmr_fee_status)..#define
mmroff(p,f)(unsigned short)((void *)(&((mmr_t *)((void
*)0 + (p)))->f) - (void *)0)
That is one piece of ugly code.
Quoted 5 lines
mmr_fee_statusbeing an element in struct mmr.
Here we end up substracting null from non-null void * pointer. Looks quite pointless, but I think sparse should be able to cope with this?
Do you have minimal code to duplicate this bug?
I try with the following testing code without much luck.
Chris
typedef struct mmr_t mmr_t;
struct mmr_t {
int a;
int mmr_fee_status;
};#define mmroff(p,f) (unsigned short)((void *)(&((mmr_t *)((void
*)0 + (p)))->f) - (void *)0)
int b;
int foo(void)
{
return mmroff(0,mmr_fee_status);
}Re: [PATCH] Null ctype should have ptr_ctype as its base type.
Quoted 18 lines · Christopher Li
Quoted 4 lines · Alexey Zaytsev
11091a11093,11130
Quoted 3 lines
drivers/net/wireless/wavelan_cs.c:362:16: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:379:17: error: subtraction of different types can't work (different base types)drivers/net/wireless/wavelan_cs.c:385:21: error: subtraction of different types can't work (different base types)Quoted 8 lines
[...]
It looks like:
...
mmroff(0, mmr_fee_status)..#define
mmroff(p,f)(unsigned short)((void *)(&((mmr_t *)((void
*)0 + (p)))->f) - (void *)0)That is one piece of ugly code.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
mmr_fee_statusbeing an element in struct mmr.
Here we end up substracting null from non-null void * pointer. Looks quite pointless, but I think sparse should be able to cope with this?Do you have minimal code to duplicate this bug?
Sorry, my bad. Ran it with a wrong sparse version.
Christopher Li
maintainerQuoted 12 lines · Tommy Thorn
Quoted 11 lines · Christopher Li
So here is what I got. A patch address both of our need. It gives warning of using sizeof(void) explicitly. void* + offset will continue to work without
warnings. It will also makeis_byte_type()continue to work as it was before.Here is my test script:
void *p;
Quoted 7 lines
Quoted 3 lines
int i = sizeof(void);
int j =sizeof(*p);I can't test it right now, but does it give a warning for both sizeof's above? If just first results in a warning, then I think that quite reasonable.
It warn for both. They are the same thing. It will be strange warn one not the other.
But my test run shows that none of the kernel code actually use sizeof(*p). If they do, I think it desert a warning.
Thanks
Chris