From patchwork Thu May 23 15:28:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Li X-Patchwork-Id: 2607791 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 8C4FADFB78 for ; Thu, 23 May 2013 15:28:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759809Ab3EWP2N (ORCPT ); Thu, 23 May 2013 11:28:13 -0400 Received: from mail-pb0-f51.google.com ([209.85.160.51]:46228 "EHLO mail-pb0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759661Ab3EWP2K (ORCPT ); Thu, 23 May 2013 11:28:10 -0400 Received: by mail-pb0-f51.google.com with SMTP id jt11so3036310pbb.10 for ; Thu, 23 May 2013 08:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type; bh=YeWMdrVKUJ4XISPpPginFjAs16bokahlScGY+4FF3yk=; b=Q4oxQwTDmeF46shQXdtxi60qdG+0qjO69D2n7xnTiFZ8TPHREXGOcm8j96av88Y5nA CuCMUDHIOgOmq5PW1UNoOy5znjymElC7siqxBft9QmpRyya6czJXWKGgePsEE56JBHK7 clfdvZV20IferfAYKT+tM8RDNnew9lk48XIjWzGk0R5IRkcGrewTTaGCQ2qUXuJQwJTZ JZLZgqJ+wrQlVSBZN0X+KqJL9u1KP74rbNn3y9IjqfZCzZEduoeepB6W9GgTKez+39C6 nnC9dT4b63XG5Qe1GjrSDKKKvTZUGjEPr5PxLrH3tdDIje90kh0xzgsUVG9oYafyAYKP hWmg== X-Received: by 10.68.114.100 with SMTP id jf4mr13299594pbb.144.1369322889329; Thu, 23 May 2013 08:28:09 -0700 (PDT) Received: from ideapad.lan ([117.136.34.64]) by mx.google.com with ESMTPSA id lq4sm13175587pab.19.2013.05.23.08.28.06 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 23 May 2013 08:28:08 -0700 (PDT) Message-ID: <519E3582.7060704@gmail.com> Date: Thu, 23 May 2013 08:28:02 -0700 From: Chris Li User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130402 Thunderbird/17.0.5 MIME-Version: 1.0 To: Ramsay Jones CC: Sparse Mailing-list Subject: Re: [PATCH 3/3] symbol.c: Set correct size of array from parenthesized string initializer References: <5195370B.9080702@ramsay1.demon.co.uk> <51972FDB.6060201@chrisli.org> <5197387E.5030000@gmail.com> <519A6EAD.8070700@ramsay1.demon.co.uk> In-Reply-To: <519A6EAD.8070700@ramsay1.demon.co.uk> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org On 05/20/2013 11:42 AM, Ramsay Jones wrote: > Unfortunately not. :( Sorry about that. > const char *paths[] = { path, NULL }; I see where I make the mistake. I need to drop the parentheses only for char type. Can you please try this patch again? It is a replacement patch of your patch No3. Not a delta. It works for the the test case above. Let's hope this one works. Chris From 5a3bd40c1e9976c0896ad2d198e202a515dcf194 Mon Sep 17 00:00:00 2001 From: Ramsay Jones Date: Thu, 16 May 2013 20:44:11 +0100 Subject: [PATCH] symbol.c: Set correct size of array from parenthesized string initializer Signed-off-by: Ramsay Jones Signed-off-by: Christopher Li --- symbol.c | 30 ++++++++++++++++++++++++++---- validation/init-char-array1.c | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 validation/init-char-array1.c diff --git a/symbol.c b/symbol.c index 80a2f23..2ec6200 100644 --- a/symbol.c +++ b/symbol.c @@ -296,9 +296,21 @@ static int count_array_initializer(struct symbol *t, struct expression *expr) if (entry->idx_to >= nr) nr = entry->idx_to+1; break; + case EXPR_PREOP: { + struct expression *e = entry; + if (is_char) { + while (e && e->type == EXPR_PREOP && e->op == '(') + e = e->unop; + if (e && e->type == EXPR_STRING) { + entry = e; case EXPR_STRING: - if (is_char) - str_len = entry->string->length; + if (is_char) + str_len = entry->string->length; + } + + + } + } default: nr++; } @@ -307,9 +319,19 @@ static int count_array_initializer(struct symbol *t, struct expression *expr) nr = str_len; break; } + case EXPR_PREOP: + if (is_char) { + struct expression *e = expr; + while (e && e->type == EXPR_PREOP && e->op == '(') + e = e->unop; + if (e && e->type == EXPR_STRING) { + expr = e; case EXPR_STRING: - if (is_char) - nr = expr->string->length; + if (is_char) + nr = expr->string->length; + } + } + break; default: break; } diff --git a/validation/init-char-array1.c b/validation/init-char-array1.c new file mode 100644 index 0000000..24fd8d8 --- /dev/null +++ b/validation/init-char-array1.c @@ -0,0 +1,27 @@ +/* + * for array of char, ("...") as the initializer is an gcc language + * extension. check that a parenthesized string initializer is handled + * correctly and that -Wparen-string warns about it's use. + */ +static const char u[] = ("hello"); +static const char v[] = {"hello"}; +static const char v1[] = {("hello")}; +static const char w[] = "hello"; +static const char x[5] = "hello"; + +static void f(void) +{ + char a[1/(sizeof(u) == 6)]; + char b[1/(sizeof(v) == 6)]; + char c[1/(sizeof(w) == 6)]; + char d[1/(sizeof(x) == 5)]; +} +/* + * check-name: parenthesized string initializer + * check-command: sparse -Wparen-string $file + * + * check-error-start +init-char-array1.c:6:26: warning: array initialized from parenthesized string constant +init-char-array1.c:8:28: warning: array initialized from parenthesized string constant + * check-error-end + */ -- 1.8.1.4