56
56
#endif
57
57
58
58
59
- #define MAX_TOKEN 256
60
-
61
59
/* callback data for check_network_callback */
62
60
typedef struct check_network_data
63
61
{
@@ -161,8 +159,8 @@ pg_isblank(const char c)
161
159
* commas, beginning of line, and end of line. Blank means space or tab.
162
160
*
163
161
* Tokens can be delimited by double quotes (this allows the inclusion of
164
- * blanks or '#', but not newlines). As in SQL, write two double-quotes
165
- * to represent a double quote.
162
+ * commas, blanks, and '#', but not newlines). As in SQL, write two
163
+ * double-quotes to represent a double quote.
166
164
*
167
165
* Comments (started by an unquoted '#') are skipped, i.e. the remainder
168
166
* of the line is ignored.
@@ -171,39 +169,34 @@ pg_isblank(const char c)
171
169
* Thus, if a continuation occurs within quoted text or a comment, the
172
170
* quoted text or comment is considered to continue to the next line.)
173
171
*
174
- * The token, if any, is returned at * buf (a buffer of size bufsz), and
175
- * *lineptr is advanced past the token.
172
+ * The token, if any, is returned into buf (replacing any previous
173
+ * contents), and *lineptr is advanced past the token.
176
174
*
177
175
* Also, we set *initial_quote to indicate whether there was quoting before
178
176
* the first character. (We use that to prevent "@x" from being treated
179
177
* as a file inclusion request. Note that @"x" should be so treated;
180
178
* we want to allow that to support embedded spaces in file paths.)
181
179
*
182
180
* We set *terminating_comma to indicate whether the token is terminated by a
183
- * comma (which is not returned).
181
+ * comma (which is not returned, nor advanced over ).
184
182
*
185
- * In event of an error, log a message at ereport level elevel, and also
186
- * set *err_msg to a string describing the error. Currently the only
187
- * possible error is token too long for buf.
183
+ * The only possible error condition is lack of terminating quote, but we
184
+ * currently do not detect that, but just return the rest of the line.
188
185
*
189
- * If successful: store null-terminated token at *buf and return true.
190
- * If no more tokens on line: set *buf = '\0' and return false.
191
- * If error: fill buf with truncated or misformatted token and return false.
186
+ * If successful: store dequoted token in buf and return true.
187
+ * If no more tokens on line: set buf to empty and return false.
192
188
*/
193
189
static bool
194
- next_token (char * * lineptr , char * buf , int bufsz ,
195
- bool * initial_quote , bool * terminating_comma ,
196
- int elevel , char * * err_msg )
190
+ next_token (char * * lineptr , StringInfo buf ,
191
+ bool * initial_quote , bool * terminating_comma )
197
192
{
198
193
int c ;
199
- char * start_buf = buf ;
200
- char * end_buf = buf + (bufsz - 1 );
201
194
bool in_quote = false;
202
195
bool was_quote = false;
203
196
bool saw_quote = false;
204
197
205
- Assert ( end_buf > start_buf );
206
-
198
+ /* Initialize output parameters */
199
+ resetStringInfo ( buf );
207
200
* initial_quote = false;
208
201
* terminating_comma = false;
209
202
@@ -226,22 +219,6 @@ next_token(char **lineptr, char *buf, int bufsz,
226
219
break ;
227
220
}
228
221
229
- if (buf >= end_buf )
230
- {
231
- * buf = '\0' ;
232
- ereport (elevel ,
233
- (errcode (ERRCODE_CONFIG_FILE_ERROR ),
234
- errmsg ("authentication file token too long, skipping: \"%s\"" ,
235
- start_buf )));
236
- * err_msg = "authentication file token too long" ;
237
- /* Discard remainder of line */
238
- while ((c = (* (* lineptr )++ )) != '\0' )
239
- ;
240
- /* Un-eat the '\0', in case we're called again */
241
- (* lineptr )-- ;
242
- return false;
243
- }
244
-
245
222
/* we do not pass back a terminating comma in the token */
246
223
if (c == ',' && !in_quote )
247
224
{
@@ -250,7 +227,7 @@ next_token(char **lineptr, char *buf, int bufsz,
250
227
}
251
228
252
229
if (c != '"' || was_quote )
253
- * buf ++ = c ;
230
+ appendStringInfoChar ( buf , c ) ;
254
231
255
232
/* Literal double-quote is two double-quotes */
256
233
if (in_quote && c == '"' )
@@ -262,7 +239,7 @@ next_token(char **lineptr, char *buf, int bufsz,
262
239
{
263
240
in_quote = !in_quote ;
264
241
saw_quote = true;
265
- if (buf == start_buf )
242
+ if (buf -> len == 0 )
266
243
* initial_quote = true;
267
244
}
268
245
@@ -275,9 +252,7 @@ next_token(char **lineptr, char *buf, int bufsz,
275
252
*/
276
253
(* lineptr )-- ;
277
254
278
- * buf = '\0' ;
279
-
280
- return (saw_quote || buf > start_buf );
255
+ return (saw_quote || buf -> len > 0 );
281
256
}
282
257
283
258
/*
@@ -409,21 +384,22 @@ static List *
409
384
next_field_expand (const char * filename , char * * lineptr ,
410
385
int elevel , int depth , char * * err_msg )
411
386
{
412
- char buf [ MAX_TOKEN ] ;
387
+ StringInfoData buf ;
413
388
bool trailing_comma ;
414
389
bool initial_quote ;
415
390
List * tokens = NIL ;
416
391
392
+ initStringInfo (& buf );
393
+
417
394
do
418
395
{
419
- if (!next_token (lineptr , buf , sizeof (buf ),
420
- & initial_quote , & trailing_comma ,
421
- elevel , err_msg ))
396
+ if (!next_token (lineptr , & buf ,
397
+ & initial_quote , & trailing_comma ))
422
398
break ;
423
399
424
400
/* Is this referencing a file? */
425
- if (!initial_quote && buf [ 0 ] == '@' && buf [ 1 ] != '\0 ' )
426
- tokens = tokenize_expand_file (tokens , filename , buf + 1 ,
401
+ if (!initial_quote && buf . len > 1 && buf . data [ 0 ] == '@ ' )
402
+ tokens = tokenize_expand_file (tokens , filename , buf . data + 1 ,
427
403
elevel , depth + 1 , err_msg );
428
404
else
429
405
{
@@ -434,11 +410,13 @@ next_field_expand(const char *filename, char **lineptr,
434
410
* for the list of tokens.
435
411
*/
436
412
oldcxt = MemoryContextSwitchTo (tokenize_context );
437
- tokens = lappend (tokens , make_auth_token (buf , initial_quote ));
413
+ tokens = lappend (tokens , make_auth_token (buf . data , initial_quote ));
438
414
MemoryContextSwitchTo (oldcxt );
439
415
}
440
416
} while (trailing_comma && (* err_msg == NULL ));
441
417
418
+ pfree (buf .data );
419
+
442
420
return tokens ;
443
421
}
444
422
0 commit comments