From patchwork Thu Jun 14 19:04:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 10465041 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 F1CFA600D0 for ; Thu, 14 Jun 2018 19:04:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1F4928C0C for ; Thu, 14 Jun 2018 19:04:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D6DC628C0D; Thu, 14 Jun 2018 19:04:45 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, 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 7291128C0C for ; Thu, 14 Jun 2018 19:04:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754780AbeFNTEo (ORCPT ); Thu, 14 Jun 2018 15:04:44 -0400 Received: from mail-wm0-f50.google.com ([74.125.82.50]:53839 "EHLO mail-wm0-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755421AbeFNTEm (ORCPT ); Thu, 14 Jun 2018 15:04:42 -0400 Received: by mail-wm0-f50.google.com with SMTP id x6-v6so12226858wmc.3 for ; Thu, 14 Jun 2018 12:04:41 -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=aS/GIs4wqk7aAQGKAXay0Upx12GRn8XmK9WxU01z6UI=; b=CV5ODkY59NOSHM083sQjnIqoiMV7liAogF0QtOulx7333zyhiN1V7RpAg82gR64TNU b40WxZNcSgfcovTrXfJbpilM+nSWV96UIAW6Aa4QQKlS/jovuetwzpzlBl/xp6gh1lhn njMi2Wi//QEPnNHfEDMM9jUmAibRqSBjor2H7s2T46nyClGWMeG1FNJp6jPG2j/dX9jy zOF9XpjgrqEeuxGAs+FVPOubwqH7GeIvNwXRMJZPrBrl2FhO1c2Q1tOdtDPb1i+hW/gp FyatjbBBG9NVcfaxPrut6wHhvT5AzYEI/hAU+ts8xIOYmgSNs/CfQLRbOB9nqxtkQ48n oB5A== 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=aS/GIs4wqk7aAQGKAXay0Upx12GRn8XmK9WxU01z6UI=; b=cEtBDbvRcJn7djaOWTymh8zWt3vWNe6ICNu7aaXyKr2mg13wOsQxEEdBPQItSQsdU+ 6xPzVrmJHWMXd5jijvfEHg+aWQTeAl4JFR/mZLsayvn/yNi1/q7okrI8TXpFHea3j4lC hOmfYrnogmT4a0YgycggZpWbM4fHKPEI1RCI7kNY77ebaBDZciQ2VWKiFrcqDOLmS/Gk oKRJs2BgoGP8vjuWYMr8koo5kDKfvyFkCOfTjq5XPEbQYYgi9JJc5r3XC8G8+/NkuA/E Gu0j65WXvE/tGhj/X7NpaMwqllUmRWSM+1NFmLdWzdrNPUgVuCjf5pU2IoV9Vs4bslZC E58g== X-Gm-Message-State: APt69E26HJJDYD3ubvMQ+s2bjVRCjRlrjhWjJl14K8SBiJrPqBubbUfb 2QizJ7WLmJkAfCgsVkwtQ6ZuakuanTg= X-Google-Smtp-Source: ADUXVKJnaNrtAhvDtzmjRZfUH7w7GxfZxyjRPsEu8mlGx34VyZw0xzL4qTXdl0zHP3vTHMbpAwUoFQ== X-Received: by 2002:a50:90cb:: with SMTP id d11-v6mr3596801eda.54.1529003080323; Thu, 14 Jun 2018 12:04:40 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:4033:b700:7008:9bbf:83ef:4c95]) by smtp.gmail.com with ESMTPSA id m42-v6sm3303524edc.94.2018.06.14.12.04.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Jun 2018 12:04:39 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 16/20] cast: add support for -Wpointer-to-int-cast Date: Thu, 14 Jun 2018 21:04:15 +0200 Message-Id: <20180614190419.28052-17-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180614190419.28052-1-luc.vanoostenryck@gmail.com> References: <20180614190419.28052-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 It's relatively common to cast a pointer to an unsigned long, for example to make some bit operations. It's much less sensical to cast a pointer to an integer smaller (or bigger) than a pointer is. So, emit a diagnostic for this, under the control of a new warning flag: -Wpointer-to-int-cast. Signed-off-by: Luc Van Oostenryck --- lib.c | 2 ++ lib.h | 1 + linearize.c | 5 +++++ validation/cast-kinds-check.c | 2 +- validation/linear/cast-kinds.c | 2 +- 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib.c b/lib.c index dd7374b53..a6e94ee8a 100644 --- a/lib.c +++ b/lib.c @@ -269,6 +269,7 @@ int Woverride_init_all = 0; int Woverride_init_whole_range = 0; int Wparen_string = 0; int Wpointer_arith = 0; +int Wpointer_to_int_cast = 1; int Wptr_subtraction_blows = 0; int Wreturn_void = 0; int Wshadow = 0; @@ -694,6 +695,7 @@ static const struct flag warnings[] = { { "override-init", &Woverride_init }, { "override-init-all", &Woverride_init_all }, { "paren-string", &Wparen_string }, + { "pointer-to-int-cast", &Wpointer_to_int_cast }, { "ptr-subtraction-blows", &Wptr_subtraction_blows }, { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, diff --git a/lib.h b/lib.h index afa02ec18..e5ba0a0e9 100644 --- a/lib.h +++ b/lib.h @@ -159,6 +159,7 @@ extern int Woverride_init_all; extern int Woverride_init_whole_range; extern int Wparen_string; extern int Wpointer_arith; +extern int Wpointer_to_int_cast; extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; diff --git a/linearize.c b/linearize.c index 45f8d05ee..6f67c4c2f 100644 --- a/linearize.c +++ b/linearize.c @@ -1293,6 +1293,11 @@ static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol * src = cast_pseudo(ep, src, from, size_t_ctype); from = size_t_ctype; break; + case OP_PTRTU: + if (from->bit_size == to->bit_size) + break; + if (Wpointer_to_int_cast) + warning(to->pos, "non size-preserving pointer to integer cast"); default: break; } diff --git a/validation/cast-kinds-check.c b/validation/cast-kinds-check.c index 736ab3910..e22b6136f 100644 --- a/validation/cast-kinds-check.c +++ b/validation/cast-kinds-check.c @@ -2,7 +2,7 @@ /* * check-name: cast-kinds check - * check-command: sparse -m64 -v $file + * check-command: sparse -m64 -v -Wno-pointer-to-int-cast $file * * check-error-start linear/cast-kinds.c:5:45: warning: cast drops bits diff --git a/validation/linear/cast-kinds.c b/validation/linear/cast-kinds.c index 6683ea931..fb16d335e 100644 --- a/validation/linear/cast-kinds.c +++ b/validation/linear/cast-kinds.c @@ -55,7 +55,7 @@ static double double_2_double(double a) { return a; } /* * check-name: cast-kinds - * check-command: test-linearize -Wno-int-to-pointer-cast -m64 $file + * check-command: test-linearize -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -m64 $file * * check-output-start uint_2_int: