Skip to content

Commit be13f22

Browse files
committed
Clean up formatting.c's logic for matching constant strings.
seq_search(), which is used to match input substrings to constants such as month and day names, had a lot of bizarre and unnecessary behaviors. It was mostly possible to avert our eyes from that before, but we don't want to duplicate those behaviors in the upcoming patch to allow recognition of non-English month and day names. So it's time to clean this up. In particular: * seq_search scribbled on the input string, which is a pretty dangerous thing to do, especially in the badly underdocumented way it was done here. Fortunately the input string is a temporary copy, but that was being made three subroutine levels away, making it something easy to break accidentally. The behavior is externally visible nonetheless, in the form of odd case-folding in error reports about unrecognized month/day names. The scribbling is evidently being done to save a few calls to pg_tolower, but that's such a cheap function (at least for ASCII data) that it's pretty pointless to worry about. In HEAD I switched it to be pg_ascii_tolower to ensure it is cheap in all cases; but there are corner cases in Turkish where this'd change behavior, so leave it as pg_tolower in the back branches. * seq_search insisted on knowing the case form (all-upper, all-lower, or initcap) of the constant strings, so that it didn't have to case-fold them to perform case-insensitive comparisons. This likewise seems like excessive micro-optimization, given that pg_tolower is certainly very cheap for ASCII data. It seems unsafe to assume that we know the case form that will come out of pg_locale.c for localized month/day names, so it's better just to define the comparison rule as "downcase all strings before comparing". (The choice between downcasing and upcasing is arbitrary so far as English is concerned, but it might not be in other locales, so follow citext's lead here.) * seq_search also had a parameter that'd cause it to report a match after a maximum number of characters, even if the constant string were longer than that. This was not actually used because no caller passed a value small enough to cut off a comparison. Replicating that behavior for localized month/day names seems expensive as well as useless, so let's get rid of that too. * from_char_seq_search used the maximum-length parameter to truncate the input string in error reports about not finding a matching name. This leads to rather confusing reports in many cases. Worse, it is outright dangerous if the input string isn't all-ASCII, because we risk truncating the string in the middle of a multibyte character. That'd lead either to delivering an illegible error message to the client, or to encoding-conversion failures that obscure the actual data problem. Get rid of that in favor of truncating at whitespace if any (a suggestion due to Alvaro Herrera). In addition to fixing these things, I const-ified the input string pointers of DCH_from_char and its subroutines, to make sure there aren't any other scribbling-on-input problems. The risk of generating a badly-encoded error message seems like enough of a bug to justify back-patching, so patch all supported branches. Discussion: https://postgr.es/m/29432.1579731087@sss.pgh.pa.us
1 parent 817a1b8 commit be13f22

File tree

2 files changed

+85
-91
lines changed

2 files changed

+85
-91
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 83 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787

8888
#include "catalog/pg_collation.h"
8989
#include "mb/pg_wchar.h"
90+
#include "parser/scansup.h"
9091
#include "utils/builtins.h"
9192
#include "utils/date.h"
9293
#include "utils/datetime.h"
@@ -278,18 +279,6 @@ static const char *const numth[] = {"st", "nd", "rd", "th", NULL};
278279
* Flags & Options:
279280
* ----------
280281
*/
281-
#define ONE_UPPER 1 /* Name */
282-
#define ALL_UPPER 2 /* NAME */
283-
#define ALL_LOWER 3 /* name */
284-
285-
#define FULL_SIZ 0
286-
287-
#define MAX_MONTH_LEN 9
288-
#define MAX_MON_LEN 3
289-
#define MAX_DAY_LEN 9
290-
#define MAX_DY_LEN 3
291-
#define MAX_RM_LEN 4
292-
293282
#define TH_UPPER 1
294283
#define TH_LOWER 2
295284

