Skip to content

Commit 8ec00dc

Browse files
committed
Remove undesirable libpq dependency on stringinfo.c.
Commit c0cb87f unwisely introduced a dependency on the StringInfo machinery in fe-connect.c. We must not use that in libpq, because it will do a summary exit(1) if it hits OOM, and that is not appropriate behavior for a general-purpose library. The goal of allowing arbitrary line lengths in service files doesn't seem like it's worth a lot of effort, so revert back to the previous method of using a stack-allocated buffer and failing on buffer overflow. This isn't an exact revert though. I kept that patch's refactoring to have a single exit path, as that seems cleaner than having each error path know what to do to clean up. Also, I made the fixed-size buffer 1024 bytes not 256, just to push off the need for an expandable buffer some more. There is more to do here; in particular the lack of any mechanical check for this type of mistake now seems pretty hazardous. But this fix gets us back to the level of robustness we had in v13, anyway. Discussion: https://postgr.es/m/daeb22ec6ca8ef61e94d766a9b35fb03cabed38e.camel@vmware.com
1 parent d5a2c41 commit 8ec00dc

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "fe-auth.h"
2929
#include "libpq-fe.h"
3030
#include "libpq-int.h"
31-
#include "lib/stringinfo.h"
3231
#include "mb/pg_wchar.h"
3332
#include "pg_config_paths.h"
3433
#include "port/pg_bswap.h"
@@ -5163,7 +5162,7 @@ parseServiceFile(const char *serviceFile,
51635162
i;
51645163
FILE *f;
51655164
char *line;
5166-
StringInfoData linebuf;
5165+
char buf[1024];
51675166

51685167
*group_found = false;
51695168

@@ -5175,18 +5174,26 @@ parseServiceFile(const char *serviceFile,
51755174
return 1;
51765175
}
51775176

5178-
initStringInfo(&linebuf);
5179-
5180-
while (pg_get_line_buf(f, &linebuf))
5177+
while ((line = fgets(buf, sizeof(buf), f)) != NULL)
51815178
{
5179+
int len;
5180+
51825181
linenr++;
51835182

5184-
/* ignore whitespace at end of line, especially the newline */
5185-
while (linebuf.len > 0 &&
5186-
isspace((unsigned char) linebuf.data[linebuf.len - 1]))
5187-
linebuf.data[--linebuf.len] = '\0';
5183+
if (strlen(line) >= sizeof(buf) - 1)
5184+
{
5185+
appendPQExpBuffer(errorMessage,
5186+
libpq_gettext("line %d too long in service file \"%s\"\n"),
5187+
linenr,
5188+
serviceFile);
5189+
result = 2;
5190+
goto exit;
5191+
}
51885192

5189-
line = linebuf.data;
5193+
/* ignore whitespace at end of line, especially the newline */
5194+
len = strlen(line);
5195+
while (len > 0 && isspace((unsigned char) line[len - 1]))
5196+
line[--len] = '\0';
51905197

51915198
/* ignore leading whitespace too */
51925199
while (*line && isspace((unsigned char) line[0]))
@@ -5303,7 +5310,6 @@ parseServiceFile(const char *serviceFile,
53035310

53045311
exit:
53055312
fclose(f);
5306-
pfree(linebuf.data);
53075313

53085314
return result;
53095315
}

0 commit comments

Comments
 (0)