Skip to content

Commit

Permalink
Use appropriate buffer size when reading BUP file.
Browse files Browse the repository at this point in the history
Previously, a fixed 1GiB buffer was allocated causing the tool to use a lot of memory.
  • Loading branch information
Niels Avonds authored and madisongh committed Jul 3, 2023
1 parent 5da9f71 commit af0713d
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions bup.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ struct bup_entry_s {
char spec[65];
};

#define BUFFERSIZE (1024 * 1024 * 1024)

/*
* API context
*/
Expand Down Expand Up @@ -551,7 +549,8 @@ free_context (bup_context_t *ctx)
close(ctx->fd);
if (ctx->entries)
free(ctx->entries);
free(ctx->buffer);
if (ctx->buffer != NULL)
free(ctx->buffer);
free(ctx);
} /* free_context */

Expand Down Expand Up @@ -580,13 +579,8 @@ bup_init (const char *pathname)
return NULL;
memset(ctx, 0, sizeof(*ctx));
ctx->fd = -1;
ctx->buffer = malloc(BUFFERSIZE);
if (ctx->buffer == NULL) {
free(ctx);
return NULL;
}
if (construct_tnspec(ctx->our_spec_str, sizeof(ctx->our_spec_str)) < 0) {
free(ctx);
free_context(ctx);
return NULL;
}
spec_split(ctx->our_spec_str, &ctx->our_tnspec);
Expand All @@ -607,7 +601,14 @@ bup_init (const char *pathname)
return NULL;
}
payload_size = st.st_size;
n = read(fd, ctx->buffer, BUFFERSIZE);

ctx->buffer = malloc(payload_size);
if (ctx->buffer == NULL) {
free_context(ctx);
return NULL;
}

n = read(fd, ctx->buffer, payload_size);
if (n < sizeof(struct bup_header_s)) {
free_context(ctx);
return NULL;
Expand Down Expand Up @@ -637,7 +638,7 @@ bup_init (const char *pathname)
return NULL;
}
totsize = hdr->header_size + hdr->entry_count * sizeof(struct bup_ods_entry_s);
if (totsize > BUFFERSIZE) {
if (totsize > payload_size) {
fprintf(stderr, "%s: cannot load all update entries\n", pathname);
free_context(ctx);
return NULL;
Expand Down

0 comments on commit af0713d

Please sign in to comment.