@@ -977,7 +966,7 @@ static void parse_format(FormatNode *node, const char *str, const KeyWord *kw,
977966

978967
static void DCH_to_char(FormatNode *node, bool is_interval,
979968
TmToChar *in, char *out, Oid collid);
980-
static void DCH_from_char(FormatNode *node, char *in, TmFromChar *out);
969+
static void DCH_from_char(FormatNode *node, const char *in, TmFromChar *out);
981970

982971
#ifdef DEBUG_TO_FROM_CHAR
983972
static void dump_index(const KeyWord *k, const int *index);
@@ -987,13 +976,15 @@ static void dump_node(FormatNode *node, int max);
987976
static const char *get_th(char *num, int type);
988977
static char *str_numth(char *dest, char *num, int type);
989978
static int adjust_partial_year_to_2020(int year);
990-
static int strspace_len(char *str);
979+
static int strspace_len(const char *str);
991980
static void from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode);
992981
static void from_char_set_int(int *dest, const int value, const FormatNode *node);
993-
static int from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node);
994-
static int from_char_parse_int(int *dest, char **src, FormatNode *node);
995-
static int seq_search(char *name, const char *const *array, int type, int max, int *len);
996-
static int from_char_seq_search(int *dest, char **src, const char *const *array, int type, int max, FormatNode *node);
982+
static int from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node);
983+
static int from_char_parse_int(int *dest, const char **src, FormatNode *node);
984+
static int seq_search(const char *name, const char *const *array, int *len);
985+
static int from_char_seq_search(int *dest, const char **src,
986+
const char *const *array,
987+
FormatNode *node);
997988
static void do_to_timestamp(text *date_txt, text *fmt,
998989
struct pg_tm *tm, fsec_t *fsec);
999990
static char *fill_str(char *str, int c, int max);
@@ -2157,7 +2148,7 @@ adjust_partial_year_to_2020(int year)
21572148

21582149

