Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fccde77

Browse files
committed
Prevent to_char(interval) from dumping core on month-related formats
when a zero-month interval is given. Per discussion with Karel. Also, some desultory const-labeling of constant tables. More could be done along that line.
1 parent a703269 commit fccde77

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -----------------------------------------------------------------------
22
* formatting.c
33
*
4-
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.85 2005/03/25 16:08:40 tgl Exp $
4+
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.86 2005/03/26 00:41:31 tgl Exp $
55
*
66
*
77
* Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
@@ -133,18 +133,19 @@ typedef struct FormatNode FormatNode;
133133

134134
typedef struct
135135
{
136-
char *name; /* keyword */
137-
/* action for keyword */
138-
int len, /* keyword length */
139-
(*action) (int arg, char *inout, int suf, int flag, FormatNode *node, void *data),
140-
id; /* keyword id */
136+
const char *name; /* keyword */
137+
int len; /* keyword length */
138+
int (*action) (int arg, char *inout, /* action for keyword */
139+
int suf, int flag,
140+
FormatNode *node, void *data);
141+
int id; /* keyword id */
141142
bool isitdigit; /* is expected output/input digit */
142143
} KeyWord;
143144

144145
struct FormatNode
145146
{
146147
int type; /* node type */
147-
KeyWord *key; /* if node type is KEYWORD */
148+
const KeyWord *key; /* if node type is KEYWORD */
148149
int character, /* if node type is CHAR */
149150
suffix; /* keyword suffix */
150151
};
@@ -648,7 +649,7 @@ typedef enum
648649
* KeyWords for DATE-TIME version
649650
* ----------
650651
*/
651-
static KeyWord DCH_keywords[] = {
652+
static const KeyWord DCH_keywords[] = {
652653
/* keyword, len, func, type, isitdigit is in Index */
653654
{"A.D.", 4, dch_date, DCH_A_D, FALSE}, /* A */
654655
{"A.M.", 4, dch_time, DCH_A_M, FALSE},
@@ -745,7 +746,7 @@ static KeyWord DCH_keywords[] = {
745746
* KeyWords for NUMBER version (now, isitdigit info is not needful here..)
746747
* ----------
747748
*/
748-
static KeyWord NUM_keywords[] = {
749+
static const KeyWord NUM_keywords[] = {
749750
/* keyword, len, func. type is in Index */
750751
{",", 1, NULL, NUM_COMMA}, /* , */
751752
{".", 1, NULL, NUM_DEC}, /* . */
@@ -792,7 +793,7 @@ static KeyWord NUM_keywords[] = {
792793
* KeyWords index for DATE-TIME version
793794
* ----------
794795
*/
795-
static int DCH_index[KeyWord_INDEX_SIZE] = {
796+
static const int DCH_index[KeyWord_INDEX_SIZE] = {
796797
/*
797798
0 1 2 3 4 5 6 7 8 9
798799
*/
@@ -816,7 +817,7 @@ static int DCH_index[KeyWord_INDEX_SIZE] = {
816817
* KeyWords index for NUMBER version
817818
* ----------
818819
*/
819-
static int NUM_index[KeyWord_INDEX_SIZE] = {
820+
static const int NUM_index[KeyWord_INDEX_SIZE] = {
820821
/*
821822
0 1 2 3 4 5 6 7 8 9
822823
*/
@@ -876,15 +877,16 @@ typedef struct NUMProc
876877
* Functions
877878
* ----------
878879
*/
879-
static KeyWord *index_seq_search(char *str, KeyWord *kw, int *index);
880+
static const KeyWord *index_seq_search(char *str, const KeyWord *kw,
881+
const int *index);
880882
static KeySuffix *suff_search(char *str, KeySuffix *suf, int type);
881883
static void NUMDesc_prepare(NUMDesc *num, FormatNode *n);
882-
static void parse_format(FormatNode *node, char *str, KeyWord *kw,
883-
KeySuffix *suf, int *index, int ver, NUMDesc *Num);
884+
static void parse_format(FormatNode *node, char *str, const KeyWord *kw,
885+
KeySuffix *suf, const int *index, int ver, NUMDesc *Num);
884886
static char *DCH_processor(FormatNode *node, char *inout, int flag, void *data);
885887

886888
#ifdef DEBUG_TO_FROM_CHAR
887-
static void dump_index(KeyWord *k, int *index);
889+
static void dump_index(const KeyWord *k, const int *index);
888890
static void dump_node(FormatNode *node, int max);
889891
#endif
890892

@@ -924,8 +926,8 @@ static void NUM_cache_remove(NUMCacheEntry *ent);
924926
* (can't be used binary search in format parsing)
925927
* ----------
926928
*/
927-
static KeyWord *
928-
index_seq_search(char *str, KeyWord *kw, int *index)
929+
static const KeyWord *
930+
index_seq_search(char *str, const KeyWord *kw, const int *index)
929931
{
930932
int poz;
931933

@@ -934,8 +936,7 @@ index_seq_search(char *str, KeyWord *kw, int *index)
934936

935937
if ((poz = *(index + (*str - ' '))) > -1)
936938
{
937-
938-
KeyWord *k = kw + poz;
939+
const KeyWord *k = kw + poz;
939940

940941
do
941942
{
@@ -1167,8 +1168,8 @@ NUMDesc_prepare(NUMDesc *num, FormatNode *n)
11671168
* ----------
11681169
*/
11691170
static void
1170-
parse_format(FormatNode *node, char *str, KeyWord *kw,
1171-
KeySuffix *suf, int *index, int ver, NUMDesc *Num)
1171+
parse_format(FormatNode *node, char *str, const KeyWord *kw,
1172+
KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
11721173
{
11731174
KeySuffix *s;
11741175
FormatNode *n;
@@ -1594,7 +1595,7 @@ seq_search(char *name, char **array, int type, int max, int *len)
15941595
* ----------
15951596
*/
15961597
static void
1597-
dump_index(KeyWord *k, int *index)
1598+
dump_index(const KeyWord *k, const int *index)
15981599
{
15991600
int i,
16001601
count = 0,
@@ -2182,6 +2183,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21822183
}
21832184
break;
21842185
case DCH_MONTH:
2186+
if (!tm->tm_mon)
2187+
return -1;
21852188
strcpy(workbuff, months_full[tm->tm_mon - 1]);
21862189
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, str_toupper(workbuff));
21872190
if (S_FM(suf))
@@ -2190,13 +2193,17 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21902193
return 8;
21912194

21922195
case DCH_Month:
2196+
if (!tm->tm_mon)
2197+
return -1;
21932198
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, months_full[tm->tm_mon - 1]);
21942199
if (S_FM(suf))
21952200
return strlen(p_inout) - 1;
21962201
else
21972202
return 8;
21982203

21992204
case DCH_month:
2205+
if (!tm->tm_mon)
2206+
return -1;
22002207
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, months_full[tm->tm_mon - 1]);
22012208
*inout = pg_tolower((unsigned char) *inout);
22022209
if (S_FM(suf))
@@ -2205,15 +2212,21 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
22052212
return 8;
22062213

22072214
case DCH_MON:
2215+
if (!tm->tm_mon)
2216+
return -1;
22082217
strcpy(inout, months[tm->tm_mon - 1]);
22092218
inout = str_toupper(inout);
22102219
return 2;
22112220

22122221
case DCH_Mon:
2222+
if (!tm->tm_mon)
2223+
return -1;
22132224
strcpy(inout, months[tm->tm_mon - 1]);
22142225
return 2;
22152226

22162227
case DCH_mon:
2228+
if (!tm->tm_mon)
2229+
return -1;
22172230
strcpy(inout, months[tm->tm_mon - 1]);
22182231
*inout = pg_tolower((unsigned char) *inout);
22192232
return 2;
@@ -2228,7 +2241,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
22282241
return strlen(p_inout) - 1;
22292242
else
22302243
return 1;
2231-
22322244
}
22332245
else if (flag == FROM_CHAR)
22342246
{
@@ -2388,7 +2400,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
23882400
return strlen(p_inout) - 1;
23892401
else
23902402
return 1;
2391-
23922403
}
23932404
else if (flag == FROM_CHAR)
23942405
{
@@ -2407,14 +2418,15 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
24072418
case DCH_Q:
24082419
if (flag == TO_CHAR)
24092420
{
2421+
if (!tm->tm_mon)
2422+
return -1;
24102423
sprintf(inout, "%d", (tm->tm_mon - 1) / 3 + 1);
24112424
if (S_THth(suf))
24122425
{
24132426
str_numth(p_inout, inout, S_TH_TYPE(suf));
24142427
return 2;
24152428
}
24162429
return 0;
2417-
24182430
}
24192431
else if (flag == FROM_CHAR)
24202432
{
@@ -2613,6 +2625,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
26132625
case DCH_RM:
26142626
if (flag == TO_CHAR)
26152627
{
2628+
if (!tm->tm_mon)
2629+
return -1;
26162630
sprintf(inout, "%*s", S_FM(suf) ? 0 : -4,
26172631
rm_months_upper[12 - tm->tm_mon]);
26182632
if (S_FM(suf))
@@ -2634,6 +2648,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
26342648
case DCH_rm:
26352649
if (flag == TO_CHAR)
26362650
{
2651+
if (!tm->tm_mon)
2652+
return -1;
26372653
sprintf(inout, "%*s", S_FM(suf) ? 0 : -4,
26382654
rm_months_lower[12 - tm->tm_mon]);
26392655
if (S_FM(suf))

0 commit comments

Comments
 (0)