Skip to content

Commit ce802af

Browse files
committed
decode: guard against panics when alloc is disabled
The existing code panics if you try to decode a too-large checksum and it fails, which is definitely wrong. Fix this so that FieldVec::from_iter does not panic, allowing the "invalid residue" error to be constructed. There is also a panic when trying to correct too-large checksums. This is arguably permissible, since it's something that's detectable at compile time (though what would be even better is if this language would support telling the compiler to do this; see rust-lang/rust#92827 for more info). But remove it anyway.
1 parent 0cbfe89 commit ce802af

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/primitives/correction.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,17 @@ pub trait CorrectableError {
6565

6666
/// Wrapper around [`Self::residue_error`] that outputs a correction context.
6767
///
68+
/// Will return None if the error is not a correctable one, or if the **alloc**
69+
/// feature is disabled and the checksum is too large. See the documentation
70+
/// for [`NO_ALLOC_MAX_LENGTH`] for more information.
71+
///
6872
/// This is the function that users should call.
6973
fn correction_context<Ck: Checksum>(&self) -> Option<Corrector<Ck>> {
74+
#[cfg(not(feature = "alloc"))]
75+
if Ck::CHECKSUM_LENGTH >= NO_ALLOC_MAX_LENGTH {
76+
return None;
77+
}
78+
7079
self.residue_error().map(|e| Corrector { residue: e.residue(), phantom: PhantomData })
7180
}
7281
}

src/primitives/decode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ impl InvalidResidueError {
10281028
/// holds the target residue but this doesn't help), the caller will need
10291029
/// to obtain the checksum from somewhere else in order to make use of this.
10301030
///
1031-
/// Not public because [`Polynomial`] is a private type.
1031+
/// Not public because [`Polynomial`] is a private type, and because the
1032+
/// subtraction will panic if this is called without checking has_data
1033+
/// on the FieldVecs.
10321034
pub(super) fn residue(&self) -> Polynomial<Fe32> { self.actual.clone() - &self.target }
10331035
}
10341036

0 commit comments

Comments
 (0)