LCOV - code coverage report
Current view: top level - src/port - snprintf.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 414 614 67.4 %
Date: 2021-12-04 22:09:09 Functions: 22 24 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1983, 1995, 1996 Eric P. Allman
       3             :  * Copyright (c) 1988, 1993
       4             :  *  The Regents of the University of California.  All rights reserved.
       5             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       6             :  *
       7             :  * Redistribution and use in source and binary forms, with or without
       8             :  * modification, are permitted provided that the following conditions
       9             :  * are met:
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  * 2. Redistributions in binary form must reproduce the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer in the
      14             :  *    documentation and/or other materials provided with the distribution.
      15             :  * 3. Neither the name of the University nor the names of its contributors
      16             :  *    may be used to endorse or promote products derived from this software
      17             :  *    without specific prior written permission.
      18             :  *
      19             :  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      20             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      21             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      22             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      23             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      24             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      25             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      26             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      27             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      28             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      29             :  * SUCH DAMAGE.
      30             :  *
      31             :  * src/port/snprintf.c
      32             :  */
      33             : 
      34             : #include "c.h"
      35             : 
      36             : #include <math.h>
      37             : 
      38             : /*
      39             :  * We used to use the platform's NL_ARGMAX here, but that's a bad idea,
      40             :  * first because the point of this module is to remove platform dependencies
      41             :  * not perpetuate them, and second because some platforms use ridiculously
      42             :  * large values, leading to excessive stack consumption in dopr().
      43             :  */
      44             : #define PG_NL_ARGMAX 31
      45             : 
      46             : 
      47             : /*
      48             :  *  SNPRINTF, VSNPRINTF and friends
      49             :  *
      50             :  * These versions have been grabbed off the net.  They have been
      51             :  * cleaned up to compile properly and support for most of the C99
      52             :  * specification has been added.  Remaining unimplemented features are:
      53             :  *
      54             :  * 1. No locale support: the radix character is always '.' and the '
      55             :  * (single quote) format flag is ignored.
      56             :  *
      57             :  * 2. No support for the "%n" format specification.
      58             :  *
      59             :  * 3. No support for wide characters ("lc" and "ls" formats).
      60             :  *
      61             :  * 4. No support for "long double" ("Lf" and related formats).
      62             :  *
      63             :  * 5. Space and '#' flags are not implemented.
      64             :  *
      65             :  * In addition, we support some extensions over C99:
      66             :  *
      67             :  * 1. Argument order control through "%n$" and "*n$", as required by POSIX.
      68             :  *
      69             :  * 2. "%m" expands to the value of strerror(errno), where errno is the
      70             :  * value that variable had at the start of the call.  This is a glibc
      71             :  * extension, but a very useful one.
      72             :  *
      73             :  *
      74             :  * Historically the result values of sprintf/snprintf varied across platforms.
      75             :  * This implementation now follows the C99 standard:
      76             :  *
      77             :  * 1. -1 is returned if an error is detected in the format string, or if
      78             :  * a write to the target stream fails (as reported by fwrite).  Note that
      79             :  * overrunning snprintf's target buffer is *not* an error.
      80             :  *
      81             :  * 2. For successful writes to streams, the actual number of bytes written
      82             :  * to the stream is returned.
      83             :  *
      84             :  * 3. For successful sprintf/snprintf, the number of bytes that would have
      85             :  * been written to an infinite-size buffer (excluding the trailing '\0')
      86             :  * is returned.  snprintf will truncate its output to fit in the buffer
      87             :  * (ensuring a trailing '\0' unless count == 0), but this is not reflected
      88             :  * in the function result.
      89             :  *
      90             :  * snprintf buffer overrun can be detected by checking for function result
      91             :  * greater than or equal to the supplied count.
      92             :  */
      93             : 
      94             : /**************************************************************
      95             :  * Original:
      96             :  * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
      97             :  * A bombproof version of doprnt (dopr) included.
      98             :  * Sigh.  This sort of thing is always nasty do deal with.  Note that
      99             :  * the version here does not include floating point. (now it does ... tgl)
     100             :  **************************************************************/
     101             : 
     102             : /* Prevent recursion */
     103             : #undef  vsnprintf
     104             : #undef  snprintf
     105             : #undef  vsprintf
     106             : #undef  sprintf
     107             : #undef  vfprintf
     108             : #undef  fprintf
     109             : #undef  vprintf
     110             : #undef  printf
     111             : 
     112             : /*
     113             :  * Info about where the formatted output is going.
     114             :  *
     115             :  * dopr and subroutines will not write at/past bufend, but snprintf
     116             :  * reserves one byte, ensuring it may place the trailing '\0' there.
     117             :  *
     118             :  * In snprintf, we use nchars to count the number of bytes dropped on the
     119             :  * floor due to buffer overrun.  The correct result of snprintf is thus
     120             :  * (bufptr - bufstart) + nchars.  (This isn't as inconsistent as it might
     121             :  * seem: nchars is the number of emitted bytes that are not in the buffer now,
     122             :  * either because we sent them to the stream or because we couldn't fit them
     123             :  * into the buffer to begin with.)
     124             :  */
     125             : typedef struct
     126             : {
     127             :     char       *bufptr;         /* next buffer output position */
     128             :     char       *bufstart;       /* first buffer element */
     129             :     char       *bufend;         /* last+1 buffer element, or NULL */
     130             :     /* bufend == NULL is for sprintf, where we assume buf is big enough */
     131             :     FILE       *stream;         /* eventual output destination, or NULL */
     132             :     int         nchars;         /* # chars sent to stream, or dropped */
     133             :     bool        failed;         /* call is a failure; errno is set */
     134             : } PrintfTarget;
     135             : 
     136             : /*
     137             :  * Info about the type and value of a formatting parameter.  Note that we
     138             :  * don't currently support "long double", "wint_t", or "wchar_t *" data,
     139             :  * nor the '%n' formatting code; else we'd need more types.  Also, at this
     140             :  * level we need not worry about signed vs unsigned values.
     141             :  */
     142             : typedef enum
     143             : {
     144             :     ATYPE_NONE = 0,
     145             :     ATYPE_INT,
     146             :     ATYPE_LONG,
     147             :     ATYPE_LONGLONG,
     148             :     ATYPE_DOUBLE,
     149             :     ATYPE_CHARPTR
     150             : } PrintfArgType;
     151             : 
     152             : typedef union
     153             : {
     154             :     int         i;
     155             :     long        l;
     156             :     long long   ll;
     157             :     double      d;
     158             :     char       *cptr;
     159             : } PrintfArgValue;
     160             : 
     161             : 
     162             : static void flushbuffer(PrintfTarget *target);
     163             : static void dopr(PrintfTarget *target, const char *format, va_list args);
     164             : 
     165             : 
     166             : /*
     167             :  * Externally visible entry points.
     168             :  *
     169             :  * All of these are just wrappers around dopr().  Note it's essential that
     170             :  * they not change the value of "errno" before reaching dopr().
     171             :  */
     172             : 
     173             : int
     174   477678706 : pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
     175             : {
     176             :     PrintfTarget target;
     177             :     char        onebyte[1];
     178             : 
     179             :     /*
     180             :      * C99 allows the case str == NULL when count == 0.  Rather than
     181             :      * special-casing this situation further down, we substitute a one-byte
     182             :      * local buffer.  Callers cannot tell, since the function result doesn't
     183             :      * depend on count.
     184             :      */
     185   477678706 :     if (count == 0)
     186             :     {
     187       64786 :         str = onebyte;
     188       64786 :         count = 1;
     189             :     }
     190   477678706 :     target.bufstart = target.bufptr = str;
     191   477678706 :     target.bufend = str + count - 1;
     192   477678706 :     target.stream = NULL;
     193   477678706 :     target.nchars = 0;
     194   477678706 :     target.failed = false;
     195   477678706 :     dopr(&target, fmt, args);
     196   477678706 :     *(target.bufptr) = '\0';
     197   955357412 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     198   477678706 :                                  + target.nchars);
     199             : }
     200             : 
     201             : int
     202    14539680 : pg_snprintf(char *str, size_t count, const char *fmt,...)
     203             : {
     204             :     int         len;
     205             :     va_list     args;
     206             : 
     207    14539680 :     va_start(args, fmt);
     208    14539680 :     len = pg_vsnprintf(str, count, fmt, args);
     209    14539680 :     va_end(args);
     210    14539680 :     return len;
     211             : }
     212             : 
     213             : int
     214     3182398 : pg_vsprintf(char *str, const char *fmt, va_list args)
     215             : {
     216             :     PrintfTarget target;
     217             : 
     218     3182398 :     target.bufstart = target.bufptr = str;
     219     3182398 :     target.bufend = NULL;
     220     3182398 :     target.stream = NULL;
     221     3182398 :     target.nchars = 0;          /* not really used in this case */
     222     3182398 :     target.failed = false;
     223     3182398 :     dopr(&target, fmt, args);
     224     3182394 :     *(target.bufptr) = '\0';
     225     6364788 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     226     3182394 :                                  + target.nchars);
     227             : }
     228             : 
     229             : int
     230     3182400 : pg_sprintf(char *str, const char *fmt,...)
     231             : {
     232             :     int         len;
     233             :     va_list     args;
     234             : 
     235     3182400 :     va_start(args, fmt);
     236     3182400 :     len = pg_vsprintf(str, fmt, args);
     237     3182394 :     va_end(args);
     238     3182394 :     return len;
     239             : }
     240             : 
     241             : int
     242     1440280 : pg_vfprintf(FILE *stream, const char *fmt, va_list args)
     243             : {
     244             :     PrintfTarget target;
     245             :     char        buffer[1024];   /* size is arbitrary */
     246             : 
     247     1440280 :     if (stream == NULL)
     248             :     {
     249           0 :         errno = EINVAL;
     250           0 :         return -1;
     251             :     }
     252     1440280 :     target.bufstart = target.bufptr = buffer;
     253     1440280 :     target.bufend = buffer + sizeof(buffer);    /* use the whole buffer */
     254     1440280 :     target.stream = stream;
     255     1440280 :     target.nchars = 0;
     256     1440280 :     target.failed = false;
     257     1440280 :     dopr(&target, fmt, args);
     258             :     /* dump any remaining buffer contents */
     259     1440280 :     flushbuffer(&target);
     260     1440280 :     return target.failed ? -1 : target.nchars;
     261             : }
     262             : 
     263             : int
     264     1005720 : pg_fprintf(FILE *stream, const char *fmt,...)
     265             : {
     266             :     int         len;
     267             :     va_list     args;
     268             : 
     269     1005720 :     va_start(args, fmt);
     270     1005720 :     len = pg_vfprintf(stream, fmt, args);
     271     1005720 :     va_end(args);
     272     1005720 :     return len;
     273             : }
     274             : 
     275             : int
     276           0 : pg_vprintf(const char *fmt, va_list args)
     277             : {
     278           0 :     return pg_vfprintf(stdout, fmt, args);
     279             : }
     280             : 
     281             : int
     282      409042 : pg_printf(const char *fmt,...)
     283             : {
     284             :     int         len;
     285             :     va_list     args;
     286             : 
     287      409042 :     va_start(args, fmt);
     288      409042 :     len = pg_vfprintf(stdout, fmt, args);
     289      409042 :     va_end(args);
     290      409042 :     return len;
     291             : }
     292             : 
     293             : /*
     294             :  * Attempt to write the entire buffer to target->stream; discard the entire
     295             :  * buffer in any case.  Call this only when target->stream is defined.
     296             :  */
     297             : static void
     298     1440696 : flushbuffer(PrintfTarget *target)
     299             : {
     300     1440696 :     size_t      nc = target->bufptr - target->bufstart;
     301             : 
     302             :     /*
     303             :      * Don't write anything if we already failed; this is to ensure we
     304             :      * preserve the original failure's errno.
     305             :      */
     306     1440696 :     if (!target->failed && nc > 0)
     307             :     {
     308             :         size_t      written;
     309             : 
     310     1290020 :         written = fwrite(target->bufstart, 1, nc, target->stream);
     311     1290020 :         target->nchars += written;
     312     1290020 :         if (written != nc)
     313           0 :             target->failed = true;
     314             :     }
     315     1440696 :     target->bufptr = target->bufstart;
     316     1440696 : }
     317             : 
     318             : 
     319             : static bool find_arguments(const char *format, va_list args,
     320             :                            PrintfArgValue *argvalues);
     321             : static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
     322             :                    int pointflag, PrintfTarget *target);
     323             : static void fmtptr(const void *value, PrintfTarget *target);
     324             : static void fmtint(long long value, char type, int forcesign,
     325             :                    int leftjust, int minlen, int zpad, int precision, int pointflag,
     326             :                    PrintfTarget *target);
     327             : static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target);
     328             : static void fmtfloat(double value, char type, int forcesign,
     329             :                      int leftjust, int minlen, int zpad, int precision, int pointflag,
     330             :                      PrintfTarget *target);
     331             : static void dostr(const char *str, int slen, PrintfTarget *target);
     332             : static void dopr_outch(int c, PrintfTarget *target);
     333             : static void dopr_outchmulti(int c, int slen, PrintfTarget *target);
     334             : static int  adjust_sign(int is_negative, int forcesign, int *signvalue);
     335             : static int  compute_padlen(int minlen, int vallen, int leftjust);
     336             : static void leading_pad(int zpad, int signvalue, int *padlen,
     337             :                         PrintfTarget *target);
     338             : static void trailing_pad(int padlen, PrintfTarget *target);
     339             : 
     340             : /*
     341             :  * If strchrnul exists (it's a glibc-ism), it's a good bit faster than the
     342             :  * equivalent manual loop.  If it doesn't exist, provide a replacement.
     343             :  *
     344             :  * Note: glibc declares this as returning "char *", but that would require
     345             :  * casting away const internally, so we don't follow that detail.
     346             :  */
     347             : #ifndef HAVE_STRCHRNUL
     348             : 
     349             : static inline const char *
     350             : strchrnul(const char *s, int c)
     351             : {
     352             :     while (*s != '\0' && *s != c)
     353             :         s++;
     354             :     return s;
     355             : }
     356             : 
     357             : #else
     358             : 
     359             : /*
     360             :  * glibc's <string.h> declares strchrnul only if _GNU_SOURCE is defined.
     361             :  * While we typically use that on glibc platforms, configure will set
     362             :  * HAVE_STRCHRNUL whether it's used or not.  Fill in the missing declaration
     363             :  * so that this file will compile cleanly with or without _GNU_SOURCE.
     364             :  */
     365             : #ifndef _GNU_SOURCE
     366             : extern char *strchrnul(const char *s, int c);
     367             : #endif
     368             : 
     369             : #endif                          /* HAVE_STRCHRNUL */
     370             : 
     371             : 
     372             : /*
     373             :  * dopr(): the guts of *printf for all cases.
     374             :  */
     375             : static void
     376   482301384 : dopr(PrintfTarget *target, const char *format, va_list args)
     377             : {
     378   482301384 :     int         save_errno = errno;
     379   482301384 :     const char *first_pct = NULL;
     380             :     int         ch;
     381             :     bool        have_dollar;
     382             :     bool        have_star;
     383             :     bool        afterstar;
     384             :     int         accum;
     385             :     int         longlongflag;
     386             :     int         longflag;
     387             :     int         pointflag;
     388             :     int         leftjust;
     389             :     int         fieldwidth;
     390             :     int         precision;
     391             :     int         zpad;
     392             :     int         forcesign;
     393             :     int         fmtpos;
     394             :     int         cvalue;
     395             :     long long   numvalue;
     396             :     double      fvalue;
     397             :     const char *strvalue;
     398             :     PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
     399             : 
     400             :     /*
     401             :      * Initially, we suppose the format string does not use %n$.  The first
     402             :      * time we come to a conversion spec that has that, we'll call
     403             :      * find_arguments() to check for consistent use of %n$ and fill the
     404             :      * argvalues array with the argument values in the correct order.
     405             :      */
     406   482301384 :     have_dollar = false;
     407             : 
     408   973138374 :     while (*format != '\0')
     409             :     {
     410             :         /* Locate next conversion specifier */
     411   560074796 :         if (*format != '%')
     412             :         {
     413             :             /* Scan to next '%' or end of string */
     414   477797458 :             const char *next_pct = strchrnul(format + 1, '%');
     415             : 
     416             :             /* Dump literal data we just scanned over */
     417   477797458 :             dostr(format, next_pct - format, target);
     418   477797458 :             if (target->failed)
     419           0 :                 break;
     420             : 
     421   477797458 :             if (*next_pct == '\0')
     422    69237802 :                 break;
     423   408559656 :             format = next_pct;
     424             :         }
     425             : 
     426             :         /*
     427             :          * Remember start of first conversion spec; if we find %n$, then it's
     428             :          * sufficient for find_arguments() to start here, without rescanning
     429             :          * earlier literal text.
     430             :          */
     431   490836994 :         if (first_pct == NULL)
     432   480315336 :             first_pct = format;
     433             : 
     434             :         /* Process conversion spec starting at *format */
     435   490836994 :         format++;
     436             : 
     437             :         /* Fast path for conversion spec that is exactly %s */
     438   490836994 :         if (*format == 's')
     439             :         {
     440    55829180 :             format++;
     441    55829180 :             strvalue = va_arg(args, char *);
     442    55829180 :             if (strvalue == NULL)
     443           0 :                 strvalue = "(null)";
     444    55829180 :             dostr(strvalue, strlen(strvalue), target);
     445    55829180 :             if (target->failed)
     446           0 :                 break;
     447    55829180 :             continue;
     448             :         }
     449             : 
     450   435007814 :         fieldwidth = precision = zpad = leftjust = forcesign = 0;
     451   435007814 :         longflag = longlongflag = pointflag = 0;
     452   435007814 :         fmtpos = accum = 0;
     453   435007814 :         have_star = afterstar = false;
     454   454558800 : nextch2:
     455   454558800 :         ch = *format++;
     456   454558800 :         switch (ch)
     457             :         {
     458      272746 :             case '-':
     459      272746 :                 leftjust = 1;
     460      272746 :                 goto nextch2;
     461         176 :             case '+':
     462         176 :                 forcesign = 1;
     463         176 :                 goto nextch2;
     464     4012938 :             case '0':
     465             :                 /* set zero padding if no nonzero digits yet */
     466     4012938 :                 if (accum == 0 && !pointflag)
     467     2881616 :                     zpad = '0';
     468             :                 /* FALL THRU */
     469             :             case '1':
     470             :             case '2':
     471             :             case '3':
     472             :             case '4':
     473             :             case '5':
     474             :             case '6':
     475             :             case '7':
     476             :             case '8':
     477             :             case '9':
     478     9247208 :                 accum = accum * 10 + (ch - '0');
     479     9247208 :                 goto nextch2;
     480     3525988 :             case '.':
     481     3525988 :                 if (have_star)
     482           0 :                     have_star = false;
     483             :                 else
     484     3525988 :                     fieldwidth = accum;
     485     3525988 :                 pointflag = 1;
     486     3525988 :                 accum = 0;
     487     3525988 :                 goto nextch2;
     488      828172 :             case '*':
     489      828172 :                 if (have_dollar)
     490             :                 {
     491             :                     /*
     492             :                      * We'll process value after reading n$.  Note it's OK to
     493             :                      * assume have_dollar is set correctly, because in a valid
     494             :                      * format string the initial % must have had n$ if * does.
     495             :                      */
     496           0 :                     afterstar = true;
     497             :                 }
     498             :                 else
     499             :                 {
     500             :                     /* fetch and process value now */
     501      828172 :                     int         starval = va_arg(args, int);
     502             : 
     503      828172 :                     if (pointflag)
     504             :                     {
     505       38146 :                         precision = starval;
     506       38146 :                         if (precision < 0)
     507             :                         {
     508           0 :                             precision = 0;
     509           0 :                             pointflag = 0;
     510             :                         }
     511             :                     }
     512             :                     else
     513             :                     {
     514      790026 :                         fieldwidth = starval;
     515      790026 :                         if (fieldwidth < 0)
     516             :                         {
     517        3764 :                             leftjust = 1;
     518        3764 :                             fieldwidth = -fieldwidth;
     519             :                         }
     520             :                     }
     521             :                 }
     522      828172 :                 have_star = true;
     523      828172 :                 accum = 0;
     524      828172 :                 goto nextch2;
     525           0 :             case '$':
     526             :                 /* First dollar sign? */
     527           0 :                 if (!have_dollar)
     528             :                 {
     529             :                     /* Yup, so examine all conversion specs in format */
     530           0 :                     if (!find_arguments(first_pct, args, argvalues))
     531           2 :                         goto bad_format;
     532           0 :                     have_dollar = true;
     533             :                 }
     534           0 :                 if (afterstar)
     535             :                 {
     536             :                     /* fetch and process star value */
     537           0 :                     int         starval = argvalues[accum].i;
     538             : 
     539           0 :                     if (pointflag)
     540             :                     {
     541           0 :                         precision = starval;
     542           0 :                         if (precision < 0)
     543             :                         {
     544           0 :                             precision = 0;
     545           0 :                             pointflag = 0;
     546             :                         }
     547             :                     }
     548             :                     else
     549             :                     {
     550           0 :                         fieldwidth = starval;
     551           0 :                         if (fieldwidth < 0)
     552             :                         {
     553           0 :                             leftjust = 1;
     554           0 :                             fieldwidth = -fieldwidth;
     555             :                         }
     556             :                     }
     557           0 :                     afterstar = false;
     558             :                 }
     559             :                 else
     560           0 :                     fmtpos = accum;
     561           0 :                 accum = 0;
     562           0 :                 goto nextch2;
     563     5505342 :             case 'l':
     564     5505342 :                 if (longflag)
     565       69138 :                     longlongflag = 1;
     566             :                 else
     567     5436204 :                     longflag = 1;
     568     5505342 :                 goto nextch2;
     569      171272 :             case 'z':
     570             : #if SIZEOF_SIZE_T == 8
     571             : #ifdef HAVE_LONG_INT_64
     572      171272 :                 longflag = 1;
     573             : #elif defined(HAVE_LONG_LONG_INT_64)
     574             :                 longlongflag = 1;
     575             : #else
     576             : #error "Don't know how to print 64bit integers"
     577             : #endif
     578             : #else
     579             :                 /* assume size_t is same size as int */
     580             : #endif
     581      171272 :                 goto nextch2;
     582          84 :             case 'h':
     583             :             case '\'':
     584             :                 /* ignore these */
     585          84 :                 goto nextch2;
     586   263628846 :             case 'd':
     587             :             case 'i':
     588   263628846 :                 if (!have_star)
     589             :                 {
     590   263602836 :                     if (pointflag)
     591           0 :                         precision = accum;
     592             :                     else
     593   263602836 :                         fieldwidth = accum;
     594             :                 }
     595   263628846 :                 if (have_dollar)
     596             :                 {
     597           0 :                     if (longlongflag)
     598           0 :                         numvalue = argvalues[fmtpos].ll;
     599           0 :                     else if (longflag)
     600           0 :                         numvalue = argvalues[fmtpos].l;
     601             :                     else
     602           0 :                         numvalue = argvalues[fmtpos].i;
     603             :                 }
     604             :                 else
     605             :                 {
     606   263628846 :                     if (longlongflag)
     607       69026 :                         numvalue = va_arg(args, long long);
     608   263559820 :                     else if (longflag)
     609     4538696 :                         numvalue = va_arg(args, long);
     610             :                     else
     611   259021124 :                         numvalue = va_arg(args, int);
     612             :                 }
     613   263628846 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     614             :                        precision, pointflag, target);
     615   263628844 :                 break;
     616   166786114 :             case 'o':
     617             :             case 'u':
     618             :             case 'x':
     619             :             case 'X':
     620   166786114 :                 if (!have_star)
     621             :                 {
     622   166786114 :                     if (pointflag)
     623           0 :                         precision = accum;
     624             :                     else
     625   166786114 :                         fieldwidth = accum;
     626             :                 }
     627   166786114 :                 if (have_dollar)
     628             :                 {
     629           0 :                     if (longlongflag)
     630           0 :                         numvalue = (unsigned long long) argvalues[fmtpos].ll;
     631           0 :                     else if (longflag)
     632           0 :                         numvalue = (unsigned long) argvalues[fmtpos].l;
     633             :                     else
     634           0 :                         numvalue = (unsigned int) argvalues[fmtpos].i;
     635             :                 }
     636             :                 else
     637             :                 {
     638   166786114 :                     if (longlongflag)
     639         112 :                         numvalue = (unsigned long long) va_arg(args, long long);
     640   166786002 :                     else if (longflag)
     641      999642 :                         numvalue = (unsigned long) va_arg(args, long);
     642             :                     else
     643   165786360 :                         numvalue = (unsigned int) va_arg(args, int);
     644             :                 }
     645   166786114 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     646             :                        precision, pointflag, target);
     647   166786114 :                 break;
     648       23044 :             case 'c':
     649       23044 :                 if (!have_star)
     650             :                 {
     651       23002 :                     if (pointflag)
     652           0 :                         precision = accum;
     653             :                     else
     654       23002 :                         fieldwidth = accum;
     655             :                 }
     656       23044 :                 if (have_dollar)
     657           0 :                     cvalue = (unsigned char) argvalues[fmtpos].i;
     658             :                 else
     659       23044 :                     cvalue = (unsigned char) va_arg(args, int);
     660       23044 :                 fmtchar(cvalue, leftjust, fieldwidth, target);
     661       23044 :                 break;
     662      784844 :             case 's':
     663      784844 :                 if (!have_star)
     664             :                 {
     665        8264 :                     if (pointflag)
     666           0 :                         precision = accum;
     667             :                     else
     668        8264 :                         fieldwidth = accum;
     669             :                 }
     670      784844 :                 if (have_dollar)
     671           0 :                     strvalue = argvalues[fmtpos].cptr;
     672             :                 else
     673      784844 :                     strvalue = va_arg(args, char *);
     674             :                 /* If string is NULL, silently substitute "(null)" */
     675      784844 :                 if (strvalue == NULL)
     676           0 :                     strvalue = "(null)";
     677      784844 :                 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
     678             :                        target);
     679      784844 :                 break;
     680          82 :             case 'p':
     681             :                 /* fieldwidth/leftjust are ignored ... */
     682          82 :                 if (have_dollar)
     683           0 :                     strvalue = argvalues[fmtpos].cptr;
     684             :                 else
     685          82 :                     strvalue = va_arg(args, char *);
     686          82 :                 fmtptr((const void *) strvalue, target);
     687          82 :                 break;
     688     3783780 :             case 'e':
     689             :             case 'E':
     690             :             case 'f':
     691             :             case 'g':
     692             :             case 'G':
     693     3783780 :                 if (!have_star)
     694             :                 {
     695     3758240 :                     if (pointflag)
     696     3487842 :                         precision = accum;
     697             :                     else
     698      270398 :                         fieldwidth = accum;
     699             :                 }
     700     3783780 :                 if (have_dollar)
     701           0 :                     fvalue = argvalues[fmtpos].d;
     702             :                 else
     703     3783780 :                     fvalue = va_arg(args, double);
     704     3783780 :                 fmtfloat(fvalue, ch, forcesign, leftjust,
     705             :                          fieldwidth, zpad,
     706             :                          precision, pointflag,
     707             :                          target);
     708     3783780 :                 break;
     709         154 :             case 'm':
     710             :                 {
     711             :                     char        errbuf[PG_STRERROR_R_BUFLEN];
     712         154 :                     const char *errm = strerror_r(save_errno,
     713             :                                                   errbuf, sizeof(errbuf));
     714             : 
     715         154 :                     dostr(errm, strlen(errm), target);
     716             :                 }
     717         154 :                 break;
     718         948 :             case '%':
     719         948 :                 dopr_outch('%', target);
     720         948 :                 break;
     721           0 :             default:
     722             : 
     723             :                 /*
     724             :                  * Anything else --- in particular, '\0' indicating end of
     725             :                  * format string --- is bogus.
     726             :                  */
     727           0 :                 goto bad_format;
     728             :         }
     729             : 
     730             :         /* Check for failure after each conversion spec */
     731   435007810 :         if (target->failed)
     732           0 :             break;
     733             :     }
     734             : 
     735   482301380 :     return;
     736             : 
     737           2 : bad_format:
     738           2 :     errno = EINVAL;
     739           2 :     target->failed = true;
     740             : }
     741             : 
     742             : /*
     743             :  * find_arguments(): sort out the arguments for a format spec with %n$
     744             :  *
     745             :  * If format is valid, return true and fill argvalues[i] with the value
     746             :  * for the conversion spec that has %i$ or *i$.  Else return false.
     747             :  */
     748             : static bool
     749           0 : find_arguments(const char *format, va_list args,
     750             :                PrintfArgValue *argvalues)
     751             : {
     752             :     int         ch;
     753             :     bool        afterstar;
     754             :     int         accum;
     755             :     int         longlongflag;
     756             :     int         longflag;
     757             :     int         fmtpos;
     758             :     int         i;
     759             :     int         last_dollar;
     760             :     PrintfArgType argtypes[PG_NL_ARGMAX + 1];
     761             : 
     762             :     /* Initialize to "no dollar arguments known" */
     763           0 :     last_dollar = 0;
     764           0 :     MemSet(argtypes, 0, sizeof(argtypes));
     765             : 
     766             :     /*
     767             :      * This loop must accept the same format strings as the one in dopr().
     768             :      * However, we don't need to analyze them to the same level of detail.
     769             :      *
     770             :      * Since we're only called if there's a dollar-type spec somewhere, we can
     771             :      * fail immediately if we find a non-dollar spec.  Per the C99 standard,
     772             :      * all argument references in the format string must be one or the other.
     773             :      */
     774           0 :     while (*format != '\0')
     775             :     {
     776             :         /* Locate next conversion specifier */
     777           0 :         if (*format != '%')
     778             :         {
     779             :             /* Unlike dopr, we can just quit if there's no more specifiers */
     780           0 :             format = strchr(format + 1, '%');
     781           0 :             if (format == NULL)
     782           0 :                 break;
     783             :         }
     784             : 
     785             :         /* Process conversion spec starting at *format */
     786           0 :         format++;
     787           0 :         longflag = longlongflag = 0;
     788           0 :         fmtpos = accum = 0;
     789           0 :         afterstar = false;
     790           0 : nextch1:
     791           0 :         ch = *format++;
     792           0 :         switch (ch)
     793             :         {
     794           0 :             case '-':
     795             :             case '+':
     796           0 :                 goto nextch1;
     797           0 :             case '0':
     798             :             case '1':
     799             :             case '2':
     800             :             case '3':
     801             :             case '4':
     802             :             case '5':
     803             :             case '6':
     804             :             case '7':
     805             :             case '8':
     806             :             case '9':
     807           0 :                 accum = accum * 10 + (ch - '0');
     808           0 :                 goto nextch1;
     809           0 :             case '.':
     810           0 :                 accum = 0;
     811           0 :                 goto nextch1;
     812           0 :             case '*':
     813           0 :                 if (afterstar)
     814           0 :                     return false;   /* previous star missing dollar */
     815           0 :                 afterstar = true;
     816           0 :                 accum = 0;
     817           0 :                 goto nextch1;
     818           0 :             case '$':
     819           0 :                 if (accum <= 0 || accum > PG_NL_ARGMAX)
     820           0 :                     return false;
     821           0 :                 if (afterstar)
     822             :                 {
     823           0 :                     if (argtypes[accum] &&
     824           0 :                         argtypes[accum] != ATYPE_INT)
     825           0 :                         return false;
     826           0 :                     argtypes[accum] = ATYPE_INT;
     827           0 :                     last_dollar = Max(last_dollar, accum);
     828           0 :                     afterstar = false;
     829             :                 }
     830             :                 else
     831           0 :                     fmtpos = accum;
     832           0 :                 accum = 0;
     833           0 :                 goto nextch1;
     834           0 :             case 'l':
     835           0 :                 if (longflag)
     836           0 :                     longlongflag = 1;
     837             :                 else
     838           0 :                     longflag = 1;
     839           0 :                 goto nextch1;
     840           0 :             case 'z':
     841             : #if SIZEOF_SIZE_T == 8
     842             : #ifdef HAVE_LONG_INT_64
     843           0 :                 longflag = 1;
     844             : #elif defined(HAVE_LONG_LONG_INT_64)
     845             :                 longlongflag = 1;
     846             : #else
     847             : #error "Don't know how to print 64bit integers"
     848             : #endif
     849             : #else
     850             :                 /* assume size_t is same size as int */
     851             : #endif
     852           0 :                 goto nextch1;
     853           0 :             case 'h':
     854             :             case '\'':
     855             :                 /* ignore these */
     856           0 :                 goto nextch1;
     857           0 :             case 'd':
     858             :             case 'i':
     859             :             case 'o':
     860             :             case 'u':
     861             :             case 'x':
     862             :             case 'X':
     863           0 :                 if (fmtpos)
     864             :                 {
     865             :                     PrintfArgType atype;
     866             : 
     867           0 :                     if (longlongflag)
     868           0 :                         atype = ATYPE_LONGLONG;
     869           0 :                     else if (longflag)
     870           0 :                         atype = ATYPE_LONG;
     871             :                     else
     872           0 :                         atype = ATYPE_INT;
     873           0 :                     if (argtypes[fmtpos] &&
     874           0 :                         argtypes[fmtpos] != atype)
     875           0 :                         return false;
     876           0 :                     argtypes[fmtpos] = atype;
     877           0 :                     last_dollar = Max(last_dollar, fmtpos);
     878             :                 }
     879             :                 else
     880           0 :                     return false;   /* non-dollar conversion spec */
     881           0 :                 break;
     882           0 :             case 'c':
     883           0 :                 if (fmtpos)
     884             :                 {
     885           0 :                     if (argtypes[fmtpos] &&
     886           0 :                         argtypes[fmtpos] != ATYPE_INT)
     887           0 :                         return false;
     888           0 :                     argtypes[fmtpos] = ATYPE_INT;
     889           0 :                     last_dollar = Max(last_dollar, fmtpos);
     890             :                 }
     891             :                 else
     892           0 :                     return false;   /* non-dollar conversion spec */
     893           0 :                 break;
     894           0 :             case 's':
     895             :             case 'p':
     896           0 :                 if (fmtpos)
     897             :                 {
     898           0 :                     if (argtypes[fmtpos] &&
     899           0 :                         argtypes[fmtpos] != ATYPE_CHARPTR)
     900           0 :                         return false;
     901           0 :                     argtypes[fmtpos] = ATYPE_CHARPTR;
     902           0 :                     last_dollar = Max(last_dollar, fmtpos);
     903             :                 }
     904             :                 else
     905           0 :                     return false;   /* non-dollar conversion spec */
     906           0 :                 break;
     907           0 :             case 'e':
     908             :             case 'E':
     909             :             case 'f':
     910             :             case 'g':
     911             :             case 'G':
     912           0 :                 if (fmtpos)
     913             :                 {
     914           0 :                     if (argtypes[fmtpos] &&
     915           0 :                         argtypes[fmtpos] != ATYPE_DOUBLE)
     916           0 :                         return false;
     917           0 :                     argtypes[fmtpos] = ATYPE_DOUBLE;
     918           0 :                     last_dollar = Max(last_dollar, fmtpos);
     919             :                 }
     920             :                 else
     921           0 :                     return false;   /* non-dollar conversion spec */
     922           0 :                 break;
     923           0 :             case 'm':
     924             :             case '%':
     925           0 :                 break;
     926           0 :             default:
     927           0 :                 return false;   /* bogus format string */
     928             :         }
     929             : 
     930             :         /*
     931             :          * If we finish the spec with afterstar still set, there's a
     932             :          * non-dollar star in there.
     933             :          */
     934           0 :         if (afterstar)
     935           0 :             return false;       /* non-dollar conversion spec */
     936             :     }
     937             : 
     938             :     /*
     939             :      * Format appears valid so far, so collect the arguments in physical
     940             :      * order.  (Since we rejected any non-dollar specs that would have
     941             :      * collected arguments, we know that dopr() hasn't collected any yet.)
     942             :      */
     943           0 :     for (i = 1; i <= last_dollar; i++)
     944             :     {
     945           0 :         switch (argtypes[i])
     946             :         {
     947           0 :             case ATYPE_NONE:
     948           0 :                 return false;
     949           0 :             case ATYPE_INT:
     950           0 :                 argvalues[i].i = va_arg(args, int);
     951           0 :                 break;
     952           0 :             case ATYPE_LONG:
     953           0 :                 argvalues[i].l = va_arg(args, long);
     954           0 :                 break;
     955           0 :             case ATYPE_LONGLONG:
     956           0 :                 argvalues[i].ll = va_arg(args, long long);
     957           0 :                 break;
     958           0 :             case ATYPE_DOUBLE:
     959           0 :                 argvalues[i].d = va_arg(args, double);
     960           0 :                 break;
     961           0 :             case ATYPE_CHARPTR:
     962           0 :                 argvalues[i].cptr = va_arg(args, char *);
     963           0 :                 break;
     964             :         }
     965           0 :     }
     966             : 
     967           0 :     return true;
     968             : }
     969             : 
     970             : static void
     971      784844 : fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
     972             :        int pointflag, PrintfTarget *target)
     973             : {
     974             :     int         padlen,
     975             :                 vallen;         /* amount to pad */
     976             : 
     977             :     /*
     978             :      * If a maxwidth (precision) is specified, we must not fetch more bytes
     979             :      * than that.
     980             :      */
     981      784844 :     if (pointflag)
     982       12606 :         vallen = strnlen(value, maxwidth);
     983             :     else
     984      772238 :         vallen = strlen(value);
     985             : 
     986      784844 :     padlen = compute_padlen(minlen, vallen, leftjust);
     987             : 
     988      784844 :     if (padlen > 0)
     989             :     {
     990      333546 :         dopr_outchmulti(' ', padlen, target);
     991      333546 :         padlen = 0;
     992             :     }
     993             : 
     994      784844 :     dostr(value, vallen, target);
     995             : 
     996      784844 :     trailing_pad(padlen, target);
     997      784844 : }
     998             : 
     999             : static void
    1000          82 : fmtptr(const void *value, PrintfTarget *target)
    1001             : {
    1002             :     int         vallen;
    1003             :     char        convert[64];
    1004             : 
    1005             :     /* we rely on regular C library's sprintf to do the basic conversion */
    1006          82 :     vallen = sprintf(convert, "%p", value);
    1007          82 :     if (vallen < 0)
    1008           0 :         target->failed = true;
    1009             :     else
    1010          82 :         dostr(convert, vallen, target);
    1011          82 : }
    1012             : 
    1013             : static void
    1014   430414956 : fmtint(long long value, char type, int forcesign, int leftjust,
    1015             :        int minlen, int zpad, int precision, int pointflag,
    1016             :        PrintfTarget *target)
    1017             : {
    1018             :     unsigned long long uvalue;
    1019             :     int         base;
    1020             :     int         dosign;
    1021   430414956 :     const char *cvt = "0123456789abcdef";
    1022   430414956 :     int         signvalue = 0;
    1023             :     char        convert[64];
    1024   430414956 :     int         vallen = 0;
    1025             :     int         padlen;         /* amount to pad */
    1026             :     int         zeropad;        /* extra leading zeroes */
    1027             : 
    1028   430414956 :     switch (type)
    1029             :     {
    1030   263628842 :         case 'd':
    1031             :         case 'i':
    1032   263628842 :             base = 10;
    1033   263628842 :             dosign = 1;
    1034   263628842 :             break;
    1035        7186 :         case 'o':
    1036        7186 :             base = 8;
    1037        7186 :             dosign = 0;
    1038        7186 :             break;
    1039   166577298 :         case 'u':
    1040   166577298 :             base = 10;
    1041   166577298 :             dosign = 0;
    1042   166577298 :             break;
    1043       31078 :         case 'x':
    1044       31078 :             base = 16;
    1045       31078 :             dosign = 0;
    1046       31078 :             break;
    1047      170552 :         case 'X':
    1048      170552 :             cvt = "0123456789ABCDEF";
    1049      170552 :             base = 16;
    1050      170552 :             dosign = 0;
    1051      170552 :             break;
    1052           0 :         default:
    1053           0 :             return;             /* keep compiler quiet */
    1054             :     }
    1055             : 
    1056             :     /* disable MSVC warning about applying unary minus to an unsigned value */
    1057             : #ifdef _MSC_VER
    1058             : #pragma warning(push)
    1059             : #pragma warning(disable: 4146)
    1060             : #endif
    1061             :     /* Handle +/- */
    1062   430414956 :     if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
    1063    52426204 :         uvalue = -(unsigned long long) value;
    1064             :     else
    1065   377988752 :         uvalue = (unsigned long long) value;
    1066             : #ifdef _MSC_VER
    1067             : #pragma warning(pop)
    1068             : #endif
    1069             : 
    1070             :     /*
    1071             :      * SUS: the result of converting 0 with an explicit precision of 0 is no
    1072             :      * characters
    1073             :      */
    1074   430414956 :     if (value == 0 && pointflag && precision == 0)
    1075           0 :         vallen = 0;
    1076             :     else
    1077             :     {
    1078             :         /*
    1079             :          * Convert integer to string.  We special-case each of the possible
    1080             :          * base values so as to avoid general-purpose divisions.  On most
    1081             :          * machines, division by a fixed constant can be done much more
    1082             :          * cheaply than a general divide.
    1083             :          */
    1084   430414956 :         if (base == 10)
    1085             :         {
    1086             :             do
    1087             :             {
    1088   700623638 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10];
    1089   700623638 :                 uvalue = uvalue / 10;
    1090   700623638 :             } while (uvalue);
    1091             :         }
    1092      208816 :         else if (base == 16)
    1093             :         {
    1094             :             do
    1095             :             {
    1096      390254 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16];
    1097      390254 :                 uvalue = uvalue / 16;
    1098      390254 :             } while (uvalue);
    1099             :         }
    1100             :         else                    /* base == 8 */
    1101             :         {
    1102             :             do
    1103             :             {
    1104       21558 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8];
    1105       21558 :                 uvalue = uvalue / 8;
    1106       21558 :             } while (uvalue);
    1107             :         }
    1108             :     }
    1109             : 
    1110   430414956 :     zeropad = Max(0, precision - vallen);
    1111             : 
    1112   430414956 :     padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
    1113             : 
    1114   430414962 :     leading_pad(zpad, signvalue, &padlen, target);
    1115             : 
    1116   430414956 :     if (zeropad > 0)
    1117           0 :         dopr_outchmulti('0', zeropad, target);
    1118             : 
    1119   430414956 :     dostr(convert + sizeof(convert) - vallen, vallen, target);
    1120             : 
    1121   430414960 :     trailing_pad(padlen, target);
    1122             : }
    1123             : 
    1124             : static void
    1125       23044 : fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
    1126             : {
    1127             :     int         padlen;         /* amount to pad */
    1128             : 
    1129       23044 :     padlen = compute_padlen(minlen, 1, leftjust);
    1130             : 
    1131       23044 :     if (padlen > 0)
    1132             :     {
    1133          42 :         dopr_outchmulti(' ', padlen, target);
    1134          42 :         padlen = 0;
    1135             :     }
    1136             : 
    1137       23044 :     dopr_outch(value, target);
    1138             : 
    1139       23044 :     trailing_pad(padlen, target);
    1140       23044 : }
    1141             : 
    1142             : static void
    1143     3783780 : fmtfloat(double value, char type, int forcesign, int leftjust,
    1144             :          int minlen, int zpad, int precision, int pointflag,
    1145             :          PrintfTarget *target)
    1146             : {
    1147     3783780 :     int         signvalue = 0;
    1148             :     int         prec;
    1149             :     int         vallen;
    1150             :     char        fmt[8];
    1151             :     char        convert[1024];
    1152     3783780 :     int         zeropadlen = 0; /* amount to pad with zeroes */
    1153             :     int         padlen;         /* amount to pad with spaces */
    1154             : 
    1155             :     /*
    1156             :      * We rely on the regular C library's sprintf to do the basic conversion,
    1157             :      * then handle padding considerations here.
    1158             :      *
    1159             :      * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
    1160             :      * too wildly more than that with other hardware.  In "f" format, sprintf
    1161             :      * could therefore generate at most 308 characters to the left of the
    1162             :      * decimal point; while we need to allow the precision to get as high as
    1163             :      * 308+17 to ensure that we don't truncate significant digits from very
    1164             :      * small values.  To handle both these extremes, we use a buffer of 1024
    1165             :      * bytes and limit requested precision to 350 digits; this should prevent
    1166             :      * buffer overrun even with non-IEEE math.  If the original precision
    1167             :      * request was more than 350, separately pad with zeroes.
    1168             :      *
    1169             :      * We handle infinities and NaNs specially to ensure platform-independent
    1170             :      * output.
    1171             :      */
    1172     3783780 :     if (precision < 0)           /* cover possible overflow of "accum" */
    1173           0 :         precision = 0;
    1174     3783780 :     prec = Min(precision, 350);
    1175             : 
    1176     3783780 :     if (isnan(value))
    1177             :     {
    1178          32 :         strcpy(convert, "NaN");
    1179          32 :         vallen = 3;
    1180             :         /* no zero padding, regardless of precision spec */
    1181             :     }
    1182             :     else
    1183             :     {
    1184             :         /*
    1185             :          * Handle sign (NaNs have no sign, so we don't do this in the case
    1186             :          * above).  "value < 0.0" will not be true for IEEE minus zero, so we
    1187             :          * detect that by looking for the case where value equals 0.0
    1188             :          * according to == but not according to memcmp.
    1189             :          */
    1190             :         static const double dzero = 0.0;
    1191             : 
    1192     7555834 :         if (adjust_sign((value < 0.0 ||
    1193     3772086 :                          (value == 0.0 &&
    1194      732894 :                           memcmp(&value, &dzero, sizeof(double)) != 0)),
    1195             :                         forcesign, &signvalue))
    1196       11662 :             value = -value;
    1197             : 
    1198     3783748 :         if (isinf(value))
    1199             :         {
    1200          64 :             strcpy(convert, "Infinity");
    1201          64 :             vallen = 8;
    1202             :             /* no zero padding, regardless of precision spec */
    1203             :         }
    1204     3783684 :         else if (pointflag)
    1205             :         {
    1206     3513286 :             zeropadlen = precision - prec;
    1207     3513286 :             fmt[0] = '%';
    1208     3513286 :             fmt[1] = '.';
    1209     3513286 :             fmt[2] = '*';
    1210     3513286 :             fmt[3] = type;
    1211     3513286 :             fmt[4] = '\0';
    1212     3513286 :             vallen = sprintf(convert, fmt, prec, value);
    1213             :         }
    1214             :         else
    1215             :         {
    1216      270398 :             fmt[0] = '%';
    1217      270398 :             fmt[1] = type;
    1218      270398 :             fmt[2] = '\0';
    1219      270398 :             vallen = sprintf(convert, fmt, value);
    1220             :         }
    1221     3783748 :         if (vallen < 0)
    1222           0 :             goto fail;
    1223             : 
    1224             :         /*
    1225             :          * Windows, alone among our supported platforms, likes to emit
    1226             :          * three-digit exponent fields even when two digits would do.  Hack
    1227             :          * such results to look like the way everyone else does it.
    1228             :          */
    1229             : #ifdef WIN32
    1230             :         if (vallen >= 6 &&
    1231             :             convert[vallen - 5] == 'e' &&
    1232             :             convert[vallen - 3] == '0')
    1233             :         {
    1234             :             convert[vallen - 3] = convert[vallen - 2];
    1235             :             convert[vallen - 2] = convert[vallen - 1];
    1236             :             vallen--;
    1237             :         }
    1238             : #endif
    1239             :     }
    1240             : 
    1241     3783780 :     padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
    1242             : 
    1243     3783780 :     leading_pad(zpad, signvalue, &padlen, target);
    1244             : 
    1245     3783780 :     if (zeropadlen > 0)
    1246             :     {
    1247             :         /* If 'e' or 'E' format, inject zeroes before the exponent */
    1248           0 :         char       *epos = strrchr(convert, 'e');
    1249             : 
    1250           0 :         if (!epos)
    1251           0 :             epos = strrchr(convert, 'E');
    1252           0 :         if (epos)
    1253             :         {
    1254             :             /* pad before exponent */
    1255           0 :             dostr(convert, epos - convert, target);
    1256           0 :             dopr_outchmulti('0', zeropadlen, target);
    1257           0 :             dostr(epos, vallen - (epos - convert), target);
    1258             :         }
    1259             :         else
    1260             :         {
    1261             :             /* no exponent, pad after the digits */
    1262           0 :             dostr(convert, vallen, target);
    1263           0 :             dopr_outchmulti('0', zeropadlen, target);
    1264             :         }
    1265             :     }
    1266             :     else
    1267             :     {
    1268             :         /* no zero padding, just emit the number as-is */
    1269     3783780 :         dostr(convert, vallen, target);
    1270             :     }
    1271             : 
    1272     3783780 :     trailing_pad(padlen, target);
    1273     3783780 :     return;
    1274             : 
    1275           0 : fail:
    1276           0 :     target->failed = true;
    1277             : }
    1278             : 
    1279             : /*
    1280             :  * Nonstandard entry point to print a double value efficiently.
    1281             :  *
    1282             :  * This is approximately equivalent to strfromd(), but has an API more
    1283             :  * adapted to what float8out() wants.  The behavior is like snprintf()
    1284             :  * with a format of "%.ng", where n is the specified precision.
    1285             :  * However, the target buffer must be nonempty (i.e. count > 0), and
    1286             :  * the precision is silently bounded to a sane range.
    1287             :  */
    1288             : int
    1289      154444 : pg_strfromd(char *str, size_t count, int precision, double value)
    1290             : {
    1291             :     PrintfTarget target;
    1292      154444 :     int         signvalue = 0;
    1293             :     int         vallen;
    1294             :     char        fmt[8];
    1295             :     char        convert[64];
    1296             : 
    1297             :     /* Set up the target like pg_snprintf, but require nonempty buffer */
    1298             :     Assert(count > 0);
    1299      154444 :     target.bufstart = target.bufptr = str;
    1300      154444 :     target.bufend = str + count - 1;
    1301      154444 :     target.stream = NULL;
    1302      154444 :     target.nchars = 0;
    1303      154444 :     target.failed = false;
    1304             : 
    1305             :     /*
    1306             :      * We bound precision to a reasonable range; the combination of this and
    1307             :      * the knowledge that we're using "g" format without padding allows the
    1308             :      * convert[] buffer to be reasonably small.
    1309             :      */
    1310      154444 :     if (precision < 1)
    1311           0 :         precision = 1;
    1312      154444 :     else if (precision > 32)
    1313           0 :         precision = 32;
    1314             : 
    1315             :     /*
    1316             :      * The rest is just an inlined version of the fmtfloat() logic above,
    1317             :      * simplified using the knowledge that no padding is wanted.
    1318             :      */
    1319      154444 :     if (isnan(value))
    1320             :     {
    1321        8048 :         strcpy(convert, "NaN");
    1322        8048 :         vallen = 3;
    1323             :     }
    1324             :     else
    1325             :     {
    1326             :         static const double dzero = 0.0;
    1327             : 
    1328      146396 :         if (value < 0.0 ||
    1329      124952 :             (value == 0.0 &&
    1330       17332 :              memcmp(&value, &dzero, sizeof(double)) != 0))
    1331             :         {
    1332       21488 :             signvalue = '-';
    1333       21488 :             value = -value;
    1334             :         }
    1335             : 
    1336      146396 :         if (isinf(value))
    1337             :         {
    1338        4720 :             strcpy(convert, "Infinity");
    1339        4720 :             vallen = 8;
    1340             :         }
    1341             :         else
    1342             :         {
    1343      141676 :             fmt[0] = '%';
    1344      141676 :             fmt[1] = '.';
    1345      141676 :             fmt[2] = '*';
    1346      141676 :             fmt[3] = 'g';
    1347      141676 :             fmt[4] = '\0';
    1348      141676 :             vallen = sprintf(convert, fmt, precision, value);
    1349      141676 :             if (vallen < 0)
    1350             :             {
    1351           0 :                 target.failed = true;
    1352           0 :                 goto fail;
    1353             :             }
    1354             : 
    1355             : #ifdef WIN32
    1356             :             if (vallen >= 6 &&
    1357             :                 convert[vallen - 5] == 'e' &&
    1358             :                 convert[vallen - 3] == '0')
    1359             :             {
    1360             :                 convert[vallen - 3] = convert[vallen - 2];
    1361             :                 convert[vallen - 2] = convert[vallen - 1];
    1362             :                 vallen--;
    1363             :             }
    1364             : #endif
    1365             :         }
    1366             :     }
    1367             : 
    1368      154444 :     if (signvalue)
    1369       21488 :         dopr_outch(signvalue, &target);
    1370             : 
    1371      154444 :     dostr(convert, vallen, &target);
    1372             : 
    1373      154444 : fail:
    1374      154444 :     *(target.bufptr) = '\0';
    1375      308888 :     return target.failed ? -1 : (target.bufptr - target.bufstart
    1376      154444 :                                  + target.nchars);
    1377             : }
    1378             : 
    1379             : 
    1380             : static void
    1381   968764898 : dostr(const char *str, int slen, PrintfTarget *target)
    1382             : {
    1383             :     /* fast path for common case of slen == 1 */
    1384   968764898 :     if (slen == 1)
    1385             :     {
    1386   362274188 :         dopr_outch(*str, target);
    1387   362274188 :         return;
    1388             :     }
    1389             : 
    1390  1211480206 :     while (slen > 0)
    1391             :     {
    1392             :         int         avail;
    1393             : 
    1394   605375944 :         if (target->bufend != NULL)
    1395   603027200 :             avail = target->bufend - target->bufptr;
    1396             :         else
    1397     2348744 :             avail = slen;
    1398   605375944 :         if (avail <= 0)
    1399             :         {
    1400             :             /* buffer full, can we dump to stream? */
    1401      386646 :             if (target->stream == NULL)
    1402             :             {
    1403      386448 :                 target->nchars += slen; /* no, lose the data */
    1404      386448 :                 return;
    1405             :             }
    1406         198 :             flushbuffer(target);
    1407         198 :             continue;
    1408             :         }
    1409   604989298 :         avail = Min(avail, slen);
    1410   604989298 :         memmove(target->bufptr, str, avail);
    1411   604989298 :         target->bufptr += avail;
    1412   604989298 :         str += avail;
    1413   604989298 :         slen -= avail;
    1414             :     }
    1415             : }
    1416             : 
    1417             : static void
    1418   415785810 : dopr_outch(int c, PrintfTarget *target)
    1419             : {
    1420   415785810 :     if (target->bufend != NULL && target->bufptr >= target->bufend)
    1421             :     {
    1422             :         /* buffer full, can we dump to stream? */
    1423       76348 :         if (target->stream == NULL)
    1424             :         {
    1425       76348 :             target->nchars++;    /* no, lose the data */
    1426       76348 :             return;
    1427             :         }
    1428           0 :         flushbuffer(target);
    1429             :     }
    1430   415709462 :     *(target->bufptr++) = c;
    1431             : }
    1432             : 
    1433             : static void
    1434     1516222 : dopr_outchmulti(int c, int slen, PrintfTarget *target)
    1435             : {
    1436             :     /* fast path for common case of slen == 1 */
    1437     1516222 :     if (slen == 1)
    1438             :     {
    1439     1028152 :         dopr_outch(c, target);
    1440     1028152 :         return;
    1441             :     }
    1442             : 
    1443      976572 :     while (slen > 0)
    1444             :     {
    1445             :         int         avail;
    1446             : 
    1447      488502 :         if (target->bufend != NULL)
    1448      481210 :             avail = target->bufend - target->bufptr;
    1449             :         else
    1450        7292 :             avail = slen;
    1451      488502 :         if (avail <= 0)
    1452             :         {
    1453             :             /* buffer full, can we dump to stream? */
    1454         218 :             if (target->stream == NULL)
    1455             :             {
    1456           0 :                 target->nchars += slen; /* no, lose the data */
    1457           0 :                 return;
    1458             :             }
    1459         218 :             flushbuffer(target);
    1460         218 :             continue;
    1461             :         }
    1462      488284 :         avail = Min(avail, slen);
    1463      488284 :         memset(target->bufptr, c, avail);
    1464      488284 :         target->bufptr += avail;
    1465      488284 :         slen -= avail;
    1466             :     }
    1467             : }
    1468             : 
    1469             : 
    1470             : static int
    1471   267412588 : adjust_sign(int is_negative, int forcesign, int *signvalue)
    1472             : {
    1473   267412588 :     if (is_negative)
    1474             :     {
    1475    52437866 :         *signvalue = '-';
    1476    52437866 :         return true;
    1477             :     }
    1478   214974722 :     else if (forcesign)
    1479         124 :         *signvalue = '+';
    1480   214974722 :     return false;
    1481             : }
    1482             : 
    1483             : 
    1484             : static int
    1485   435006624 : compute_padlen(int minlen, int vallen, int leftjust)
    1486             : {
    1487             :     int         padlen;
    1488             : 
    1489   435006624 :     padlen = minlen - vallen;
    1490   435006624 :     if (padlen < 0)
    1491   431353834 :         padlen = 0;
    1492   435006624 :     if (leftjust)
    1493      276510 :         padlen = -padlen;
    1494   435006624 :     return padlen;
    1495             : }
    1496             : 
    1497             : 
    1498             : static void
    1499   434198736 : leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
    1500             : {
    1501             :     int         maxpad;
    1502             : 
    1503   434198736 :     if (*padlen > 0 && zpad)
    1504             :     {
    1505     1070554 :         if (signvalue)
    1506             :         {
    1507         120 :             dopr_outch(signvalue, target);
    1508         120 :             --(*padlen);
    1509         120 :             signvalue = 0;
    1510             :         }
    1511     1070554 :         if (*padlen > 0)
    1512             :         {
    1513     1070542 :             dopr_outchmulti(zpad, *padlen, target);
    1514     1070542 :             *padlen = 0;
    1515             :         }
    1516             :     }
    1517   434198736 :     maxpad = (signvalue != 0);
    1518   434198736 :     if (*padlen > maxpad)
    1519             :     {
    1520        8468 :         dopr_outchmulti(' ', *padlen - maxpad, target);
    1521        8468 :         *padlen = maxpad;
    1522             :     }
    1523   434198736 :     if (signvalue)
    1524             :     {
    1525    52437870 :         dopr_outch(signvalue, target);
    1526    52437870 :         if (*padlen > 0)
    1527           0 :             --(*padlen);
    1528    52437870 :         else if (*padlen < 0)
    1529           0 :             ++(*padlen);
    1530             :     }
    1531   434198736 : }
    1532             : 
    1533             : 
    1534             : static void
    1535   435006628 : trailing_pad(int padlen, PrintfTarget *target)
    1536             : {
    1537   435006628 :     if (padlen < 0)
    1538      103624 :         dopr_outchmulti(' ', -padlen, target);
    1539   435006628 : }

Generated by: LCOV version 1.14