Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / backend / utils / misc / tzparser.c
blob0b388269f3371a8824e4be63a462fd340f8af763
1 /*-------------------------------------------------------------------------
3 * tzparser.c
4 * Functions for parsing timezone offset files
6 * Note: we generally should not throw any errors in this file, but instead
7 * try to return an error code. This is not completely bulletproof at
8 * present --- in particular out-of-memory will throw an error. Could
9 * probably fix with PG_TRY if necessary.
12 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * IDENTIFICATION
16 * $PostgreSQL$
18 *-------------------------------------------------------------------------
21 #include "postgres.h"
23 #include <ctype.h>
25 #include "miscadmin.h"
26 #include "storage/fd.h"
27 #include "utils/datetime.h"
28 #include "utils/memutils.h"
29 #include "utils/tzparser.h"
32 #define WHITESPACE " \t\n\r"
34 static int tz_elevel; /* to avoid passing this around a lot */
36 static bool validateTzEntry(tzEntry *tzentry);
37 static bool splitTzLine(const char *filename, int lineno,
38 char *line, tzEntry *tzentry);
39 static int addToArray(tzEntry **base, int *arraysize, int n,
40 tzEntry *entry, bool override);
41 static int ParseTzFile(const char *filename, int depth,
42 tzEntry **base, int *arraysize, int n);
46 * Apply additional validation checks to a tzEntry
48 * Returns TRUE if OK, else false
50 static bool
51 validateTzEntry(tzEntry *tzentry)
53 unsigned char *p;
56 * Check restrictions imposed by datetkntbl storage format (see
57 * datetime.c)
59 if (strlen(tzentry->abbrev) > TOKMAXLEN)
61 ereport(tz_elevel,
62 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
63 errmsg("time zone abbreviation \"%s\" is too long (maximum %d characters) in time zone file \"%s\", line %d",
64 tzentry->abbrev, TOKMAXLEN,
65 tzentry->filename, tzentry->lineno)));
66 return false;
68 if (tzentry->offset % 900 != 0)
70 ereport(tz_elevel,
71 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
72 errmsg("time zone offset %d is not a multiple of 900 sec (15 min) in time zone file \"%s\", line %d",
73 tzentry->offset,
74 tzentry->filename, tzentry->lineno)));
75 return false;
79 * Sanity-check the offset: shouldn't exceed 14 hours
81 if (tzentry->offset > 14 * 60 * 60 ||
82 tzentry->offset < -14 * 60 * 60)
84 ereport(tz_elevel,
85 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
86 errmsg("time zone offset %d is out of range in time zone file \"%s\", line %d",
87 tzentry->offset,
88 tzentry->filename, tzentry->lineno)));
89 return false;
93 * Convert abbrev to lowercase (must match datetime.c's conversion)
95 for (p = (unsigned char *) tzentry->abbrev; *p; p++)
96 *p = pg_tolower(*p);
98 return true;
102 * Attempt to parse the line as a timezone abbrev spec (name, offset, dst)
104 * Returns TRUE if OK, else false; data is stored in *tzentry
106 static bool
107 splitTzLine(const char *filename, int lineno, char *line, tzEntry *tzentry)
109 char *abbrev;
110 char *offset;
111 char *offset_endptr;
112 char *remain;
113 char *is_dst;
115 tzentry->lineno = lineno;
116 tzentry->filename = filename;
118 abbrev = strtok(line, WHITESPACE);
119 if (!abbrev)
121 ereport(tz_elevel,
122 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
123 errmsg("missing time zone abbreviation in time zone file \"%s\", line %d",
124 filename, lineno)));
125 return false;
127 tzentry->abbrev = abbrev;
129 offset = strtok(NULL, WHITESPACE);
130 if (!offset)
132 ereport(tz_elevel,
133 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
134 errmsg("missing time zone offset in time zone file \"%s\", line %d",
135 filename, lineno)));
136 return false;
138 tzentry->offset = strtol(offset, &offset_endptr, 10);
139 if (offset_endptr == offset || *offset_endptr != '\0')
141 ereport(tz_elevel,
142 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
143 errmsg("invalid number for time zone offset in time zone file \"%s\", line %d",
144 filename, lineno)));
145 return false;
148 is_dst = strtok(NULL, WHITESPACE);
149 if (is_dst && pg_strcasecmp(is_dst, "D") == 0)
151 tzentry->is_dst = true;
152 remain = strtok(NULL, WHITESPACE);
154 else
156 /* there was no 'D' dst specifier */
157 tzentry->is_dst = false;
158 remain = is_dst;
161 if (!remain) /* no more non-whitespace chars */
162 return true;
164 if (remain[0] != '#') /* must be a comment */
166 ereport(tz_elevel,
167 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
168 errmsg("invalid syntax in time zone file \"%s\", line %d",
169 filename, lineno)));
170 return false;
172 return true;
176 * Insert entry into sorted array
178 * *base: base address of array (changeable if must enlarge array)
179 * *arraysize: allocated length of array (changeable if must enlarge array)
180 * n: current number of valid elements in array
181 * entry: new data to insert
182 * override: TRUE if OK to override
184 * Returns the new array length (new value for n), or -1 if error
186 static int
187 addToArray(tzEntry **base, int *arraysize, int n,
188 tzEntry *entry, bool override)
190 tzEntry *arrayptr;
191 int low;
192 int high;
195 * Search the array for a duplicate; as a useful side effect, the array is
196 * maintained in sorted order. We use strcmp() to ensure we match the
197 * sort order datetime.c expects.
199 arrayptr = *base;
200 low = 0;
201 high = n - 1;
202 while (low <= high)
204 int mid = (low + high) >> 1;
205 tzEntry *midptr = arrayptr + mid;
206 int cmp;
208 cmp = strcmp(entry->abbrev, midptr->abbrev);
209 if (cmp < 0)
210 high = mid - 1;
211 else if (cmp > 0)
212 low = mid + 1;
213 else
216 * Found a duplicate entry; complain unless it's the same.
218 if (midptr->offset == entry->offset &&
219 midptr->is_dst == entry->is_dst)
221 /* return unchanged array */
222 return n;
224 if (override)
226 /* same abbrev but something is different, override */
227 midptr->offset = entry->offset;
228 midptr->is_dst = entry->is_dst;
229 return n;
231 /* same abbrev but something is different, complain */
232 ereport(tz_elevel,
233 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
234 errmsg("time zone abbreviation \"%s\" is multiply defined",
235 entry->abbrev),
236 errdetail("Entry in time zone file \"%s\", line %d, conflicts with entry in file \"%s\", line %d.",
237 midptr->filename, midptr->lineno,
238 entry->filename, entry->lineno)));
239 return -1;
244 * No match, insert at position "low".
246 if (n >= *arraysize)
248 *arraysize *= 2;
249 *base = (tzEntry *) repalloc(*base, *arraysize * sizeof(tzEntry));
252 arrayptr = *base + low;
254 memmove(arrayptr + 1, arrayptr, (n - low) * sizeof(tzEntry));
256 memcpy(arrayptr, entry, sizeof(tzEntry));
258 /* Must dup the abbrev to ensure it survives */
259 arrayptr->abbrev = pstrdup(entry->abbrev);
261 return n + 1;
265 * Parse a single timezone abbrev file --- can recurse to handle @INCLUDE
267 * filename: user-specified file name (does not include path)
268 * depth: current recursion depth
269 * *base: array for results (changeable if must enlarge array)
270 * *arraysize: allocated length of array (changeable if must enlarge array)
271 * n: current number of valid elements in array
273 * Returns the new array length (new value for n), or -1 if error
275 static int
276 ParseTzFile(const char *filename, int depth,
277 tzEntry **base, int *arraysize, int n)
279 char share_path[MAXPGPATH];
280 char file_path[MAXPGPATH];
281 FILE *tzFile;
282 char tzbuf[1024];
283 char *line;
284 tzEntry tzentry;
285 int lineno = 0;
286 bool override = false;
287 const char *p;
290 * We enforce that the filename is all alpha characters. This may be
291 * overly restrictive, but we don't want to allow access to anything
292 * outside the timezonesets directory, so for instance '/' *must* be
293 * rejected.
295 for (p = filename; *p; p++)
297 if (!isalpha((unsigned char) *p))
299 /* at level 0, we need no ereport since guc.c will say enough */
300 if (depth > 0)
301 ereport(tz_elevel,
302 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
303 errmsg("invalid time zone file name \"%s\"",
304 filename)));
305 return -1;
310 * The maximal recursion depth is a pretty arbitrary setting. It is hard
311 * to imagine that someone needs more than 3 levels so stick with this
312 * conservative setting until someone complains.
314 if (depth > 3)
316 ereport(tz_elevel,
317 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
318 errmsg("time zone file recursion limit exceeded in file \"%s\"",
319 filename)));
320 return -1;
323 get_share_path(my_exec_path, share_path);
324 snprintf(file_path, sizeof(file_path), "%s/timezonesets/%s",
325 share_path, filename);
326 tzFile = AllocateFile(file_path, "r");
327 if (!tzFile)
330 * Check to see if the problem is not the filename but the directory.
331 * This is worth troubling over because if the installation share/
332 * directory is missing or unreadable, this is likely to be the first
333 * place we notice a problem during postmaster startup.
335 int save_errno = errno;
336 DIR *tzdir;
338 snprintf(file_path, sizeof(file_path), "%s/timezonesets",
339 share_path);
340 tzdir = AllocateDir(file_path);
341 if (tzdir == NULL)
343 ereport(tz_elevel,
344 (errcode_for_file_access(),
345 errmsg("could not open directory \"%s\": %m",
346 file_path),
347 errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
348 my_exec_path)));
349 return -1;
351 FreeDir(tzdir);
352 errno = save_errno;
355 * otherwise, if file doesn't exist and it's level 0, guc.c's
356 * complaint is enough
358 if (errno != ENOENT || depth > 0)
359 ereport(tz_elevel,
360 (errcode_for_file_access(),
361 errmsg("could not read time zone file \"%s\": %m",
362 filename)));
364 return -1;
367 while (!feof(tzFile))
369 lineno++;
370 if (fgets(tzbuf, sizeof(tzbuf), tzFile) == NULL)
372 if (ferror(tzFile))
374 ereport(tz_elevel,
375 (errcode_for_file_access(),
376 errmsg("could not read time zone file \"%s\": %m",
377 filename)));
378 return -1;
380 /* else we're at EOF after all */
381 break;
383 if (strlen(tzbuf) == sizeof(tzbuf) - 1)
385 /* the line is too long for tzbuf */
386 ereport(tz_elevel,
387 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
388 errmsg("line is too long in time zone file \"%s\", line %d",
389 filename, lineno)));
390 return -1;
393 /* skip over whitespace */
394 line = tzbuf;
395 while (*line && isspace((unsigned char) *line))
396 line++;
398 if (*line == '\0') /* empty line */
399 continue;
400 if (*line == '#') /* comment line */
401 continue;
403 if (pg_strncasecmp(line, "@INCLUDE", strlen("@INCLUDE")) == 0)
405 /* pstrdup so we can use filename in result data structure */
406 char *includeFile = pstrdup(line + strlen("@INCLUDE"));
408 includeFile = strtok(includeFile, WHITESPACE);
409 if (!includeFile || !*includeFile)
411 ereport(tz_elevel,
412 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
413 errmsg("@INCLUDE without file name in time zone file \"%s\", line %d",
414 filename, lineno)));
415 return -1;
417 n = ParseTzFile(includeFile, depth + 1,
418 base, arraysize, n);
419 if (n < 0)
420 return -1;
421 continue;
424 if (pg_strncasecmp(line, "@OVERRIDE", strlen("@OVERRIDE")) == 0)
426 override = true;
427 continue;
430 if (!splitTzLine(filename, lineno, line, &tzentry))
431 return -1;
432 if (!validateTzEntry(&tzentry))
433 return -1;
434 n = addToArray(base, arraysize, n, &tzentry, override);
435 if (n < 0)
436 return -1;
439 FreeFile(tzFile);
441 return n;
445 * load_tzoffsets --- read and parse the specified timezone offset file
447 * filename: name specified by user
448 * doit: whether to actually apply the new values, or just check
449 * elevel: elog reporting level (will be less than ERROR)
451 * Returns TRUE if OK, FALSE if not; should avoid erroring out
453 bool
454 load_tzoffsets(const char *filename, bool doit, int elevel)
456 MemoryContext tmpContext;
457 MemoryContext oldContext;
458 tzEntry *array;
459 int arraysize;
460 int n;
462 tz_elevel = elevel;
465 * Create a temp memory context to work in. This makes it easy to clean
466 * up afterwards.
468 tmpContext = AllocSetContextCreate(CurrentMemoryContext,
469 "TZParserMemory",
470 ALLOCSET_SMALL_MINSIZE,
471 ALLOCSET_SMALL_INITSIZE,
472 ALLOCSET_SMALL_MAXSIZE);
473 oldContext = MemoryContextSwitchTo(tmpContext);
475 /* Initialize array at a reasonable size */
476 arraysize = 128;
477 array = (tzEntry *) palloc(arraysize * sizeof(tzEntry));
479 /* Parse the file(s) */
480 n = ParseTzFile(filename, 0, &array, &arraysize, 0);
482 /* If no errors and we should apply the result, pass it to datetime.c */
483 if (n >= 0 && doit)
484 InstallTimeZoneAbbrevs(array, n);
486 /* Clean up */
487 MemoryContextSwitchTo(oldContext);
488 MemoryContextDelete(tmpContext);
490 return (n >= 0);