Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Don't allocate large buffer on the stack in pg_verifybackup
authorAndrew Dunstan <andrew@dunslane.net>
Fri, 12 Apr 2024 14:52:25 +0000 (10:52 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Fri, 12 Apr 2024 14:52:25 +0000 (10:52 -0400)
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

src/bin/pg_verifybackup/pg_verifybackup.c

index 9594c615c7cb508fb4d1ec99a29af6c18f0b7d27..fd610c20a654639c7365a4570d5e429962c38a81 100644 (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;