From patchwork Wed Sep 30 23:18:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810489 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9AA9113B2 for ; Wed, 30 Sep 2020 23:18:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7FEC52075F for ; Wed, 30 Sep 2020 23:18:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GznDmN8z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731064AbgI3XSp (ORCPT ); Wed, 30 Sep 2020 19:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729912AbgI3XSg (ORCPT ); Wed, 30 Sep 2020 19:18:36 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2118C061755 for ; Wed, 30 Sep 2020 16:18:35 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id y15so1140640wmi.0 for ; Wed, 30 Sep 2020 16:18:35 -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 :mime-version:content-transfer-encoding; bh=M710i58RkfmQFQ+xtAJYFUkWitVKVEfpIwfolwAKIqg=; b=GznDmN8zhiw6HEmTZKTf5cK0seGXepQH0xm2qHItpRAnODt+UoSndJ7CAA/ptHYpL2 a4KbXuI78USybHnEd2TzQi3cONRPRO+/jg+yHWPKcI1rWkgdUxRype91FGmsomUBhqUP TKt8rJYbASDnPtS9eXm/p/tGclUsflEmLjlWFoxxHYCknYN7rWLtu0yKgqFh/gb66oCw Zv8vnxCaP+7M3qnacjI9xXJEyjn3dxXJ4G2sTFlfGJGEPcbP293MijV0f+srsP937eYO isq70VfgmSDib6p/at2+HiLlzF2KnrWvMHmNNMi8ODi6sJTi+sGc+XUCqyP6zsxurmJz rsTQ== 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:mime-version:content-transfer-encoding; bh=M710i58RkfmQFQ+xtAJYFUkWitVKVEfpIwfolwAKIqg=; b=g4iPIwK+F8CgunAXwD6rKmeAqzgkc5DWb68EFDNTkMnGg//C2SrgxiJ4oR7zurJjys /rpJcRAeO10MOW/v7PPSSPH6OKLj0x7piRcOzq3viU4uWEsqeN5G19d5pKQDcfGYRSTp xW4et6XJvbrSHEFtszw263/NRfSsV2xS7j3rGNtEmG0+PvxLwslhTJ6LYcooN1I875UH NtP5sjBYNs3IlBzumOKy5a8ZuECxvNNJWJ672oT/5bwT2oQmj5Wss2qZKeu8ZhWnNi3o V2nJOUcO2Y/6phN88/ywijlWZZCDAmrlt3AmEV7LBegCwffUXhDMy4QG5utzL3epjd3J Rrmw== X-Gm-Message-State: AOAM532vjwi0eCysbvNmmIvfy5cbOz6SVSwrhMRSmgqGhMdo98EsQ6eq J70c0nhnqFZBg/JQ36J/3P0MW8iPAx4= X-Google-Smtp-Source: ABdhPJxRuT5x32Zb5KubRbZnY+xfuuqi4CFy0KO9MZXkYRE3Bw5lrJm6ARsEVOEt1WM6qMybU7WzCg== X-Received: by 2002:a1c:f70b:: with SMTP id v11mr4714524wmh.21.1601507914030; Wed, 30 Sep 2020 16:18:34 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:33 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 01/13] flex-array: add testcases Date: Thu, 1 Oct 2020 01:18:16 +0200 Message-Id: <20200930231828.66751-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Signed-off-by: Luc Van Oostenryck --- validation/flex-array-align.c | 19 +++++++++++++++++++ validation/flex-array-array.c | 16 ++++++++++++++++ validation/flex-array-error.c | 27 +++++++++++++++++++++++++++ validation/flex-array-nested.c | 30 ++++++++++++++++++++++++++++++ validation/flex-array-padding.c | 22 ++++++++++++++++++++++ validation/flex-array-sizeof.c | 19 +++++++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 validation/flex-array-align.c create mode 100644 validation/flex-array-array.c create mode 100644 validation/flex-array-error.c create mode 100644 validation/flex-array-nested.c create mode 100644 validation/flex-array-padding.c create mode 100644 validation/flex-array-sizeof.c diff --git a/validation/flex-array-align.c b/validation/flex-array-align.c new file mode 100644 index 000000000000..0cc67ab36997 --- /dev/null +++ b/validation/flex-array-align.c @@ -0,0 +1,19 @@ +struct s { + __INT32_TYPE__ x; + __INT16_TYPE__ y; + unsigned char f[]; +}; + +static int foo(struct s *s) +{ + return (sizeof(*s) << 16) | __builtin_offsetof(typeof(*s), f); +} + +/* + * check-name: flex-array-align + * check-command: test-linearize -Wno-flexible-array-sizeof $file + * check-known-to-fail + * + * check-output-ignore + * check-output-contains: ret\\..*\\$0x80006 + */ diff --git a/validation/flex-array-array.c b/validation/flex-array-array.c new file mode 100644 index 000000000000..bda80d7a0a20 --- /dev/null +++ b/validation/flex-array-array.c @@ -0,0 +1,16 @@ +struct s { + int i; + long f[]; +}; + +static struct s a[2]; + +/* + * check-name: flex-array-array + * check-command: sparse -Wflexible-array-array $file + * check-known-to-fail + * + * check-error-start +flex-array-array.c:6:18: warning: array of flexible structures + * check-error-end + */ diff --git a/validation/flex-array-error.c b/validation/flex-array-error.c new file mode 100644 index 000000000000..89601e42daf6 --- /dev/null +++ b/validation/flex-array-error.c @@ -0,0 +1,27 @@ +struct s { + int i; + long f[]; + int j; +}; + +union u { + int i; + long f[]; +}; + +// trigger the examination of the offending types +static int foo(struct s *s, union u *u) +{ + return __builtin_offsetof(typeof(*s), i) + + __builtin_offsetof(typeof(*u), i); +} + +/* + * check-name: flex-array-error + * check-known-to-fail + * + * check-error-start +flex-array-error.c:3:14: error: flexible array member 'f' is not last +flex-array-error.c:9:14: error: flexible array member 'f' in a union + * check-error-end + */ diff --git a/validation/flex-array-nested.c b/validation/flex-array-nested.c new file mode 100644 index 000000000000..3503c329d7c3 --- /dev/null +++ b/validation/flex-array-nested.c @@ -0,0 +1,30 @@ +struct f { + int i; + long f[]; +}; + +struct s { + struct f f; +}; + +union u { + struct f f; +}; + +// trigger the examination of the offending types +static int foo(struct s *s, union u *u) +{ + return __builtin_offsetof(typeof(*s), f) + + __builtin_offsetof(typeof(*u), f); +} + +/* + * check-name: flex-array-nested + * check-command: sparse -Wflexible-array-nested $file + * check-known-to-fail + * + * check-error-start +flex-array-nested.c:6:8: warning: nested flexible arrays +flex-array-nested.c:10:7: warning: nested flexible arrays + * check-error-end + */ diff --git a/validation/flex-array-padding.c b/validation/flex-array-padding.c new file mode 100644 index 000000000000..2ba77971266e --- /dev/null +++ b/validation/flex-array-padding.c @@ -0,0 +1,22 @@ +struct s { + __INT32_TYPE__ x; + __INT16_TYPE__ y; + unsigned char f[]; +}; + +static int foo(struct s *s) +{ + return __builtin_offsetof(typeof(*s), f); +} + +/* + * check-name: flex-array-padding + * check-command: test-linearize -Wflexible-array-padding $file + * check-known-to-fail + * + * check-output-ignore + * + * check-error-start +flex-array-padding.c:4:23: warning: flexible array member has padding + * check-error-end + */ diff --git a/validation/flex-array-sizeof.c b/validation/flex-array-sizeof.c new file mode 100644 index 000000000000..3359509d0084 --- /dev/null +++ b/validation/flex-array-sizeof.c @@ -0,0 +1,19 @@ +struct s { + int i; + long f[]; +}; + +static int foo(struct s *s) +{ + return sizeof(*s); +} + +/* + * check-name: flex-array-sizeof + * check-command: sparse -Wflexible-array-sizeof $file + * check-known-to-fail + * + * check-error-start +flex-array-sizeof.c:8:16: warning: using sizeof on a flexible structure + * check-error-end + */ From patchwork Wed Sep 30 23:18:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810499 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8BB5613B2 for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6F1042076A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vf8KvK1I" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731402AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732164AbgI3XSi (ORCPT ); Wed, 30 Sep 2020 19:18:38 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB6FAC061755 for ; Wed, 30 Sep 2020 16:18:36 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id e2so1128162wme.1 for ; Wed, 30 Sep 2020 16:18:36 -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 :mime-version:content-transfer-encoding; bh=U4eWEw5lYC71dJVNgfeud3T/BUth3UQzZlVDmobacog=; b=Vf8KvK1IVnFqqnBvLkkpKBWe3hN/FswciahRD9laFODE8NdBgm9eBOjEA+XHuOe9FT vIOVix22KOMmBYvA0HnPsE21Cr2EF4u3h8ERXN7yyxpmW1mdY5xFi5PTlL0hTEdzDU0b OmwIcwRvuou9LtPPJHNuiOBwfV87dHbuxl6LQv7/2wqhT8NOtIi+WN/xlUIuwTuBe8LV 6AXFZiYKzrN+YI4jX+i6iocUfxE7rvw9c4cyfc+uHWWn8Gd/ofmsb/siAyhTcbwvfGN6 KIsZxUF9cLwiZ0vwJmO4xuHnGbNschn/TsgNWRNIxMkBIpWmBKLsTpmhiefU6FuDwNbn owvw== 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:mime-version:content-transfer-encoding; bh=U4eWEw5lYC71dJVNgfeud3T/BUth3UQzZlVDmobacog=; b=gMNHnkp+06ZEKGcda2ZIlSE6fj468MdosJESvcv7oVLPlB6qtCkdfEDAYPUxrTlfdz TzqSLkrypy48y7hbr5HtTp3XCiZZd4K6gEKetU2inrhbvc987GlNYDzYvzmp62Jj5Fke 34Ha2YpW5LxBvN86qcDRLqbaPEYM1Xff/A+dMFw/GlO+7zYQhwN9+5Qjuvb3HEDlj2VG BAvUbyfSVZVki/mB35NiM/Xt9aydofT12sxG2UroOTJ3//V9CQpXe21kXL83Zvyf25ox sp5mfzL/U74iFspF8CkOxZk5hYiUnsLIV0JwzwqYPVmQ5FJf0+7mN9mWHYS07rwkCsuU x49w== X-Gm-Message-State: AOAM532emiS6Hgef2S/ACWqPGNAoQJnl8XyaOn2bdkgdhvBtLQaJL2jl 01M6p3y3qaVhpMdozGG7+sFMe5PSzXk= X-Google-Smtp-Source: ABdhPJwon8e/cT2piC8TUhaSHY32z63mFBu6biVPSJHa91Gdcz9o0D0Qjrf1Fn6nHLI4/Evw58RmSA== X-Received: by 2002:a7b:c09a:: with SMTP id r26mr650303wmh.45.1601507915147; Wed, 30 Sep 2020 16:18:35 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:34 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 02/13] flex-array: factor out common part of lay_out_{struct,union}() Date: Thu, 1 Oct 2020 01:18:17 +0200 Message-Id: <20200930231828.66751-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org This is a preparatory step for later patches. Signed-off-by: Luc Van Oostenryck --- symbol.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/symbol.c b/symbol.c index 7f0c85580f06..365351a0a756 100644 --- a/symbol.c +++ b/symbol.c @@ -94,14 +94,6 @@ struct struct_union_info { */ static void lay_out_union(struct symbol *sym, struct struct_union_info *info) { - examine_symbol_type(sym); - - // Unnamed bitfields do not affect alignment. - if (sym->ident || !is_bitfield_type(sym)) { - if (sym->ctype.alignment > info->max_align) - info->max_align = sym->ctype.alignment; - } - if (sym->bit_size > info->bit_size) info->bit_size = sym->bit_size; @@ -125,14 +117,6 @@ static void lay_out_struct(struct symbol *sym, struct struct_union_info *info) unsigned long bit_size, align_bit_mask; int base_size; - examine_symbol_type(sym); - - // Unnamed bitfields do not affect alignment. - if (sym->ident || !is_bitfield_type(sym)) { - if (sym->ctype.alignment > info->max_align) - info->max_align = sym->ctype.alignment; - } - bit_size = info->bit_size; base_size = sym->bit_size; @@ -196,6 +180,14 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident)); member->ctype.base_type = &incomplete_ctype; } + examine_symbol_type(member); + + if (member->ctype.alignment > info.max_align) { + // Unnamed bitfields do not affect alignment. + if (member->ident || !is_bitfield_type(member)) + info.max_align = member->ctype.alignment; + } + fn(member, &info); } END_FOR_EACH_PTR(member); From patchwork Wed Sep 30 23:18:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810495 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E232139A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 21C852076A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GTgpRsmH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732105AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731064AbgI3XSi (ORCPT ); Wed, 30 Sep 2020 19:18:38 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C406DC0613D0 for ; Wed, 30 Sep 2020 16:18:37 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id k10so3556525wru.6 for ; Wed, 30 Sep 2020 16:18:37 -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 :mime-version:content-transfer-encoding; bh=Ke5S1b+rZNOHOoWlqdl7RMz+CTpsSeIWhrOWtW03TZM=; b=GTgpRsmHFzvnSlvzKSCS1KP6NLIPV+2vN8GsjARJ46yZ+jFM8GyLYuLo7qLlvZyopk udKSFPcAt7zwiTM0tloPbfP/kFgrhtBFfOtccVl1HvUoDjseU8hTv/J8uWZfEto9/+19 1t7K4XyMHRgHlbUxBstgOYm+iegc286DRIQZhUjYPRQ/N4OFAgAKeugTf3JoruNV01hI 18vtQOWuQ1IYL3c6a6G2PgRrZBkB//hZpzbC1St7IDMJSVsbykiCma6vjIc4vJrMBtP/ 6HGc7/Ra0+qEtriFYEFCn9OSSEA5TI6UGD35eLMuzfBxJThBu6M4i54RNmwXkj7e446v 5VYw== 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:mime-version:content-transfer-encoding; bh=Ke5S1b+rZNOHOoWlqdl7RMz+CTpsSeIWhrOWtW03TZM=; b=TSc2joAelD1xlPOReEcTkykzM4XU4EGT6U34wR8V06skOjYDSNhMjOuKowbVEOWsLT WdRqORdJvKeJ4/eDdaDRBmrm0pV81bcPIdU8DLVtneIFlsqCZAfI0G30lx6PAxswa6W4 aKsvLsDLdcLrKNSLnJdo04X6Jjm4i+FvJvo9Vkh0BxfeRiQelLepficACmdUfDcYyeZJ AfOhftSH5LYHBLoTefIsvOb/r/i9I4Uj6ndr9xx+YWXgsDjWFSrftVnCE9oLB6Qnj2dH QzaMCrf0hEf1ZNFEMP1JDfN3KKxANFyvn7k5dEwbj5efc86+OKrMqESg8AIaZ8wge2Cj xG4w== X-Gm-Message-State: AOAM5334A1xDnwrI0fKu+5eS8NY0apJUBQGt5SVcFed3YmpmCeydtR8e shQJucxiINPdp14j1jq/mMw6GuaV+rE= X-Google-Smtp-Source: ABdhPJzOKNjSJT7dSYekk6OfCpuiKwsCZPQYF5wARTSQseI6Qg+F9hZsAlKigCggAiZTOBVwXm6d3Q== X-Received: by 2002:adf:df87:: with SMTP id z7mr5647077wrl.239.1601507916298; Wed, 30 Sep 2020 16:18:36 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:35 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 03/13] flex-array: do not lay out invalid struct members Date: Thu, 1 Oct 2020 01:18:18 +0200 Message-Id: <20200930231828.66751-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Do not bother trying to lay out invalid struct members, ignore them as it will avoid to special case them later. Signed-off-by: Luc Van Oostenryck --- symbol.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/symbol.c b/symbol.c index 365351a0a756..4c93a0fb4880 100644 --- a/symbol.c +++ b/symbol.c @@ -121,10 +121,12 @@ static void lay_out_struct(struct symbol *sym, struct struct_union_info *info) base_size = sym->bit_size; /* - * Unsized arrays cause us to not align the resulting - * structure size + * If the member is unsized, either it's a flexible array or + * it's invalid and a warning has already been issued. */ if (base_size < 0) { + if (!is_array_type(sym)) + return; info->align_size = 0; base_size = 0; } From patchwork Wed Sep 30 23:18:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810501 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B368F17D4 for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95B472076B for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Bx/k+gbK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730470AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732166AbgI3XSi (ORCPT ); Wed, 30 Sep 2020 19:18:38 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C6D3C061755 for ; Wed, 30 Sep 2020 16:18:38 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id k15so3542229wrn.10 for ; Wed, 30 Sep 2020 16:18:38 -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 :mime-version:content-transfer-encoding; bh=mFQL3i9zVNObV8k0iWLnJ89/KEx2uauUzxx+FFe+hXs=; b=Bx/k+gbKuKN47gLJlE3PyXYwSVKakXUsYE95eG3pb2pp41w287XbaEWwldfvG9Rq4B MA4i2tUiu5I/ZK0Hesz2Aqu8VXhRmsIX4btvyVPzHo4eAUckOxXZa/kpoSpPT6k22hWi 0RiRFEbbKrG/+B/3JkMJBNI0/UcBvTvYNUmjMdwlBiJnx9d0MKo6V2LOTnFtwYMI4IKN knDDOpR8vhd3yQKPkN5cmfe+eLxj4aEgqXvq43NEdbM+BrLm6itkTVNaUs5E8o5Jz7LD arQCvP8nfunTJOrXOVhiD/D/uyew8dDqndNY5K+Y0uqzWXd/qc/emCHW+6AVL0/aZz79 QrTQ== 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:mime-version:content-transfer-encoding; bh=mFQL3i9zVNObV8k0iWLnJ89/KEx2uauUzxx+FFe+hXs=; b=Vy5Tbj3TDrZdEJh6SF6OBBfcZEkQxDZDejk4Oftvm9s0nsgf1gS4K/QUtlfE6sPBuc IGcXf2L2WL3peCvhgurnVobXTvPp1l428zPw9PtTlVhO1PTAkKDeEkwD2f0mtJACB31y PjLqkgM79/mSPZE7meU6nSvHY0c5m+/K2PtHS4h+R8yrw8I/vnycb3CoxJPZbx2a9E6h qt1qsTqq2lB6inPa/GgSvlK9xT4dTEjExcLomyWHH1fnmqjMQTZdAIBIbdTA9qHeW1rr tJtoccVe37AbE/ZUPVXm6BDviPF2v//YlIV3c0CZZXbt/X+E2/x9aQhbgKLToS5TUnrT 86bA== X-Gm-Message-State: AOAM531bEn8IshKNPyrTngMw4YPsNmz/k192G9BqCSv+Q9qEIzkLn48R 2Fdz+4mPrBG7xGUpxQgotrWwcSlmDW8= X-Google-Smtp-Source: ABdhPJyv40LxhvPOWtffzCYMHw2r4m9TufS1gmtVQLz4DpJicKc3E4/cc2I/Ks4SHEm6KNuKI2WfRQ== X-Received: by 2002:adf:f88b:: with SMTP id u11mr5297252wrp.376.1601507916998; Wed, 30 Sep 2020 16:18:36 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:36 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 04/13] flex-array: flexible array members have zero size and alignment is OK Date: Thu, 1 Oct 2020 01:18:19 +0200 Message-Id: <20200930231828.66751-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org When doing the layout of structures, flexible arrays used to not align the resulting structure size. However, the standard specify that while for most purposes flexible arrays can be handled as if not present, they still may add some trailing padding (cfr. C11's 6.7.2.1p18). So, there is no reason to reset the alignment. Signed-off-by: Luc Van Oostenryck --- symbol.c | 1 - validation/flex-array-align.c | 1 - 2 files changed, 2 deletions(-) diff --git a/symbol.c b/symbol.c index 4c93a0fb4880..9acffeea7fad 100644 --- a/symbol.c +++ b/symbol.c @@ -127,7 +127,6 @@ static void lay_out_struct(struct symbol *sym, struct struct_union_info *info) if (base_size < 0) { if (!is_array_type(sym)) return; - info->align_size = 0; base_size = 0; } diff --git a/validation/flex-array-align.c b/validation/flex-array-align.c index 0cc67ab36997..9f28942a1ee1 100644 --- a/validation/flex-array-align.c +++ b/validation/flex-array-align.c @@ -12,7 +12,6 @@ static int foo(struct s *s) /* * check-name: flex-array-align * check-command: test-linearize -Wno-flexible-array-sizeof $file - * check-known-to-fail * * check-output-ignore * check-output-contains: ret\\..*\\$0x80006 From patchwork Wed Sep 30 23:18:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810485 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8859317E6 for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C1B22075F for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D3zRqG/L" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730544AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732169AbgI3XSj (ORCPT ); Wed, 30 Sep 2020 19:18:39 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33DDDC061755 for ; Wed, 30 Sep 2020 16:18:39 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id d4so1081717wmd.5 for ; Wed, 30 Sep 2020 16:18:39 -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 :mime-version:content-transfer-encoding; bh=gjso956F3U82lCvbV8gzVlNNJET9eKYzhAlPBp9cTjw=; b=D3zRqG/L+xfVCzOFZ2ogl25sfzXYXIuOaSXOtc9VIBvik8M3LOv34A0imEI5HWF2fv SOE2j4y6Li7dHxbJfckktV/+6jhkAODLixcT7Jz61zckaQhJvIgOg2AAROQMu511Pfo/ e5R1hd3iLzHCqHq+Zgc4d8Q0zlKDmNB+4QIQfjt2HKiQdISW4WqnOBYOOyHz87xGk5kn 4plCJkROsa3vv2SdhFw1f9wxwZM8QxK/rIzVA3ee2791kbIpe1ifDtuCOgtDCj7gkNCL /Gf3TKfH/ZK4fzYO2mBuY/fv4EJymKGig6xCuZDZzycrZ9g7J/w3FHV7wP6pfzZ7F4cp MGrA== 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:mime-version:content-transfer-encoding; bh=gjso956F3U82lCvbV8gzVlNNJET9eKYzhAlPBp9cTjw=; b=s3/I+p+DlefnANxZC5MQQVWE+LuDHm8cPD2OCD0b+tV47av3p9Zh6HN51KF1lX7QF5 ywCVssQIxSFlJIRwDTr7LZ491aAsuJfaqNc8jqo9YffHHAw7xSXthnKyLBDgdlIb2vRt H/z9Bwkw1roqvssn9KKxvzUhZ0vaF1/k/Dne15AqJi21PzQvzFrmAXBiJosibyZ0BWV0 m+FSgFF0+RK/13+MQ72B2uGfnCtziOIfVhHMgy9vAC8Q1kpGwGI0lj1W8qEilVHWjvip LR1BpUR4qWZk7ThbP5i0k34zPiirYKgYapzdTYy61U+Dd7lGRPuQQLHmhuOjdbP8h/3L uviw== X-Gm-Message-State: AOAM5323FstF8ebLPKevrcS5xkogDk9jYxAdps8mzOThwJGAxokHZNaK nWtM13RPL90lkN98kDjdPfTxEbkhwNs= X-Google-Smtp-Source: ABdhPJzVroMM8rgArVLXilGqFpO28RY2Nrgzixdxo42uc3uqEaxKOKGMDjMmJZE2yz5v9f7tyvehOA== X-Received: by 2002:a1c:35c1:: with SMTP id c184mr5285857wma.161.1601507917737; Wed, 30 Sep 2020 16:18:37 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:37 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 05/13] flex-array: detect structures with a flexible array member Date: Thu, 1 Oct 2020 01:18:20 +0200 Message-Id: <20200930231828.66751-6-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org This is a preparatory step for doing the checks and warnings. Signed-off-by: Luc Van Oostenryck --- symbol.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/symbol.c b/symbol.c index 9acffeea7fad..6633e89de4a9 100644 --- a/symbol.c +++ b/symbol.c @@ -87,6 +87,7 @@ struct struct_union_info { unsigned long max_align; unsigned long bit_size; int align_size; + struct symbol *flex_array; }; /* @@ -128,6 +129,7 @@ static void lay_out_struct(struct symbol *sym, struct struct_union_info *info) if (!is_array_type(sym)) return; base_size = 0; + info->flex_array = sym; } align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1; From patchwork Wed Sep 30 23:18:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810483 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 61F9A174A for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DB5F2075F for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Lt/547lu" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730453AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731402AbgI3XSk (ORCPT ); Wed, 30 Sep 2020 19:18:40 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA328C061755 for ; Wed, 30 Sep 2020 16:18:39 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id x23so1094675wmi.3 for ; Wed, 30 Sep 2020 16:18:39 -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 :mime-version:content-transfer-encoding; bh=WpPjPLvLkMQo3d30mXkfg7bcBPKMGRWxEg96TVxlw58=; b=Lt/547luXqfB9/Jm9jvWPq/AbMSDhv55kUlX8NIAMvlBD7Jw853Ai33QVOpoczTJ/0 hxfD+pLaY3SXP1ozGh7nalVVL51D/5vWSwOGQ9dy8Kkmou60DQII0F1vh0Pg+7Nnv3ar prP2CxojKqSbA3IDnZaaNaiDCmhqIYdL2pE1f3dDBPgeH6VqyTvWa1cF7XOduY+5JQbh rPYaeRKVRQ6yOdzQv0pqnKWCQi/XVM0Wlq1oGg4kTBaNDsYwR9DkreRK/5YAqM2LIwMG M4cVpO8RoHcJh8z2r9lhOqZW+lXlC00QQ8Gh8BtolJM70wzgI2eVMDfmkDWxR44H+zy3 LvwA== 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:mime-version:content-transfer-encoding; bh=WpPjPLvLkMQo3d30mXkfg7bcBPKMGRWxEg96TVxlw58=; b=mLKIawvYrEseH6YsX7dlcV2q0A6Wt+XHQZbls3nyI2DvSVbGbAlMkTlz4I31/uy3wd POcwBK38EDXNScwVE2sCGdgysDx3cLEzGzqxVFZ7DMX261Gz4qYOGquItjGYalyf4CQs qc82jFiJdNqPFmKGXEZJ7e6oud4aqrPIyvwtpeQ/FKVVRrnuZ2zlyQHBDTZUcchE+t3R +LJMX/mrCPBQ7dX4JQSWjWxlFE0eItzDH3F7MKYCMDpG4OO1MpW5q4uzi+9hmmRbTGVi JRQEGA8AcUfpMRlFKSAHMC1AU9pUMJ47Z0bBHB+MAWdvuyGGnxDK21hX6uhc0gaENWl5 je2A== X-Gm-Message-State: AOAM531OvmLOT4ZnmqeX4+XDIsECu+P8vbAfi3Rp/mKH2QvRjyEj+sBN F+n8dl/lG2xiJNAueTIxst1PYlQ6RqU= X-Google-Smtp-Source: ABdhPJxRdLlQkkpeGGWJN3p8psCoZ/6+FzBallW2fTBilf7KY9xWzjoFsG1H5HHEqjmRZQ0Rb0/bWA== X-Received: by 2002:a05:600c:2109:: with SMTP id u9mr5027956wml.147.1601507918502; Wed, 30 Sep 2020 16:18:38 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:38 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 06/13] flex-array: warn on flexible arrays in unions Date: Thu, 1 Oct 2020 01:18:21 +0200 Message-Id: <20200930231828.66751-7-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Flexible array members are not allowed in unions. So, warn if one is present. Signed-off-by: Luc Van Oostenryck --- symbol.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/symbol.c b/symbol.c index 6633e89de4a9..e578b1a840a3 100644 --- a/symbol.c +++ b/symbol.c @@ -95,6 +95,9 @@ struct struct_union_info { */ static void lay_out_union(struct symbol *sym, struct struct_union_info *info) { + if (sym->bit_size < 0 && is_array_type(sym)) + sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident)); + if (sym->bit_size > info->bit_size) info->bit_size = sym->bit_size; From patchwork Wed Sep 30 23:18:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810479 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 10356139A for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E926B2076B for ; Wed, 30 Sep 2020 23:18:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Aq1C3L1J" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732173AbgI3XSn (ORCPT ); Wed, 30 Sep 2020 19:18:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732171AbgI3XSm (ORCPT ); Wed, 30 Sep 2020 19:18:42 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0298CC061755 for ; Wed, 30 Sep 2020 16:18:41 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id z1so3561820wrt.3 for ; Wed, 30 Sep 2020 16:18:40 -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 :mime-version:content-transfer-encoding; bh=B46//BeD53v0CzUMVYJoHKCEFQGG8ONWyP2hBCQWACE=; b=Aq1C3L1JGM+oHq/vOeE86PnVA3w0TYL4abUbf0ORWhxuKARU119cB1DaHSA/Jz23CE FKgQ7cSL6ZKTeEFmD8Ft9CM55wuvM3hhep799mCH8/WsvNIVsf1tDILLmSZas4XUbetR zTouDAmRXoyLjIsohYfOkW7KhT7ztCadr5f356njm7voQehH2Gl56ChYx8Eia7poL0NV uKqMDalbYNBEVbQuFDUhj0vWFgWpGxX894mKNJEFscYLBUDUEaQE97Y1LTW6u2/Cqkh7 TqTiSmN/AhNxxOVwWwUTaMUedtTHwW/pYZNGyDFVGu4SgnXQ2fiP+rnOCvtS3tfP5E+I UYOg== 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:mime-version:content-transfer-encoding; bh=B46//BeD53v0CzUMVYJoHKCEFQGG8ONWyP2hBCQWACE=; b=j67tUIkSVy1UvWpw2w83W9SD11r3HzYtUxvoz97JY4hl2MGnbTQk3mY1Npd0rqmn4a p26Ubs5bbxaPXKyGiWPSp+zNNjdi5b8xamV9BBnJ7GzPii5PO9JkVA5qCcYJFcwat5al KmkNJwHaWzzs8mrZtjD0+eDZ9/3Oz+wt1VbJGbVTkIcGnJIpDBSzlQbFrjvwGFebElaO 2uWN+qyKTeo7qWak4ni5P894NOO8ePbtlBd6AOXXGouIVsz3zj+tv/IhiHWu1jud2yLo TyF0pq2UpJAQqMwDXEFVckKXRHFKZJlt0z/tocGiICBkwAa1SuPZvciJIc/cP+Bfb516 0n5w== X-Gm-Message-State: AOAM53018qps+8e554sldom4lykxdgWbNBQYNp13xq7/ny9+mLJ4ctAc U25pNWTOG53QpqRhy5n+BiNR/ezPRjU= X-Google-Smtp-Source: ABdhPJyOgZgPQ9tpGGO4c/T3aKhMAXGVYE0QAb3f/CDsZ3Aib/Cv1NNkN4I7SRCIFFL9Fwfmq3C0cA== X-Received: by 2002:a5d:4486:: with SMTP id j6mr5310387wrq.278.1601507919490; Wed, 30 Sep 2020 16:18:39 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:39 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 07/13] flex-array: warn if flexible array is not last Date: Thu, 1 Oct 2020 01:18:22 +0200 Message-Id: <20200930231828.66751-8-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Flexible array members must be the last in a structure. Warn if it is not the case. Signed-off-by: Luc Van Oostenryck --- symbol.c | 2 ++ validation/flex-array-error.c | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/symbol.c b/symbol.c index e578b1a840a3..b4c5e471372b 100644 --- a/symbol.c +++ b/symbol.c @@ -186,6 +186,8 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident)); member->ctype.base_type = &incomplete_ctype; } + if (info.flex_array) + sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident)); examine_symbol_type(member); if (member->ctype.alignment > info.max_align) { diff --git a/validation/flex-array-error.c b/validation/flex-array-error.c index 89601e42daf6..2b7e6953050c 100644 --- a/validation/flex-array-error.c +++ b/validation/flex-array-error.c @@ -18,7 +18,6 @@ static int foo(struct s *s, union u *u) /* * check-name: flex-array-error - * check-known-to-fail * * check-error-start flex-array-error.c:3:14: error: flexible array member 'f' is not last From patchwork Wed Sep 30 23:18:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810481 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3450C13B2 for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 18A9C2075F for ; Wed, 30 Sep 2020 23:18:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DvCq+pXh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732174AbgI3XSn (ORCPT ); Wed, 30 Sep 2020 19:18:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732170AbgI3XSm (ORCPT ); Wed, 30 Sep 2020 19:18:42 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5219C0613D0 for ; Wed, 30 Sep 2020 16:18:41 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id g4so3570887wrs.5 for ; Wed, 30 Sep 2020 16:18: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 :mime-version:content-transfer-encoding; bh=ARt8KG7zV1fRZnUN/ydWn313HkkzGFCeK4o0EQzQEeM=; b=DvCq+pXhoiFGZDlByoNR2s23evf9nJUAkaBRkyZvBqiDqw6KQQU8czcna/1GoElKK7 HwX7FkvQCh7gpeRpDhNfX6d0/r5mIk1PfqKAj/u6paEdIdNFiTMZxE6tZG/p5wu0MO39 xv+u58ac19SmLqaQO3EytZRQXhPP5qMU9pNNL18hbilVe3Sb/YsjotExrCkfh0PUg/zI qH+uyYbBTFoqY/rFQRyWv2OAm7tCg5wbi4h9i0AvHsb/yF97Ofd/z1dNztQY/auFVD7U VBBunMdLnf0P7kX3o7ljs958tQXw//1jTcHY8BROO1jchX+9/M9NPrtJ3BEOFHjZYsPP 5tCw== 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:mime-version:content-transfer-encoding; bh=ARt8KG7zV1fRZnUN/ydWn313HkkzGFCeK4o0EQzQEeM=; b=YBhr6jybNOfWKHtiTlptFKss+cq9lEC0LlnOeJVGbhQZm1byFgfk8tPQ5lFLb1Wfwy YN7ztKTs5Dz9a/ZtjFiM0TrtTIw4ZEOaq3POQNREvzYOBhmq+7ekIt6ly7JfBgJqxqIJ F24dS2OqPJvBIUMz70JQcjMEAXwK7hqVqrY7PHdaiuKIl8PvpSBXRljB7VAsB3i44K/T smeWFzVUX/MtQCDS7GUD7blSikqtGH6WVGFKJCagplJ7/48Vk7xB7P0R4x+xwNd81iGg siTZZtUrrU+WYxE7GsjjaQfXjweMw/0ORQoNGVCS3x+f6dhWEwTxvMbrEEgwF36gTmj9 c/cQ== X-Gm-Message-State: AOAM530v9YXstyz55ZS+5K4R7Dc0eEOGkF8vXZ3GSRwQBzt6mN6tboSE wxWf/+tkhkiQPw1/5GjqGRjmur3i2iQ= X-Google-Smtp-Source: ABdhPJwbqf8Cp2lqBh5FiziaXqYfX7FAcmtdrOfmCuhRk1w2EsQ8d1FXyVaInkPaB5GFYG6f5C3frw== X-Received: by 2002:a5d:45cc:: with SMTP id b12mr5473844wrs.395.1601507920274; Wed, 30 Sep 2020 16:18:40 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:39 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 08/13] flex-array: identify structures with a flexible array member Date: Thu, 1 Oct 2020 01:18:23 +0200 Message-Id: <20200930231828.66751-9-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Structures containing a flexible array must not be nested. So, as a preparatory step, detect structures or union containing a flexible array, possibly recursively and mark the corresponding type with a dedicated flag. Signed-off-by: Luc Van Oostenryck --- symbol.c | 6 ++++++ symbol.h | 1 + 2 files changed, 7 insertions(+) diff --git a/symbol.c b/symbol.c index b4c5e471372b..bffdc135ba69 100644 --- a/symbol.c +++ b/symbol.c @@ -87,6 +87,7 @@ struct struct_union_info { unsigned long max_align; unsigned long bit_size; int align_size; + char has_flex_array; struct symbol *flex_array; }; @@ -206,6 +207,11 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance bit_align = bytes_to_bits(sym->ctype.alignment)-1; bit_size = (bit_size + bit_align) & ~bit_align; } + if (info.flex_array) { + info.has_flex_array = 1; + } + if (info.has_flex_array) + sym->has_flex_array = 1; sym->bit_size = bit_size; return sym; } diff --git a/symbol.h b/symbol.h index a3ed95678ee5..287df0a3a0ee 100644 --- a/symbol.h +++ b/symbol.h @@ -185,6 +185,7 @@ struct symbol { examined:1, expanding:1, evaluated:1, + has_flex_array:1, string:1, designated_init:1, forced_arg:1, From patchwork Wed Sep 30 23:18:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810477 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DE37A6CA for ; Wed, 30 Sep 2020 23:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C280D2076A for ; Wed, 30 Sep 2020 23:18:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oRQ0ef45" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732154AbgI3XSn (ORCPT ); Wed, 30 Sep 2020 19:18:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732173AbgI3XSm (ORCPT ); Wed, 30 Sep 2020 19:18:42 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82F58C0613D0 for ; Wed, 30 Sep 2020 16:18:42 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id g4so3570902wrs.5 for ; Wed, 30 Sep 2020 16:18: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 :mime-version:content-transfer-encoding; bh=6Y31UCU3wbqyJz2tZFq95U1yNF1X6XoMI8S2Ky/MzvY=; b=oRQ0ef45Ip08rSLeOymOwL4Mg4L3TpKnV40jy5xcuMYM0XfZOjrjfOyIrumjQGvYRq s2b6JGx+Bqkl2kraWtyeHbTIZukezLUWKBZn1bFHurud4KACpyv0gJl2i4VMWdwo4+Qc eujBiyKq6x3q/419dVmwlczVu5vtCgrBb6LGYpxpoTehGODev6oWdFpSASV1iY9Jq4Sm aFPF84cnd2z+FfkQQMhkOBInSvjEelTrWv1kdX4Fjalgc7BwfDDntxaW6MVzY0JzPepl YbvO36M1H832tyqPKzRi1cHHWzwFT5vbTmCrPWGBztsO5hWAsdIhX1EU5liT48p2Whbw qH0A== 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:mime-version:content-transfer-encoding; bh=6Y31UCU3wbqyJz2tZFq95U1yNF1X6XoMI8S2Ky/MzvY=; b=f74/sKYjI9GvZB4jtv7n6R6WB3PHykRurOb5A1GVZ0GP6U/7/329Hd0NOKXo4sSsW5 Y9Ybkxpm1lmz4ZJ/DfANsVVUMt7YY2TlyZkwvCQ/ajloA+NEZ9oOu5jqikHq7KX3+q2C s9BYBt3/98QTE161R0n1dq1FF9rDQKmghx0tjvy4MTD65B7stOoGFptbmfmGvZcauvVp m5m6NKFCDn2YpGNWrG/WsPgw3FlCNPd7s3K5UjFd+oAKGNfKNYAAicc48hV2R7l3KTya c8wsFLLdmiRAu7nOoTiIF1/xrKwbDCchMFXYlFgHwhtDPNnB/Xm+lc4HG4XIgtDt7z38 VjMA== X-Gm-Message-State: AOAM531/+MV4FeR27yH8ZuXrDI9tb9prRCZlg1LEajy4PZKYRUGD3Dci hoP7/6S3EOk1i1ZxgoUw4hp3qR685F0= X-Google-Smtp-Source: ABdhPJybVayQD6e1w3EcBgkqk0BTejDJu1UAY2ON4xcejzB8XiAAub13wEuKqK99f5J3p8j7A1LwVQ== X-Received: by 2002:a5d:4088:: with SMTP id o8mr5381463wrp.112.1601507921031; Wed, 30 Sep 2020 16:18:41 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:40 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 09/13] flex-array: add helper has_flexible_array() Date: Thu, 1 Oct 2020 01:18:24 +0200 Message-Id: <20200930231828.66751-10-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org This will make later checks easier & clearer. Signed-off-by: Luc Van Oostenryck --- symbol.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/symbol.h b/symbol.h index 287df0a3a0ee..c9e6c7fe4439 100644 --- a/symbol.h +++ b/symbol.h @@ -507,6 +507,13 @@ static inline int is_extern_inline(struct symbol *sym) is_function(sym->ctype.base_type); } +static inline int has_flexible_array(struct symbol *type) +{ + if (type->type == SYM_NODE) + type = type->ctype.base_type; + return type->has_flex_array; +} + static inline int get_sym_type(struct symbol *type) { if (type->type == SYM_NODE) From patchwork Wed Sep 30 23:18:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810503 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 10C726CA for ; Wed, 30 Sep 2020 23:18:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E86D12076A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Kg9wrhaf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730357AbgI3XSw (ORCPT ); Wed, 30 Sep 2020 19:18:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732171AbgI3XSn (ORCPT ); Wed, 30 Sep 2020 19:18:43 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5740DC061755 for ; Wed, 30 Sep 2020 16:18:43 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id m6so3571912wrn.0 for ; Wed, 30 Sep 2020 16:18:43 -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 :mime-version:content-transfer-encoding; bh=1m4GeymSLrdvni0gyQthurgsXukJu7vZrBewa9DhEoU=; b=Kg9wrhafbZ9XNZXfDJWZ/e+haPATk2/3HlZO70mhyZy71MXY1V9hJBBLr9viADIVbw hmpeOwPnxYbHftnujaJNT/qt68OXV9eli6N3uFLc5XD/J3qwhr23kVBP7sBxcrigRtD+ Wpb+3UqeSUB5Ut18IEmtUn2/AMfBVI2nX/NkGktmLTvNzOLxO3ZEL7+nzRTb61V08BjB xjkyx8/u5Lo5JykOUEK63PWGnUElwpGpYu02a6HCtx3glIVY0BVslqrpNs8mR3BPQeXa bjd8c1DQAVgolX845RbqPaHcZoh0J5gA5rZkkZtaGgxaZCXxRSiC2R2hrdbKlzTKE2Wg 2hmw== 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:mime-version:content-transfer-encoding; bh=1m4GeymSLrdvni0gyQthurgsXukJu7vZrBewa9DhEoU=; b=AN4DsuUN9g2RL5tkV4h0LieAJFW/UcBDh6kaY3OhQqfkswpDGf2AZX+dyNrJtoMplQ TET1Q3lg1mAdBxm2aYKPIHLKmoTGRbD3wVs19JBOa+WyZxLEkRl1TYnfuKJf6YhlBslU gttkSAIKlpo7Efk9015VqgLgA4o9Ke4gUzfHuxWlRKriVL3JGjEqksOmOF7BtskPx4K1 +ot7mXJQWx6mdYfUU2W0KC2+L8PvQ08PSh1JIlVbFdeQl45HZm4hYVG9fBbDQ5Ie4BnU 25fr7F659QVZvutJmCRNjBiAvRMMl5goFM41PfKOOXBaRq2fX3hIsrj0qF0F3VVtIbtT qhVQ== X-Gm-Message-State: AOAM530IJH+l2cyPcxrTdbQkIaYfcHOiR8yfrl5tFhQ1lW6CT5FUZ+bL WKLFlr3UCJz5DmD6eX5W+ieT2wS98H0= X-Google-Smtp-Source: ABdhPJwj88l8FqJrzrlwDfzti9Z5+Vqb5/SUHKY5eSAugmXrKDLbhzVGa+/7OfU17QyNji9vHMasww== X-Received: by 2002:adf:e292:: with SMTP id v18mr5462445wri.256.1601507921845; Wed, 30 Sep 2020 16:18:41 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:41 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 10/13] flex-array: warn when using sizeof() on a flexible array Date: Thu, 1 Oct 2020 01:18:25 +0200 Message-Id: <20200930231828.66751-11-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Using sizeof() on a structure containing a flexible array will ignore the 'flexible' part. This is maybe what is expected but maybe not, so add an option -Wflexible-array-sizeof to warn on such usage. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 3 +++ options.c | 2 ++ options.h | 1 + sparse.1 | 7 +++++++ validation/flex-array-sizeof.c | 1 - 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/evaluate.c b/evaluate.c index c1ef348a475e..cfbb6ada4153 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2253,6 +2253,9 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = bits_in_char; } + if (has_flexible_array(type) && Wflexible_array_sizeof) + warning(expr->pos, "using sizeof on a flexible structure"); + if (is_array_type(type) && size < 0) { // VLA, 1-dimension only struct expression *base, *size; struct symbol *base_type; diff --git a/options.c b/options.c index 294dfd3be77a..ce88fbf6ed61 100644 --- a/options.c +++ b/options.c @@ -100,6 +100,7 @@ int Wdesignated_init = 1; int Wdo_while = 0; int Wenum_mismatch = 1; int Wexternal_function_has_definition = 1; +int Wflexible_array_sizeof = 0; int Wimplicit_int = 1; int Winit_cstring = 0; int Wint_to_pointer_cast = 1; @@ -840,6 +841,7 @@ static const struct flag warnings[] = { { "do-while", &Wdo_while }, { "enum-mismatch", &Wenum_mismatch }, { "external-function-has-definition", &Wexternal_function_has_definition }, + { "flexible-array-sizeof", &Wflexible_array_sizeof }, { "implicit-int", &Wimplicit_int }, { "init-cstring", &Winit_cstring }, { "int-to-pointer-cast", &Wint_to_pointer_cast }, diff --git a/options.h b/options.h index abdf08645ad2..feb351a36c9e 100644 --- a/options.h +++ b/options.h @@ -99,6 +99,7 @@ extern int Wdesignated_init; extern int Wdo_while; extern int Wenum_mismatch; extern int Wexternal_function_has_definition; +extern int Wflexible_array_sizeof; extern int Wimplicit_int; extern int Winit_cstring; extern int Wint_to_pointer_cast; diff --git a/sparse.1 b/sparse.1 index 48dab7a9a5c1..5f98df33a231 100644 --- a/sparse.1 +++ b/sparse.1 @@ -257,6 +257,13 @@ Sparse issues these warnings by default. To turn them off, use \fB\-Wno\-external\-function\-has\-definition\fR. . .TP +.B -Wflexible-array-sizeof +Warn about using the sizeof operator on a structure containing a flexible array, +possibly recursively. + +Sparse does not issue these warnings by default. +. +.TP .B \-Winit\-cstring Warn about initialization of a char array with a too long constant C string. diff --git a/validation/flex-array-sizeof.c b/validation/flex-array-sizeof.c index 3359509d0084..05394e19a6b2 100644 --- a/validation/flex-array-sizeof.c +++ b/validation/flex-array-sizeof.c @@ -11,7 +11,6 @@ static int foo(struct s *s) /* * check-name: flex-array-sizeof * check-command: sparse -Wflexible-array-sizeof $file - * check-known-to-fail * * check-error-start flex-array-sizeof.c:8:16: warning: using sizeof on a flexible structure From patchwork Wed Sep 30 23:18:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810497 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 65DA6174A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4814A2076A for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R15aPmUy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730849AbgI3XSw (ORCPT ); Wed, 30 Sep 2020 19:18:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730357AbgI3XSo (ORCPT ); Wed, 30 Sep 2020 19:18:44 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FAF9C0613D0 for ; Wed, 30 Sep 2020 16:18:44 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id o5so3545280wrn.13 for ; Wed, 30 Sep 2020 16:18:44 -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 :mime-version:content-transfer-encoding; bh=TbQLmC8gdfb42znA/sMTP6J8RyOXPvyy0YjXSQYAby4=; b=R15aPmUy38g/FD2VE3+a6+vTrVqLWPlMPzMpVD9P5rN3iyaFjV/Zg2rr4lNleNaR5+ 21slKeEf5lHNjIPd6uMEzNhkl9tv6B54H9M0W2AZUo0KS2w4WO6mDl5d6wWCQ6wT7gi9 GlKYceD6EqHcBF8kdQnWU5fS0GoZhd8/Uw81eCASRLm/RQr9A+KM1wcLnFyLN5Dg0uJT wRiGYjNHl84HvcucmmhmRVxCxyQYCjHG/gTKvvYGU7nuFfly1Ml+lAYLetYcn8LmJEra FIFOIfiYQRoOtT1ls7lrQ6BhwNEuj0RMBOemmSiyeqAoY20DWd6sTdUSF8NC23R7Spat taEw== 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:mime-version:content-transfer-encoding; bh=TbQLmC8gdfb42znA/sMTP6J8RyOXPvyy0YjXSQYAby4=; b=sokEM9F6JlqfxLFpnItNPBntgA6TLtjJleORilPCa2znCxZMRRT8XdO4u3X/uvJJ0U ttO1UQXs2J8bhbAc97cWH31Vnfdv9bAtqBRmhP8NW7RrU0s6S+766ke1JMwKhaPHKHdX k/fI77Of2I+gN2NON4wtDcqJrfGQrTOXt3Aly0SmZ4xc+pKTlxxiG/LjkAWhJGW0F+sK M7VVbk/0zcZnvpzvE/8OkhmYYej839n5lYLOzwGNXCNhSwzoiUdwywbaa0HJkb0gEbDB ysD0kPVnca4j6l7Mnf/wA/gaj6MuyN9HxnJa/uFa9mMkJKyx7DQR/1+ZVQkVvDSIdiFt WfJw== X-Gm-Message-State: AOAM5329HTKrTPbQVMXoS366cJ+p7xQqpS6gI8l9Cj9ENYdI4CNCyZCV 9KuiFbc16Tunb4UkAeND9GaHApJhfEo= X-Google-Smtp-Source: ABdhPJw2srdzacWoHTdo94QPt7wo0rHJpxaMp4G9bCj3bvzWRbx42K4mNvV3R5iYUHbe31yDHeNHJQ== X-Received: by 2002:a5d:50cd:: with SMTP id f13mr5418177wrt.211.1601507922566; Wed, 30 Sep 2020 16:18:42 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:42 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 11/13] flex-array: warn an arrays containing a flexible array Date: Thu, 1 Oct 2020 01:18:26 +0200 Message-Id: <20200930231828.66751-12-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org An array of some aggregate type containing, possibly recursively, a flexible array is pretty non-sensical. So, add an option -Wflexible-array-array to warn on such usage. Signed-off-by: Luc Van Oostenryck --- options.c | 2 ++ options.h | 1 + sparse.1 | 7 +++++++ symbol.c | 2 ++ validation/flex-array-array.c | 1 - 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/options.c b/options.c index ce88fbf6ed61..a8129ac1e952 100644 --- a/options.c +++ b/options.c @@ -100,6 +100,7 @@ int Wdesignated_init = 1; int Wdo_while = 0; int Wenum_mismatch = 1; int Wexternal_function_has_definition = 1; +int Wflexible_array_array = 1; int Wflexible_array_sizeof = 0; int Wimplicit_int = 1; int Winit_cstring = 0; @@ -841,6 +842,7 @@ static const struct flag warnings[] = { { "do-while", &Wdo_while }, { "enum-mismatch", &Wenum_mismatch }, { "external-function-has-definition", &Wexternal_function_has_definition }, + { "flexible-array-array", &Wflexible_array_array }, { "flexible-array-sizeof", &Wflexible_array_sizeof }, { "implicit-int", &Wimplicit_int }, { "init-cstring", &Winit_cstring }, diff --git a/options.h b/options.h index feb351a36c9e..7bcf925b6912 100644 --- a/options.h +++ b/options.h @@ -99,6 +99,7 @@ extern int Wdesignated_init; extern int Wdo_while; extern int Wenum_mismatch; extern int Wexternal_function_has_definition; +extern int Wflexible_array_array; extern int Wflexible_array_sizeof; extern int Wimplicit_int; extern int Winit_cstring; diff --git a/sparse.1 b/sparse.1 index 5f98df33a231..c1a74153c618 100644 --- a/sparse.1 +++ b/sparse.1 @@ -257,6 +257,13 @@ Sparse issues these warnings by default. To turn them off, use \fB\-Wno\-external\-function\-has\-definition\fR. . .TP +.B -Wflexible-array-array +Warn about arrays of structures containing a flexible array. + +Sparse issues these warnings by default. To turn them off, use +\fB-Wno-flexible-array-array\fR. +. +.TP .B -Wflexible-array-sizeof Warn about using the sizeof operator on a structure containing a flexible array, possibly recursively. diff --git a/symbol.c b/symbol.c index bffdc135ba69..02b9066e966a 100644 --- a/symbol.c +++ b/symbol.c @@ -267,6 +267,8 @@ static struct symbol * examine_array_type(struct symbol *sym) bit_size = -1; } } + if (has_flexible_array(base_type) && Wflexible_array_array) + warning(sym->pos, "array of flexible structures"); alignment = base_type->ctype.alignment; if (!sym->ctype.alignment) sym->ctype.alignment = alignment; diff --git a/validation/flex-array-array.c b/validation/flex-array-array.c index bda80d7a0a20..921a0698bb28 100644 --- a/validation/flex-array-array.c +++ b/validation/flex-array-array.c @@ -8,7 +8,6 @@ static struct s a[2]; /* * check-name: flex-array-array * check-command: sparse -Wflexible-array-array $file - * check-known-to-fail * * check-error-start flex-array-array.c:6:18: warning: array of flexible structures From patchwork Wed Sep 30 23:18:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810493 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 011B717D4 for ; Wed, 30 Sep 2020 23:18:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D0D022075F for ; Wed, 30 Sep 2020 23:18:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="suCCZQau" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732169AbgI3XSv (ORCPT ); Wed, 30 Sep 2020 19:18:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730849AbgI3XSp (ORCPT ); Wed, 30 Sep 2020 19:18:45 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECFC0C0613D1 for ; Wed, 30 Sep 2020 16:18:44 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id v12so1118444wmh.3 for ; Wed, 30 Sep 2020 16:18:44 -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 :mime-version:content-transfer-encoding; bh=/WKLM2C6qMLkIZKOBs5AbUjGucYg3UVIUWBwmAZ7iUc=; b=suCCZQaucZ+MgkqK9Rb370zLfwqghNOWgGU5gGO/xEeI7vCCPTmyVt8T3fmjtz7+wE odi0M0yggU87p86n7jrmVjqp+NNQAxYMRS89HgPXJFgljjjK5MBkz7ZJb1SG/kB/NzuL oPnGdMLU6JKj8rwW+x4ffpEvRq2cZ3DpyccrRxArnLNkTBy+/X4zAv/hjKxNfQwKEoIh vFlq1olLAzq7tkYYzdjLq1EQYKEwbVAxSFP+1/CHm2ju6t55tfNtGaVgvZIB+vGxKQSk I5z/i4zWVDytate2M6jOWDv7t9iMylAZqp9eJh9X2Xs3knlIoZC4Aqz5A6xDi8VWdRJs hamA== 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:mime-version:content-transfer-encoding; bh=/WKLM2C6qMLkIZKOBs5AbUjGucYg3UVIUWBwmAZ7iUc=; b=kL8zKMXbiCXFnHxVdJgWarcTS1ozmZRibFjkRFa341NAMjZYjdQxAsqoZrX5I3NYKv infjhFYhKskmGiDhiiSJ5REAlKtlgeAQ4dFbT7g5SoPbY5ou88uZcF+X+KVuX5sbpaYn HB4vXFOw9rZywt5J50waJoeyd8X56ElY+VG+WHv5RBl5UQgpUebOOUmKR1apmB61d62o 1Z6LY2T7DwxdLEAAKCa2qKkJ56Tsjm77HLKilr12GyPeLyL1jrxJ4/MDBxwwWQHW39c/ AQAsXBEabh3geByDnWd0NGvUlmKJHaNZ7grvLswf9N/bL95QaXAbLeYy3snz+OU1Xj/Z 06fg== X-Gm-Message-State: AOAM532W8A9u5ub3TW0zSRWJJeNKSH3E7T5cgaMslMOfGFVoKxEHXxCW KpRc0xIHh/SxIRI1b0U9yyq/zxxLaEk= X-Google-Smtp-Source: ABdhPJzdWJOeVt2NnFUIqJVeC0IwlqmUQrdQ7+WxpK1CncZjlfcciQUOQi8k6pgy5SGrlbLulgrNYg== X-Received: by 2002:a1c:20ce:: with SMTP id g197mr5489188wmg.72.1601507923383; Wed, 30 Sep 2020 16:18:43 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:42 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 12/13] flex-array: warn on flexible array in nested aggregate types Date: Thu, 1 Oct 2020 01:18:27 +0200 Message-Id: <20200930231828.66751-13-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org A structure or a union containing another aggregate type containing, possibly recursively, a flexible array is quite error prone and make not much sense. So, add an option -Wflexible-array-nested to warn on such usage. Signed-off-by: Luc Van Oostenryck --- options.c | 2 ++ options.h | 1 + sparse.1 | 7 +++++++ symbol.c | 4 ++++ validation/flex-array-nested.c | 1 - 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/options.c b/options.c index a8129ac1e952..b46900b973a6 100644 --- a/options.c +++ b/options.c @@ -101,6 +101,7 @@ int Wdo_while = 0; int Wenum_mismatch = 1; int Wexternal_function_has_definition = 1; int Wflexible_array_array = 1; +int Wflexible_array_nested = 0; int Wflexible_array_sizeof = 0; int Wimplicit_int = 1; int Winit_cstring = 0; @@ -843,6 +844,7 @@ static const struct flag warnings[] = { { "enum-mismatch", &Wenum_mismatch }, { "external-function-has-definition", &Wexternal_function_has_definition }, { "flexible-array-array", &Wflexible_array_array }, + { "flexible-array-nested", &Wflexible_array_nested }, { "flexible-array-sizeof", &Wflexible_array_sizeof }, { "implicit-int", &Wimplicit_int }, { "init-cstring", &Winit_cstring }, diff --git a/options.h b/options.h index 7bcf925b6912..d23ed472eaac 100644 --- a/options.h +++ b/options.h @@ -100,6 +100,7 @@ extern int Wdo_while; extern int Wenum_mismatch; extern int Wexternal_function_has_definition; extern int Wflexible_array_array; +extern int Wflexible_array_nested; extern int Wflexible_array_sizeof; extern int Wimplicit_int; extern int Winit_cstring; diff --git a/sparse.1 b/sparse.1 index c1a74153c618..9b1a59c6b9d4 100644 --- a/sparse.1 +++ b/sparse.1 @@ -264,6 +264,13 @@ Sparse issues these warnings by default. To turn them off, use \fB-Wno-flexible-array-array\fR. . .TP +.B -Wflexible-array-nested +Warn about structures containing a flexible array being contained into +another structure, union or array. + +Sparse does not issue these warnings by default. +. +.TP .B -Wflexible-array-sizeof Warn about using the sizeof operator on a structure containing a flexible array, possibly recursively. diff --git a/symbol.c b/symbol.c index 02b9066e966a..a9f646eb053f 100644 --- a/symbol.c +++ b/symbol.c @@ -197,6 +197,10 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance info.max_align = member->ctype.alignment; } + if (has_flexible_array(member)) + info.has_flex_array = 1; + if (has_flexible_array(member) && Wflexible_array_nested) + warning(sym->pos, "nested flexible arrays"); fn(member, &info); } END_FOR_EACH_PTR(member); diff --git a/validation/flex-array-nested.c b/validation/flex-array-nested.c index 3503c329d7c3..63767683c56a 100644 --- a/validation/flex-array-nested.c +++ b/validation/flex-array-nested.c @@ -21,7 +21,6 @@ static int foo(struct s *s, union u *u) /* * check-name: flex-array-nested * check-command: sparse -Wflexible-array-nested $file - * check-known-to-fail * * check-error-start flex-array-nested.c:6:8: warning: nested flexible arrays From patchwork Wed Sep 30 23:18:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11810491 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C7E916CA for ; Wed, 30 Sep 2020 23:18:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A25B22075F for ; Wed, 30 Sep 2020 23:18:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WbbaRumx" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729912AbgI3XSv (ORCPT ); Wed, 30 Sep 2020 19:18:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732166AbgI3XSq (ORCPT ); Wed, 30 Sep 2020 19:18:46 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABF27C0613D2 for ; Wed, 30 Sep 2020 16:18:45 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id g4so3570997wrs.5 for ; Wed, 30 Sep 2020 16:18:45 -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 :mime-version:content-transfer-encoding; bh=uUHPvqTQBUJT7n0W3kOTdVjHQlKrWX1qMDvyZQ81Rbc=; b=WbbaRumxtyUz7LrxjfqtxUVTiJW4UwLne9zbUPK7c+5qlWFKVkoPy8b0QnOp+8Q4co 9yWYXZneewImDVEHrvXoU+NgHmVDKH0pL8ZzKoKOLYsBnSlIpqMyvvbcbTlvhazSGHrl JhNA5wMJPHFSY6njAoHyWxu6glVDp0Qv5al1/NzCy0P7qhe0pDT9xt544YnD7RUXDC8X z4q896sL9BTW5Xbzhuc7U8oTZZHpczzTiT+qn7otNg0B5niIaLytUbirQ35nFzRBc98+ xisNxz0aAG0TBpW1pmi2fYvkkhz6vUDTB4atxJB1Qrk1YcdN5BadJs3/hZk1Mo8soQRU uiBA== 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:mime-version:content-transfer-encoding; bh=uUHPvqTQBUJT7n0W3kOTdVjHQlKrWX1qMDvyZQ81Rbc=; b=LyHb1HB52tPLPKzPV3UVpxEuDUgOX8cBqLPI4UyVvRWFv3eQEHsCX5wVdK+8pR3blf 6x2cadeUcFhZONDecFwhoZktgfGxHvqnBqNR/tq70XTFXqEQ6ghmAVltBh+PXFbJapuq fCb++/U3xp1Dx20ggGf05PQW5AOaUTtpIqeY9RiN1VaTGIwcAc9U+OPcRPYu9rgU4tXt dfw2xbNM3S8gwSj4imtGABKv9AoAUHC1o28tSezGLzFCCDNKmDORCwkdWhALZiQ+0rK3 Qx0PCbzOIiuWE6w3YJjmw7m6+xw+zs2lcqoiGAO046lbJP7td7PLjXkRyP1FWIy6c184 fW2A== X-Gm-Message-State: AOAM531qqyb/s6AVPPbGngdUQSnttUCjSJ9KnZobvYl9SO5eYyNivBPQ qUlyCyD7EcVH9LHscaNlQQAALWoOAoE= X-Google-Smtp-Source: ABdhPJx6bBAAGaUUXBVGgW6QmOD8qbo87NufzLZN7t7F21XA3dKk+YKUKy1uN+QbORTOiTb0er4BPw== X-Received: by 2002:adf:f290:: with SMTP id k16mr5862638wro.124.1601507924172; Wed, 30 Sep 2020 16:18:44 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:d0c8:dbcc:51b0:75b9]) by smtp.gmail.com with ESMTPSA id l8sm5460065wrx.22.2020.09.30.16.18.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 16:18:43 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [PATCH 13/13] flex-array: warn when a flexible array member has some padding Date: Thu, 1 Oct 2020 01:18:28 +0200 Message-Id: <20200930231828.66751-14-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> References: <20200930231828.66751-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org If some padding is added because of the presence of a flexible array member, the size of the structure will be greater than the offset of this flexible array which can cause some problems if the assumption is made that these 2 size must be identical (which is easy to do since such flexible arrays are conceptually 'after' the structure itself). Signed-off-by: Luc Van Oostenryck --- options.c | 2 ++ options.h | 1 + sparse.1 | 6 ++++++ symbol.c | 2 ++ validation/flex-array-padding.c | 1 - 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/options.c b/options.c index b46900b973a6..92d64e6ab64e 100644 --- a/options.c +++ b/options.c @@ -102,6 +102,7 @@ int Wenum_mismatch = 1; int Wexternal_function_has_definition = 1; int Wflexible_array_array = 1; int Wflexible_array_nested = 0; +int Wflexible_array_padding = 0; int Wflexible_array_sizeof = 0; int Wimplicit_int = 1; int Winit_cstring = 0; @@ -845,6 +846,7 @@ static const struct flag warnings[] = { { "external-function-has-definition", &Wexternal_function_has_definition }, { "flexible-array-array", &Wflexible_array_array }, { "flexible-array-nested", &Wflexible_array_nested }, + { "flexible-array-padding", &Wflexible_array_padding }, { "flexible-array-sizeof", &Wflexible_array_sizeof }, { "implicit-int", &Wimplicit_int }, { "init-cstring", &Winit_cstring }, diff --git a/options.h b/options.h index d23ed472eaac..31d9c5859977 100644 --- a/options.h +++ b/options.h @@ -101,6 +101,7 @@ extern int Wenum_mismatch; extern int Wexternal_function_has_definition; extern int Wflexible_array_array; extern int Wflexible_array_nested; +extern int Wflexible_array_padding; extern int Wflexible_array_sizeof; extern int Wimplicit_int; extern int Winit_cstring; diff --git a/sparse.1 b/sparse.1 index 9b1a59c6b9d4..8c61e869dc57 100644 --- a/sparse.1 +++ b/sparse.1 @@ -268,6 +268,12 @@ Sparse issues these warnings by default. To turn them off, use Warn about structures containing a flexible array being contained into another structure, union or array. +Sparse does not issue these warnings by default. +. +.TP +.B -Wflexible-array-padding +Warn about padding alignments caused by the presence of a flexible array member. + Sparse does not issue these warnings by default. . .TP diff --git a/symbol.c b/symbol.c index a9f646eb053f..eabb2226651f 100644 --- a/symbol.c +++ b/symbol.c @@ -213,6 +213,8 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance } if (info.flex_array) { info.has_flex_array = 1; + if (sym->offset != bits_to_bytes(bit_size) && Wflexible_array_padding) + warning(info.flex_array->pos, "flexible array member has padding"); } if (info.has_flex_array) sym->has_flex_array = 1; diff --git a/validation/flex-array-padding.c b/validation/flex-array-padding.c index 2ba77971266e..95b349e1199a 100644 --- a/validation/flex-array-padding.c +++ b/validation/flex-array-padding.c @@ -12,7 +12,6 @@ static int foo(struct s *s) /* * check-name: flex-array-padding * check-command: test-linearize -Wflexible-array-padding $file - * check-known-to-fail * * check-output-ignore *