Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1848204iog; Tue, 14 Jun 2022 14:59:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1unGcwrQu2x8u9pTjhnICSF3gBQU75jDx5M6gVjfpjpWwrx43St+NPsAk3TNJYLioSGt1ly X-Received: by 2002:a17:903:234d:b0:167:4b11:aa7 with SMTP id c13-20020a170903234d00b001674b110aa7mr6379619plh.89.1655243977903; Tue, 14 Jun 2022 14:59:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655243977; cv=none; d=google.com; s=arc-20160816; b=ianUKMF7YEFCFPE5O/dwYFWZvurSVUQznRZsDgNzrUJ20zQvPus5u6oetyO5X6/MGt srmoSLp6NB5pzvDQi7uLRXSrqoh0bhXtKhw6oaHF5Js5QBCEGzs9dsuYWxI4DBeH4emv RUAUB1W2LvDY3bP9ZjL778Z9hsAZW0+V58TjGU/5U33Gjbey4YWns56ENKAMPrhBqVDm cFeb7cgPwAvMqo1pzvdK/keh6/WhmzIURYKFLPUHOZnMV5NjMoISilI/6ount29XTb9h RMUG473humC6hxlbXydDFtid4zobs0xjfLEq29t011qA8P0vW3Eo0UbjpnffXuerDiVr 4MDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=nzYZRa1evVe83xIVv0cSDShxcYiNafl9q4WtwoTq08g=; b=ua0bkrgJ3YkuH+0oAbHXuuFyqUAwKHoVyLPIiPk0Vr6MU95K2KmqllG5wJEGHtmSGH tKC6Tc6Nqd6uMsfJAjuejT+qn8JVgVuC+maYs2zFYaw30U2YumTxQg+dNDWSniOo2UBd kP48Z/bwDoNRowmNDuFBPAIRENVFmlVt0aWMTA+1hArzXHdSgl2TcTK09Jp+lOxL52V5 2oPZo/xbEh5ruqRpi2wTXnnuNo9q1awCahKDHJVCSOXKvzKZ+zZhwyEOWBtnB2934f86 oInCTS3X+R99d7TgPfrSHsR3OihljZ9wRtF1aGye4xwv4lEIp24in3QHFpqKazGivXb0 XtIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y10-20020a170903010a00b00158f80642a6si11974854plc.109.2022.06.14.14.59.26; Tue, 14 Jun 2022 14:59:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234727AbiFNVtR (ORCPT + 99 others); Tue, 14 Jun 2022 17:49:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358083AbiFNVtO (ORCPT ); Tue, 14 Jun 2022 17:49:14 -0400 Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 67D74515BA; Tue, 14 Jun 2022 14:49:13 -0700 (PDT) Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 25ELeePu014655; Tue, 14 Jun 2022 16:40:40 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 25ELeePL014654; Tue, 14 Jun 2022 16:40:40 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Tue, 14 Jun 2022 16:40:39 -0500 From: Segher Boessenkool To: Alexander Potapenko Cc: Linus Torvalds , Evgenii Stepanov , Kees Cook , Marco Elver , Nathan Chancellor , Nick Desaulniers , Thomas Gleixner , Vitaly Buka , Linux Kernel Mailing List , linux-toolchains Subject: Re: [PATCH] [RFC] Initialization of unused function parameters Message-ID: <20220614214039.GA25951@gate.crashing.org> References: <20220614144853.3693273-1-glider@google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi! On Tue, Jun 14, 2022 at 10:19:53PM +0200, Alexander Potapenko wrote: > ================ > char *kmalloc(int size); > > char *kmalloc_or_not(int flag, int size, char *p) { > if (flag) > return kmalloc(size); > else > return p; > } > > char global[16]; > > char *p(int flag) { > char *c; > int size; > if (flag) > return kmalloc_or_not(1, 4, c); > else > return kmalloc_or_not(0, size, global); > } > ================ Since C11, lvalue conversion of an automatic variable that does not have its address taken is explicitly undefined behaviour (6.3.2.1/2). So in function "p", both where "c" and where "size" are passed causes UB (so that executing "p" always causes UB btw). > In this example `size` is passed into kmalloc_or_not() initialized, > however it is never used, so the code probably has defined behavior. No such luck: the passing itself already causes UB. GCC does not warn, it has already optimised the code to what you expect by the time this warning is done. If you use -fno-inline it does warn for both "c" and "size" (via -Wmaybe-uninitialized). But it is still UB! All bets are off, no compiler can do any correct translation of your program, since there *is none*. Segher