From nobody Mon Feb 9 02:08:05 2026 Received: from smtp.smtpout.orange.fr (smtp-15.smtpout.orange.fr [80.12.242.15]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 20E886D1BE for ; Sun, 18 Feb 2024 16:13:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.12.242.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708272830; cv=none; b=k845zrWG7Q8UHz4a4vUGG1YKTlk9mZsaw4BEXyzPSZuhef3QVhOdpNlpmTE5MyeVFFzsJmK+QeCNVIIAhTNF47Wql9NxrH/8hZYQT1HZJPGLw4eJUp7+PV1JlE/9hJ62OuvHtwsiQuomElrUkNcU4Y0GDPjTzVovLZDd0uLSlBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708272830; c=relaxed/simple; bh=bbVXN7nFPxD2+8T/Qp3xY6pFsgPWqO0g3GtqcUTohS4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=SLtOjaCZfvf9wLlMfpjwjNhr38t6O4EpkF/0cAd40vAvfBEojEZdApD5wUlqM0i3Nz9Vnk188x9yI5Nx3M+VYAv5UZP/78ztafZwm4VODL19gsbJYmZc15OPeoGEBBpz9uV2QqSKmK8bNYi7j/+zlScjS/5sncnC9z1h7HV060k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=wanadoo.fr; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b=JHaOFZbA; arc=none smtp.client-ip=80.12.242.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="JHaOFZbA" Received: from fedora.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id bjmLrpU1LVqNHbjmLrVuBk; Sun, 18 Feb 2024 17:12:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1708272754; bh=yP76YXZufoYcbf/1hIaf1uWL8S4sDWOfmF4KUI6YdvU=; h=From:To:Cc:Subject:Date; b=JHaOFZbAvu0zG1EvsB/x423fdxAWjYdx+Flpo+0ruoCfh8f0EnbAsRDDWzio52hMD gqImRib0PIjw31bAQaABDabNQjHimRkR1HYvo1VOtSQkqpSHWXo7t9sFmscYKOBZP5 9mtdG71zMCFgwKWJEj687d9tdx4+uqqidiCAa0SCrczJ8e9oI+gVPA/zQTg9XGfU64 bTOlvP3vYm3zvCE3LqOCH1LLeYL+Wh8iZXftj8dNT+YUQO36EHtMhGMGFgQeaOZ6w7 MmQClqXECsTRopQXvfGmb4gNNJM5d4g8Lr4eBtD2GFzTxL9YCvMpDEcSxWfksBW3V4 e7Z5HvIfNy0GQ== X-ME-Helo: fedora.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 18 Feb 2024 17:12:34 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Kent Overstreet , Brian Foster Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-bcachefs@vger.kernel.org Subject: [PATCH v2] bcachefs: Avoid a potential useless over memory allocation in bch2_prt_[v]printf() Date: Sun, 18 Feb 2024 17:12:28 +0100 Message-ID: <4c614db674887346ea418acaeafd6bf86502ec77.1708272713.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.43.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Christophe JAILLET --- Un-tested v2: - Use printbuf_remaining_size() instead of hand-writing it. [Brian Fos= ter] - Reword the commit log, hoping it is clearer. - Synch with -next-20240215 v1: https://lore.kernel.org/all/0f40108bed3e084057223bdbe32c4b37f8500ff3.16= 94845203.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; =20 va_copy(args2, args); - len =3D vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, arg= s2); + len =3D vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), + fmt, args2); va_end(args2); - } while (len + 1 >=3D printbuf_remaining(out) && + } while (len >=3D printbuf_remaining_size(out) && !bch2_printbuf_make_room(out, len + 1)); =20 len =3D min_t(size_t, len, @@ -72,9 +73,10 @@ void bch2_prt_printf(struct printbuf *out, const char *f= mt, ...) =20 do { va_start(args, fmt); - len =3D vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, arg= s); + len =3D vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), + fmt, args); va_end(args); - } while (len + 1 >=3D printbuf_remaining(out) && + } while (len >=3D printbuf_remaining_size(out) && !bch2_printbuf_make_room(out, len + 1)); =20 len =3D min_t(size_t, len, --=20 2.43.2