Skip to content

Commit 553d3d2

Browse files
committed
fn COVER_best_finish: make safe
1 parent 6812634 commit 553d3d2

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

lib/dictBuilder/cover.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -974,29 +974,27 @@ pub(super) fn COVER_best_start(best: &mut COVER_best_t) {
974974
best.liveJobs += 1;
975975
}
976976

977-
pub(super) unsafe fn COVER_best_finish(
977+
pub(super) fn COVER_best_finish(
978978
best: &mut COVER_best_t,
979979
parameters: ZDICT_cover_params_t,
980980
selection: &COVER_dictSelection_t,
981981
) {
982-
let dict = selection.dictContent.as_ptr() as *const core::ffi::c_void;
983982
let compressedSize = selection.totalCompressedSize;
984983
let dictSize = selection.dictSize;
985984
let mut liveJobs: size_t = 0;
986985
let _guard = best.mutex.lock().unwrap();
987986
best.liveJobs = (best.liveJobs).wrapping_sub(1);
988987
liveJobs = best.liveJobs;
989988
if compressedSize < best.compressedSize {
990-
if best.dictSize < dictSize {
991-
drop(core::mem::take(&mut best.dict));
992-
best.dict = Box::from(vec![0u8; dictSize]);
993-
}
994-
if !dict.is_null() {
995-
memcpy(best.dict.as_mut_ptr().cast(), dict, dictSize);
996-
best.dictSize = dictSize;
997-
best.parameters = parameters;
998-
best.compressedSize = compressedSize;
989+
if let Some(slice) = best.dict.get_mut(..selection.dictContent.len()) {
990+
slice.copy_from_slice(&selection.dictContent);
991+
} else {
992+
best.dict = selection.dictContent.clone();
999993
}
994+
995+
best.dictSize = dictSize;
996+
best.parameters = parameters;
997+
best.compressedSize = compressedSize;
1000998
}
1001999
if liveJobs == 0 {
10021000
best.cond.notify_all();

0 commit comments

Comments
 (0)