From patchwork Sat Mar 2 12:16:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 10836569 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EF0AF1390 for ; Sat, 2 Mar 2019 12:16:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D012D2A49A for ; Sat, 2 Mar 2019 12:16:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C1AC12B980; Sat, 2 Mar 2019 12:16: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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 D3E412A49A for ; Sat, 2 Mar 2019 12:16:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726323AbfCBMQM (ORCPT ); Sat, 2 Mar 2019 07:16:12 -0500 Received: from ned.t-8ch.de ([212.47.237.191]:37464 "EHLO ned.t-8ch.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726246AbfCBMQM (ORCPT ); Sat, 2 Mar 2019 07:16:12 -0500 X-Greylist: delayed 395 seconds by postgrey-1.27 at vger.kernel.org; Sat, 02 Mar 2019 07:16:11 EST From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=t-8ch.de; s=mail; t=1551528968; bh=5ne49mpnsKb7qhCk7tjdvlvdcquqrWPjNt9V8wF7U0U=; h=From:To:Cc:Subject:Date:From; b=K8SO3H4NjFvunWu/lp/za8dGmTw4Zk745lmdbYA+3U9BC7y9HtsZplg+JTl0xdZ15 v3DJ49wxpRBafjzobV/jqcMioEj4vVc7+gYQUBedv2ILYlB8MLvbpI00XpIilhl/FF wDJ43lG4SGtV/dIB3h+eY1OxDRwy4C0vm9XRcq50= To: linux-sparse@vger.kernel.org Cc: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Subject: [PATCH] Simplify shift operations on constants Date: Sat, 2 Mar 2019 13:16:05 +0100 Message-Id: <20190302121605.23372-1-thomas@t-8ch.de> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 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 The result of a shift operation on a constants by a constant value is also constant. Found through a false positive VLA detected in the Linux kernel. The array size was computed through min() on a shifted constant value and sparse complained about it. Signed-off-by: Thomas Weißschuh Signed-off-by: Thomas Weißschuh Signed-off-by: Luc Van Oostenryck Signed-off-by: Luc Van Oostenryck --- expand.c | 2 +- validation/simplify-binops.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 validation/simplify-binops.c diff --git a/expand.c b/expand.c index e8e50b0..7936e29 100644 --- a/expand.c +++ b/expand.c @@ -197,7 +197,7 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype) return 0; r = right->value; if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) { - if (conservative) + if (conservative && left->type != EXPR_VALUE) return 0; check_shift_count(expr, right); } diff --git a/validation/simplify-binops.c b/validation/simplify-binops.c new file mode 100644 index 0000000..e464c37 --- /dev/null +++ b/validation/simplify-binops.c @@ -0,0 +1,12 @@ +#define __is_constexpr(x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +static void test(int x) { + static int b[] = { + [__builtin_choose_expr(__is_constexpr(1 << 1), 1, x)] = 0, + }; +} + +/* + * check-name: simplify-binops + */