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

Commit d9b01c1

Browse files
committed
Avoid failures in cash_out and cash_words for INT_MIN.
Also, 'fourty' -> 'forty'.
1 parent a2b4dbd commit d9b01c1

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/backend/utils/adt/cash.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* workings can be found in the book "Software Solutions in C" by
1010
* Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
1111
*
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.51 2001/10/25 05:49:43 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.52 2002/02/19 22:19:34 tgl Exp $
1313
*/
1414

1515
#include "postgres.h"
@@ -287,7 +287,7 @@ cash_out(PG_FUNCTION_ARGS)
287287
if (value < 0)
288288
{
289289
minus = 1;
290-
value *= -1;
290+
value = -value;
291291
}
292292

293293
/* allow for trailing negative strings */
@@ -301,8 +301,8 @@ cash_out(PG_FUNCTION_ARGS)
301301
else if (comma && count % (mon_group + 1) == comma_position)
302302
buf[count--] = comma;
303303

304-
buf[count--] = (value % 10) + '0';
305-
value /= 10;
304+
buf[count--] = ((unsigned int) value % 10) + '0';
305+
value = ((unsigned int) value) / 10;
306306
}
307307

308308
strncpy((buf + count - strlen(csymbol) + 1), csymbol, strlen(csymbol));
@@ -664,6 +664,7 @@ Datum
664664
cash_words(PG_FUNCTION_ARGS)
665665
{
666666
Cash value = PG_GETARG_CASH(0);
667+
unsigned int val;
667668
char buf[128];
668669
char *p = buf;
669670
Cash m0;
@@ -682,10 +683,13 @@ cash_words(PG_FUNCTION_ARGS)
682683
else
683684
buf[0] = '\0';
684685

685-
m0 = value % 100; /* cents */
686-
m1 = (value / 100) % 1000; /* hundreds */
687-
m2 = (value / 100000) % 1000; /* thousands */
688-
m3 = value / 100000000 % 1000; /* millions */
686+
/* Now treat as unsigned, to avoid trouble at INT_MIN */
687+
val = (unsigned int) value;
688+
689+
m0 = val % 100; /* cents */
690+
m1 = (val / 100) % 1000; /* hundreds */
691+
m2 = (val / 100000) % 1000; /* thousands */
692+
m3 = val / 100000000 % 1000; /* millions */
689693

690694
if (m3)
691695
{
@@ -705,7 +709,7 @@ cash_words(PG_FUNCTION_ARGS)
705709
if (!*p)
706710
strcat(buf, "zero");
707711

708-
strcat(buf, (int) (value / 100) == 1 ? " dollar and " : " dollars and ");
712+
strcat(buf, (val / 100) == 1 ? " dollar and " : " dollars and ");
709713
strcat(buf, num_word(m0));
710714
strcat(buf, m0 == 1 ? " cent" : " cents");
711715

@@ -733,7 +737,7 @@ num_word(Cash value)
733737
"zero", "one", "two", "three", "four", "five", "six", "seven",
734738
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
735739
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
736-
"thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
740+
"thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
737741
};
738742
const char **big = small + 18;
739743
int tu = value % 100;

0 commit comments

Comments
 (0)