From nobody Thu Feb 12 09:34:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1146CD37B5 for ; Sat, 16 Sep 2023 06:21:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238738AbjIPGVJ (ORCPT ); Sat, 16 Sep 2023 02:21:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237972AbjIPGUh (ORCPT ); Sat, 16 Sep 2023 02:20:37 -0400 Received: from smtp.smtpout.orange.fr (smtp-26.smtpout.orange.fr [80.12.242.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 516D919A0 for ; Fri, 15 Sep 2023 23:20:31 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id hOfKq4neiXyjrhOfKqtMkJ; Sat, 16 Sep 2023 08:20:29 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1694845229; bh=NpZEkdpmUURyXQQ8IOswWsw2MID9l3LvCoklUVqRxWQ=; h=From:To:Cc:Subject:Date; b=DOWhnNS+NJtCPbiE9W4YqtuJXUQKSvgaUxKLcg8+zkJWr2r+Z/rUpoq5RDAYfTVV8 ylMh7+iiPQ4YJzfK6kILGUA9tJckMqWCqgvHw6IQsKehfjZBEeTqKLEp7cz2uIb+LZ 1YsU5EKs86qNi/jfn8UhsWOGhRkCwHz6cqtxiPyjgiXxMv8dzrhK4ANni96I+MqC5B yP1NSJfc78JtnzGfX2vzL9lg1DJZwW/8slSB3m/24uMERUg0BDSa0MSR/khvQ9IC27 PXpRH76C0gJnZgsFcntRGy3qY8UqpRW2+6LXOvx3zSNFmLlKrOEztHD4gnJGCdtWAg +cZxGPc7s8zdQ== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 16 Sep 2023 08:20:29 +0200 X-ME-IP: 86.243.2.178 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] bcachefs: Avoid a potential useless over memory allocation in bch2_prt_vprintf() Date: Sat, 16 Sep 2023 08:20:24 +0200 Message-Id: <0f40108bed3e084057223bdbe32c4b37f8500ff3.1694845203.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" printbuf_remaining() returns the number of characters we can print to the output buffer - i.e. excluding the terminating null. vsnprintf() takes the size of the buffer, including the trailing null space. It is truncated if the returned value is greater than or equal to the size of the buffer. Knowing all that, buffer sizes and overflow checks can be fixed in order to potentially avoid a useless memory over-allocation. Signed-off-by: Christophe JAILLET --- Un-tested --- 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 de41f9a14492..77bee9060bfe 100644 --- a/fs/bcachefs/printbuf.c +++ b/fs/bcachefs/printbuf.c @@ -54,8 +54,9 @@ void bch2_prt_vprintf(struct printbuf *out, const char *f= mt, va_list args) va_list args2; =20 va_copy(args2, args); - len =3D vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, arg= s2); - } while (len + 1 >=3D printbuf_remaining(out) && + len =3D vsnprintf(out->buf + out->pos, printbuf_remaining(out) + 1, + fmt, args2); + } while (len >=3D printbuf_remaining(out) + 1 && !bch2_printbuf_make_room(out, len + 1)); =20 len =3D min_t(size_t, len, @@ -70,9 +71,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(out) + 1, + fmt, args); va_end(args); - } while (len + 1 >=3D printbuf_remaining(out) && + } while (len >=3D printbuf_remaining(out) + 1 && !bch2_printbuf_make_room(out, len + 1)); =20 len =3D min_t(size_t, len, --=20 2.34.1