From patchwork Sun Sep 7 11:37:39 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 4858521 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 68ED99F32F for ; Sun, 7 Sep 2014 11:37:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5FAD920138 for ; Sun, 7 Sep 2014 11:37:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 68EDD20122 for ; Sun, 7 Sep 2014 11:37:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751258AbaIGLhq (ORCPT ); Sun, 7 Sep 2014 07:37:46 -0400 Received: from mail-wg0-f51.google.com ([74.125.82.51]:44611 "EHLO mail-wg0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751151AbaIGLhp (ORCPT ); Sun, 7 Sep 2014 07:37:45 -0400 Received: by mail-wg0-f51.google.com with SMTP id l18so13685434wgh.22 for ; Sun, 07 Sep 2014 04:37:43 -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=Rbw28NBgtDAz/ul6R/8/sQDnP8VOB+dndt03lF+R6nY=; b=bFbZr/qShVlvJbgdzvSVY0CRhL1/b33bJ/864cntJd6/NlBXnmhiS0fgiDvIih6t8Q kNoVmZZTfkn35/13Ga6lAiimqE+/Ix0+6ly3uR1lgx+a0o2k45fL+Yc1Vl+qyns0GeCl elhFqwhANPpq7QJRekWq8uj47RA16He/KdIl1wirMBNIpwDxLV6MYeehr7vpO6thZR6t VGJBR5x/rotWy20MrivmM77ri2DlNWX0HrRTprW01CiRc0zoSq1xj4VV827GX2KOwoEU zyoxluGYmJoMtadt5o8TFHfK6+Pq5osIXLSRWiY4mmbeF6utqVjrUCKLsmolTLTftywM hDmw== X-Gm-Message-State: ALoCoQlPhPpRSdz2IKGr+TXskAxnx5SR8qgyXYspUjTX2fhAwN8aSD4NNjZ9L8/E2j/fEq55gZfM X-Received: by 10.180.13.195 with SMTP id j3mr15471960wic.70.1410089863805; Sun, 07 Sep 2014 04:37:43 -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 v4sm7478833wjv.6.2014.09.07.04.37.42 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 07 Sep 2014 04:37:43 -0700 (PDT) From: Ard Biesheuvel To: linux-sparse@vger.kernel.org Cc: will.deacon@arm.com, Ard Biesheuvel Subject: [PATCH] sparse: treat function pointers as pointers to const data Date: Sun, 7 Sep 2014 13:37:39 +0200 Message-Id: <1410089859-22957-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 --- evaluate.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/evaluate.c b/evaluate.c index 66556150ddac..6428312f3e61 100644 --- a/evaluate.c +++ b/evaluate.c @@ -794,6 +794,14 @@ static unsigned long target_qualifiers(struct symbol *type) unsigned long mod = type->ctype.modifiers & MOD_IGN; if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY) mod = 0; + + /* + * Dereferencing a function pointer does not produce an lvalue, + * which means its target is implicitly 'const', and assigning + * a pointer-to-const value to it is ok. + */ + if (type->ctype.base_type && type->ctype.base_type->type == SYM_FN) + mod |= MOD_CONST; return mod; }