Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp314595ybl; Wed, 22 Jan 2020 22:18:46 -0800 (PST) X-Google-Smtp-Source: APXvYqyvAYLPfDPPezBJJPl0dljwTNnoY/QGT97k9b8I3xgzI3/zVxn5Iq1H47uSGlMn5IPfE6QW X-Received: by 2002:aca:b70a:: with SMTP id h10mr3402377oif.20.1579760326389; Wed, 22 Jan 2020 22:18:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579760326; cv=none; d=google.com; s=arc-20160816; b=N+gDGQ4uQcWd/tc4osmUlzOE6C532D94bXAFQfypnpv3mnOqC/zIVaLcjhr0BieTpp f9mIgtDrRePPPD8xbt/ybuiSg1oNoej5b5m/bQNt+YZSTJUyCOUdtLIhZd8hdDwV+fut nKfY4RVOG+LDrSjW9aalGa7OPGcYiTW4eN4wlpN5xtSnpLhqNlgWMyW0dDIXnzDrrbug u5yO/FBiOn6AG6Dug2k+BNTSPC93eikgsnYRVHnv53zGBE8+kjBodKyJtsF1KE9Ny+Lb OrDXsJzUTGgGN7dP5GzOXlQV5b8sBk6G/xn0v0D9zDkvl8nC/UgobjU5WSwRjLZUwt2L 1Xug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=kfE35KBwl+2nFSjY/GRPMZyp+lnRwUJWyIthsUdFhdM=; b=T+UHOeTtqAZiwnE7zjXGA07nRBuSk0/5qHnMEriZjSr1zSJ7AeaiKiv/8ogUD1ac9T dntcaTNRRBrqyMksTIQXDHSF/GjYglqFuP+KjWRh7p+PO2WS+KEWrH/iaWxWIDJYAKSj vdwOemQ9PnMQRrZIQnHPdTaMkY3pB2ZmEupM1R6XxyMtesGVWCs76v9YRVW39AAPPKlY vtitOk6CQrTKuvzr+kfaGef2ksT/lUuhwaq01k5S0nZlet5ETsdMuYFpUpvmZUjSRs5b QyFQNQeFy+2VmHKr/Qe4QLTRl/CAqIp3rENwipT3TLVZHjY8Nn2ZSgAU9NrNoEU61JJL 0zQw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k10si399300oik.276.2020.01.22.22.18.33; Wed, 22 Jan 2020 22:18:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726026AbgAWGQf (ORCPT + 99 others); Thu, 23 Jan 2020 01:16:35 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:39297 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725818AbgAWGQe (ORCPT ); Thu, 23 Jan 2020 01:16:34 -0500 Received: by mail-wm1-f67.google.com with SMTP id 20so1219326wmj.4 for ; Wed, 22 Jan 2020 22:16:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=kfE35KBwl+2nFSjY/GRPMZyp+lnRwUJWyIthsUdFhdM=; b=PVcboj1FWamGFbuIzQlJXZj4oXDKehC0qYyWXlYMym1sIAhPqcEosRDDgz8qgDnT7w TdI/LWdeu76+2wPC9XethMXU/VJmtC9OjcfN1DvWzV76bAkqgzoXFNzijuEB0izEqP8M EUM7cMbjnKdLm83lbaFimHTw1aJ3WmWLMcPnEb8AWwZB8u6uXK1fsRzXD+zwYXoDoc8k npWdxRDMt1Hl+J8dZ+ZxVfTbrFEBOfx/tBXzQ7kzyiJ/nhEoJegPF1HQNZGnncJEZD3x bDfDE5ogaBt8wImJBCNuTVPiDL0n4pqaTyanpXw2ZdByie1VEVS+ShJHk3WJvnxLtr5I beKg== X-Gm-Message-State: APjAAAVneimg5cTQ1mGda0q3lexkgdshz6H11q83C8bX9AS9cJsZ5FYm VES8tD/Fl5Wnsajycb4mmOUrq9N9qhk= X-Received: by 2002:a1c:f210:: with SMTP id s16mr2120599wmc.57.1579760191372; Wed, 22 Jan 2020 22:16:31 -0800 (PST) Received: from ?IPv6:2a0b:e7c0:0:107::49? ([2a0b:e7c0:0:107::49]) by smtp.gmail.com with ESMTPSA id n3sm1626766wrs.8.2020.01.22.22.16.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Jan 2020 22:16:30 -0800 (PST) Subject: Re: [PATCH v2] tty: n_hdlc: Use flexible-array member and struct_size() helper To: "Gustavo A. R. Silva" , Greg Kroah-Hartman , Mikael Magnusson Cc: linux-kernel@vger.kernel.org References: <20200121172138.GA3162@embeddedor> From: Jiri Slaby Autocrypt: addr=jslaby@suse.com; prefer-encrypt=mutual; keydata= mQINBE6S54YBEACzzjLwDUbU5elY4GTg/NdotjA0jyyJtYI86wdKraekbNE0bC4zV+ryvH4j rrcDwGs6tFVrAHvdHeIdI07s1iIx5R/ndcHwt4fvI8CL5PzPmn5J+h0WERR5rFprRh6axhOk rSD5CwQl19fm4AJCS6A9GJtOoiLpWn2/IbogPc71jQVrupZYYx51rAaHZ0D2KYK/uhfc6neJ i0WqPlbtIlIrpvWxckucNu6ZwXjFY0f3qIRg3Vqh5QxPkojGsq9tXVFVLEkSVz6FoqCHrUTx wr+aw6qqQVgvT/McQtsI0S66uIkQjzPUrgAEtWUv76rM4ekqL9stHyvTGw0Fj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TWxrXdNq/pu0n7hA6Btx7NYjBnnD8 lV8Qlb0lencEUBXNFDmdWussMAlnxjmKhZyb30m1IgjFfG30UloZzUGCyLkr/53JMovAswmC IHNtXHwb58Ikn1i2U049aFso+WtDz4BjnYBqCL1Y2F7pd8l2HmDqm2I4gubffSaRHiBbqcSB lXIjJOrd6Q66u5+1Yv32qk/nOL542syYtFDH2J5wM2AWvfjZH1tMOVvVMu5Fv7+0n3x/9shY ivRypCapDfcWBGGsbX5eaXpRfInaMTGaU7wmWO44Z5diHpmQgTLOrN9/MEtdkK6OVhAMVenI w1UnZnA+ZfaZYShi5oFTQk3vAz7/NaA5/bNHCES4PcDZw7Y/GiIh/JQR8H1JKZ99or9LjFeg HrC8YQ1nzkeDfsLtYM11oC3peHa5AiXLmCuSC9ammQ3LhkfET6N42xTu2A== Message-ID: <31591fc2-7f4c-f73e-6041-0f9af698dbcd@suse.com> Date: Thu, 23 Jan 2020 07:16:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20200121172138.GA3162@embeddedor> Content-Type: text/plain; charset=iso-8859-2 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 21. 01. 20, 18:21, Gustavo A. R. Silva wrote: > Old code in the kernel uses 1-byte and 0-byte arrays to indicate the > presence of a "variable length array": > > struct something { > int length; > u8 data[1]; > }; > > struct something *instance; > > instance = kmalloc(sizeof(*instance) + size, GFP_KERNEL); > instance->length = size; > memcpy(instance->data, source, size); > > There is also 0-byte arrays. Both cases pose confusion for things like > sizeof(), CONFIG_FORTIFY_SOURCE, etc.[1] Instead, the preferred mechanism > to declare variable-length types such as the one above is a flexible array > member[2] which need to be the last member of a structure and empty-sized: > > struct something { > int stuff; > u8 data[]; > }; > > Also, by making use of the mechanism above, we will get a compiler warning > in case the flexible array does not occur last in the structure, which > will help us prevent some kind of undefined behavior bugs from being > inadvertenly introduced[3] to the codebase from now on. > > Lastly, make use of the struct_size() helper to safely calculate the > allocation size for instances of struct n_hdlc_buf and avoid any potential > type mistakes[4][5]. > > [1] https://github.com/KSPP/linux/issues/21 > [2] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html > [3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour") > [4] https://lore.kernel.org/lkml/60e14fb7-8596-e21c-f4be-546ce39e7bdb@embeddedor.com/ > [5] commit 553d66cb1e86 ("iommu/vt-d: Use struct_size() helper") > > Signed-off-by: Gustavo A. R. Silva Now it looks good to me: Reviewed-by: Jiri Slaby thanks, -- js suse labs