diff options
| author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2012-10-13 12:48:14 +0300 |
|---|---|---|
| committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2012-10-15 10:54:33 +0300 |
| commit | 1c95b5eea6bec742d5d6ee097c55690712dd3be6 (patch) | |
| tree | fb7c1706e3059b5f8b05a7a5c3f2195c2c43922a /src/bin/initdb/initdb.c | |
| parent | 5c07de4bdfadd2ea73c66dcad3b0e4d5ca4ceef7 (diff) | |
Fix race condition in pg_ctl reading postmaster.pid.
If postmaster changed postmaster.pid while pg_ctl was reading it, pg_ctl
could overrun the buffer it allocated for the file. Fix by reading the
whole file to memory with one read() call.
initdb contains an identical copy of the readfile() function, but the files
that initdb reads are static, not modified concurrently. Nevertheless, add
a simple bounds-check there, if only to silence static analysis tools.
Per report from Dave Vitek. Backpatch to all supported branches.
Diffstat (limited to 'src/bin/initdb/initdb.c')
| -rw-r--r-- | src/bin/initdb/initdb.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index d5101fad2fc..f3e1d90dc41 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -368,6 +368,7 @@ readfile(const char *path) int maxlength = 1, linelen = 0; int nlines = 0; + int n; char **result; char *buffer; int c; @@ -405,13 +406,13 @@ readfile(const char *path) /* now reprocess the file and store the lines */ rewind(infile); - nlines = 0; - while (fgets(buffer, maxlength + 1, infile) != NULL) - result[nlines++] = xstrdup(buffer); + n = 0; + while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines) + result[n++] = xstrdup(buffer); fclose(infile); free(buffer); - result[nlines] = NULL; + result[n] = NULL; return result; } |
