Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp907402rwb; Thu, 10 Nov 2022 08:47:17 -0800 (PST) X-Google-Smtp-Source: AMsMyM4szhHf11gR6W/l5zMKQrswBY/vtQ0PNnfkUtHokk9x6I7xESS1621VFjQ1ir1O3q1RcVIq X-Received: by 2002:a17:906:9be5:b0:7ae:3684:2118 with SMTP id de37-20020a1709069be500b007ae36842118mr3025680ejc.127.1668098836882; Thu, 10 Nov 2022 08:47:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098836; cv=none; d=google.com; s=arc-20160816; b=CAMhXfwgMY/x1RwOuUtyUTViJExRe6iaqhiPJ6Pr3h6+BRh9SVRnaihIW6ZsTIey3G hEsunQMgMMrfXndhcODDI5NCcRTcWdZtlrANzT4EHSzgUJvpe4o/ZN6CBplXYM//MYBA jeFihdeTKOLX9oZbJq6iPJ9bWWQC3oeI/cueepB5jZobFTa8oqlJ5ipbrWZ21MBjYFpl +sFJU2pvYnLcNCd9lJZ8MYTzadPz9+4B5WlQJ43W4X0TxuIJi54/PX3Mz9oxwuxg3Ul+ 3UTjS3xUIiIAFoLO9FFxV3IKvR71dUFuxZQMtmfIy/f7Q82xmja6jvyAFyQ2bozL2u1z noTA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sSYGagKDke4dSn3ed5xnAjLUnlKza3VZtpfsA97909I=; b=S+dwCr47ZTpv1ZaD8wRzmYKoR/NH8CgwnXEiktGQQhLlK8aJvPT4bAED7xFX+yxWcn AU/ucFGPhdbfhfwBfMlgXtWHx4/L+t0sZJWUOjyCjFAeI9iZIwaSNzGDg9alktvu2BLi hcjmGCVQ6Tot+C9dlapiV+rtzx/S4ULbBx/Vme2PDFT+HjWYWqTnaujJ8u9C7dPYw2Mw Zu3vXxZ4LSrPQeb2vr3poerYZcnMfeA/hRXYy6EL1aGUMkk3k23thJXyH9cxvqryYEbd NZiHqDh/JNiglXAjiuGGxcf1tKeQTv+CXjCRnmHREVAFUUEJVnXU5ZP3jL+2XICpWmHC KUtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JLFepvqh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z14-20020a056402274e00b004667ef1bc6csi114388edd.14.2022.11.10.08.46.48; Thu, 10 Nov 2022 08:47:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JLFepvqh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232557AbiKJQoj (ORCPT + 93 others); Thu, 10 Nov 2022 11:44:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232343AbiKJQn5 (ORCPT ); Thu, 10 Nov 2022 11:43:57 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3DFB47321; Thu, 10 Nov 2022 08:43:20 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9539FB82258; Thu, 10 Nov 2022 16:43:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D687C433C1; Thu, 10 Nov 2022 16:43:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098598; bh=Z3Poal8/0sehEb7NLOgdH3l5JP1/vq7TUA9vjjBRHIM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JLFepvqhskfPiO9iSLzm0an7+SN+UMlAWx99yK/eHXyeVW2bZmLa5cfif0sVu9kbw WFqYdTyDbQ85XqNWhZzwuvnRhBUEZWWEefvXDU6kFv3/RChF+U/SikwzuntqV6qJlK e3zLr3B0QjaCQ9YRnHDZMm/0/rr1dhUtM6KG58Y3LD3Cv0knc0j8mKtQMq3Kosb8/5 0vrXxNLcocW5m4rz3TLhiJXIBGneTrap2b2acp+O5K73KzyfRQUZ/tjzWe65CPqWsV 5hztVAqrW3FioMbMiXR26ZeWvUvTcu7k7SstTfGM0j0ltAMEgXqNFRIDgYCKke6Jl0 s/IFAJzVZ/DqQ== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye Subject: [PATCH v1 20/28] rust: str: add `Formatter` type Date: Thu, 10 Nov 2022 17:41:32 +0100 Message-Id: <20221110164152.26136-21-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wedson Almeida Filho Add the `Formatter` type, which leverages `RawFormatter`, but fails if callers attempt to write more than will fit in the buffer. In order to so, implement the `RawFormatter::from_buffer()` constructor as well. Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Signed-off-by: Wedson Almeida Filho [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 3fb73b888dce..db6473db31c6 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -415,6 +415,23 @@ impl RawFormatter { } } + /// Creates a new instance of [`RawFormatter`] with the given buffer. + /// + /// # Safety + /// + /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes + /// for the lifetime of the returned [`RawFormatter`]. + pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self { + let pos = buf as usize; + // INVARIANT: We ensure that `end` is never less then `buf`, and the safety requirements + // guarantees that the memory region is valid for writes. + Self { + pos, + beg: pos, + end: pos.saturating_add(len), + } + } + /// Returns the current insert position. /// /// N.B. It may point to invalid memory. @@ -448,3 +465,43 @@ impl fmt::Write for RawFormatter { Ok(()) } } + +/// Allows formatting of [`fmt::Arguments`] into a raw buffer. +/// +/// Fails if callers attempt to write more than will fit in the buffer. +pub(crate) struct Formatter(RawFormatter); + +impl Formatter { + /// Creates a new instance of [`Formatter`] with the given buffer. + /// + /// # Safety + /// + /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes + /// for the lifetime of the returned [`Formatter`]. + #[allow(dead_code)] + pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self { + // SAFETY: The safety requirements of this function satisfy those of the callee. + Self(unsafe { RawFormatter::from_buffer(buf, len) }) + } +} + +impl Deref for Formatter { + type Target = RawFormatter; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl fmt::Write for Formatter { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.0.write_str(s)?; + + // Fail the request if we go past the end of the buffer. + if self.0.pos > self.0.end { + Err(fmt::Error) + } else { + Ok(()) + } + } +} -- 2.38.1