Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp711907rdb; Sun, 18 Feb 2024 08:14:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVXgIRmIYgaL4cvQddcZ6YFBE8ZgeuMd5QSSTCqs9MR8lfddhq29CE9b+ia04/olFZTedTjcT6qupdT3QZgqy4oK1Hw6YG2fVPUY80n+w== X-Google-Smtp-Source: AGHT+IHn//922AWL+c7R4EqT4q97271EgJW1k7M9U/vlTx6ki3JaTASMfWnIgPqf4sTXnhTUde2x X-Received: by 2002:a05:6a21:1799:b0:19e:bf18:61fe with SMTP id nx25-20020a056a21179900b0019ebf1861femr12763265pzb.11.1708272841740; Sun, 18 Feb 2024 08:14:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708272841; cv=pass; d=google.com; s=arc-20160816; b=Dlc7OHmbEaCOyGyDOKdzzLi5R7rjPNy9fiZ3LyGIrqzl8GXgqkT4SBX3/j87jW2l4Q 5zOIwOSV65ylQTlvJL4+ay+MC1cig+FK+nINyzZsAwPCrmLJvc/GoVyCsGs2KUAiYFwP h8lspKkudohKLR5krhTIqfDeHOnhdHKKed7eaXzq2nOubEXRZkNawoZMWf7OL6LfODIJ 63NpmdXlqwzqDkJ87f5/Yq7wk+UGaWeXBpIRaA5A8XGtZaArCsQTMGkOBq7H34kvei/3 Gza8KzEbfzRJfJDE+BqIpFGNdDKXjDuouEL7kmrwrqs+zlTH9gONvcpwT6MGGU+V/VAN 1xmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=yP76YXZufoYcbf/1hIaf1uWL8S4sDWOfmF4KUI6YdvU=; fh=m3UyueLH4KDy0H9ZPaSnoQRYWYiD76Xl1Jq/Gorb8Ss=; b=0DDM2KYznf4ylRf2sBMKBG2ZYNWs5YkBXSacmp8VK/8eaiXErCBkoB0KA3eEw39B1R ohnsalPp/r44hhwduoKPVqKpbDdC0MSauLUGPgc4b7i54+FNLdJ63m+1T6jXAClWpa85 r/MwxsFE42xTzzRnkxguaa5NoX25JyZAIaJ1HbkQ1Zy4D5jfpvrfh8gAqdPj8/CaM56E dlfxev8JGWH+RX0CbLKMa5VmNZ+YyWTpY406F9intedzfm7YFtQAHJAMY8WLKXklj36d 2SfiI1Dxz6blRhOjJjI+7X7XomYCloqYjUVdyVRaPZjO61sU2yiGus25iMPq+L66laC0 eegw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=JHaOFZbA; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-70438-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70438-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id t191-20020a6381c8000000b005dc8a58a853si3028335pgd.525.2024.02.18.08.14.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 08:14:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70438-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=JHaOFZbA; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-70438-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70438-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr 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 0E7B4B20D6A for ; Sun, 18 Feb 2024 16:13:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A158B6D1C7; Sun, 18 Feb 2024 16:13:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="JHaOFZbA" 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 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: 8bit 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 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