Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbd8913

Browse files
committed
Remove unportable assumption that it's okay to use the target buffer
of an sprintf() as a source string. Demonstrably does not work with recent gcc and/or glibc on some platforms.
1 parent 40f32f3 commit cbd8913

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 9 additions & 7 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.83 2005/01/01 05:43:07 momjian Exp $
4+
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.84 2005/01/13 01:40:13 tgl Exp $
55
*
66
*
77
* Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
@@ -1462,7 +1462,9 @@ get_th(char *num, int type)
14621462
static char *
14631463
str_numth(char *dest, char *num, int type)
14641464
{
1465-
sprintf(dest, "%s%s", num, get_th(num, type));
1465+
if (dest != num)
1466+
strcpy(dest, num);
1467+
strcat(dest, get_th(num, type));
14661468
return dest;
14671469
}
14681470

@@ -2057,6 +2059,7 @@ static int
20572059
dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
20582060
{
20592061
char buff[DCH_CACHE_SIZE],
2062+
workbuff[32],
20602063
*p_inout;
20612064
int i,
20622065
len;
@@ -2117,7 +2120,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21172120

21182121
switch (arg)
21192122
{
2120-
21212123
case DCH_A_D:
21222124
case DCH_B_C:
21232125
if (flag == TO_CHAR)
@@ -2179,8 +2181,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21792181
}
21802182
break;
21812183
case DCH_MONTH:
2182-
strcpy(inout, months_full[tm->tm_mon - 1]);
2183-
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, str_toupper(inout));
2184+
strcpy(workbuff, months_full[tm->tm_mon - 1]);
2185+
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, str_toupper(workbuff));
21842186
if (S_FM(suf))
21852187
return strlen(p_inout) - 1;
21862188
else
@@ -2242,8 +2244,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
22422244
}
22432245
break;
22442246
case DCH_DAY:
2245-
strcpy(inout, days[tm->tm_wday]);
2246-
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, str_toupper(inout));
2247+
strcpy(workbuff, days[tm->tm_wday]);
2248+
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, str_toupper(workbuff));
22472249
if (S_FM(suf))
22482250
return strlen(p_inout) - 1;
22492251
else

0 commit comments

Comments
 (0)