Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1380902rdb; Mon, 19 Feb 2024 13:42:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW07L1TkiXi0AuyIULmtLPCf3h4c6X5ALXZi9NO3xmWMuvNXbse91k1GA00xLVrSULZdfmmc6uiwmYzo5ffsrL2PdH4OzNtcfnwYd+Tag== X-Google-Smtp-Source: AGHT+IFP8ZZc9ccdimsqJ9wIhxENCy5i+hlb9wy7jwEVIi3SN3mE6ahV+XdSsMGNjWbKbmzpPfS4 X-Received: by 2002:a17:906:2a91:b0:a3d:7532:15ad with SMTP id l17-20020a1709062a9100b00a3d753215admr8770649eje.39.1708378938131; Mon, 19 Feb 2024 13:42:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708378938; cv=pass; d=google.com; s=arc-20160816; b=GqEAeqiInnjp1kU2XM56p7mAfbn6xLa4SJnub+3GRip+kRZRGohGy0vasCLzrtJ+KH 4TF3jC4yU/Zvf1nj+zxjlLLZRWcYga6dODVrZonNn7fSDjbEUQaB3eGAmLxR1ZWMhJdi MMqwPctKEakPCSfnzAr/CSwCPHWd/6WijOM1znQR5qnmN36hofgBSaEgji1wSiOG9xFI n35Up4dIEqPOpi2JT///QWn4YQLa56QUTXyyMmRO9RbeEL0bHgGTtOn/jwYmisWqJSTc iVbJFXcO++MwIHc619XRx46WyGkcy4Qe1SiNlznOF0BpbdRPzx2z/roROGTVOI5LQn6h tz9w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:dkim-signature:date; bh=nHWVuGms67eXElpk+GQbCw/8AGqs4biTimQboa9uwN4=; fh=QlPQTDiUXlEBUVsLubp5sa1HfmxIc7/7f2wx9fAzjEI=; b=ZGTgICfxHx7qAEa5wQElBnmoDy3N0GQJtKmtnQMg1lXWH/NbVo5kjHj/KhnU/rlpIj NjRzJdNDpsMpy5cuyrPlmUoFpxD9Emp1uzHc9aQJiZthJ4sgM8gSTqzm4B5sD3gXWCbk 0TeqCN9u6OzkCQNnvxXp4o9tQBLcASA+ipKhJnPMVb13af35GpWq2c2xmMZ7QW/cPAdc UdCFoAG72NgNzv2i1NScmGC1k0UQ3UVdN7m6UOko6y3gd+oAS2oCIvJaYnoRlXUhdt+r cwoysU30sQ0RrxgOV+Jshqw6W9kuqKKpOe4j73TJlW7mhCcvhTAIoVqSurEmJa703nA+ EM9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CEfenD8+; 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-72012-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72012-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id l13-20020a1709065a8d00b00a3ea0f29aa3si1022912ejq.582.2024.02.19.13.42.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 13:42:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72012-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CEfenD8+; 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-72012-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72012-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 0AD931F22703 for ; Mon, 19 Feb 2024 21:36:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D9DA54747; Mon, 19 Feb 2024 21:36:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="CEfenD8+" Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) (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 9C7DD53E16 for ; Mon, 19 Feb 2024 21:36:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.186 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708378578; cv=none; b=H9iW9EpayiOplZg4qnZNvXwa/tHGu8bjOYsI2sUcnatjAnYeMekylLRlaeu8D8EENjsNace5j8OwAyo6cBbzSd143rTjRCfrER9MMOcZhl9W3K09wlb9JGa5wafXczm/Q1fyI2ZAVfhZ1rWOfJVqS0C4Wn9acBI0GzRn2UMNAnc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708378578; c=relaxed/simple; bh=m9hU62GZDlzRYkm5TflwXBi9gQJhj+hqg7sdx9BPbQs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cfqf7QHRekttEJX+ZWPElTbHnwU8+oi3tay/r11zr4D1e0vVFwPvkpmeCf500DJXqVyaw2h9RS2b4h/AOgwltqt6rtQW7ep8n6F+fcu+wE3ehlviYNIBNbmKQ370TGhij2UkCD/avP64WAWU363Sw8omzybMQdhK7TmW/Dye+Ms= 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=CEfenD8+; arc=none smtp.client-ip=95.215.58.186 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: Mon, 19 Feb 2024 16:36:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1708378573; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nHWVuGms67eXElpk+GQbCw/8AGqs4biTimQboa9uwN4=; b=CEfenD8+3EJDfCF8/kmLMYKXU6y35aU46WIWwSJJB356+dXfd/9gbPuyr240gOR4UaCLWG D71lvlpBHmfECRSHcvmmRB9oo/r/LA4JYFB9GKvYtPeBwKAtm2Vm3vA7kRSO6M82aN3fFD KiS4pObDhkHQIzhSjmNJbWzugkqSAx8= 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Migadu-Flow: FLOW_OUT On Mon, Feb 19, 2024 at 07:10:10PM +0100, Christophe JAILLET wrote: > Le 19/02/2024 à 04:39, Kent Overstreet a écrit : > > 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. > > > > nope, buggy. > > > > passing printbuf_remaining_size() to snprintf() is correct, but > > snprintf() returns the number of charecters wrote _excluding_ the null > > Hi, > > I think that the patch is correct. You didn't test it, I did. In the future, no more patches that you haven't tested - thanks. > snprintf() returns the number of characters wrote _excluding_ the null. That > is why the test is: len >= size_of_the_buffer ==> more space is needed. > The case you describe is handled by the == part of >=. > > On the contrary, if len is < size_of_the_buffer, then we have at least 1 > place for the terminating NULL. It will then eventually lead to: > len_of_the_string + space_for_'\0' == size_of_the_buffer > So it does not overflow. > > > Anyway, feel free to ignore the patch completely if it sounds too risky, > take only half of it (s/printbuf_remaining/printbuf_remaining_size/) if you > are more confident with it, or the complete patch if the explanation above > convinced you. > > From my PoV, the 3 options lead to the same run-time output, with more or > less memory allocated. > > Best regards. > > CJ > > > > > > > > > > > > > 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. > > > > > > Signed-off-by: Christophe JAILLET > > > --- > > > Un-tested > > > > > > v2: - Use printbuf_remaining_size() instead of hand-writing it. [Brian Foster] > > > - Reword the commit log, hoping it is clearer. > > > - Synch with -next-20240215 > > > > > > v1: https://lore.kernel.org/all/0f40108bed3e084057223bdbe32c4b37f8500ff3.1694845203.git.christophe.jaillet@wanadoo.fr/ > > > --- > > > fs/bcachefs/printbuf.c | 10 ++++++---- > > > 1 file changed, 6 insertions(+), 4 deletions(-) > > > > > > diff --git a/fs/bcachefs/printbuf.c b/fs/bcachefs/printbuf.c > > > index b27d22925929..8cee9c2f88c4 100644 > > > --- a/fs/bcachefs/printbuf.c > > > +++ b/fs/bcachefs/printbuf.c > > > @@ -55,9 +55,10 @@ void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args) > > > va_list args2; > > > va_copy(args2, args); > > > - len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args2); > > > + len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), > > > + fmt, args2); > > > va_end(args2); > > > - } while (len + 1 >= printbuf_remaining(out) && > > > + } while (len >= printbuf_remaining_size(out) && > > > !bch2_printbuf_make_room(out, len + 1)); > > > len = min_t(size_t, len, > > > @@ -72,9 +73,10 @@ void bch2_prt_printf(struct printbuf *out, const char *fmt, ...) > > > do { > > > va_start(args, fmt); > > > - len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args); > > > + len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), > > > + fmt, args); > > > va_end(args); > > > - } while (len + 1 >= printbuf_remaining(out) && > > > + } while (len >= printbuf_remaining_size(out) && > > > !bch2_printbuf_make_room(out, len + 1)); > > > len = min_t(size_t, len, > > > -- > > > 2.43.2 > > > > > > > >