From patchwork Sun Sep 7 12:36:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 4858561 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0D7229F32F for ; Sun, 7 Sep 2014 12:37:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1B1C520127 for ; Sun, 7 Sep 2014 12:37:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1E16E20120 for ; Sun, 7 Sep 2014 12:37:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751685AbaIGMg6 (ORCPT ); Sun, 7 Sep 2014 08:36:58 -0400 Received: from mail-wg0-f44.google.com ([74.125.82.44]:40429 "EHLO mail-wg0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751151AbaIGMg6 (ORCPT ); Sun, 7 Sep 2014 08:36:58 -0400 Received: by mail-wg0-f44.google.com with SMTP id k14so842552wgh.27 for ; Sun, 07 Sep 2014 05:36:56 -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=XptyzFQhoJUjUVAf1esom7B+tqfSQNfASzUIhW1coHY=; b=NN7I/DfLutR1cVOp6IiKIqnt4s/CkQrfyde/X/aKuzNL3r3F9O4He/1NGmJHNf8HhF LDv1MUYw9Bly6T+lnjgOloSks16tX4vQgE4a6NR0UhOSiDa0LU5OPcZAjgWBedPPCkS0 uhCPiddNcUnMIBFDKz903QoSqqVR8B7PWmWi0e/tzyAmz9M4nmyaLp8GBWPQT2AwfPeu g94uTYhUBubT32WL9UV59Tt6PqaX+XxftSnI6WpDGUJb4A78ZUfcbTTco7jaclNhCDsX fYnSAJnid/JkBweXeRV9aDujnbqcbtVl8mPShokgbMz+mJl9rNun8DgPoP8oOvjC7E7p C4wg== X-Gm-Message-State: ALoCoQnUs0zSdJOzhLSL6FdlYGMEvzYjJ2TD/ZL0BjSmFcuJv3+qYQJgQAGzkcM3AbvPdQpxuHLg X-Received: by 10.180.37.108 with SMTP id x12mr16101093wij.16.1410093416598; Sun, 07 Sep 2014 05:36:56 -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 bt9sm7589969wjc.44.2014.09.07.05.36.55 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 07 Sep 2014 05:36:55 -0700 (PDT) From: Ard Biesheuvel To: sparse@chrisli.org, linux-sparse@vger.kernel.org Cc: will.deacon@arm.com, Ard Biesheuvel Subject: [PATCH v2] sparse: treat function pointers as pointers to const data Date: Sun, 7 Sep 2014 14:36:53 +0200 Message-Id: <1410093413-3075-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=-8.6 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. Signed-off-by: Ard Biesheuvel Reviewed-by: Josh Triplett --- OK, so while my v1 did solve the example case, it turns out universally treating function pointers as pointers to const data produces so much fallout that it does more harm than good. Instead, this v2 only addresses function pointer initializers and assignments. 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;