Skip to content

Commit fbeb9da

Browse files
committed
Improve error reporting for problems in text search configuration files
by installing an error context subroutine that will provide the file name and line number for all errors detected while reading a config file. Some of the reader routines were already doing that in an ad-hoc way for errors detected directly in the reader, but it didn't help for problems detected in subroutines, such as encoding violations. Back-patch to 8.3 because 8.3 is where people will be trying to debug configuration files.
1 parent 9de09c0 commit fbeb9da

File tree

7 files changed

+183
-103
lines changed

7 files changed

+183
-103
lines changed

contrib/dict_xsyn/dict_xsyn.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2007-2008, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/contrib/dict_xsyn/dict_xsyn.c,v 1.4 2008/01/01 20:31:21 tgl Exp $
9+
* $PostgreSQL: pgsql/contrib/dict_xsyn/dict_xsyn.c,v 1.5 2008/06/18 20:55:42 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -16,7 +16,6 @@
1616

1717
#include "commands/defrem.h"
1818
#include "fmgr.h"
19-
#include "storage/fd.h"
2019
#include "tsearch/ts_locale.h"
2120
#include "tsearch/ts_utils.h"
2221

@@ -75,17 +74,17 @@ static void
7574
read_dictionary(DictSyn *d, char *filename)
7675
{
7776
char *real_filename = get_tsearch_config_filename(filename, "rules");
78-
FILE *fin;
77+
tsearch_readline_state trst;
7978
char *line;
8079
int cur = 0;
8180

82-
if ((fin = AllocateFile(real_filename, "r")) == NULL)
81+
if (!tsearch_readline_begin(&trst, real_filename))
8382
ereport(ERROR,
8483
(errcode(ERRCODE_CONFIG_FILE_ERROR),
8584
errmsg("could not open synonym file \"%s\": %m",
8685
real_filename)));
8786

88-
while ((line = t_readline(fin)) != NULL)
87+
while ((line = tsearch_readline(&trst)) != NULL)
8988
{
9089
char *value;
9190
char *key;
@@ -119,7 +118,7 @@ read_dictionary(DictSyn *d, char *filename)
119118
cur++;
120119
}
121120

122-
FreeFile(fin);
121+
tsearch_readline_end(&trst);
123122

124123
d->len = cur;
125124
if (cur > 1)

src/backend/tsearch/dict_synonym.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/tsearch/dict_synonym.c,v 1.8 2008/03/10 03:01:28 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/tsearch/dict_synonym.c,v 1.9 2008/06/18 20:55:42 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616
#include "commands/defrem.h"
17-
#include "storage/fd.h"
1817
#include "tsearch/ts_locale.h"
1918
#include "tsearch/ts_public.h"
2019
#include "tsearch/ts_utils.h"
@@ -79,7 +78,7 @@ dsynonym_init(PG_FUNCTION_ARGS)
7978
ListCell *l;
8079
char *filename = NULL;
8180
bool case_sensitive = false;
82-
FILE *fin;
81+
tsearch_readline_state trst;
8382
char *starti,
8483
*starto,
8584
*end = NULL;
@@ -108,15 +107,15 @@ dsynonym_init(PG_FUNCTION_ARGS)
108107

109108
filename = get_tsearch_config_filename(filename, "syn");
110109

111-
if ((fin = AllocateFile(filename, "r")) == NULL)
110+
if (!tsearch_readline_begin(&trst, filename))
112111
ereport(ERROR,
113112
(errcode(ERRCODE_CONFIG_FILE_ERROR),
114113
errmsg("could not open synonym file \"%s\": %m",
115114
filename)));
116115

117116
d = (DictSyn *) palloc0(sizeof(DictSyn));
118117

119-
while ((line = t_readline(fin)) != NULL)
118+
while ((line = tsearch_readline(&trst)) != NULL)
120119
{
121120
starti = findwrd(line, &end);
122121
if (!starti)
@@ -175,7 +174,7 @@ dsynonym_init(PG_FUNCTION_ARGS)
175174
pfree(line);
176175
}
177176

178-
FreeFile(fin);
177+
tsearch_readline_end(&trst);
179178

180179
d->len = cur;
181180
qsort(d->syn, d->len, sizeof(Syn), compareSyn);

src/backend/tsearch/dict_thesaurus.c

+9-24
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/tsearch/dict_thesaurus.c,v 1.11 2008/01/01 19:45:52 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/tsearch/dict_thesaurus.c,v 1.12 2008/06/18 20:55:42 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616
#include "catalog/namespace.h"
1717
#include "commands/defrem.h"
18-
#include "storage/fd.h"
1918
#include "tsearch/ts_cache.h"
2019
#include "tsearch/ts_locale.h"
2120
#include "tsearch/ts_public.h"
@@ -169,30 +168,26 @@ addWrd(DictThesaurus *d, char *b, char *e, uint16 idsubst, uint16 nwrd, uint16 p
169168
static void
170169
thesaurusRead(char *filename, DictThesaurus *d)
171170
{
172-
FILE *fh;
173-
int lineno = 0;
171+
tsearch_readline_state trst;
174172
uint16 idsubst = 0;
175173
bool useasis = false;
176174
char *line;
177175

178176
filename = get_tsearch_config_filename(filename, "ths");
179-
fh = AllocateFile(filename, "r");
180-
if (!fh)
177+
if (!tsearch_readline_begin(&trst, filename))
181178
ereport(ERROR,
182179
(errcode(ERRCODE_CONFIG_FILE_ERROR),
183180
errmsg("could not open thesaurus file \"%s\": %m",
184181
filename)));
185182

186-
while ((line = t_readline(fh)) != NULL)
183+
while ((line = tsearch_readline(&trst)) != NULL)
187184
{
188185
char *ptr;
189186
int state = TR_WAITLEX;
190187
char *beginwrd = NULL;
191188
uint16 posinsubst = 0;
192189
uint16 nwrd = 0;
193190

194-
lineno++;
195-
196191
ptr = line;
197192

198193
/* is it a comment? */
@@ -213,13 +208,9 @@ thesaurusRead(char *filename, DictThesaurus *d)
213208
if (t_iseq(ptr, ':'))
214209
{
215210
if (posinsubst == 0)
216-
{
217-
FreeFile(fh);
218211
ereport(ERROR,
219212
(errcode(ERRCODE_CONFIG_FILE_ERROR),
220-
errmsg("unexpected delimiter at line %d of thesaurus file \"%s\"",
221-
lineno, filename)));
222-
}
213+
errmsg("unexpected delimiter")));
223214
state = TR_WAITSUBS;
224215
}
225216
else if (!t_isspace(ptr))
@@ -269,8 +260,7 @@ thesaurusRead(char *filename, DictThesaurus *d)
269260
if (ptr == beginwrd)
270261
ereport(ERROR,
271262
(errcode(ERRCODE_CONFIG_FILE_ERROR),
272-
errmsg("unexpected end of line or lexeme at line %d of thesaurus file \"%s\"",
273-
lineno, filename)));
263+
errmsg("unexpected end of line or lexeme")));
274264
addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
275265
state = TR_WAITSUBS;
276266
}
@@ -286,28 +276,23 @@ thesaurusRead(char *filename, DictThesaurus *d)
286276
if (ptr == beginwrd)
287277
ereport(ERROR,
288278
(errcode(ERRCODE_CONFIG_FILE_ERROR),
289-
errmsg("unexpected end of line or lexeme at line %d of thesaurus file \"%s\"",
290-
lineno, filename)));
279+
errmsg("unexpected end of line or lexeme")));
291280
addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
292281
}
293282

294283
idsubst++;
295284

296285
if (!(nwrd && posinsubst))
297-
{
298-
FreeFile(fh);
299286
ereport(ERROR,
300287
(errcode(ERRCODE_CONFIG_FILE_ERROR),
301-
errmsg("unexpected end of line at line %d of thesaurus file \"%s\"",
302-
lineno, filename)));
303-
}
288+
errmsg("unexpected end of line")));
304289

305290
pfree(line);
306291
}
307292

308293
d->nsubst = idsubst;
309294

310-
FreeFile(fh);
295+
tsearch_readline_end(&trst);
311296
}
312297

313298
static TheLexeme *

0 commit comments

Comments
 (0)