Comments (1)
One potential patch for this is the following, since the callers do uphold this invariant. Safety docs would also need to be updated.
A better way to do this is to either use some POD trait (I don't recall if there's one in use in lexical
already), or just expose a read_u32()
or read_u64()
Patch
diff --git a/lexical-parse-float/src/slow.rs b/lexical-parse-float/src/slow.rs
index 26a0d90..8891bba 100644
--- a/lexical-parse-float/src/slow.rs
+++ b/lexical-parse-float/src/slow.rs
@@ -335,7 +335,7 @@ macro_rules! round_up_nonzero {
// First try reading 8-digits at a time.
if iter.is_contiguous() {
- while let Some(value) = iter.read::<u64>() {
+ while let Some(value) = unsafe { iter.read::<u64>() } {
// SAFETY: safe since we have at least 8 bytes in the buffer.
unsafe { iter.step_by_unchecked(8) };
if value != 0x3030_3030_3030_3030 {
diff --git a/lexical-parse-integer/src/algorithm.rs b/lexical-parse-integer/src/algorithm.rs
index 3caedc6..bcc66a0 100644
--- a/lexical-parse-integer/src/algorithm.rs
+++ b/lexical-parse-integer/src/algorithm.rs
@@ -217,7 +217,7 @@ where
debug_assert!(Iter::IS_CONTIGUOUS);
// Read our digits, validate the input, and check from there.
- let bytes = u32::from_le(iter.read::<u32>()?);
+ let bytes = unsafe { u32::from_le(iter.read::<u32>()?) };
if is_4digits::<FORMAT>(bytes) {
// SAFETY: safe since we have at least 4 bytes in the buffer.
unsafe { iter.step_by_unchecked(4) };
@@ -289,7 +289,7 @@ where
debug_assert!(Iter::IS_CONTIGUOUS);
// Read our digits, validate the input, and check from there.
- let bytes = u64::from_le(iter.read::<u64>()?);
+ let bytes = unsafe { u64::from_le(iter.read::<u64>()?) };
if is_8digits::<FORMAT>(bytes) {
// SAFETY: safe since we have at least 8 bytes in the buffer.
unsafe { iter.step_by_unchecked(8) };
diff --git a/lexical-util/src/iterator.rs b/lexical-util/src/iterator.rs
index bb5f86d..18c760c 100644
--- a/lexical-util/src/iterator.rs
+++ b/lexical-util/src/iterator.rs
@@ -122,7 +122,7 @@ pub trait BytesIter<'a>: Iterator<Item = &'a u8> {
/// Try to read a value of a different type from the iterator.
/// This advances the internal state of the iterator.
- fn read<V>(&self) -> Option<V>;
+ unsafe fn read<V>(&self) -> Option<V>;
/// Advance the internal slice by `N` elements.
///
diff --git a/lexical-util/src/noskip.rs b/lexical-util/src/noskip.rs
index 22c511b..9df4cfb 100644
--- a/lexical-util/src/noskip.rs
+++ b/lexical-util/src/noskip.rs
@@ -120,7 +120,7 @@ impl<'a, const __: u128> Bytes<'a, __> {
/// Try to read a value of a different type from the iterator.
/// This advances the internal state of the iterator.
#[inline]
- pub fn read<V>(&self) -> Option<V> {
+ unsafe fn read<V>(&self) -> Option<V> {
if Self::IS_CONTIGUOUS && self.as_slice().len() >= mem::size_of::<V>() {
// SAFETY: safe since we've guaranteed the buffer is greater than
// the number of elements read.
@@ -288,7 +288,7 @@ impl<'a: 'b, 'b, const __: u128> BytesIter<'a> for BytesIterator<'a, 'b, __> {
}
#[inline]
- fn read<V>(&self) -> Option<V> {
+ unsafe fn read<V>(&self) -> Option<V> {
self.byte.read()
}
diff --git a/lexical-util/src/skip.rs b/lexical-util/src/skip.rs
index 4daaaa9..55e098f 100644
--- a/lexical-util/src/skip.rs
+++ b/lexical-util/src/skip.rs
@@ -442,7 +442,7 @@ impl<'a, const FORMAT: u128> Bytes<'a, FORMAT> {
/// Try to read a value of a different type from the iterator.
/// This advances the internal state of the iterator.
#[inline]
- pub fn read<V>(&self) -> Option<V> {
+ unsafe fn read<V>(&self) -> Option<V> {
if Self::IS_CONTIGUOUS && self.as_slice().len() >= mem::size_of::<V>() {
// SAFETY: safe since we've guaranteed the buffer is greater than
// the number of elements read.
from rust-lexical.
Related Issues (20)
- [OTHER] Improve internal safety comments and architecture
- [BUG] Unsoundness: `try_parse_{4,8}digits` appear to advance iterators out of bounds
- [BUG] BytesIter should be an `unsafe trait` or private
- [BUG] Bounds for integer parsing are not correctly checked HOT 1
- [FEATURE] Update Dragonbox Algorithm HOT 2
- [BUG] `umul192_lower128` can error on due to overflow on addition
- [FEATURE] Add thousands digit separator for ```WriteIntegerOptions``` to use with ```to_string_with_options```
- parse_partial ignoring minus sign in presence of trailing characters HOT 3
- [BUG] Assertion failure on parsing hexadecimal floats HOT 1
- [BUG] Tests fail to compile because quickcheck is missing from Cargo.toml in the crates.io tarball. HOT 4
- [QUESTION] Use of min_significant_digits with max_significant digits
- [BUG] Integer overflow checking logic is incorrect
- [FEATURE] `write` APIs should take `buffer: &mut [MaybeUninit<u8>]`
- [QUESTION] support custom types (i256)
- [BUG] -0.0 printed as "0.0" HOT 1
- [BUG] Safety comments for MaybeUninit::assume_init calls are wrong, calls are UB
- [BUG] consecutive_digit_separator has no effect
- [BUG] Inconsistent error between int and float parsing
- [BUG] `no_integer_leading_zeros(true)` incorrectly parses input
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-lexical.