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

Commit 4868e44

Browse files
committed
Ensure that snprintf.c's fmtint() doesn't overflow when printing INT64_MIN.
This isn't actually a live bug, as the output happens to be the same. But it upsets tools like UBSan, which makes it worthwhile to fix. As it's an issue without practical consequences, don't backpatch. Author: Andres Freund Discussion: https://postgr.es/m/20180928001121.hhx5n6dsygqxr5wu@alap3.anarazel.de
1 parent 9a3cebe commit 4868e44

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/port/snprintf.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ fmtint(long long value, char type, int forcesign, int leftjust,
10071007
PrintfTarget *target)
10081008
{
10091009
unsigned long long base;
1010+
unsigned long long uvalue;
10101011
int dosign;
10111012
const char *cvt = "0123456789abcdef";
10121013
int signvalue = 0;
@@ -1045,7 +1046,9 @@ fmtint(long long value, char type, int forcesign, int leftjust,
10451046

10461047
/* Handle +/- */
10471048
if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
1048-
value = -value;
1049+
uvalue = -(uint64) value;
1050+
else
1051+
uvalue = (uint64) value;
10491052

10501053
/*
10511054
* SUS: the result of converting 0 with an explicit precision of 0 is no
@@ -1056,8 +1059,6 @@ fmtint(long long value, char type, int forcesign, int leftjust,
10561059
else
10571060
{
10581061
/* make integer string */
1059-
unsigned long long uvalue = (unsigned long long) value;
1060-
10611062
do
10621063
{
10631064
convert[sizeof(convert) - (++vallen)] = cvt[uvalue % base];

0 commit comments

Comments
 (0)