Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp918439rdb; Sun, 18 Feb 2024 19:43:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVnvYtbyZcL1sEB5CyY7llhg1GSdk3GAJAG9JaNom6KqZAtApmlLRUHYkWHykqOUWnuGkGAlHXXAJsTcY7NEaa+vT6+xekMOUTkTrew8g== X-Google-Smtp-Source: AGHT+IFDcD2Q/MSnZ/4pLslMHu38o2FkZdzBFEzAIw3NJPQ6tACFfH4TuHaZHrUv+ynBO3T+I7NM X-Received: by 2002:aca:1a0c:0:b0:3bf:f452:651a with SMTP id a12-20020aca1a0c000000b003bff452651amr10791678oia.52.1708314187469; Sun, 18 Feb 2024 19:43:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708314187; cv=pass; d=google.com; s=arc-20160816; b=IP/0SycVje4DxLL+Y1NjoSLt9DIv7IMvC55vNehsvaBpBUFn+fCHyC5tn4tAYaiWzo g2fjFvYBcCwzbQkNViVlF1SG12//lFwLHJlHEDLxxrDUyt95w8Np7/JLwhdgZHv+Yfk/ JYZmm2FrNJVTZOiaQ0L5HDJgLUNu6dc6HDpFu+UWas7NphbO3VLHGQb/26lzBQWNtdcv ufzKeSPPyVCGNht0xcnePLTQ/eyf6IrVURuPPie6DUJ1t3d7jxoVL0SEVlUQ6Bysb86S VwWKBrbO86F2tIPiXxWGUQA7sNFRi459wBYgcqbcjnBl/EvInh33+JXTxPibXKn/pr5Q xOvg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=fMBwARP4Q4ySTt1H8X84ni9Xt4iLGGrdNJSUec9FL+w=; fh=QlPQTDiUXlEBUVsLubp5sa1HfmxIc7/7f2wx9fAzjEI=; b=mO43VEeRkYvb+iKmYG0UTDyAP3BBL3H9s+jyCR/AhIPvU2GiBop4KVrVHtkn3/e3ia H80ripn8lFweW5qbsyKl4HG+zZLidAyDNutXI57W8KpKY0zMaTAMVS8LJS9ImqXBrP9w 4CKdUV0FZWYXASOyMEsfZhfpu8XimE2jzhvvEHjb4B3gXrJWgbndrbeG+d8rvb58skuT XDSKG/6u5YYgAOJs6yF2KawElV65PbfV4Q2ESAoTzJqjZ9ZQJBFijA1KqkSahrb+5xfi ZqUBASqlWHGDUvt2VTIiihghJrbZZkOpvmqcrzMycSXm7io7Ujnw/7Fe4rXic8SCrJyz uaDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=jNpxQQUQ; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-70682-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70682-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ll9-20020a056a00728900b006de1e26adb5si3690419pfb.275.2024.02.18.19.43.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 19:43:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70682-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=jNpxQQUQ; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-70682-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70682-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 35F83B21618 for ; Mon, 19 Feb 2024 03:43:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE8B0442D; Mon, 19 Feb 2024 03:42:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="jNpxQQUQ" Received: from out-185.mta1.migadu.com (out-185.mta1.migadu.com [95.215.58.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 886E95220 for ; Mon, 19 Feb 2024 03:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.185 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708314178; cv=none; b=AAjMTp5imfeOXjt8P4cLUuYTLwl40BFyDclrwXoWkafBzBZwlAMcArWeEar8sNtsPLJHTZdHzyYyHSz5oWdhtYEGclggJ6LZSH5RtWM1T9ScdrKyD0CEee7T90ObqRuHjfcZPS1M4YhdmYyqFGkA46puZilThiAIRXXte+EAn9Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708314178; c=relaxed/simple; bh=roIxuG/Ac9DQVq9EUiZ8Mru2gszw1mNDmxOgzDSP9QU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jXQzFmLdc+e5DJrjqNiafbWaWIjHLrue0RWX3no0AAu9O1KV6X/pt3TYHutDjFg0ax0q3Mm8NVKurTyRKkAZ92AjK5g4TWzn2RXzMnN+0+4j7u0DCBE/l201uvfus7cgE80c9SVk8sKC4UvKqI5hmx7lprGL3iejBE5RGZWqbog= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jNpxQQUQ; arc=none smtp.client-ip=95.215.58.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Sun, 18 Feb 2024 22:42:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1708314174; h=from:from: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=fMBwARP4Q4ySTt1H8X84ni9Xt4iLGGrdNJSUec9FL+w=; b=jNpxQQUQKEVNBEBOdIDkyxwFAIIM9CZNvveIFMdOMcaqnlnjV4FoVu+Omcf3UXyxLyCcde AMUmEAJNsNPNND7LAkSchxqb+XkajW8hp/69uVAHTZ5+xP8gohWMjkdUu9RMoiKNcUXOsN Ad9lqXrQUHAV6Al7D+AKg+SS5YOYAWk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Christophe JAILLET Cc: Brian Foster , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, linux-bcachefs@vger.kernel.org Subject: Re: [PATCH v2] bcachefs: Avoid a potential useless over memory allocation in bch2_prt_[v]printf() Message-ID: References: <4c614db674887346ea418acaeafd6bf86502ec77.1708272713.git.christophe.jaillet@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4c614db674887346ea418acaeafd6bf86502ec77.1708272713.git.christophe.jaillet@wanadoo.fr> X-Migadu-Flow: FLOW_OUT On Sun, Feb 18, 2024 at 05:12:28PM +0100, Christophe JAILLET wrote: > 2 issues related to incorrect available space in the output buffer > computation may lead to some extra memory allocation. > > > First: vsnprintf() takes the size of the buffer, *including* the space for > the trailing null. > > But printbuf_remaining() returns the number of characters we can print > to the output buffer, *excluding* the terminating null. > > So, use printbuf_remaining_size(), which includes the space for the > terminating null. > > > Second: if the return value of vsnprintf() is greater than or equal to the > passed size, the resulting string is truncated. > So, in order to see if some extra space is needed, the check needs to be > changed. btw, the patch was suspect to begin with in cases where off-by-one errors are difficult to avoid, but harmless in one direction - just over allocate.