aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/evaluate.c
diff options
authorMasatake YAMATO <yamato@redhat.com>2013-04-07 01:58:55 +0900
committerChristopher Li <sparse@chrisli.org>2013-04-19 20:53:56 -0700
commitc3430e57bc57ff40d5ce95ef2d26b8d70346f58f (patch)
treeedb275321fa638e2abbb2809940c2745b583c19c /evaluate.c
parentc560e34449fa9e804a01204ca50bb9ed2a190935 (diff)
downloadsparse-dev-c3430e57bc57ff40d5ce95ef2d26b8d70346f58f.tar.gz
Warn about initialization of a char array with a too long constant C string.
This patch adds new option -Winit-cstring to sparse. With the option sparse can Warn about initialization of a char array with a too long constant C string. If the size of the char array and the length of the string is the same, there is no space for the last nul char of the string in the array. char s[3] = "abc"; If the array is used as just a byte array, not as C string, this warning is just noise. However, if the array is passed to functions dealing with C string like printf(%s) and strcmp, it may cause a trouble. Here is a example of such trouble: http://www.spinics.net/lists/netdev/msg229765.html http://www.spinics.net/lists/netdev/msg229870.html Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Christopher Li <sparse@chrisli.org>
Diffstat (limited to 'evaluate.c')
-rw-r--r--evaluate.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/evaluate.c b/evaluate.c
index d09f271a..9f2c4acc 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2592,10 +2592,14 @@ String:
p = alloc_expression(e->pos, EXPR_STRING);
*p = *e;
type = evaluate_expression(p);
- if (ctype->bit_size != -1 &&
- ctype->bit_size + bits_in_char < type->bit_size) {
- warning(e->pos,
- "too long initializer-string for array of char");
+ if (ctype->bit_size != -1) {
+ if (ctype->bit_size + bits_in_char < type->bit_size)
+ warning(e->pos,
+ "too long initializer-string for array of char");
+ else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
+ warning(e->pos,
+ "too long initializer-string for array of char(no space for nul char)");
+ }
}
*ep = p;
return 1;