0001-Use-a-stringInfo-instead-of-a-char-for-replace_strin.patch
application/octet-stream
Filename: 0001-Use-a-stringInfo-instead-of-a-char-for-replace_strin.patch
Type: application/octet-stream
Part: 0
Patch
Format: format-patch
Series: patch 0001
Subject: Use a stringInfo instead of a char for replace_string in pg_regress
| File | + | − |
|---|---|---|
| src/test/regress/pg_regress.c | 37 | 8 |
From 0834fecf59ae3a413af1028a1149a38b73dda822 Mon Sep 17 00:00:00 2001
From: Georgios Kokolatos <gkokolatos@protonmail.com>
Date: Wed, 5 Aug 2020 12:30:53 +0530
Subject: [PATCH] Use a stringInfo instead of a char for replace_string in
pg_regress
The exposed function replase_string() in pg_regress assumes that there exists
enough space in the string it operates on. While this is documented and
expected, the consequence can be subtle failures in the tests which might be
hard to trace back to the root cause.
Provide an alternative function that operates on a StringInfo instead.
Co-authored-by: Asim R P <pasim@vmware.com>
---
src/test/regress/pg_regress.c | 45 ++++++++++++++++++++++++++++-------
1 file changed, 37 insertions(+), 8 deletions(-)
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index d82e0189dcf..738069f61c4 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -33,6 +33,7 @@
#include "common/restricted_token.h"
#include "common/username.h"
#include "getopt_long.h"
+#include "lib/stringinfo.h"
#include "libpq/pqcomm.h" /* needed for UNIXSOCK_PATH() */
#include "pg_config_paths.h"
#include "pg_regress.h"
@@ -454,6 +455,27 @@ replace_string(char *string, const char *replace, const char *replacement)
}
}
+/*
+ * Replace all occurrences of a string in a stringInfo with a different string.
+ */
+static void
+replace_stringInfo(StringInfo string, const char *replace, const char *replacement)
+{
+ char *ptr;
+
+ while ((ptr = strstr(string->data, replace)) != NULL)
+ {
+ char *suffix = pnstrdup(ptr + strlen(replace), string->maxlen);
+ size_t pos = ptr - string->data;
+
+ string->len = pos;
+ appendStringInfoString(string, replacement);
+ appendStringInfoString(string, suffix);
+
+ free(suffix);
+ }
+}
+
/*
* Convert *.source found in the "source" directory, replacing certain tokens
* in the file contents with their intended values, and put the resulting files
@@ -521,7 +543,9 @@ convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const ch
char prefix[MAXPGPATH];
FILE *infile,
*outfile;
- char line[1024];
+ const char *fline;
+ size_t flinelen;
+ StringInfoData line;
/* reject filenames not finishing in ".source" */
if (strlen(*name) < 8)
@@ -551,15 +575,20 @@ convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const ch
progname, destfile, strerror(errno));
exit(2);
}
- while (fgets(line, sizeof(line), infile))
+
+ initStringInfo(&line);
+ while ((fline = fgetln(infile, &flinelen)))
{
- replace_string(line, "@abs_srcdir@", inputdir);
- replace_string(line, "@abs_builddir@", outputdir);
- replace_string(line, "@testtablespace@", testtablespace);
- replace_string(line, "@libdir@", dlpath);
- replace_string(line, "@DLSUFFIX@", DLSUFFIX);
- fputs(line, outfile);
+ appendBinaryStringInfo(&line, fline, flinelen);
+ replace_stringInfo(&line, "@abs_srcdir@", inputdir);
+ replace_stringInfo(&line, "@abs_builddir@", outputdir);
+ replace_stringInfo(&line, "@testtablespace@", testtablespace);
+ replace_stringInfo(&line, "@libdir@", dlpath);
+ replace_stringInfo(&line, "@DLSUFFIX@", DLSUFFIX);
+ fputs(line.data, outfile);
+ resetStringInfo(&line);
}
+ pfree(line.data);
fclose(infile);
fclose(outfile);
}
--
2.24.2 (Apple Git-127)