Don't allocate large buffer on the stack in pg_verifybackup

Per complaint from Andres Freund. Follow his suggestion to allocate the
buffer once in the calling routine instead.

Also make a tiny indentation improvement.

Discussion: https://postgr.es/m/20240411190147.a3yries632olfcgg@awork3.anarazel.de
This commit is contained in:
Andrew Dunstan 2024-04-12 10:52:25 -04:00
parent 42fa4b6601
commit 929c05774b
1 changed files with 11 additions and 6 deletions

View File

@ -144,7 +144,8 @@ static void verify_control_file(const char *controlpath,
static void report_extra_backup_files(verifier_context *context);
static void verify_backup_checksums(verifier_context *context);
static void verify_file_checksum(verifier_context *context,
manifest_file *m, char *fullpath);
manifest_file *m, char *fullpath,
uint8 *buffer);
static void parse_required_wal(verifier_context *context,
char *pg_waldump_path,
char *wal_directory);
@ -480,8 +481,8 @@ parse_manifest_file(char *manifest_path)
(long long int) statbuf.st_size);
}
bytes_left -= rc;
json_parse_manifest_incremental_chunk(
inc_state, buffer, rc, bytes_left == 0);
json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
bytes_left == 0);
}
/* Release the incremental state memory */
@ -812,9 +813,12 @@ verify_backup_checksums(verifier_context *context)
manifest_data *manifest = context->manifest;
manifest_files_iterator it;
manifest_file *m;
uint8 *buffer;
progress_report(false);
buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
manifest_files_start_iterate(manifest->files, &it);
while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
{
@ -828,13 +832,15 @@ verify_backup_checksums(verifier_context *context)
m->pathname);
/* Do the actual checksum verification. */
verify_file_checksum(context, m, fullpath);
verify_file_checksum(context, m, fullpath, buffer);
/* Avoid leaking memory. */
pfree(fullpath);
}
}
pfree(buffer);
progress_report(true);
}
@ -843,14 +849,13 @@ verify_backup_checksums(verifier_context *context)
*/
static void
verify_file_checksum(verifier_context *context, manifest_file *m,
char *fullpath)
char *fullpath, uint8 *buffer)
{
pg_checksum_context checksum_ctx;
char *relpath = m->pathname;
int fd;
int rc;
size_t bytes_read = 0;
uint8 buffer[READ_CHUNK_SIZE];
uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
int checksumlen;