Commit 595a0eab authored by Tom Lane's avatar Tom Lane

Rationalize snprintf.c's handling of "ll" formats.

Although all known platforms define "long long" as 64 bits, it still feels
a bit shaky to be using "va_arg(args, int64)" to pull out an argument that
the caller thought was declared "long long".  The reason it was coded like
this, way back in commit 3311c766, was to work around the possibility that
the compiler had no type named "long long" --- and, at the time, that it
maybe didn't have 64-bit ints at all.  Now that we're requiring compilers
to support C99, those concerns are moot.  Let's make the code clearer and
more bulletproof by writing "long long" where we mean "long long".

This does introduce a hazard that we'd inefficiently use 128-bit arithmetic
to convert plain old integers.  The way to tackle that would be to provide
two versions of fmtint(), one for "long long" and one for narrower types.
Since, as of today, no platforms require that, we won't bother with the
extra code for now.

Discussion: https://postgr.es/m/1680.1538587115@sss.pgh.pa.us
parent 6d842be6
...@@ -157,7 +157,7 @@ typedef union ...@@ -157,7 +157,7 @@ typedef union
{ {
int i; int i;
long l; long l;
int64 ll; long long ll;
double d; double d;
char *cptr; char *cptr;
} PrintfArgValue; } PrintfArgValue;
...@@ -319,7 +319,7 @@ static bool find_arguments(const char *format, va_list args, ...@@ -319,7 +319,7 @@ static bool find_arguments(const char *format, va_list args,
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
int pointflag, PrintfTarget *target); int pointflag, PrintfTarget *target);
static void fmtptr(void *value, PrintfTarget *target); static void fmtptr(void *value, PrintfTarget *target);
static void fmtint(int64 value, char type, int forcesign, static void fmtint(long long value, char type, int forcesign,
int leftjust, int minlen, int zpad, int precision, int pointflag, int leftjust, int minlen, int zpad, int precision, int pointflag,
PrintfTarget *target); PrintfTarget *target);
static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target); static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target);
...@@ -390,7 +390,7 @@ dopr(PrintfTarget *target, const char *format, va_list args) ...@@ -390,7 +390,7 @@ dopr(PrintfTarget *target, const char *format, va_list args)
int forcesign; int forcesign;
int fmtpos; int fmtpos;
int cvalue; int cvalue;
int64 numvalue; long long numvalue;
double fvalue; double fvalue;
char *strvalue; char *strvalue;
PrintfArgValue argvalues[PG_NL_ARGMAX + 1]; PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
...@@ -603,7 +603,7 @@ nextch2: ...@@ -603,7 +603,7 @@ nextch2:
else else
{ {
if (longlongflag) if (longlongflag)
numvalue = va_arg(args, int64); numvalue = va_arg(args, long long);
else if (longflag) else if (longflag)
numvalue = va_arg(args, long); numvalue = va_arg(args, long);
else else
...@@ -626,7 +626,7 @@ nextch2: ...@@ -626,7 +626,7 @@ nextch2:
if (have_dollar) if (have_dollar)
{ {
if (longlongflag) if (longlongflag)
numvalue = (uint64) argvalues[fmtpos].ll; numvalue = (unsigned long long) argvalues[fmtpos].ll;
else if (longflag) else if (longflag)
numvalue = (unsigned long) argvalues[fmtpos].l; numvalue = (unsigned long) argvalues[fmtpos].l;
else else
...@@ -635,7 +635,7 @@ nextch2: ...@@ -635,7 +635,7 @@ nextch2:
else else
{ {
if (longlongflag) if (longlongflag)
numvalue = (uint64) va_arg(args, int64); numvalue = (unsigned long long) va_arg(args, long long);
else if (longflag) else if (longflag)
numvalue = (unsigned long) va_arg(args, long); numvalue = (unsigned long) va_arg(args, long);
else else
...@@ -944,7 +944,7 @@ nextch1: ...@@ -944,7 +944,7 @@ nextch1:
argvalues[i].l = va_arg(args, long); argvalues[i].l = va_arg(args, long);
break; break;
case ATYPE_LONGLONG: case ATYPE_LONGLONG:
argvalues[i].ll = va_arg(args, int64); argvalues[i].ll = va_arg(args, long long);
break; break;
case ATYPE_DOUBLE: case ATYPE_DOUBLE:
argvalues[i].d = va_arg(args, double); argvalues[i].d = va_arg(args, double);
...@@ -1002,11 +1002,11 @@ fmtptr(void *value, PrintfTarget *target) ...@@ -1002,11 +1002,11 @@ fmtptr(void *value, PrintfTarget *target)
} }
static void static void
fmtint(int64 value, char type, int forcesign, int leftjust, fmtint(long long value, char type, int forcesign, int leftjust,
int minlen, int zpad, int precision, int pointflag, int minlen, int zpad, int precision, int pointflag,
PrintfTarget *target) PrintfTarget *target)
{ {
uint64 base; unsigned long long base;
int dosign; int dosign;
const char *cvt = "0123456789abcdef"; const char *cvt = "0123456789abcdef";
int signvalue = 0; int signvalue = 0;
...@@ -1056,7 +1056,7 @@ fmtint(int64 value, char type, int forcesign, int leftjust, ...@@ -1056,7 +1056,7 @@ fmtint(int64 value, char type, int forcesign, int leftjust,
else else
{ {
/* make integer string */ /* make integer string */
uint64 uvalue = (uint64) value; unsigned long long uvalue = (unsigned long long) value;
do do
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment