Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail when writing key lengths >= 256 bytes to Store #90

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/store/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Read, Shared, Write, KV};
use crate::Result;
use crate::{Error, Result};

// TODO: figure out how to let users set DefaultBackingStore, similar to setting
// the global allocator in the standard library
Expand Down Expand Up @@ -77,6 +77,16 @@ impl<S: Read> Read for Store<S> {
impl<S: Write> Write for Store<S> {
#[inline]
fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
// merk has a hard limit of 256 bytes for keys, but it does not create
// an error until comitting. we assert the key length here so that
// writes will fail early rather than making the entire block fail. this
// assertion can be removed if the merk key length limit is removed, or
// if we instead check this statically using known encoding lengths via
// ed.
if key.len() + self.prefix.len() >= 256 {
return Err(Error::Store("Store keys must be < 256 bytes".into()));
}

let prefixed = concat(self.prefix.as_slice(), key.as_slice());
self.store.put(prefixed, value)
}
Expand Down