Skip to content

Commit c0cb87f

Browse files
committed
Remove arbitrary line length limit for libpq service files.
Use a StringInfo instead of a fixed-size buffer in parseServiceInfo(). While we've not heard complaints about the existing 255-byte limit, it certainly seems possible that complex cases could run afoul of it. Daniel Gustafsson Discussion: https://postgr.es/m/48A4FA71-524E-41B9-953A-FD04EF36E2E7@yesql.se
1 parent 9314870 commit c0cb87f

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "fe-auth.h"
2929
#include "libpq-fe.h"
3030
#include "libpq-int.h"
31+
#include "lib/stringinfo.h"
3132
#include "mb/pg_wchar.h"
3233
#include "pg_config_paths.h"
3334
#include "port/pg_bswap.h"
@@ -5011,8 +5012,6 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
50115012

50125013
#endif /* USE_LDAP */
50135014

5014-
#define MAXBUFSIZE 256
5015-
50165015
/*
50175016
* parseServiceInfo: if a service name has been given, look it up and absorb
50185017
* connection options from it into *options.
@@ -5099,11 +5098,14 @@ parseServiceFile(const char *serviceFile,
50995098
PQExpBuffer errorMessage,
51005099
bool *group_found)
51015100
{
5102-
int linenr = 0,
5101+
int result = 0,
5102+
linenr = 0,
51035103
i;
51045104
FILE *f;
5105-
char buf[MAXBUFSIZE],
5106-
*line;
5105+
char *line;
5106+
StringInfoData linebuf;
5107+
5108+
*group_found = false;
51075109

51085110
f = fopen(serviceFile, "r");
51095111
if (f == NULL)
@@ -5113,26 +5115,18 @@ parseServiceFile(const char *serviceFile,
51135115
return 1;
51145116
}
51155117

5116-
while ((line = fgets(buf, sizeof(buf), f)) != NULL)
5117-
{
5118-
int len;
5118+
initStringInfo(&linebuf);
51195119

5120+
while (pg_get_line_buf(f, &linebuf))
5121+
{
51205122
linenr++;
51215123

5122-
if (strlen(line) >= sizeof(buf) - 1)
5123-
{
5124-
fclose(f);
5125-
printfPQExpBuffer(errorMessage,
5126-
libpq_gettext("line %d too long in service file \"%s\"\n"),
5127-
linenr,
5128-
serviceFile);
5129-
return 2;
5130-
}
5131-
51325124
/* ignore whitespace at end of line, especially the newline */
5133-
len = strlen(line);
5134-
while (len > 0 && isspace((unsigned char) line[len - 1]))
5135-
line[--len] = '\0';
5125+
while (linebuf.len > 0 &&
5126+
isspace((unsigned char) linebuf.data[linebuf.len - 1]))
5127+
linebuf.data[--linebuf.len] = '\0';
5128+
5129+
line = linebuf.data;
51365130

51375131
/* ignore leading whitespace too */
51385132
while (*line && isspace((unsigned char) line[0]))
@@ -5147,9 +5141,8 @@ parseServiceFile(const char *serviceFile,
51475141
{
51485142
if (*group_found)
51495143
{
5150-
/* group info already read */
5151-
fclose(f);
5152-
return 0;
5144+
/* end of desired group reached; return success */
5145+
goto exit;
51535146
}
51545147

51555148
if (strncmp(line + 1, service, strlen(service)) == 0 &&
@@ -5178,12 +5171,11 @@ parseServiceFile(const char *serviceFile,
51785171
switch (rc)
51795172
{
51805173
case 0:
5181-
fclose(f);
5182-
return 0;
5174+
goto exit;
51835175
case 1:
51845176
case 3:
5185-
fclose(f);
5186-
return 3;
5177+
result = 3;
5178+
goto exit;
51875179
case 2:
51885180
continue;
51895181
}
@@ -5198,8 +5190,8 @@ parseServiceFile(const char *serviceFile,
51985190
libpq_gettext("syntax error in service file \"%s\", line %d\n"),
51995191
serviceFile,
52005192
linenr);
5201-
fclose(f);
5202-
return 3;
5193+
result = 3;
5194+
goto exit;
52035195
}
52045196
*val++ = '\0';
52055197

@@ -5209,8 +5201,8 @@ parseServiceFile(const char *serviceFile,
52095201
libpq_gettext("nested service specifications not supported in service file \"%s\", line %d\n"),
52105202
serviceFile,
52115203
linenr);
5212-
fclose(f);
5213-
return 3;
5204+
result = 3;
5205+
goto exit;
52145206
}
52155207

52165208
/*
@@ -5228,8 +5220,8 @@ parseServiceFile(const char *serviceFile,
52285220
{
52295221
printfPQExpBuffer(errorMessage,
52305222
libpq_gettext("out of memory\n"));
5231-
fclose(f);
5232-
return 3;
5223+
result = 3;
5224+
goto exit;
52335225
}
52345226
found_keyword = true;
52355227
break;
@@ -5242,16 +5234,18 @@ parseServiceFile(const char *serviceFile,
52425234
libpq_gettext("syntax error in service file \"%s\", line %d\n"),
52435235
serviceFile,
52445236
linenr);
5245-
fclose(f);
5246-
return 3;
5237+
result = 3;
5238+
goto exit;
52475239
}
52485240
}
52495241
}
52505242
}
52515243

5244+
exit:
52525245
fclose(f);
5246+
pfree(linebuf.data);
52535247

5254-
return 0;
5248+
return result;
52555249
}
52565250

52575251

0 commit comments

Comments
 (0)