fix bug where dictionaries with 1 entry fail to sync

also ensure that at least 1 byte is reserved for any key
that is allocated; this helps avoid edge cases dealing with
zero-length key entries later on.
This commit is contained in:
bunnie 2022-03-29 04:38:15 +08:00
parent afb9974309
commit 9a41b18400

View file

@ -558,11 +558,14 @@ impl DictCacheEntry {
self.rebuild_free_pool();
}
let pool_candidate = self.small_pool_free.pop().expect("Free pool was allocated & rebuilt, but still empty.");
let reservation = if alloc_hint.unwrap_or(0) > data.len() + offset {
let mut reservation = if alloc_hint.unwrap_or(0) > data.len() + offset {
alloc_hint.unwrap_or(0)
} else {
data.len() + offset
};
if reservation == 0 { // this case happens if someone tries to just create an empty key entry without giving an alloc hint
reservation = 1; // at least make a reservation for 1 byte of data
}
let index = if pool_candidate.avail as usize >= reservation {
// it fits in the current candidate slot, use this as the index
let ksp = &mut self.small_pool[pool_candidate.index];
@ -595,15 +598,15 @@ impl DictCacheEntry {
for &b in data {
alloc_data.push(b);
}
/*
for fk in self.free_keys.iter() {
log::info!("fk start: {} run: {}", fk.0.start, fk.0.run);
}*/
let descriptor_index = if let Some(di) = self.get_free_key_index() {
di
} else {
return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of key indices in dictionary"));
};
/* log::info!("storing in index {:?}", descriptor_index);
for fk in self.free_keys.iter() {
log::info!("fk start: {} run: {}", fk.0.start, fk.0.run);
} */
let kcache = KeyCacheEntry {
start: small_storage_base_vaddr_from_indices(self.index, index),
len: (data.len() + offset) as u64,
@ -868,7 +871,7 @@ impl DictCacheEntry {
})
)
}
if index > self.last_disk_key_index {
if index >= self.last_disk_key_index {
// if the new index is outside the currently known set, raise the search extent for the brute-force search
self.last_disk_key_index = index + 1;
}
@ -913,6 +916,8 @@ impl DictCacheEntry {
}
FreeKeyCases::Within => {
log::error!("Double-free error in free_keys()");
log::info!("free_key_vec[i].0: {:?}", free_key_vec[i].0);
log::info!("index: {}", index);
panic!("Double-free error in free_keys()");
}
FreeKeyCases::RightAdjacent => {
@ -1034,7 +1039,8 @@ pub(crate) struct Dictionary {
/// Number of keys in the dictionary
pub(crate) num_keys: u32,
/// Free index starting space. While this is a derived parameter, its value is recorded to avoid
/// an expensive, long search operation during the creation of a dictionary cache record.
/// an expensive, long search operation during the creation of a dictionary cache record. 0 is an invalid index,
/// as this is where the header goes. Maybe this should be a NonZeroU32.
pub(crate) free_key_index: u32,
/// Name. Length should pad out the record to exactly 127 bytes.
pub(crate) name: DictName,
@ -1113,7 +1119,7 @@ pub(crate) enum FreeKeyCases {
}
#[derive(Eq, Copy, Clone, Debug)]
pub(crate) struct FreeKeyRange {
/// This index should be free
/// This index should be free. Smallest value is 1; 0-index is for the dict header. Maybe this should be a NonZeroU32?
pub(crate) start: u32,
/// Additional free keys after the start one. Run = 0 means just the start key is free, and the
/// next one should be used. Run = 2 means {start, start+1} are free, etc.