21592150
static int
2160-
strspace_len(char *str)
2151+
strspace_len(const char *str)
21612152
{
21622153
int len = 0;
21632154

@@ -2231,11 +2222,11 @@ from_char_set_int(int *dest, const int value, const FormatNode *node)
22312222
* with DD and MI).
22322223
*/
22332224
static int
2234-
from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
2225+
from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node)
22352226
{
22362227
long result;
22372228
char copy[DCH_MAX_ITEM_SIZ + 1];
2238-
char *init = *src;
2229+
const char *init = *src;
22392230
int used;
22402231

22412232
/*
@@ -2252,8 +2243,11 @@ from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
22522243
* This node is in Fill Mode, or the next node is known to be a
22532244
* non-digit value, so we just slurp as many characters as we can get.
22542245
*/
2246+
char *endptr;
2247+
22552248
errno = 0;
2256-
result = strtol(init, src, 10);
2249+
result = strtol(init, &endptr, 10);
2250+
*src = endptr;
22572251
}
22582252
else
22592253
{
@@ -2321,76 +2315,61 @@ from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
23212315
* required length explicitly.
23222316
*/
23232317
static int
2324-
from_char_parse_int(int *dest, char **src, FormatNode *node)
2318+
from_char_parse_int(int *dest, const char **src, FormatNode *node)
23252319
{
23262320
return from_char_parse_int_len(dest, src, node->key->len, node);
23272321
}
23282322

2329-
/* ----------
2330-
* Sequential search with to upper/lower conversion
2331-
* ----------
2323+
/*
2324+
* Sequentially search null-terminated "array" for a case-insensitive match
2325+
* to the initial character(s) of "name".
2326+
*
2327+
* Returns array index of match, or -1 for no match.
2328+
*
2329+
* *len is set to the length of the match, or 0 for no match.
2330+
*
2331+
* Case-insensitivity is defined per pg_tolower, so this is only
2332+
* suitable for comparisons to ASCII strings.
23322333
*/
23332334
static int
2334-
seq_search(char *name, const char *const *array, int type, int max, int *len)
2335+
seq_search(const char *name, const char *const *array, int *len)
23352336
{
2336-
const char *p;
2337+
unsigned char firstc;
23372338
const char *const *a;
2338-
char *n;
2339-
int last,
2340-
i;
23412339

23422340
*len = 0;
23432341

2342+
/* empty string can't match anything */
23442343
if (!*name)
23452344
return -1;
23462345

2347-
/* set first char */
2348-
if (type == ONE_UPPER || type == ALL_UPPER)
2349-
*name = pg_toupper((unsigned char) *name);
2350-
else if (type == ALL_LOWER)
2351-
*name = pg_tolower((unsigned char) *name);
2346+
/* we handle first char specially to gain some speed */
2347+
firstc = pg_tolower((unsigned char) *name);
23522348

2353-
for (last = 0, a = array; *a != NULL; a++)
2349+
for (a = array; *a != NULL; a++)
23542350
{
2351+
const char *p;
2352+
const char *n;
2353+
23552354
/* compare first chars */
2356-
if (*name != **a)
2355+
if (pg_tolower((unsigned char) **a) != firstc)
23572356
continue;
23582357

2359-
for (i = 1, p = *a + 1, n = name + 1;; n++, p++, i++)
2358+
/* compare rest of string */
2359+
for (p = *a + 1, n = name + 1;; p++, n++)
23602360
{
2361-
/* search fragment (max) only */
2362-
if (max && i == max)
2363-
{
2364-
*len = i;
2365-
return a - array;
2366-
}
2367-
/* full size */
2361+
/* return success if we matched whole array entry */
23682362
if (*p == '\0')
23692363
{
2370-
*len = i;
2364+
*len = n - name;
23712365
return a - array;
23722366
}
2373-
/* Not found in array 'a' */
2367+
/* else, must have another character in "name" ... */
23742368
if (*n == '\0')
23752369
break;
2376-
2377-
/*
2378-
* Convert (but convert new chars only)
2379-
*/
2380-
if (i > last)
2381-
{
2382-
if (type == ONE_UPPER || type == ALL_LOWER)
2383-
*n = pg_tolower((unsigned char) *n);
2384-
else if (type == ALL_UPPER)
2385-
*n = pg_toupper((unsigned char) *n);
2386-
last = i;
2387-
}
2388-
2389-
#ifdef DEBUG_TO_FROM_CHAR
2390-
elog(DEBUG_elog_output, "N: %c, P: %c, A: %s (%s)",
2391-
*n, *p, *a, name);
2392-
#endif
2393-
if (*n != *p)
2370+
/* ... and it must match */
2371+
if (pg_tolower((unsigned char) *p) !=
2372+
pg_tolower((unsigned char) *n))
23942373
break;
23952374
}
23962375
}
@@ -2399,28 +2378,43 @@ seq_search(char *name, const char *const *array, int type, int max, int *len)
23992378
}
24002379

24012380
/*
2402-
* Perform a sequential search in 'array' for text matching the first 'max'
2403-
* characters of the source string.
2381+
* Perform a sequential search in 'array' for an entry matching the first
2382+
* character(s) of the 'src' string case-insensitively.
24042383
*
24052384
* If a match is found, copy the array index of the match into the integer
24062385
* pointed to by 'dest', advance 'src' to the end of the part of the string
24072386
* which matched, and return the number of characters consumed.
24082387
*
24092388
* If the string doesn't match, throw an error.
2389+
*
2390+
* 'node' is used only for error reports: node->key->name identifies the
2391+
* field type we were searching for.
24102392
*/
24112393
static int
2412-
from_char_seq_search(int *dest, char **src, const char *const *array, int type, int max,
2394+
from_char_seq_search(int *dest, const char **src, const char *const *array,
24132395
FormatNode *node)
24142396
{
24152397
int len;
24162398

2417-
*dest = seq_search(*src, array, type, max, &len);
2399+
*dest = seq_search(*src, array, &len);
2400+
24182401
if (len <= 0)
24192402
{
2420-
char copy[DCH_MAX_ITEM_SIZ + 1];
2403+
/*
2404+
* In the error report, truncate the string at the next whitespace (if
2405+
* any) to avoid including irrelevant data.
2406+
*/
2407+
char *copy = pstrdup(*src);
2408+
char *c;
24212409

2422-
Assert(max <= DCH_MAX_ITEM_SIZ);
2423-
strlcpy(copy, *src, max + 1);
2410+
for (c = copy; *c; c++)
2411+
{
2412+
if (scanner_isspace(*c))
2413+
{
2414+
*c = '\0';
2415+
break;
2416+
}
2417+
}
24242418

24252419
ereport(ERROR,
24262420
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
@@ -3019,10 +3013,10 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
30193013
* ----------
30203014
*/
30213015
static void
3022-
DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
3016+
DCH_from_char(FormatNode *node, const char *in, TmFromChar *out)
30233017
{
30243018
FormatNode *n;
3025-
char *s;
3019+
const char *s;
30263020
int len,
30273021
value;
30283022
bool fx_mode = false;
@@ -3114,7 +3108,7 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
31143108
case DCH_a_m:
31153109
case DCH_p_m:
31163110
from_char_seq_search(&value, &s, ampm_strings_long,
3117-
ALL_UPPER, n->key->len, n);
3111+
n);
31183112
from_char_set_int(&out->pm, value % 2, n);
31193113
out->clock = CLOCK_12_HOUR;
31203114
break;
@@ -3123,7 +3117,7 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
31233117
case DCH_am:
31243118
case DCH_pm:
31253119
from_char_seq_search(&value, &s, ampm_strings,
3126-
ALL_UPPER, n->key->len, n);
3120+
n);
31273121
from_char_set_int(&out->pm, value % 2, n);
31283122
out->clock = CLOCK_12_HOUR;
31293123
break;
@@ -3214,29 +3208,29 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
32143208
case DCH_a_d:
32153209
case DCH_b_c:
32163210
from_char_seq_search(&value, &s, adbc_strings_long,
3217-
ALL_UPPER, n->key->len, n);
3211+
n);
32183212
from_char_set_int(&out->bc, value % 2, n);
32193213
break;
32203214
case DCH_AD:
32213215
case DCH_BC:
32223216
case DCH_ad:
32233217
case DCH_bc:
32243218
from_char_seq_search(&value, &s, adbc_strings,
3225-
ALL_UPPER, n->key->len, n);
3219+
n);
32263220
from_char_set_int(&out->bc, value % 2, n);
32273221
break;
32283222
case DCH_MONTH:
32293223
case DCH_Month:
32303224
case DCH_month:
3231-
from_char_seq_search(&value, &s, months_full, ONE_UPPER,
3232-
MAX_MONTH_LEN, n);
3225+
from_char_seq_search(&value, &s, months_full,
3226+
n);
32333227
from_char_set_int(&out->mm, value + 1, n);
32343228
break;
32353229
case DCH_MON:
32363230
case DCH_Mon:
32373231
case DCH_mon:
3238-
from_char_seq_search(&value, &s, months, ONE_UPPER,
3239-
MAX_MON_LEN, n);
3232+
from_char_seq_search(&value, &s, months,
3233+
n);
32403234
from_char_set_int(&out->mm, value + 1, n);
32413235
break;
32423236
case DCH_MM:
@@ -3246,16 +3240,16 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
32463240
case DCH_DAY:
32473241
case DCH_Day:
32483242
case DCH_day:
3249-
from_char_seq_search(&value, &s, days, ONE_UPPER,
3250-
MAX_DAY_LEN, n);
3243+
from_char_seq_search(&value, &s, days,
3244+
n);
32513245
from_char_set_int(&out->d, value, n);
32523246
out->d++;
32533247
break;
32543248
case DCH_DY:
32553249
case DCH_Dy:
32563250
case DCH_dy:
3257-
from_char_seq_search(&value, &s, days, ONE_UPPER,
3258-
MAX_DY_LEN, n);
3251+
from_char_seq_search(&value, &s, days,
3252+
n);
32593253
from_char_set_int(&out->d, value, n);
32603254
out->d++;
32613255
break;
@@ -3354,12 +3348,12 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
33543348
break;
33553349
case DCH_RM:
33563350
from_char_seq_search(&value, &s, rm_months_upper,
3357-
ALL_UPPER, MAX_RM_LEN, n);
3351+
n);
33583352
from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n);
33593353
break;
33603354
case DCH_rm:
33613355
from_char_seq_search(&value, &s, rm_months_lower,
3362-
ALL_LOWER, MAX_RM_LEN, n);
3356+
n);
33633357
from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n);
33643358
break;
33653359
case DCH_W:

src/test/regress/expected/horology.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
26162616
(1 row)
26172617

26182618
SELECT to_timestamp('97/Feb/16', 'YYMonDD');
2619-
ERROR: invalid value "/Fe" for "Mon"
2619+
ERROR: invalid value "/Feb/16" for "Mon"
26202620
DETAIL: The given value did not match any of the allowed values for this field.
26212621
SELECT to_timestamp('97/Feb/16', 'YY:Mon:DD');
26222622
to_timestamp
@@ -2862,7 +2862,7 @@ SELECT to_timestamp('2000 ++ JUN', 'YYYY MON');
28622862
(1 row)
28632863

28642864
SELECT to_timestamp('2000 + + JUN', 'YYYY MON');
2865-
ERROR: invalid value "+ J" for "MON"
2865+
ERROR: invalid value "+" for "MON"
28662866
DETAIL: The given value did not match any of the allowed values for this field.
28672867
SELECT to_timestamp('2000 + + JUN', 'YYYY MON');
28682868
to_timestamp

0 commit comments

Comments
 (0)