Skip to content

Commit 929c057

Browse files
committed
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
1 parent 42fa4b6 commit 929c057

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/bin/pg_verifybackup/pg_verifybackup.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ static void verify_control_file(const char *controlpath,
144144
static void report_extra_backup_files(verifier_context *context);
145145
static void verify_backup_checksums(verifier_context *context);
146146
static void verify_file_checksum(verifier_context *context,
147-
manifest_file *m, char *fullpath);
147+
manifest_file *m, char *fullpath,
148+
uint8 *buffer);
148149
static void parse_required_wal(verifier_context *context,
149150
char *pg_waldump_path,
150151
char *wal_directory);
@@ -480,8 +481,8 @@ parse_manifest_file(char *manifest_path)
480481
(long long int) statbuf.st_size);
481482
}
482483
bytes_left -= rc;
483-
json_parse_manifest_incremental_chunk(
484-
inc_state, buffer, rc, bytes_left == 0);
484+
json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
485+
bytes_left == 0);
485486
}
486487

487488
/* Release the incremental state memory */
@@ -812,9 +813,12 @@ verify_backup_checksums(verifier_context *context)
812813
manifest_data *manifest = context->manifest;
813814
manifest_files_iterator it;
814815
manifest_file *m;
816+
uint8 *buffer;
815817

816818
progress_report(false);
817819

820+
buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
821+
818822
manifest_files_start_iterate(manifest->files, &it);
819823
while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
820824
{
@@ -828,13 +832,15 @@ verify_backup_checksums(verifier_context *context)
828832
m->pathname);
829833

830834
/* Do the actual checksum verification. */
831-
verify_file_checksum(context, m, fullpath);
835+
verify_file_checksum(context, m, fullpath, buffer);
832836

833837
/* Avoid leaking memory. */
834838
pfree(fullpath);
835839
}
836840
}
837841

842+
pfree(buffer);
843+
838844
progress_report(true);
839845
}
840846

@@ -843,14 +849,13 @@ verify_backup_checksums(verifier_context *context)
843849
*/
844850
static void
845851
verify_file_checksum(verifier_context *context, manifest_file *m,
846-
char *fullpath)
852+
char *fullpath, uint8 *buffer)
847853
{
848854
pg_checksum_context checksum_ctx;
849855
char *relpath = m->pathname;
850856
int fd;
851857
int rc;
852858
size_t bytes_read = 0;
853-
uint8 buffer[READ_CHUNK_SIZE];
854859
uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
855860
int checksumlen;
856861

0 commit comments

Comments
 (0)