From patchwork Thu Nov 10 16:41:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18221 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp252428wru; Thu, 10 Nov 2022 08:45:55 -0800 (PST) X-Google-Smtp-Source: AMsMyM6w85KB8xX6NxMgrHuv9F0BS/lyG8+i3p4b9aVvG7hI7bj0wkU2RTGQQfgtkjS90hEbp5eF X-Received: by 2002:a05:6402:1203:b0:461:a09b:aaf2 with SMTP id c3-20020a056402120300b00461a09baaf2mr2595595edw.328.1668098755635; Thu, 10 Nov 2022 08:45:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098755; cv=none; d=google.com; s=arc-20160816; b=ahJpU4eDjwrzsR01AdeF9y50EaAfDGXUhjneITCRNX0zJO0w3cRb8Hv6RwlfZ4Qfxu 0alH0eOpyo2lsnlV9ptZLr2/wuIekc/ZkBNUPU27Jz5/TbukBW1gcN4FgJdUpZWqv1Um RkliGWTZ/hq4BmqquKOsCb75+aVwz+obVX/caQ7k6PvnIf+wtNz2BadC9uCd/tLzmR3n +87GI88wmXk1pM58KVfWni+wfuqFzK3pVr5IOtqztv+v/W3MAsUbUTzEc41YYPIlUsuL rRs2b9UqLSNewWu5xxUBlpD3EPe6nrrpaT3awItapFCuvYiM5bUakQ+eoeNuShpUtM8T 4OwQ== 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 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.45.31; Thu, 10 Nov 2022 08:45:55 -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 + 99 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_Roy_Baron?= 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749128320784671721?= X-GMAIL-MSGID: =?utf-8?q?1749128320784671721?= 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 Reviewed-by: Gary Guo --- 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(()) + } + } +}