From patchwork Fri Mar 31 01:44:48 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: 9655391 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 69CE060352 for ; Fri, 31 Mar 2017 01:46:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5BFAD28688 for ; Fri, 31 Mar 2017 01:46:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5112C28696; Fri, 31 Mar 2017 01:46:44 +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 ED38B28688 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935001AbdCaBqo (ORCPT ); Thu, 30 Mar 2017 21:46:44 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:36047 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934889AbdCaBqn (ORCPT ); Thu, 30 Mar 2017 21:46:43 -0400 Received: by mail-wr0-f196.google.com with SMTP id k6so15310014wre.3 for ; Thu, 30 Mar 2017 18:46:42 -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=/T5aeK6hdSoGgx1akj9ay5OZSZWH1pc6R7CapA56b/4=; b=f/oI3v74+AF831Hsr0Gl+gpoxJBeFrK/NOmAyZX3jXPDUPs2y+39L7gTkmMv18Gwby kxDnJHnlIPtuoKOpZ6k0BBYfuo9dqdpjHFOvFoV/72+MzkweV3w2D0Wbg9qGwGZocXzP FPLCT4uTBQAUHFMxnZ0mK67W7NVwNIPihBAtleVBP2u9Tg3vvpsUEbyPSS9wf9gNLtrS HyIeoGV9lUGjbHI5PR6P8fvoAWq6Rbz/4DLHdUA5yiPbGF1UhPLqsHgPsD5lAneh9VTt y9bCiEF35/laP2+6ZahV2O3Dw5DIqDhFilFzUhuGdit4oIMcUO+kjRw/NRB41anUbhVE 8T+g== 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=/T5aeK6hdSoGgx1akj9ay5OZSZWH1pc6R7CapA56b/4=; b=LMzyt9XkunrCy0BtQElIpIkbf+7fuVVUS6XlfAMToWhy36TtLWWyGxKOWlbHcQj/Gk xP3NsJbyAryvbi2zRBa5MxcWcmn8tTCD+Sa2KxAX8KQrU4npNzXMlEYzGeWdmwXgJiWL D7bYD5EaGrYX5JfVrGe/xWoQp5XxG6itM6vXIf6qPk45/sH4qrPo9xGL+sVpfzDw5tTq 0zguEI4lz0pG9T5WdvfKOTrQ7bmgpXOKZyI0rmfoOuxbu+HiU3eC+vTrL8y8w5j7EsZc 4lLpEAz9pre0p6KreV8wsQL7lU6GoKSkQUq1GWuw4Ck2erb3wApS8CIET9mv51WrJVSz gRpw== X-Gm-Message-State: AFeK/H30UTrHeaB9NaDNd6zi9KeXFK/F7e57W0z2ewHyA4HisZqY1RQt1Td3B/JWQ0UWWA== X-Received: by 10.28.147.129 with SMTP id v123mr389028wmd.51.1490924801254; Thu, 30 Mar 2017 18:46:41 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:88d:1900:cdad:92b8:5279:ae19]) by smtp.gmail.com with ESMTPSA id 189sm917023wmm.31.2017.03.30.18.46.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:40 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 14/25] constexpr: recognize address constants created through pointer arithmetic Date: Fri, 31 Mar 2017 03:44:48 +0200 Message-Id: <20170331014459.9351-15-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170331014459.9351-1-luc.vanoostenryck@gmail.com> References: <20170331014459.9351-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 From: Nicolai Stange An address constant +/- an integer constant expression qualifies as an address constant again. Furthermore, the array-subscript operator "[]" may be used in the creation of address constant. Handle both cases by making evaluate_ptr_add() check whether an integer constant expression is added to an address constant and tag the result as being an address constant again if so. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 7 +++++++ validation/constexpr-pointer-arith.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 validation/constexpr-pointer-arith.c diff --git a/evaluate.c b/evaluate.c index ce2e52e15..e58d9c373 100644 --- a/evaluate.c +++ b/evaluate.c @@ -571,6 +571,13 @@ static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *i classify_type(degenerate(expr->left), &ctype); base = examine_pointer_target(ctype); + /* + * An address constant +/- an integer constant expression + * yields an address constant again [6.6(7)]. + */ + if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE)) + expr->flags = CEF_ADDR; + if (!base) { expression_error(expr, "missing type information"); return NULL; diff --git a/validation/constexpr-pointer-arith.c b/validation/constexpr-pointer-arith.c new file mode 100644 index 000000000..a92202800 --- /dev/null +++ b/validation/constexpr-pointer-arith.c @@ -0,0 +1,28 @@ +static int a = 1; +static int b[2] = {1, 1}; + +static int *c = &b[1]; // OK +static int *d = (int*)0 + 1; // OK +static int *e = &b[1] + 1; // OK +static int *f = b + 1; // OK +static int *g = d + 1; // KO +static int *h = &a + 1; // OK +static int *i = &b[1] + 1; // OK +static int *j = b + 1; // OK +static int *k = d + 1; // KO +static int *l = &*&b[1]; // OK +static int *m = &*(&a + 1); // OK +static int *n = &*(&b[1] + 1); // OK +static int *o = &*(b + 1); // OK +static int *p = &*(d + 1); // KO + +/* + * check-name: pointer arithmetic constness verification. + * check-command: sparse -Wconstexpr-not-const $file + * + * check-error-start +constexpr-pointer-arith.c:8:19: warning: non-constant initializer for static object +constexpr-pointer-arith.c:12:19: warning: non-constant initializer for static object +constexpr-pointer-arith.c:17:22: warning: non-constant initializer for static object + * check-error-end + */