Message ID | 20221202161502.385525-21-ojeda@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp948218wrr; Fri, 2 Dec 2022 08:19:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Nj+VxF6i2NU53+Xku0RyUKiwr9532ztvAnca46xZxIvIkR8VlYc4kKJ/7sqHyqRcPZFZM X-Received: by 2002:a17:906:dfe8:b0:7c0:8711:7a4 with SMTP id lc8-20020a170906dfe800b007c0871107a4mr16373212ejc.667.1669997961313; Fri, 02 Dec 2022 08:19:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997961; cv=none; d=google.com; s=arc-20160816; b=KF6jyu3EYJqlvfSad9J/kGtVWjAhp5BrUnI7VQWGi2gyxueLfIRjFyhJbyyiR8m6XE Co7a/3uzisCvXSDNZNvbz4wgFbF7luE0qbRyt3rXvtdqQInzr+bnd1Yxd5oM78RSofZM Yb88Y1JBLXYP9b1Eu2CR/DPaCRE6QuoCwpVnuEybxcwQsp62eb4GN8w0qHBeGubv+L9v +o0pxQADCTqB+WteuzFbuVBU0fI5fhhuYnfiJEkQXOCIVRCiv9XMndw0PtNg+aaH3ba8 Fc9yftEVyftOEfXZH2/7DRhsL+U2w0XOun/aJeIxDqS9FcoFV96MVJDoT2JjzyicV57g 9KTg== 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=WU2Bapy8NrQadUjLKlB9GfwCe5rNMp+0+TMym3iKpTc=; b=QSs6MomupO/HWqf1W5eV4/MUbe51O2I3Pj5vQ4KKor6Dmrh0kzVnPzlLgFL6M/K0RF fmm2Pqx5UXcPcrQwqfQSdiojS2V7Tms9njNMMvGlWkgUS+kiTrFze9pHR/L3sQcrwago vIsM0QdnZd3g3C7qsTBpGwvC8Gc7U2OTWYfa7txEpBBMdOJGwjv2zRBGAaepui/4yZzS NQ/EJ4rJnNyiDNAPRIc/wvBgCJMqLgYXQFRHYvXOLFQnn6hqtuzL2HxPPf64TTtf+HWd Sdi72bMXab40OkyWL2gUtpRowYrtSBAD6baEyWAr4QA0gKlCwQDdkVVqajedtYFKs5Qv m9VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eZA4rWwQ; 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 u11-20020aa7d88b000000b0046a279823d6si6050610edq.298.2022.12.02.08.18.56; Fri, 02 Dec 2022 08:19:21 -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=eZA4rWwQ; 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 S233979AbiLBQRg (ORCPT <rfc822;lhua1029@gmail.com> + 99 others); Fri, 2 Dec 2022 11:17:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233937AbiLBQQs (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 2 Dec 2022 11:16:48 -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 C34DDBE6A1; Fri, 2 Dec 2022 08:16:27 -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 7C22BB821EF; Fri, 2 Dec 2022 16:16:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B1C9C433D6; Fri, 2 Dec 2022 16:16:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997785; bh=lNOFPVRbAKVrfjnVGGlh1mHXRjctVq4x9YbdvqaJ5Pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eZA4rWwQlLf/QhcBhrlLCwVeLdG0tbiNvtMljCe5GPUaS1snLGeCqEF0fOQBXL7lc O2Ou8zvs6Vta595iqoUkxn4qyPgwQuNC5GP2RciAUllOKsi6dtrrQYCKCo9U0jkxh+ sxWfFO04JADRtp9ZcKcTKc7ap54ysr+Z8tjDdc+I1gtNedTFaZU7/OHjDSQdVfGCl5 xhAkSvQCowbAgvVNgnOAehjsD8GEYd/OPcBMJIl5vfBqi7Nm3LBbA9yDFVqDVTKnFT 1+SBvnDxZ77h5m0ZRF2lCbhdTzUhYh1tR7kSG4rMVVbTib/D3BnZcwOWS/UJ/C1Hq6 lqzZKBgD4TMFg== From: ojeda@kernel.org To: Miguel Ojeda <ojeda@kernel.org>, Wedson Almeida Filho <wedsonaf@gmail.com>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com> Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye <ark.email@gmail.com> Subject: [PATCH v2 20/28] rust: str: add `Formatter` type Date: Fri, 2 Dec 2022 17:14:51 +0100 Message-Id: <20221202161502.385525-21-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119782429416579?= X-GMAIL-MSGID: =?utf-8?q?1751119782429416579?= |
Series |
[v2,01/28] rust: prelude: split re-exports into groups
|
|
Commit Message
Miguel Ojeda
Dec. 2, 2022, 4:14 p.m. UTC
From: Wedson Almeida Filho <wedsonaf@gmail.com> 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 <ark.email@gmail.com> Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <ojeda@kernel.org> --- rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
Comments
* ojeda@kernel.org (ojeda@kernel.org) wrote: > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > 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 <ark.email@gmail.com> > Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com> > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Reviewed-by: Gary Guo <gary@garyguo.net> > [Reworded, adapted for upstream and applied latest changes] > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > --- > rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 57 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index a995db36486f..ce207d1b3d2a 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -406,6 +406,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. > @@ -439,3 +456,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. Reading this for the first time, I'm surprised by this, perhaps a bit more comment is needed? I was expecting that nothing would let pos pass end. Dave > + if self.0.pos > self.0.end { > + Err(fmt::Error) > + } else { > + Ok(()) > + } > + } > +} > -- > 2.38.1 >
On Sun, 4 Dec 2022 at 15:41, Dr. David Alan Gilbert <dave@treblig.org> wrote: > > * ojeda@kernel.org (ojeda@kernel.org) wrote: > > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > > > 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 <ark.email@gmail.com> > > Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com> > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > > Reviewed-by: Gary Guo <gary@garyguo.net> > > [Reworded, adapted for upstream and applied latest changes] > > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > > --- > > rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 57 insertions(+) > > > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > > index a995db36486f..ce207d1b3d2a 100644 > > --- a/rust/kernel/str.rs > > +++ b/rust/kernel/str.rs > > @@ -406,6 +406,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. > > @@ -439,3 +456,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); Here we mention that `Formatter` fails if callers attempt to write more than will fit in the buffer. This is in contrast with `RawFormatter`, which doesn't fail in such cases. There's also a comment there explaining it (not visible in this patch because it's already there), but I reproduce below: /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// It does not fail if callers write past the end of the buffer so that they can calculate the /// size required to fit everything. /// /// # Invariants /// /// The memory region between `pos` (inclusive) and `end` (exclusive) is valid for writes if `pos` /// is less than `end`. pub(crate) struct RawFormatter { `RawFormatter` is used to implement the "%pA" printf specifier, which requires this behaviour. > > + > > +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. > > Reading this for the first time, I'm surprised by this, perhaps a > bit more comment is needed? I was expecting that nothing would > let pos pass end. Given the comments I highlight above, do you think we still need more? (My impression is that you're reading this without the context I tried to explain above, and this context may perhaps be sufficient.) Thanks, -Wedson
* Wedson Almeida Filho (wedsonaf@gmail.com) wrote: > On Sun, 4 Dec 2022 at 15:41, Dr. David Alan Gilbert <dave@treblig.org> wrote: > > > > * ojeda@kernel.org (ojeda@kernel.org) wrote: > > > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > > > > > 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 <ark.email@gmail.com> > > > Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com> > > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > > > Reviewed-by: Gary Guo <gary@garyguo.net> > > > [Reworded, adapted for upstream and applied latest changes] > > > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > > > --- > > > rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ > > > 1 file changed, 57 insertions(+) > > > > > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > > > index a995db36486f..ce207d1b3d2a 100644 > > > --- a/rust/kernel/str.rs > > > +++ b/rust/kernel/str.rs > > > @@ -406,6 +406,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. > > > @@ -439,3 +456,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); > > Here we mention that `Formatter` fails if callers attempt to write > more than will fit in the buffer. > > This is in contrast with `RawFormatter`, which doesn't fail in such > cases. There's also a comment there explaining it (not visible in this > patch because it's already there), but I reproduce below: > > /// Allows formatting of [`fmt::Arguments`] into a raw buffer. > /// > /// It does not fail if callers write past the end of the buffer so > that they can calculate the > /// size required to fit everything. > /// > /// # Invariants > /// > /// The memory region between `pos` (inclusive) and `end` (exclusive) > is valid for writes if `pos` > /// is less than `end`. > pub(crate) struct RawFormatter { > > `RawFormatter` is used to implement the "%pA" printf specifier, which > requires this behaviour. > > > > + > > > +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. > > > > Reading this for the first time, I'm surprised by this, perhaps a > > bit more comment is needed? I was expecting that nothing would > > let pos pass end. > > Given the comments I highlight above, do you think we still need more? > > (My impression is that you're reading this without the context I tried > to explain above, and this context may perhaps be sufficient.) Thanks for the pointer; I guess I find it trickier when I can't see the type in self.0 to immediately see it's RawFormatter, and 'Raw' is abstract enough to need to go hunt to see it's behaviour. With that context, I wouldn't object to what's there, but how about something like: // RawFormatter (self.0) still updates pos if the buffer // is too small, but doesn't fail - we want to fail the request. Dave > Thanks, > -Wedson
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index a995db36486f..ce207d1b3d2a 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -406,6 +406,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. @@ -439,3 +456,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(()) + } + } +}