28
28
#include "fe-auth.h"
29
29
#include "libpq-fe.h"
30
30
#include "libpq-int.h"
31
+ #include "lib/stringinfo.h"
31
32
#include "mb/pg_wchar.h"
32
33
#include "pg_config_paths.h"
33
34
#include "port/pg_bswap.h"
@@ -5011,8 +5012,6 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
5011
5012
5012
5013
#endif /* USE_LDAP */
5013
5014
5014
- #define MAXBUFSIZE 256
5015
-
5016
5015
/*
5017
5016
* parseServiceInfo: if a service name has been given, look it up and absorb
5018
5017
* connection options from it into *options.
@@ -5099,11 +5098,14 @@ parseServiceFile(const char *serviceFile,
5099
5098
PQExpBuffer errorMessage ,
5100
5099
bool * group_found )
5101
5100
{
5102
- int linenr = 0 ,
5101
+ int result = 0 ,
5102
+ linenr = 0 ,
5103
5103
i ;
5104
5104
FILE * f ;
5105
- char buf [MAXBUFSIZE ],
5106
- * line ;
5105
+ char * line ;
5106
+ StringInfoData linebuf ;
5107
+
5108
+ * group_found = false;
5107
5109
5108
5110
f = fopen (serviceFile , "r" );
5109
5111
if (f == NULL )
@@ -5113,26 +5115,18 @@ parseServiceFile(const char *serviceFile,
5113
5115
return 1 ;
5114
5116
}
5115
5117
5116
- while ((line = fgets (buf , sizeof (buf ), f )) != NULL )
5117
- {
5118
- int len ;
5118
+ initStringInfo (& linebuf );
5119
5119
5120
+ while (pg_get_line_buf (f , & linebuf ))
5121
+ {
5120
5122
linenr ++ ;
5121
5123
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
-
5132
5124
/* 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 ;
5136
5130
5137
5131
/* ignore leading whitespace too */
5138
5132
while (* line && isspace ((unsigned char ) line [0 ]))
@@ -5147,9 +5141,8 @@ parseServiceFile(const char *serviceFile,
5147
5141
{
5148
5142
if (* group_found )
5149
5143
{
5150
- /* group info already read */
5151
- fclose (f );
5152
- return 0 ;
5144
+ /* end of desired group reached; return success */
5145
+ goto exit ;
5153
5146
}
5154
5147
5155
5148
if (strncmp (line + 1 , service , strlen (service )) == 0 &&
@@ -5178,12 +5171,11 @@ parseServiceFile(const char *serviceFile,
5178
5171
switch (rc )
5179
5172
{
5180
5173
case 0 :
5181
- fclose (f );
5182
- return 0 ;
5174
+ goto exit ;
5183
5175
case 1 :
5184
5176
case 3 :
5185
- fclose ( f ) ;
5186
- return 3 ;
5177
+ result = 3 ;
5178
+ goto exit ;
5187
5179
case 2 :
5188
5180
continue ;
5189
5181
}
@@ -5198,8 +5190,8 @@ parseServiceFile(const char *serviceFile,
5198
5190
libpq_gettext ("syntax error in service file \"%s\", line %d\n" ),
5199
5191
serviceFile ,
5200
5192
linenr );
5201
- fclose ( f ) ;
5202
- return 3 ;
5193
+ result = 3 ;
5194
+ goto exit ;
5203
5195
}
5204
5196
* val ++ = '\0' ;
5205
5197
@@ -5209,8 +5201,8 @@ parseServiceFile(const char *serviceFile,
5209
5201
libpq_gettext ("nested service specifications not supported in service file \"%s\", line %d\n" ),
5210
5202
serviceFile ,
5211
5203
linenr );
5212
- fclose ( f ) ;
5213
- return 3 ;
5204
+ result = 3 ;
5205
+ goto exit ;
5214
5206
}
5215
5207
5216
5208
/*
@@ -5228,8 +5220,8 @@ parseServiceFile(const char *serviceFile,
5228
5220
{
5229
5221
printfPQExpBuffer (errorMessage ,
5230
5222
libpq_gettext ("out of memory\n" ));
5231
- fclose ( f ) ;
5232
- return 3 ;
5223
+ result = 3 ;
5224
+ goto exit ;
5233
5225
}
5234
5226
found_keyword = true;
5235
5227
break ;
@@ -5242,16 +5234,18 @@ parseServiceFile(const char *serviceFile,
5242
5234
libpq_gettext ("syntax error in service file \"%s\", line %d\n" ),
5243
5235
serviceFile ,
5244
5236
linenr );
5245
- fclose ( f ) ;
5246
- return 3 ;
5237
+ result = 3 ;
5238
+ goto exit ;
5247
5239
}
5248
5240
}
5249
5241
}
5250
5242
}
5251
5243
5244
+ exit :
5252
5245
fclose (f );
5246
+ pfree (linebuf .data );
5253
5247
5254
- return 0 ;
5248
+ return result ;
5255
5249
}
5256
5250
5257
5251
0 commit comments