LCOV - code coverage report
Current view: top level - src/port - snprintf.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 410 614 66.8 %
Date: 2024-04-19 02:11:33 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-2024, 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   363586014 : 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   363586014 :     if (count == 0)
     186             :     {
     187      105714 :         str = onebyte;
     188      105714 :         count = 1;
     189             :     }
     190   363586014 :     target.bufstart = target.bufptr = str;
     191   363586014 :     target.bufend = str + count - 1;
     192   363586014 :     target.stream = NULL;
     193   363586014 :     target.nchars = 0;
     194   363586014 :     target.failed = false;
     195   363586014 :     dopr(&target, fmt, args);
     196   363586016 :     *(target.bufptr) = '\0';
     197   727172032 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     198   363586016 :                                  + target.nchars);
     199             : }
     200             : 
     201             : int
     202    22338974 : pg_snprintf(char *str, size_t count, const char *fmt,...)
     203             : {
     204             :     int         len;
     205             :     va_list     args;
     206             : 
     207    22338974 :     va_start(args, fmt);
     208    22338974 :     len = pg_vsnprintf(str, count, fmt, args);
     209    22338976 :     va_end(args);
     210    22338976 :     return len;
     211             : }
     212             : 
     213             : int
     214     6948598 : pg_vsprintf(char *str, const char *fmt, va_list args)
     215             : {
     216             :     PrintfTarget target;
     217             : 
     218     6948598 :     target.bufstart = target.bufptr = str;
     219     6948598 :     target.bufend = NULL;
     220     6948598 :     target.stream = NULL;
     221     6948598 :     target.nchars = 0;          /* not really used in this case */
     222     6948598 :     target.failed = false;
     223     6948598 :     dopr(&target, fmt, args);
     224     6948598 :     *(target.bufptr) = '\0';
     225    13897196 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     226     6948598 :                                  + target.nchars);
     227             : }
     228             : 
     229             : int
     230     6948598 : pg_sprintf(char *str, const char *fmt,...)
     231             : {
     232             :     int         len;
     233             :     va_list     args;
     234             : 
     235     6948598 :     va_start(args, fmt);
     236     6948598 :     len = pg_vsprintf(str, fmt, args);
     237     6948598 :     va_end(args);
     238     6948598 :     return len;
     239             : }
     240             : 
     241             : int
     242     3780510 : pg_vfprintf(FILE *stream, const char *fmt, va_list args)
     243             : {
     244             :     PrintfTarget target;
     245             :     char        buffer[1024];   /* size is arbitrary */
     246             : 
     247     3780510 :     if (stream == NULL)
     248             :     {
     249           0 :         errno = EINVAL;
     250           0 :         return -1;
     251             :     }
     252     3780510 :     target.bufstart = target.bufptr = buffer;
     253     3780510 :     target.bufend = buffer + sizeof(buffer);    /* use the whole buffer */
     254     3780510 :     target.stream = stream;
     255     3780510 :     target.nchars = 0;
     256     3780510 :     target.failed = false;
     257     3780510 :     dopr(&target, fmt, args);
     258             :     /* dump any remaining buffer contents */
     259     3780510 :     flushbuffer(&target);
     260     3780510 :     return target.failed ? -1 : target.nchars;
     261             : }
     262             : 
     263             : int
     264     1580446 : pg_fprintf(FILE *stream, const char *fmt,...)
     265             : {
     266             :     int         len;
     267             :     va_list     args;
     268             : 
     269     1580446 :     va_start(args, fmt);
     270     1580446 :     len = pg_vfprintf(stream, fmt, args);
     271     1580446 :     va_end(args);
     272     1580446 :     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     2177506 : pg_printf(const char *fmt,...)
     283             : {
     284             :     int         len;
     285             :     va_list     args;
     286             : 
     287     2177506 :     va_start(args, fmt);
     288     2177506 :     len = pg_vfprintf(stdout, fmt, args);
     289     2177506 :     va_end(args);
     290     2177506 :     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     3781122 : flushbuffer(PrintfTarget *target)
     299             : {
     300     3781122 :     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     3781122 :     if (!target->failed && nc > 0)
     307             :     {
     308             :         size_t      written;
     309             : 
     310     3527784 :         written = fwrite(target->bufstart, 1, nc, target->stream);
     311     3527784 :         target->nchars += written;
     312     3527784 :         if (written != nc)
     313           0 :             target->failed = true;
     314             :     }
     315     3781122 :     target->bufptr = target->bufstart;
     316     3781122 : }
     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   374315118 : dopr(PrintfTarget *target, const char *format, va_list args)
     377             : {
     378   374315118 :     int         save_errno = errno;
     379   374315118 :     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   374315118 :     have_dollar = false;
     407             : 
     408   761881524 :     while (*format != '\0')
     409             :     {
     410             :         /* Locate next conversion specifier */
     411   478151160 :         if (*format != '%')
     412             :         {
     413             :             /* Scan to next '%' or end of string */
     414   365181180 :             const char *next_pct = strchrnul(format + 1, '%');
     415             : 
     416             :             /* Dump literal data we just scanned over */
     417   365181180 :             dostr(format, next_pct - format, target);
     418   365181178 :             if (target->failed)
     419           0 :                 break;
     420             : 
     421   365181178 :             if (*next_pct == '\0')
     422    90584756 :                 break;
     423   274596422 :             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   387566402 :         if (first_pct == NULL)
     432   368164578 :             first_pct = format;
     433             : 
     434             :         /* Process conversion spec starting at *format */
     435   387566402 :         format++;
     436             : 
     437             :         /* Fast path for conversion spec that is exactly %s */
     438   387566402 :         if (*format == 's')
     439             :         {
     440    62674962 :             format++;
     441    62674962 :             strvalue = va_arg(args, char *);
     442    62674966 :             if (strvalue == NULL)
     443           0 :                 strvalue = "(null)";
     444    62674966 :             dostr(strvalue, strlen(strvalue), target);
     445    62674968 :             if (target->failed)
     446           0 :                 break;
     447    62674968 :             continue;
     448             :         }
     449             : 
     450   324891440 :         fieldwidth = precision = zpad = leftjust = forcesign = 0;
     451   324891440 :         longflag = longlongflag = pointflag = 0;
     452   324891440 :         fmtpos = accum = 0;
     453   324891440 :         have_star = afterstar = false;
     454   352986106 : nextch2:
     455   352986106 :         ch = *format++;
     456   352986106 :         switch (ch)
     457             :         {
     458      987070 :             case '-':
     459      987070 :                 leftjust = 1;
     460      987070 :                 goto nextch2;
     461         264 :             case '+':
     462         264 :                 forcesign = 1;
     463         264 :                 goto nextch2;
     464     8282118 :             case '0':
     465             :                 /* set zero padding if no nonzero digits yet */
     466     8282118 :                 if (accum == 0 && !pointflag)
     467     7727702 :                     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    18606126 :                 accum = accum * 10 + (ch - '0');
     479    18606126 :                 goto nextch2;
     480      156778 :             case '.':
     481      156778 :                 if (have_star)
     482           0 :                     have_star = false;
     483             :                 else
     484      156778 :                     fieldwidth = accum;
     485      156778 :                 pointflag = 1;
     486      156778 :                 accum = 0;
     487      156778 :                 goto nextch2;
     488     1361084 :             case '*':
     489     1361084 :                 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     1361084 :                     int         starval = va_arg(args, int);
     502             : 
     503     1361084 :                     if (pointflag)
     504             :                     {
     505       58924 :                         precision = starval;
     506       58924 :                         if (precision < 0)
     507             :                         {
     508           0 :                             precision = 0;
     509           0 :                             pointflag = 0;
     510             :                         }
     511             :                     }
     512             :                     else
     513             :                     {
     514     1302160 :                         fieldwidth = starval;
     515     1302160 :                         if (fieldwidth < 0)
     516             :                         {
     517        5646 :                             leftjust = 1;
     518        5646 :                             fieldwidth = -fieldwidth;
     519             :                         }
     520             :                     }
     521             :                 }
     522     1361084 :                 have_star = true;
     523     1361084 :                 accum = 0;
     524     1361084 :                 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           0 :                         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     6665626 :             case 'l':
     564     6665626 :                 if (longflag)
     565       46194 :                     longlongflag = 1;
     566             :                 else
     567     6619432 :                     longflag = 1;
     568     6665626 :                 goto nextch2;
     569      317630 :             case 'z':
     570             : #if SIZEOF_SIZE_T == 8
     571             : #ifdef HAVE_LONG_INT_64
     572      317630 :                 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      317630 :                 goto nextch2;
     582          88 :             case 'h':
     583             :             case '\'':
     584             :                 /* ignore these */
     585          88 :                 goto nextch2;
     586   210580934 :             case 'd':
     587             :             case 'i':
     588   210580934 :                 if (!have_star)
     589             :                 {
     590   210541768 :                     if (pointflag)
     591           0 :                         precision = accum;
     592             :                     else
     593   210541768 :                         fieldwidth = accum;
     594             :                 }
     595   210580934 :                 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   210580934 :                     if (longlongflag)
     607       45282 :                         numvalue = va_arg(args, long long);
     608   210535652 :                     else if (longflag)
     609     4804496 :                         numvalue = va_arg(args, long);
     610             :                     else
     611   205731156 :                         numvalue = va_arg(args, int);
     612             :                 }
     613   210580938 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     614             :                        precision, pointflag, target);
     615   210580934 :                 break;
     616   111862408 :             case 'o':
     617             :             case 'u':
     618             :             case 'x':
     619             :             case 'X':
     620   111862408 :                 if (!have_star)
     621             :                 {
     622   111862408 :                     if (pointflag)
     623           0 :                         precision = accum;
     624             :                     else
     625   111862408 :                         fieldwidth = accum;
     626             :                 }
     627   111862408 :                 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   111862408 :                     if (longlongflag)
     639         912 :                         numvalue = (unsigned long long) va_arg(args, long long);
     640   111861496 :                     else if (longflag)
     641     2086372 :                         numvalue = (unsigned long) va_arg(args, long);
     642             :                     else
     643   109775124 :                         numvalue = (unsigned int) va_arg(args, int);
     644             :                 }
     645   111862408 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     646             :                        precision, pointflag, target);
     647   111862408 :                 break;
     648       38902 :             case 'c':
     649       38902 :                 if (!have_star)
     650             :                 {
     651       38860 :                     if (pointflag)
     652           0 :                         precision = accum;
     653             :                     else
     654       38860 :                         fieldwidth = accum;
     655             :                 }
     656       38902 :                 if (have_dollar)
     657           0 :                     cvalue = (unsigned char) argvalues[fmtpos].i;
     658             :                 else
     659       38902 :                     cvalue = (unsigned char) va_arg(args, int);
     660       38902 :                 fmtchar(cvalue, leftjust, fieldwidth, target);
     661       38902 :                 break;
     662     1799734 :             case 's':
     663     1799734 :                 if (!have_star)
     664             :                 {
     665      517430 :                     if (pointflag)
     666           0 :                         precision = accum;
     667             :                     else
     668      517430 :                         fieldwidth = accum;
     669             :                 }
     670     1799734 :                 if (have_dollar)
     671           0 :                     strvalue = argvalues[fmtpos].cptr;
     672             :                 else
     673     1799734 :                     strvalue = va_arg(args, char *);
     674             :                 /* If string is NULL, silently substitute "(null)" */
     675     1799734 :                 if (strvalue == NULL)
     676           0 :                     strvalue = "(null)";
     677     1799734 :                 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
     678             :                        target);
     679     1799734 :                 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      606398 :             case 'e':
     689             :             case 'E':
     690             :             case 'f':
     691             :             case 'g':
     692             :             case 'G':
     693      606398 :                 if (!have_star)
     694             :                 {
     695      566828 :                     if (pointflag)
     696       97854 :                         precision = accum;
     697             :                     else
     698      468974 :                         fieldwidth = accum;
     699             :                 }
     700      606398 :                 if (have_dollar)
     701           0 :                     fvalue = argvalues[fmtpos].d;
     702             :                 else
     703      606398 :                     fvalue = va_arg(args, double);
     704      606398 :                 fmtfloat(fvalue, ch, forcesign, leftjust,
     705             :                          fieldwidth, zpad,
     706             :                          precision, pointflag,
     707             :                          target);
     708      606398 :                 break;
     709         266 :             case 'm':
     710             :                 {
     711             :                     char        errbuf[PG_STRERROR_R_BUFLEN];
     712         266 :                     const char *errm = strerror_r(save_errno,
     713             :                                                   errbuf, sizeof(errbuf));
     714             : 
     715         266 :                     dostr(errm, strlen(errm), target);
     716             :                 }
     717         266 :                 break;
     718        2716 :             case '%':
     719        2716 :                 dopr_outch('%', target);
     720        2716 :                 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   324891438 :         if (target->failed)
     732           0 :             break;
     733             :     }
     734             : 
     735   374315120 :     return;
     736             : 
     737           0 : bad_format:
     738           0 :     errno = EINVAL;
     739           0 :     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           0 :     int         last_dollar = 0;    /* Init to "no dollar arguments known" */
     760           0 :     PrintfArgType argtypes[PG_NL_ARGMAX + 1] = {0};
     761             : 
     762             :     /*
     763             :      * This loop must accept the same format strings as the one in dopr().
     764             :      * However, we don't need to analyze them to the same level of detail.
     765             :      *
     766             :      * Since we're only called if there's a dollar-type spec somewhere, we can
     767             :      * fail immediately if we find a non-dollar spec.  Per the C99 standard,
     768             :      * all argument references in the format string must be one or the other.
     769             :      */
     770           0 :     while (*format != '\0')
     771             :     {
     772             :         /* Locate next conversion specifier */
     773           0 :         if (*format != '%')
     774             :         {
     775             :             /* Unlike dopr, we can just quit if there's no more specifiers */
     776           0 :             format = strchr(format + 1, '%');
     777           0 :             if (format == NULL)
     778           0 :                 break;
     779             :         }
     780             : 
     781             :         /* Process conversion spec starting at *format */
     782           0 :         format++;
     783           0 :         longflag = longlongflag = 0;
     784           0 :         fmtpos = accum = 0;
     785           0 :         afterstar = false;
     786           0 : nextch1:
     787           0 :         ch = *format++;
     788           0 :         switch (ch)
     789             :         {
     790           0 :             case '-':
     791             :             case '+':
     792           0 :                 goto nextch1;
     793           0 :             case '0':
     794             :             case '1':
     795             :             case '2':
     796             :             case '3':
     797             :             case '4':
     798             :             case '5':
     799             :             case '6':
     800             :             case '7':
     801             :             case '8':
     802             :             case '9':
     803           0 :                 accum = accum * 10 + (ch - '0');
     804           0 :                 goto nextch1;
     805           0 :             case '.':
     806           0 :                 accum = 0;
     807           0 :                 goto nextch1;
     808           0 :             case '*':
     809           0 :                 if (afterstar)
     810           0 :                     return false;   /* previous star missing dollar */
     811           0 :                 afterstar = true;
     812           0 :                 accum = 0;
     813           0 :                 goto nextch1;
     814           0 :             case '$':
     815           0 :                 if (accum <= 0 || accum > PG_NL_ARGMAX)
     816           0 :                     return false;
     817           0 :                 if (afterstar)
     818             :                 {
     819           0 :                     if (argtypes[accum] &&
     820           0 :                         argtypes[accum] != ATYPE_INT)
     821           0 :                         return false;
     822           0 :                     argtypes[accum] = ATYPE_INT;
     823           0 :                     last_dollar = Max(last_dollar, accum);
     824           0 :                     afterstar = false;
     825             :                 }
     826             :                 else
     827           0 :                     fmtpos = accum;
     828           0 :                 accum = 0;
     829           0 :                 goto nextch1;
     830           0 :             case 'l':
     831           0 :                 if (longflag)
     832           0 :                     longlongflag = 1;
     833             :                 else
     834           0 :                     longflag = 1;
     835           0 :                 goto nextch1;
     836           0 :             case 'z':
     837             : #if SIZEOF_SIZE_T == 8
     838             : #ifdef HAVE_LONG_INT_64
     839           0 :                 longflag = 1;
     840             : #elif defined(HAVE_LONG_LONG_INT_64)
     841             :                 longlongflag = 1;
     842             : #else
     843             : #error "Don't know how to print 64bit integers"
     844             : #endif
     845             : #else
     846             :                 /* assume size_t is same size as int */
     847             : #endif
     848           0 :                 goto nextch1;
     849           0 :             case 'h':
     850             :             case '\'':
     851             :                 /* ignore these */
     852           0 :                 goto nextch1;
     853           0 :             case 'd':
     854             :             case 'i':
     855             :             case 'o':
     856             :             case 'u':
     857             :             case 'x':
     858             :             case 'X':
     859           0 :                 if (fmtpos)
     860             :                 {
     861             :                     PrintfArgType atype;
     862             : 
     863           0 :                     if (longlongflag)
     864           0 :                         atype = ATYPE_LONGLONG;
     865           0 :                     else if (longflag)
     866           0 :                         atype = ATYPE_LONG;
     867             :                     else
     868           0 :                         atype = ATYPE_INT;
     869           0 :                     if (argtypes[fmtpos] &&
     870           0 :                         argtypes[fmtpos] != atype)
     871           0 :                         return false;
     872           0 :                     argtypes[fmtpos] = atype;
     873           0 :                     last_dollar = Max(last_dollar, fmtpos);
     874             :                 }
     875             :                 else
     876           0 :                     return false;   /* non-dollar conversion spec */
     877           0 :                 break;
     878           0 :             case 'c':
     879           0 :                 if (fmtpos)
     880             :                 {
     881           0 :                     if (argtypes[fmtpos] &&
     882           0 :                         argtypes[fmtpos] != ATYPE_INT)
     883           0 :                         return false;
     884           0 :                     argtypes[fmtpos] = ATYPE_INT;
     885           0 :                     last_dollar = Max(last_dollar, fmtpos);
     886             :                 }
     887             :                 else
     888           0 :                     return false;   /* non-dollar conversion spec */
     889           0 :                 break;
     890           0 :             case 's':
     891             :             case 'p':
     892           0 :                 if (fmtpos)
     893             :                 {
     894           0 :                     if (argtypes[fmtpos] &&
     895           0 :                         argtypes[fmtpos] != ATYPE_CHARPTR)
     896           0 :                         return false;
     897           0 :                     argtypes[fmtpos] = ATYPE_CHARPTR;
     898           0 :                     last_dollar = Max(last_dollar, fmtpos);
     899             :                 }
     900             :                 else
     901           0 :                     return false;   /* non-dollar conversion spec */
     902           0 :                 break;
     903           0 :             case 'e':
     904             :             case 'E':
     905             :             case 'f':
     906             :             case 'g':
     907             :             case 'G':
     908           0 :                 if (fmtpos)
     909             :                 {
     910           0 :                     if (argtypes[fmtpos] &&
     911           0 :                         argtypes[fmtpos] != ATYPE_DOUBLE)
     912           0 :                         return false;
     913           0 :                     argtypes[fmtpos] = ATYPE_DOUBLE;
     914           0 :                     last_dollar = Max(last_dollar, fmtpos);
     915             :                 }
     916             :                 else
     917           0 :                     return false;   /* non-dollar conversion spec */
     918           0 :                 break;
     919           0 :             case 'm':
     920             :             case '%':
     921           0 :                 break;
     922           0 :             default:
     923           0 :                 return false;   /* bogus format string */
     924             :         }
     925             : 
     926             :         /*
     927             :          * If we finish the spec with afterstar still set, there's a
     928             :          * non-dollar star in there.
     929             :          */
     930           0 :         if (afterstar)
     931           0 :             return false;       /* non-dollar conversion spec */
     932             :     }
     933             : 
     934             :     /*
     935             :      * Format appears valid so far, so collect the arguments in physical
     936             :      * order.  (Since we rejected any non-dollar specs that would have
     937             :      * collected arguments, we know that dopr() hasn't collected any yet.)
     938             :      */
     939           0 :     for (i = 1; i <= last_dollar; i++)
     940             :     {
     941           0 :         switch (argtypes[i])
     942             :         {
     943           0 :             case ATYPE_NONE:
     944           0 :                 return false;
     945           0 :             case ATYPE_INT:
     946           0 :                 argvalues[i].i = va_arg(args, int);
     947           0 :                 break;
     948           0 :             case ATYPE_LONG:
     949           0 :                 argvalues[i].l = va_arg(args, long);
     950           0 :                 break;
     951           0 :             case ATYPE_LONGLONG:
     952           0 :                 argvalues[i].ll = va_arg(args, long long);
     953           0 :                 break;
     954           0 :             case ATYPE_DOUBLE:
     955           0 :                 argvalues[i].d = va_arg(args, double);
     956           0 :                 break;
     957           0 :             case ATYPE_CHARPTR:
     958           0 :                 argvalues[i].cptr = va_arg(args, char *);
     959           0 :                 break;
     960             :         }
     961           0 :     }
     962             : 
     963           0 :     return true;
     964             : }
     965             : 
     966             : static void
     967     1799734 : fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
     968             :        int pointflag, PrintfTarget *target)
     969             : {
     970             :     int         padlen,
     971             :                 vallen;         /* amount to pad */
     972             : 
     973             :     /*
     974             :      * If a maxwidth (precision) is specified, we must not fetch more bytes
     975             :      * than that.
     976             :      */
     977     1799734 :     if (pointflag)
     978       19354 :         vallen = strnlen(value, maxwidth);
     979             :     else
     980     1780380 :         vallen = strlen(value);
     981             : 
     982     1799734 :     padlen = compute_padlen(minlen, vallen, leftjust);
     983             : 
     984     1799734 :     if (padlen > 0)
     985             :     {
     986      543348 :         dopr_outchmulti(' ', padlen, target);
     987      543348 :         padlen = 0;
     988             :     }
     989             : 
     990     1799734 :     dostr(value, vallen, target);
     991             : 
     992     1799734 :     trailing_pad(padlen, target);
     993     1799734 : }
     994             : 
     995             : static void
     996          82 : fmtptr(const void *value, PrintfTarget *target)
     997             : {
     998             :     int         vallen;
     999             :     char        convert[64];
    1000             : 
    1001             :     /* we rely on regular C library's snprintf to do the basic conversion */
    1002          82 :     vallen = snprintf(convert, sizeof(convert), "%p", value);
    1003          82 :     if (vallen < 0)
    1004           0 :         target->failed = true;
    1005             :     else
    1006          82 :         dostr(convert, vallen, target);
    1007          82 : }
    1008             : 
    1009             : static void
    1010   322443344 : fmtint(long long value, char type, int forcesign, int leftjust,
    1011             :        int minlen, int zpad, int precision, int pointflag,
    1012             :        PrintfTarget *target)
    1013             : {
    1014             :     unsigned long long uvalue;
    1015             :     int         base;
    1016             :     int         dosign;
    1017   322443344 :     const char *cvt = "0123456789abcdef";
    1018   322443344 :     int         signvalue = 0;
    1019             :     char        convert[64];
    1020   322443344 :     int         vallen = 0;
    1021             :     int         padlen;         /* amount to pad */
    1022             :     int         zeropad;        /* extra leading zeroes */
    1023             : 
    1024   322443344 :     switch (type)
    1025             :     {
    1026   210580938 :         case 'd':
    1027             :         case 'i':
    1028   210580938 :             base = 10;
    1029   210580938 :             dosign = 1;
    1030   210580938 :             break;
    1031       12386 :         case 'o':
    1032       12386 :             base = 8;
    1033       12386 :             dosign = 0;
    1034       12386 :             break;
    1035   108984064 :         case 'u':
    1036   108984064 :             base = 10;
    1037   108984064 :             dosign = 0;
    1038   108984064 :             break;
    1039       56872 :         case 'x':
    1040       56872 :             base = 16;
    1041       56872 :             dosign = 0;
    1042       56872 :             break;
    1043     2809086 :         case 'X':
    1044     2809086 :             cvt = "0123456789ABCDEF";
    1045     2809086 :             base = 16;
    1046     2809086 :             dosign = 0;
    1047     2809086 :             break;
    1048           0 :         default:
    1049           0 :             return;             /* keep compiler quiet */
    1050             :     }
    1051             : 
    1052             :     /* disable MSVC warning about applying unary minus to an unsigned value */
    1053             : #ifdef _MSC_VER
    1054             : #pragma warning(push)
    1055             : #pragma warning(disable: 4146)
    1056             : #endif
    1057             :     /* Handle +/- */
    1058   322443346 :     if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
    1059    28146602 :         uvalue = -(unsigned long long) value;
    1060             :     else
    1061   294296744 :         uvalue = (unsigned long long) value;
    1062             : #ifdef _MSC_VER
    1063             : #pragma warning(pop)
    1064             : #endif
    1065             : 
    1066             :     /*
    1067             :      * SUS: the result of converting 0 with an explicit precision of 0 is no
    1068             :      * characters
    1069             :      */
    1070   322443346 :     if (value == 0 && pointflag && precision == 0)
    1071           0 :         vallen = 0;
    1072             :     else
    1073             :     {
    1074             :         /*
    1075             :          * Convert integer to string.  We special-case each of the possible
    1076             :          * base values so as to avoid general-purpose divisions.  On most
    1077             :          * machines, division by a fixed constant can be done much more
    1078             :          * cheaply than a general divide.
    1079             :          */
    1080   322443346 :         if (base == 10)
    1081             :         {
    1082             :             do
    1083             :             {
    1084   583785748 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10];
    1085   583785748 :                 uvalue = uvalue / 10;
    1086   583785748 :             } while (uvalue);
    1087             :         }
    1088     2878344 :         else if (base == 16)
    1089             :         {
    1090             :             do
    1091             :             {
    1092     9929534 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16];
    1093     9929534 :                 uvalue = uvalue / 16;
    1094     9929534 :             } while (uvalue);
    1095             :         }
    1096             :         else                    /* base == 8 */
    1097             :         {
    1098             :             do
    1099             :             {
    1100       37158 :                 convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8];
    1101       37158 :                 uvalue = uvalue / 8;
    1102       37158 :             } while (uvalue);
    1103             :         }
    1104             :     }
    1105             : 
    1106   322443346 :     zeropad = Max(0, precision - vallen);
    1107             : 
    1108   322443346 :     padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
    1109             : 
    1110   322443348 :     leading_pad(zpad, signvalue, &padlen, target);
    1111             : 
    1112   322443344 :     if (zeropad > 0)
    1113           0 :         dopr_outchmulti('0', zeropad, target);
    1114             : 
    1115   322443344 :     dostr(convert + sizeof(convert) - vallen, vallen, target);
    1116             : 
    1117   322443344 :     trailing_pad(padlen, target);
    1118             : }
    1119             : 
    1120             : static void
    1121       38902 : fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
    1122             : {
    1123             :     int         padlen;         /* amount to pad */
    1124             : 
    1125       38902 :     padlen = compute_padlen(minlen, 1, leftjust);
    1126             : 
    1127       38902 :     if (padlen > 0)
    1128             :     {
    1129          42 :         dopr_outchmulti(' ', padlen, target);
    1130          42 :         padlen = 0;
    1131             :     }
    1132             : 
    1133       38902 :     dopr_outch(value, target);
    1134             : 
    1135       38902 :     trailing_pad(padlen, target);
    1136       38902 : }
    1137             : 
    1138             : static void
    1139      606398 : fmtfloat(double value, char type, int forcesign, int leftjust,
    1140             :          int minlen, int zpad, int precision, int pointflag,
    1141             :          PrintfTarget *target)
    1142             : {
    1143      606398 :     int         signvalue = 0;
    1144             :     int         prec;
    1145             :     int         vallen;
    1146             :     char        fmt[8];
    1147             :     char        convert[1024];
    1148      606398 :     int         zeropadlen = 0; /* amount to pad with zeroes */
    1149             :     int         padlen;         /* amount to pad with spaces */
    1150             : 
    1151             :     /*
    1152             :      * We rely on the regular C library's snprintf to do the basic conversion,
    1153             :      * then handle padding considerations here.
    1154             :      *
    1155             :      * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
    1156             :      * too wildly more than that with other hardware.  In "f" format, snprintf
    1157             :      * could therefore generate at most 308 characters to the left of the
    1158             :      * decimal point; while we need to allow the precision to get as high as
    1159             :      * 308+17 to ensure that we don't truncate significant digits from very
    1160             :      * small values.  To handle both these extremes, we use a buffer of 1024
    1161             :      * bytes and limit requested precision to 350 digits; this should prevent
    1162             :      * buffer overrun even with non-IEEE math.  If the original precision
    1163             :      * request was more than 350, separately pad with zeroes.
    1164             :      *
    1165             :      * We handle infinities and NaNs specially to ensure platform-independent
    1166             :      * output.
    1167             :      */
    1168      606398 :     if (precision < 0)           /* cover possible overflow of "accum" */
    1169           0 :         precision = 0;
    1170      606398 :     prec = Min(precision, 350);
    1171             : 
    1172      606398 :     if (isnan(value))
    1173             :     {
    1174         140 :         strcpy(convert, "NaN");
    1175         140 :         vallen = 3;
    1176             :         /* no zero padding, regardless of precision spec */
    1177             :     }
    1178             :     else
    1179             :     {
    1180             :         /*
    1181             :          * Handle sign (NaNs have no sign, so we don't do this in the case
    1182             :          * above).  "value < 0.0" will not be true for IEEE minus zero, so we
    1183             :          * detect that by looking for the case where value equals 0.0
    1184             :          * according to == but not according to memcmp.
    1185             :          */
    1186             :         static const double dzero = 0.0;
    1187             : 
    1188     1196496 :         if (adjust_sign((value < 0.0 ||
    1189      590238 :                          (value == 0.0 &&
    1190      115796 :                           memcmp(&value, &dzero, sizeof(double)) != 0)),
    1191             :                         forcesign, &signvalue))
    1192       16020 :             value = -value;
    1193             : 
    1194      606258 :         if (isinf(value))
    1195             :         {
    1196          96 :             strcpy(convert, "Infinity");
    1197          96 :             vallen = 8;
    1198             :             /* no zero padding, regardless of precision spec */
    1199             :         }
    1200      606162 :         else if (pointflag)
    1201             :         {
    1202      137188 :             zeropadlen = precision - prec;
    1203      137188 :             fmt[0] = '%';
    1204      137188 :             fmt[1] = '.';
    1205      137188 :             fmt[2] = '*';
    1206      137188 :             fmt[3] = type;
    1207      137188 :             fmt[4] = '\0';
    1208      137188 :             vallen = snprintf(convert, sizeof(convert), fmt, prec, value);
    1209             :         }
    1210             :         else
    1211             :         {
    1212      468974 :             fmt[0] = '%';
    1213      468974 :             fmt[1] = type;
    1214      468974 :             fmt[2] = '\0';
    1215      468974 :             vallen = snprintf(convert, sizeof(convert), fmt, value);
    1216             :         }
    1217      606258 :         if (vallen < 0)
    1218           0 :             goto fail;
    1219             : 
    1220             :         /*
    1221             :          * Windows, alone among our supported platforms, likes to emit
    1222             :          * three-digit exponent fields even when two digits would do.  Hack
    1223             :          * such results to look like the way everyone else does it.
    1224             :          */
    1225             : #ifdef WIN32
    1226             :         if (vallen >= 6 &&
    1227             :             convert[vallen - 5] == 'e' &&
    1228             :             convert[vallen - 3] == '0')
    1229             :         {
    1230             :             convert[vallen - 3] = convert[vallen - 2];
    1231             :             convert[vallen - 2] = convert[vallen - 1];
    1232             :             vallen--;
    1233             :         }
    1234             : #endif
    1235             :     }
    1236             : 
    1237      606398 :     padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
    1238             : 
    1239      606398 :     leading_pad(zpad, signvalue, &padlen, target);
    1240             : 
    1241      606398 :     if (zeropadlen > 0)
    1242             :     {
    1243             :         /* If 'e' or 'E' format, inject zeroes before the exponent */
    1244           0 :         char       *epos = strrchr(convert, 'e');
    1245             : 
    1246           0 :         if (!epos)
    1247           0 :             epos = strrchr(convert, 'E');
    1248           0 :         if (epos)
    1249             :         {
    1250             :             /* pad before exponent */
    1251           0 :             dostr(convert, epos - convert, target);
    1252           0 :             dopr_outchmulti('0', zeropadlen, target);
    1253           0 :             dostr(epos, vallen - (epos - convert), target);
    1254             :         }
    1255             :         else
    1256             :         {
    1257             :             /* no exponent, pad after the digits */
    1258           0 :             dostr(convert, vallen, target);
    1259           0 :             dopr_outchmulti('0', zeropadlen, target);
    1260             :         }
    1261             :     }
    1262             :     else
    1263             :     {
    1264             :         /* no zero padding, just emit the number as-is */
    1265      606398 :         dostr(convert, vallen, target);
    1266             :     }
    1267             : 
    1268      606398 :     trailing_pad(padlen, target);
    1269      606398 :     return;
    1270             : 
    1271           0 : fail:
    1272           0 :     target->failed = true;
    1273             : }
    1274             : 
    1275             : /*
    1276             :  * Nonstandard entry point to print a double value efficiently.
    1277             :  *
    1278             :  * This is approximately equivalent to strfromd(), but has an API more
    1279             :  * adapted to what float8out() wants.  The behavior is like snprintf()
    1280             :  * with a format of "%.ng", where n is the specified precision.
    1281             :  * However, the target buffer must be nonempty (i.e. count > 0), and
    1282             :  * the precision is silently bounded to a sane range.
    1283             :  */
    1284             : int
    1285      230030 : pg_strfromd(char *str, size_t count, int precision, double value)
    1286             : {
    1287             :     PrintfTarget target;
    1288      230030 :     int         signvalue = 0;
    1289             :     int         vallen;
    1290             :     char        fmt[8];
    1291             :     char        convert[64];
    1292             : 
    1293             :     /* Set up the target like pg_snprintf, but require nonempty buffer */
    1294             :     Assert(count > 0);
    1295      230030 :     target.bufstart = target.bufptr = str;
    1296      230030 :     target.bufend = str + count - 1;
    1297      230030 :     target.stream = NULL;
    1298      230030 :     target.nchars = 0;
    1299      230030 :     target.failed = false;
    1300             : 
    1301             :     /*
    1302             :      * We bound precision to a reasonable range; the combination of this and
    1303             :      * the knowledge that we're using "g" format without padding allows the
    1304             :      * convert[] buffer to be reasonably small.
    1305             :      */
    1306      230030 :     if (precision < 1)
    1307           0 :         precision = 1;
    1308      230030 :     else if (precision > 32)
    1309           0 :         precision = 32;
    1310             : 
    1311             :     /*
    1312             :      * The rest is just an inlined version of the fmtfloat() logic above,
    1313             :      * simplified using the knowledge that no padding is wanted.
    1314             :      */
    1315      230030 :     if (isnan(value))
    1316             :     {
    1317       12084 :         strcpy(convert, "NaN");
    1318       12084 :         vallen = 3;
    1319             :     }
    1320             :     else
    1321             :     {
    1322             :         static const double dzero = 0.0;
    1323             : 
    1324      217946 :         if (value < 0.0 ||
    1325      185552 :             (value == 0.0 &&
    1326       26866 :              memcmp(&value, &dzero, sizeof(double)) != 0))
    1327             :         {
    1328       32460 :             signvalue = '-';
    1329       32460 :             value = -value;
    1330             :         }
    1331             : 
    1332      217946 :         if (isinf(value))
    1333             :         {
    1334        7092 :             strcpy(convert, "Infinity");
    1335        7092 :             vallen = 8;
    1336             :         }
    1337             :         else
    1338             :         {
    1339      210854 :             fmt[0] = '%';
    1340      210854 :             fmt[1] = '.';
    1341      210854 :             fmt[2] = '*';
    1342      210854 :             fmt[3] = 'g';
    1343      210854 :             fmt[4] = '\0';
    1344      210854 :             vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
    1345      210854 :             if (vallen < 0)
    1346             :             {
    1347           0 :                 target.failed = true;
    1348           0 :                 goto fail;
    1349             :             }
    1350             : 
    1351             : #ifdef WIN32
    1352             :             if (vallen >= 6 &&
    1353             :                 convert[vallen - 5] == 'e' &&
    1354             :                 convert[vallen - 3] == '0')
    1355             :             {
    1356             :                 convert[vallen - 3] = convert[vallen - 2];
    1357             :                 convert[vallen - 2] = convert[vallen - 1];
    1358             :                 vallen--;
    1359             :             }
    1360             : #endif
    1361             :         }
    1362             :     }
    1363             : 
    1364      230030 :     if (signvalue)
    1365       32460 :         dopr_outch(signvalue, &target);
    1366             : 
    1367      230030 :     dostr(convert, vallen, &target);
    1368             : 
    1369      230030 : fail:
    1370      230030 :     *(target.bufptr) = '\0';
    1371      460060 :     return target.failed ? -1 : (target.bufptr - target.bufstart
    1372      230030 :                                  + target.nchars);
    1373             : }
    1374             : 
    1375             : 
    1376             : static void
    1377   752935996 : dostr(const char *str, int slen, PrintfTarget *target)
    1378             : {
    1379             :     /* fast path for common case of slen == 1 */
    1380   752935996 :     if (slen == 1)
    1381             :     {
    1382   280285332 :         dopr_outch(*str, target);
    1383   280285332 :         return;
    1384             :     }
    1385             : 
    1386   942771198 :     while (slen > 0)
    1387             :     {
    1388             :         int         avail;
    1389             : 
    1390   470572630 :         if (target->bufend != NULL)
    1391   466285216 :             avail = target->bufend - target->bufptr;
    1392             :         else
    1393     4287414 :             avail = slen;
    1394   470572630 :         if (avail <= 0)
    1395             :         {
    1396             :             /* buffer full, can we dump to stream? */
    1397      452594 :             if (target->stream == NULL)
    1398             :             {
    1399      452096 :                 target->nchars += slen; /* no, lose the data */
    1400      452096 :                 return;
    1401             :             }
    1402         498 :             flushbuffer(target);
    1403         498 :             continue;
    1404             :         }
    1405   470120036 :         avail = Min(avail, slen);
    1406   470120036 :         memmove(target->bufptr, str, avail);
    1407   470120036 :         target->bufptr += avail;
    1408   470120036 :         str += avail;
    1409   470120036 :         slen -= avail;
    1410             :     }
    1411             : }
    1412             : 
    1413             : static void
    1414   312527232 : dopr_outch(int c, PrintfTarget *target)
    1415             : {
    1416   312527232 :     if (target->bufend != NULL && target->bufptr >= target->bufend)
    1417             :     {
    1418             :         /* buffer full, can we dump to stream? */
    1419       82530 :         if (target->stream == NULL)
    1420             :         {
    1421       82530 :             target->nchars++;    /* no, lose the data */
    1422       82530 :             return;
    1423             :         }
    1424           0 :         flushbuffer(target);
    1425             :     }
    1426   312444702 :     *(target->bufptr++) = c;
    1427             : }
    1428             : 
    1429             : static void
    1430     6825122 : dopr_outchmulti(int c, int slen, PrintfTarget *target)
    1431             : {
    1432             :     /* fast path for common case of slen == 1 */
    1433     6825122 :     if (slen == 1)
    1434             :     {
    1435     4005014 :         dopr_outch(c, target);
    1436     4005014 :         return;
    1437             :     }
    1438             : 
    1439     5640438 :     while (slen > 0)
    1440             :     {
    1441             :         int         avail;
    1442             : 
    1443     2820330 :         if (target->bufend != NULL)
    1444     2809880 :             avail = target->bufend - target->bufptr;
    1445             :         else
    1446       10450 :             avail = slen;
    1447     2820330 :         if (avail <= 0)
    1448             :         {
    1449             :             /* buffer full, can we dump to stream? */
    1450         114 :             if (target->stream == NULL)
    1451             :             {
    1452           0 :                 target->nchars += slen; /* no, lose the data */
    1453           0 :                 return;
    1454             :             }
    1455         114 :             flushbuffer(target);
    1456         114 :             continue;
    1457             :         }
    1458     2820216 :         avail = Min(avail, slen);
    1459     2820216 :         memset(target->bufptr, c, avail);
    1460     2820216 :         target->bufptr += avail;
    1461     2820216 :         slen -= avail;
    1462             :     }
    1463             : }
    1464             : 
    1465             : 
    1466             : static int
    1467   211187194 : adjust_sign(int is_negative, int forcesign, int *signvalue)
    1468             : {
    1469   211187194 :     if (is_negative)
    1470             :     {
    1471    28162622 :         *signvalue = '-';
    1472    28162622 :         return true;
    1473             :     }
    1474   183024572 :     else if (forcesign)
    1475         186 :         *signvalue = '+';
    1476   183024572 :     return false;
    1477             : }
    1478             : 
    1479             : 
    1480             : static int
    1481   324888378 : compute_padlen(int minlen, int vallen, int leftjust)
    1482             : {
    1483             :     int         padlen;
    1484             : 
    1485   324888378 :     padlen = minlen - vallen;
    1486   324888378 :     if (padlen < 0)
    1487   313847122 :         padlen = 0;
    1488   324888378 :     if (leftjust)
    1489      992716 :         padlen = -padlen;
    1490   324888378 :     return padlen;
    1491             : }
    1492             : 
    1493             : 
    1494             : static void
    1495   323049742 : leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
    1496             : {
    1497             :     int         maxpad;
    1498             : 
    1499   323049742 :     if (*padlen > 0 && zpad)
    1500             :     {
    1501     4054278 :         if (signvalue)
    1502             :         {
    1503         180 :             dopr_outch(signvalue, target);
    1504         180 :             --(*padlen);
    1505         180 :             signvalue = 0;
    1506             :         }
    1507     4054278 :         if (*padlen > 0)
    1508             :         {
    1509     4054260 :             dopr_outchmulti(zpad, *padlen, target);
    1510     4054260 :             *padlen = 0;
    1511             :         }
    1512             :     }
    1513   323049742 :     maxpad = (signvalue != 0);
    1514   323049742 :     if (*padlen > maxpad)
    1515             :     {
    1516     1544592 :         dopr_outchmulti(' ', *padlen - maxpad, target);
    1517     1544592 :         *padlen = maxpad;
    1518             :     }
    1519   323049742 :     if (signvalue)
    1520             :     {
    1521    28162628 :         dopr_outch(signvalue, target);
    1522    28162628 :         if (*padlen > 0)
    1523           0 :             --(*padlen);
    1524    28162628 :         else if (*padlen < 0)
    1525           0 :             ++(*padlen);
    1526             :     }
    1527   323049742 : }
    1528             : 
    1529             : 
    1530             : static void
    1531   324888374 : trailing_pad(int padlen, PrintfTarget *target)
    1532             : {
    1533   324888374 :     if (padlen < 0)
    1534      682880 :         dopr_outchmulti(' ', -padlen, target);
    1535   324888374 : }

Generated by: LCOV version 1.14