Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
libsparse: Propagate failures when resparsing files.
Browse files Browse the repository at this point in the history
MappedFile creation can fail due to out of memory, but this condition is
not handled properly when resparsing. The error is silently ignored and
the result is a corrupt file.

Bug: 273933042
Bug: 268872725
Test: fastboot update on Windows
Google: 2495615
Change-Id: I4d0f24d6ba390e2328de8f0e3637d17663743df5
Signed-off-by: CxDxVER <[email protected]>
  • Loading branch information
dvandercorp authored and CxDxVER committed Aug 29, 2023
1 parent 939e13a commit 487c5fd
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions libsparse/sparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ unsigned int sparse_file_block_size(struct sparse_file* s) {
return s->block_size;
}

static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to,
unsigned int len) {
static int move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to, unsigned int len,
backed_block** out_bb) {
int64_t count = 0;
struct output_file* out_counter;
struct backed_block* last_bb = nullptr;
Expand All @@ -282,7 +282,7 @@ static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, stru
out_counter = output_file_open_callback(out_counter_write, &count, to->block_size, to->len, false,
true, 0, false);
if (!out_counter) {
return nullptr;
return -1;
}

for (bb = start; bb; bb = backed_block_iter_next(bb)) {
Expand Down Expand Up @@ -319,7 +319,8 @@ static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, stru
out:
output_file_close(out_counter);

return bb;
*out_bb = bb;
return 0;
}

int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct sparse_file** out_s,
Expand All @@ -337,7 +338,15 @@ int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct
do {
s = sparse_file_new(in_s->block_size, in_s->len);

bb = move_chunks_up_to_len(in_s, s, max_len);
if (move_chunks_up_to_len(in_s, s, max_len, &bb) < 0) {
sparse_file_destroy(s);
for (int i = 0; i < c && i < out_s_count; i++) {
sparse_file_destroy(out_s[i]);
out_s[i] = nullptr;
}
sparse_file_destroy(tmp);
return -1;
}

if (c < out_s_count) {
out_s[c] = s;
Expand Down

0 comments on commit 487c5fd

Please sign in to comment.