Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp130998pxb; Mon, 31 Jan 2022 17:17:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYaptS4pIYmNyVGrzKZqk3KRK6uXmvPb5SED/NiUjQXneF7dxm48LjyiWmDHnkVJ7egHlT X-Received: by 2002:a17:907:1c0b:: with SMTP id nc11mr18849548ejc.665.1643678224934; Mon, 31 Jan 2022 17:17:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643678224; cv=none; d=google.com; s=arc-20160816; b=l9MvoJ2kmCe6x6AtshYx4Lqagfe2wPjS2sbomIAmyxHsgdqszsDKVd8dcT78DD/Qtz wY3l/dXALwv3a7dfJqgccIRHy+z+NHRc9upKn/Ci0q60bFfDs5MZLn5Cyk/58Ya3Kuh4 dCUWRPpuuT7p+wSs2TGCt9Mx+/3zb54dxl8pn8mMNSpQjzVuglZmLcXtq87YpgQirIux ZZt05la1zID47UZu1Ij8WUbrZjKNb1II+Qv+n/1JyWn/dCO71jeX8mY3hF83WXpYYwcd 3wPJ6jtoXNJ9sslKtpFTYjBN2VjuoWTWas2liig+yTGfYzO0asnpMG3ho84TWaHHi6Ov nDTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=cr+ebJaUo9nx6h/WUxTauaoEwHuILetaAEgQRvXMOro=; b=HT9I+ISDY78//xJVkSK1XnVlG/nFJc+Nef/yaeKcXTYSuwamCGFWDf0y35+SJrcl0Y ERp48JqD9q644dIIiib88D3F09f0i6a0QsDS5j+h/NXxhY8l24z+DQzQ0y9ex0g4ZMfD csIm1encuLT5IT+h2kyEejMkx1zUbWhQmukzAs+9Dxqu6QH3n1tRT3lutJIXYfe/GrUX w2SwndZK1KGGN4FCVnrRwkCdCg6AAZdF+a31h8efIdFyQvdnhBlV19ScNWtFvZgizp65 UfFy3vcnjUE4NcpxqV06nUvUP2ZFGDSPVyVIsr5jLEnTkjFOFHvPWo6xWKrfm7KbEeMV vtkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dRA62j4z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jg5si10358544ejc.569.2022.01.31.17.16.40; Mon, 31 Jan 2022 17:17:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dRA62j4z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231860AbiA2Ix7 (ORCPT + 99 others); Sat, 29 Jan 2022 03:53:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57353 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231802AbiA2Ix4 (ORCPT ); Sat, 29 Jan 2022 03:53:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643446436; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=cr+ebJaUo9nx6h/WUxTauaoEwHuILetaAEgQRvXMOro=; b=dRA62j4zXS43T6AoV1ygKJIp7AFF+cMudtWBB8RnBn4ed4aKhphwolp3JpoQc+UagOFzuV oBpwaQUlFnMSDyMNdRmNrGjeOd0jghBYoLVoB7GjPS637m9Sz5qwQIhOZQ/BQTYaVugtlo bJ/tZl8BO4BYrxE+0zqhEVGUzO83U14= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-546-fp6E1wwFPOyIn1D5-JEt7Q-1; Sat, 29 Jan 2022 03:53:54 -0500 X-MC-Unique: fp6E1wwFPOyIn1D5-JEt7Q-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4A2A28144E1; Sat, 29 Jan 2022 08:53:53 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D1EEA1086481; Sat, 29 Jan 2022 08:53:52 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 20T8rnxD3397221 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 29 Jan 2022 09:53:50 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20T8rm013397220; Sat, 29 Jan 2022 09:53:48 +0100 Date: Sat, 29 Jan 2022 09:53:48 +0100 From: Jakub Jelinek To: "Justin M. Forbes" Cc: linux-kernel@vger.kernel.org, jmforbes@linuxtx.org Subject: Re: [PATCH] Fix for gcc12 compile issues in ubcmd-util.h Message-ID: <20220129085348.GT2646553@tucnak> Reply-To: Jakub Jelinek References: <20220129010215.781601-1-jforbes@fedoraproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220129010215.781601-1-jforbes@fedoraproject.org> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 28, 2022 at 07:02:14PM -0600, Justin M. Forbes wrote: > While the current code builds fine with gcc 11, it does not with gcc 12, > resulting in: > > In file included from help.c:12: > In function 'xrealloc', > inlined from 'add_cmdname' at help.c:24:2: > subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free] > 56 | ret = realloc(ptr, size); > | ^~~~~~~~~~~~~~~~~~ > subcmd-util.h:52:21: note: call to 'realloc' here > 52 | void *ret = realloc(ptr, size); > | ^~~~~~~~~~~~~~~~~~ > subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free] > 58 | ret = realloc(ptr, 1); > | ^~~~~~~~~~~~~~~ > subcmd-util.h:52:21: note: call to 'realloc' here > 52 | void *ret = realloc(ptr, size); > | ^~~~~~~~~~~~~~~~~~ > > The was mentioned in upstream gcc bug > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104069 where it was > determined that gcc was correct and the kernel needed to change. This is due to the different behaviors of realloc when size is 0 as described in http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_400 and as the code explicitly tries to cope with !size, there is at least theoretical chance that it is called with size 0. In glibc/AIX, for non-NULL ptr realloc(ptr, 0) will free(ptr) and return NULL, so the code as written in that case will in void *ret = realloc(ptr, 0); if (!ret && !0) ret = realloc(ptr, 1); effectively free(ptr); ret = realloc(ptr, 1); and so try to reallocate freed memory. On BSD libcs it will work properly though. Jakub