From patchwork Mon Sep 8 06:30:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 4860011 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3B69DC0338 for ; Mon, 8 Sep 2014 06:30:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2FD7520125 for ; Mon, 8 Sep 2014 06:30:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2A7CF2011B for ; Mon, 8 Sep 2014 06:30:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751962AbaIHGaI (ORCPT ); Mon, 8 Sep 2014 02:30:08 -0400 Received: from mail-wg0-f45.google.com ([74.125.82.45]:55794 "EHLO mail-wg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751404AbaIHGaH (ORCPT ); Mon, 8 Sep 2014 02:30:07 -0400 Received: by mail-wg0-f45.google.com with SMTP id z12so588399wgg.28 for ; Sun, 07 Sep 2014 23:30:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=9FBurWSWU77zBXKofDXa2M8BcFJkVxR03FouMzaR/zQ=; b=Rkz1Z1k+7IHNSwoRrHbK2XBiKZJH/Te56T86SVd2iCl0Z9CpuFcd38y0Sva4GUBKLE 4GDJWwdXl5qpLG4rTjhYsUl9ghlgF4vIgROjhxAdQNwSeNyWo//Un1LCrQm4cadCFWoK Zmk7tZ/biV6Agycvvsga03XcskZLsA7I7tMkUTGd2vnIlYPUAIHl0Eg9MVw7S+krXdug /dutSQoZwfYZ1o7Za1hUFwBdvJFWjHCVaj93XGeuHOdDpx6Y5gMvB8mbT1aZPamcUVqV xjvavNKMle5sTcECKWipgsYK3mBWxeb6HSQX5Hrw8ZM2aFPQgr2BWBZZsVwaymtAHk17 LIzw== X-Gm-Message-State: ALoCoQlgWFqLEtT0tVwszvLk4qHtwkiQK2QlSWPDYwNs88fHQ8Q8QUAT0P/UXRwp9RN52hnY+maE X-Received: by 10.180.105.8 with SMTP id gi8mr19943683wib.46.1410157805407; Sun, 07 Sep 2014 23:30:05 -0700 (PDT) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id dc9sm10679928wib.5.2014.09.07.23.30.03 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 07 Sep 2014 23:30:04 -0700 (PDT) From: Ard Biesheuvel To: sparse@chrisli.org, linux-sparse@vger.kernel.org Cc: will.deacon@arm.com, Ard Biesheuvel Subject: [PATCH v3] sparse: treat function pointers as pointers to const data Date: Mon, 8 Sep 2014 08:30:03 +0200 Message-Id: <1410157803-3658-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This code snippet: static void bar(void const *arg) { int (*foo)(void) = arg; } produces the following warning: test.c:4:28: warning: incorrect type in initializer (different modifiers) test.c:4:28: expected int ( *foo )( ... ) test.c:4:28: got void const *arg which is caused by the fact that the function pointer 'foo' is not annotated as being a pointer to const data. However, dereferencing a function pointer does not produce an lvalue, so a function pointer points to const data by definition, and we should treat it accordingly. To avoid producing a warning on the inverse case, i.e., static void bar(void) { void *foo = bar; } we only address the case where the function pointer is the target of an assignment. Reviewed-by: Josh Triplett Signed-off-by: Ard Biesheuvel --- v3: add rationale for restriction to assignments to commit message add R-b v2: only treat function pointers as pointers to const data when they are the target of an assignment evaluate.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/evaluate.c b/evaluate.c index 66556150ddac..a5a830978bda 100644 --- a/evaluate.c +++ b/evaluate.c @@ -1359,6 +1359,15 @@ static int compatible_assignment_types(struct expression *expr, struct symbol *t typediff = "different address spaces"; goto Err; } + /* + * If this is a function pointer assignment, it is + * actually fine to assign a pointer to const data to + * it, as a function pointer points to const data + * implicitly, i.e., dereferencing it does not produce + * an lvalue. + */ + if (b1->type == SYM_FN) + mod1 |= MOD_CONST; if (mod2 & ~mod1) { typediff = "different modifiers"; goto Err;