From patchwork Fri May 19 02:47:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9735685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 323BC601C2 for ; Fri, 19 May 2017 02:47:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 194D0287C3 for ; Fri, 19 May 2017 02:47:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E0C7287DA; Fri, 19 May 2017 02:47:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6C93C287CA for ; Fri, 19 May 2017 02:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754670AbdESCrM (ORCPT ); Thu, 18 May 2017 22:47:12 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:34438 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755562AbdESCrL (ORCPT ); Thu, 18 May 2017 22:47:11 -0400 Received: by mail-wm0-f65.google.com with SMTP id d127so15140382wmf.1 for ; Thu, 18 May 2017 19:47:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=p22X3lwkcV+8SOs/wkm8ayrHkempTaC6HCIKiQv5H6M=; b=DfmrPcFki8ZzNMb+iiJSCiJ+2eUYVYls/M36AUdscZNI71wdknf1Kr8G66yGZfPWPl kzWyIxCr6DTdwViNOLUQ4Vz3wGxL/8CLCGLo0zLA4a4/IOqYIVGdqMZk5CVCSOpNwjo9 o4/fe80QE+DPGK4MaOAg9dTA9//47Rz7hQSLZP7B0aQuTBMuZO2YBlipWoB7QUVenzTs sgoDd9gBOPUytBxMDjeskLJoNS1JLqn3JlU5fZ6Bt+SXfn+rVb7q+LvwMrl1muv8b/78 Yt1yPxMH2d9115j+W2qPXUWQQOgJSZ9hJ8Op/tJe+RpyOBojJiEZzj/2qNycP83B58kT V3pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=p22X3lwkcV+8SOs/wkm8ayrHkempTaC6HCIKiQv5H6M=; b=SX7nCUef/cpR2DqMX6bNoHnZEATAKv3Mn3Q16onPQjXs/dgGYC4zzkG8Zt3CGN7TgH 5Un0lA01kGynXLSWxGhbn2NyKECcLd8c0rQY20MF7i/lHeqOLNATdbedtwltizV5CMYj dwUKRApmKvjhYn1bR5B27LRp2w4BsCgjCkXr6Q3SNjmKjP0PMak2hpPNbLzAH68LKxCM FACS3oMO1xHBYPaRkZA4kNCB8pdQpeJK/TrYinWtQhwHW8pkMZ50TGn98RFUAeBsu8Gy 0qgwhadEVZmOQEi2tflD/CXffR4Cy0uRjwhnWiRrDaWPQ6Rk2664IvlSCZr9moCehVOe 6MgA== X-Gm-Message-State: AODbwcCGmY7Rb6OPo5K698slM4PbgCvctcDjWSFcALbslR65rQlKKVLl Zx8ICY/Q0jMhZPwufMs= X-Received: by 10.80.188.1 with SMTP id j1mr5546538edh.100.1495162029588; Thu, 18 May 2017 19:47:09 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:82d:ce00:702e:d47b:4e6:a8c8]) by smtp.gmail.com with ESMTPSA id q42sm4306582edd.24.2017.05.18.19.47.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 May 2017 19:47:09 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Al Viro , Luc Van Oostenryck Subject: [PATCH v3 1/2] avoid warning on implicit 'bool <- restricted' casts Date: Fri, 19 May 2017 04:47:01 +0200 Message-Id: <20170519024702.31443-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170519024702.31443-1-luc.vanoostenryck@gmail.com> References: <20170519024702.31443-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning. Fix this by catching implicit 'bool <- restricted type' casts and not emit a warning like for others casts of restricted type to non-restrictes ones. Originally-by: Al Viro Signed-off-by: Luc Van Oostenryck --- evaluate.c | 6 ++++++ validation/bool-cast-bad.c | 3 --- validation/bool-cast-implicit.c | 3 --- validation/bool-cast-restricted.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 validation/bool-cast-restricted.c diff --git a/evaluate.c b/evaluate.c index 976857915..3dc26fc09 100644 --- a/evaluate.c +++ b/evaluate.c @@ -1345,6 +1345,12 @@ static int check_assignment_types(struct symbol *target, struct expression **rp, return 1; } else if (!(sclass & TYPE_RESTRICT)) goto Cast; + if (t == &bool_ctype) { + if (is_fouled_type(s)) + warning((*rp)->pos, "%s degrades to integer", + show_typename(s->ctype.base_type)); + goto Cast; + } *typediff = "different base types"; return 0; } diff --git a/validation/bool-cast-bad.c b/validation/bool-cast-bad.c index b7e7c058d..a0b091e1c 100644 --- a/validation/bool-cast-bad.c +++ b/validation/bool-cast-bad.c @@ -15,9 +15,6 @@ static _Bool fstse(struct s a) { return (_Bool)a; } * check-command: sparse $file * * check-error-start -bool-cast-bad.c:8:41: warning: incorrect type in return expression (different base types) -bool-cast-bad.c:8:41: expected bool -bool-cast-bad.c:8:41: got restricted le16 [usertype] a bool-cast-bad.c:9:42: warning: cast from restricted le16 bool-cast-bad.c:10:41: warning: incorrect type in return expression (different base types) bool-cast-bad.c:10:41: expected bool diff --git a/validation/bool-cast-implicit.c b/validation/bool-cast-implicit.c index ee8b705b9..9d89443b1 100644 --- a/validation/bool-cast-implicit.c +++ b/validation/bool-cast-implicit.c @@ -21,8 +21,5 @@ static _Bool fres(le16 a) { return a; } * check-output-excludes: cast\\. * * check-error-start -bool-cast-implicit.c:15:36: warning: incorrect type in return expression (different base types) -bool-cast-implicit.c:15:36: expected bool -bool-cast-implicit.c:15:36: got restricted le16 [usertype] a * check-error-end */ diff --git a/validation/bool-cast-restricted.c b/validation/bool-cast-restricted.c new file mode 100644 index 000000000..0aa9f35b4 --- /dev/null +++ b/validation/bool-cast-restricted.c @@ -0,0 +1,34 @@ +typedef unsigned int __attribute__((bitwise)) large_t; +#define LBIT ((__attribute__((force)) large_t) 1) + +_Bool lfoo(large_t x) { return x; } +_Bool qfoo(large_t x) { _Bool r = x; return r; } +_Bool lbar(large_t x) { return ~x; } +_Bool qbar(large_t x) { _Bool r = ~x; return r; } +_Bool lbaz(large_t x) { return !x; } +_Bool qbaz(large_t x) { _Bool r = !x; return r; } +_Bool lqux(large_t x) { return x & LBIT; } +_Bool qqux(large_t x) { _Bool r = x & LBIT; return r; } + + +typedef unsigned short __attribute__((bitwise)) small_t; +#define SBIT ((__attribute__((force)) small_t) 1) + +_Bool sfoo(small_t x) { return x; } +_Bool tfoo(small_t x) { _Bool r = x; return r; } +_Bool sbar(small_t x) { return ~x; } +_Bool tbar(small_t x) { _Bool r = ~x; return r; } +_Bool sbaz(small_t x) { return !x; } +_Bool tbaz(small_t x) { _Bool r = !x; return r; } +_Bool squx(small_t x) { return x & SBIT; } +_Bool tqux(small_t x) { _Bool r = x & SBIT; return r; } + +/* + * check-name: bool-cast-restricted.c + * check-command: sparse -Wno-decl $file + * + * check-error-start +bool-cast-restricted.c:19:32: warning: restricted small_t degrades to integer +bool-cast-restricted.c:20:35: warning: restricted small_t degrades to integer + * check-error-end + */