Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp5210086rdb; Sat, 16 Sep 2023 07:18:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGo6jnCFgnLWgXrc4bVYu3TFvVtKkmlFSRgSTMXGqWJ6TyeWjdsuEXCfSqyoD+T3heNn7W5 X-Received: by 2002:a05:6e02:1105:b0:34f:1ee7:2ad4 with SMTP id u5-20020a056e02110500b0034f1ee72ad4mr5645503ilk.15.1694873899185; Sat, 16 Sep 2023 07:18:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694873899; cv=none; d=google.com; s=arc-20160816; b=WUoP953vlnr9eIw/hR3vgSoGe9AGTdS/xsButzGpYX8i3tJ/CZrk2/TjzECAr1vEL5 qBRYLI454REPZy/Zc0wDGAYshmy1/rWJpgFe6pyDVI+USNs0X46Wv7Mt3c2tSlhDgEZK iAp2I3DEer4mbgp5cjWL9hf3rfaq8mMfbMy2E/ztTvVomSsYIwVuU0xovTuywSdhnP2N v9S/eddWiXgzpfRdeaqxEute4udUU4uvJdd7Q4t1xJPYXwN0p5nNPvdy57VCzf0mNSyB DIye/cbS9a/rCAjyBZ7BT7QlXanA5xMPc3apVzGZogB4FRnfMuDC5QqIa/xreLqWsOMg DrXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=NpZEkdpmUURyXQQ8IOswWsw2MID9l3LvCoklUVqRxWQ=; fh=m3UyueLH4KDy0H9ZPaSnoQRYWYiD76Xl1Jq/Gorb8Ss=; b=ZDgZxgkkJr41NNErtzzCZSyNGPJEEBa/rtsXoSalwbBi47centRtDBtsO2yObbfbLK YjZkayzniWgB0qYJa19rWzwxiknHIBorw3azwPeLCHJLn/JCH2FJl0w4tY9Ev6+JZPOh kFy1tE2WTXFmgqoGW33VS7hIm2i2wojnc4Dep7y/RBicYMiyo0mqH4NbA0oRwiXIOaIf kaFeJbeYrtq9LFQotN5KnxA08DkDM8MWO1llc/kzggxpr1rLfTUJMRISqlWMNyJN0j20 g7oM/kSXfVcNmS2GNEd4ZhyWkUuw8bXkCgtXPOJi2sWi2Msrcl35w2wnlwrvWTYeta/1 3Caw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=DOWhnNS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id l19-20020a656813000000b005777125ed83si4942575pgt.238.2023.09.16.07.18.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 07:18:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=DOWhnNS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 34B4A849FA66; Fri, 15 Sep 2023 23:21:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238294AbjIPGVI (ORCPT + 99 others); Sat, 16 Sep 2023 02:21:08 -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: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Fri, 15 Sep 2023 23:21:41 -0700 (PDT) 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 *fmt, va_list args) va_list args2; va_copy(args2, args); - len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args2); - } while (len + 1 >= printbuf_remaining(out) && + len = vsnprintf(out->buf + out->pos, printbuf_remaining(out) + 1, + fmt, args2); + } while (len >= printbuf_remaining(out) + 1 && !bch2_printbuf_make_room(out, len + 1)); len = min_t(size_t, len, @@ -70,9 +71,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(out) + 1, + fmt, args); va_end(args); - } while (len + 1 >= printbuf_remaining(out) && + } while (len >= printbuf_remaining(out) + 1 && !bch2_printbuf_make_room(out, len + 1)); len = min_t(size_t, len, -- 2.34.1