Skip to content

Commit f2782d4

Browse files
committed
[WARP] Only merge chunks in create from view command if an existing file was given
Fixes the case where we are generating many smaller chunks for a given view and then unintentionally merging them back together, throwing away all data.
1 parent 5fb4ebf commit f2782d4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

plugins/warp/src/plugin/create.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,39 @@ impl CreateFromCurrentView {
164164

165165
if let Err(err) = file {
166166
binaryninja::interaction::show_message_box(
167-
"Error",
168-
&format!("Failed to create signature file: {}", err),
167+
"Failed to create signature file",
168+
&err.to_string(),
169169
MessageBoxButtonSet::OKButtonSet,
170170
MessageBoxIcon::ErrorIcon,
171171
);
172172
log::error!("Failed to create signature file: {}", err);
173173
return None;
174174
}
175175

176+
let background_task = BackgroundTask::new("Creating WARP File...", false);
176177
let mut file = file.unwrap();
177178
// Add back the existing chunks if the user selected to keep them.
178-
file.chunks.extend(existing_chunks);
179-
// TODO: Make merging optional?
180-
file.chunks = Chunk::merge(&file.chunks, compression_type.into());
179+
if !existing_chunks.is_empty() {
180+
file.chunks.extend(existing_chunks);
181+
// TODO: Make merging optional?
182+
// TODO: Merging can lose chunk data if it goes above the maximum table count.
183+
// TODO: We should probably solve that in the warp crate itself?
184+
file.chunks = Chunk::merge(&file.chunks, compression_type.into());
181185

182-
if std::fs::write(&file_path, file.to_bytes()).is_err() {
186+
// After merging, we should have at least one chunk. If not, merging actually removed data.
187+
if file.chunks.len() < 1 {
188+
log::error!("Failed to merge chunks! Please report this, it should not happen.");
189+
return None;
190+
}
191+
}
192+
193+
let file_bytes = file.to_bytes();
194+
let file_size = file_bytes.len();
195+
if std::fs::write(&file_path, file_bytes).is_err() {
183196
log::error!("Failed to write data to signature file!");
184197
}
185198
log::info!("Saved signature file to: '{}'", file_path.display());
199+
background_task.finish();
186200

187201
// Show a report of the generate signatures, if desired.
188202
let report_generator = ReportGenerator::new();

0 commit comments

Comments
 (0)