LCOV - code coverage report
Current view: top level - src/port - snprintf.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 371 554 67.0 %
Date: 2019-11-13 22:07:24 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-2019, 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   230920948 : 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   230920948 :     if (count == 0)
     186             :     {
     187       45830 :         str = onebyte;
     188       45830 :         count = 1;
     189             :     }
     190   230920948 :     target.bufstart = target.bufptr = str;
     191   230920948 :     target.bufend = str + count - 1;
     192   230920948 :     target.stream = NULL;
     193   230920948 :     target.nchars = 0;
     194   230920948 :     target.failed = false;
     195   230920948 :     dopr(&target, fmt, args);
     196   230920948 :     *(target.bufptr) = '\0';
     197   461841896 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     198   230920948 :                                  + target.nchars);
     199             : }
     200             : 
     201             : int
     202     9817898 : pg_snprintf(char *str, size_t count, const char *fmt,...)
     203             : {
     204             :     int         len;
     205             :     va_list     args;
     206             : 
     207     9817898 :     va_start(args, fmt);
     208     9817898 :     len = pg_vsnprintf(str, count, fmt, args);
     209     9817898 :     va_end(args);
     210     9817898 :     return len;
     211             : }
     212             : 
     213             : int
     214     1289676 : pg_vsprintf(char *str, const char *fmt, va_list args)
     215             : {
     216             :     PrintfTarget target;
     217             : 
     218     1289676 :     target.bufstart = target.bufptr = str;
     219     1289676 :     target.bufend = NULL;
     220     1289676 :     target.stream = NULL;
     221     1289676 :     target.nchars = 0;          /* not really used in this case */
     222     1289676 :     target.failed = false;
     223     1289676 :     dopr(&target, fmt, args);
     224     1289682 :     *(target.bufptr) = '\0';
     225     2579364 :     return target.failed ? -1 : (target.bufptr - target.bufstart
     226     1289682 :                                  + target.nchars);
     227             : }
     228             : 
     229             : int
     230     1289670 : pg_sprintf(char *str, const char *fmt,...)
     231             : {
     232             :     int         len;
     233             :     va_list     args;
     234             : 
     235     1289670 :     va_start(args, fmt);
     236     1289670 :     len = pg_vsprintf(str, fmt, args);
     237     1289682 :     va_end(args);
     238     1289682 :     return len;
     239             : }
     240             : 
     241             : int
     242     1106518 : pg_vfprintf(FILE *stream, const char *fmt, va_list args)
     243             : {
     244             :     PrintfTarget target;
     245             :     char        buffer[1024];   /* size is arbitrary */
     246             : 
     247     1106518 :     if (stream == NULL)
     248             :     {
     249           0 :         errno = EINVAL;
     250           0 :         return -1;
     251             :     }
     252     1106518 :     target.bufstart = target.bufptr = buffer;
     253     1106518 :     target.bufend = buffer + sizeof(buffer);    /* use the whole buffer */
     254     1106518 :     target.stream = stream;
     255     1106518 :     target.nchars = 0;
     256     1106518 :     target.failed = false;
     257     1106518 :     dopr(&target, fmt, args);
     258             :     /* dump any remaining buffer contents */
     259     1106518 :     flushbuffer(&target);
     260     1106518 :     return target.failed ? -1 : target.nchars;
     261             : }
     262             : 
     263             : int
     264      834886 : pg_fprintf(FILE *stream, const char *fmt,...)
     265             : {
     266             :     int         len;
     267             :     va_list     args;
     268             : 
     269      834886 :     va_start(args, fmt);
     270      834886 :     len = pg_vfprintf(stream, fmt, args);
     271      834886 :     va_end(args);
     272      834886 :     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      247712 : pg_printf(const char *fmt,...)
     283             : {
     284             :     int         len;
     285             :     va_list     args;
     286             : 
     287      247712 :     va_start(args, fmt);
     288      247712 :     len = pg_vfprintf(stdout, fmt, args);
     289      247712 :     va_end(args);
     290      247712 :     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     1106834 : flushbuffer(PrintfTarget *target)
     299             : {
     300     1106834 :     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     1106834 :     if (!target->failed && nc > 0)
     307             :     {
     308             :         size_t      written;
     309             : 
     310      984326 :         written = fwrite(target->bufstart, 1, nc, target->stream);
     311      984326 :         target->nchars += written;
     312      984326 :         if (written != nc)
     313           0 :             target->failed = true;
     314             :     }
     315     1106834 :     target->bufptr = target->bufstart;
     316     1106834 : }
     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(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   233317136 : dopr(PrintfTarget *target, const char *format, va_list args)
     377             : {
     378   233317136 :     int         save_errno = errno;
     379   233317138 :     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             :     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   233317138 :     have_dollar = false;
     407             : 
     408   706036994 :     while (*format != '\0')
     409             :     {
     410             :         /* Locate next conversion specifier */
     411   288669632 :         if (*format != '%')
     412             :         {
     413             :             /* Scan to next '%' or end of string */
     414   232805768 :             const char *next_pct = strchrnul(format + 1, '%');
     415             : 
     416             :             /* Dump literal data we just scanned over */
     417   232805768 :             dostr(format, next_pct - format, target);
     418   232805768 :             if (target->failed)
     419           0 :                 break;
     420             : 
     421   232805768 :             if (*next_pct == '\0')
     422    49266922 :                 break;
     423   183538846 :             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   239402710 :         if (first_pct == NULL)
     432   232099164 :             first_pct = format;
     433             : 
     434             :         /* Process conversion spec starting at *format */
     435   239402710 :         format++;
     436             : 
     437             :         /* Fast path for conversion spec that is exactly %s */
     438   239402710 :         if (*format == 's')
     439             :         {
     440    31019818 :             format++;
     441    31019818 :             strvalue = va_arg(args, char *);
     442             :             Assert(strvalue != NULL);
     443    31019818 :             dostr(strvalue, strlen(strvalue), target);
     444    31019818 :             if (target->failed)
     445           0 :                 break;
     446    31019818 :             continue;
     447             :         }
     448             : 
     449   208382892 :         fieldwidth = precision = zpad = leftjust = forcesign = 0;
     450   208382892 :         longflag = longlongflag = pointflag = 0;
     451   208382892 :         fmtpos = accum = 0;
     452   208382892 :         have_star = afterstar = false;
     453             : nextch2:
     454   220595528 :         ch = *format++;
     455   220595528 :         switch (ch)
     456             :         {
     457             :             case '-':
     458      247328 :                 leftjust = 1;
     459      247328 :                 goto nextch2;
     460             :             case '+':
     461          40 :                 forcesign = 1;
     462          40 :                 goto nextch2;
     463             :             case '0':
     464             :                 /* set zero padding if no nonzero digits yet */
     465     1791140 :                 if (accum == 0 && !pointflag)
     466     1038270 :                     zpad = '0';
     467             :                 /* FALL THRU */
     468             :             case '1':
     469             :             case '2':
     470             :             case '3':
     471             :             case '4':
     472             :             case '5':
     473             :             case '6':
     474             :             case '7':
     475             :             case '8':
     476             :             case '9':
     477     4237354 :                 accum = accum * 10 + (ch - '0');
     478     4237354 :                 goto nextch2;
     479             :             case '.':
     480     2155910 :                 if (have_star)
     481           0 :                     have_star = false;
     482             :                 else
     483     2155910 :                     fieldwidth = accum;
     484     2155910 :                 pointflag = 1;
     485     2155910 :                 accum = 0;
     486     2155910 :                 goto nextch2;
     487             :             case '*':
     488      710830 :                 if (have_dollar)
     489             :                 {
     490             :                     /*
     491             :                      * We'll process value after reading n$.  Note it's OK to
     492             :                      * assume have_dollar is set correctly, because in a valid
     493             :                      * format string the initial % must have had n$ if * does.
     494             :                      */
     495           0 :                     afterstar = true;
     496             :                 }
     497             :                 else
     498             :                 {
     499             :                     /* fetch and process value now */
     500      710830 :                     int         starval = va_arg(args, int);
     501             : 
     502      710830 :                     if (pointflag)
     503             :                     {
     504       30874 :                         precision = starval;
     505       30874 :                         if (precision < 0)
     506             :                         {
     507           0 :                             precision = 0;
     508           0 :                             pointflag = 0;
     509             :                         }
     510             :                     }
     511             :                     else
     512             :                     {
     513      679956 :                         fieldwidth = starval;
     514      679956 :                         if (fieldwidth < 0)
     515             :                         {
     516        3556 :                             leftjust = 1;
     517        3556 :                             fieldwidth = -fieldwidth;
     518             :                         }
     519             :                     }
     520             :                 }
     521      710830 :                 have_star = true;
     522      710830 :                 accum = 0;
     523      710830 :                 goto nextch2;
     524             :             case '$':
     525             :                 /* First dollar sign? */
     526           0 :                 if (!have_dollar)
     527             :                 {
     528             :                     /* Yup, so examine all conversion specs in format */
     529           0 :                     if (!find_arguments(first_pct, args, argvalues))
     530           0 :                         goto bad_format;
     531           0 :                     have_dollar = true;
     532             :                 }
     533           0 :                 if (afterstar)
     534             :                 {
     535             :                     /* fetch and process star value */
     536           0 :                     int         starval = argvalues[accum].i;
     537             : 
     538           0 :                     if (pointflag)
     539             :                     {
     540           0 :                         precision = starval;
     541           0 :                         if (precision < 0)
     542             :                         {
     543           0 :                             precision = 0;
     544           0 :                             pointflag = 0;
     545             :                         }
     546             :                     }
     547             :                     else
     548             :                     {
     549           0 :                         fieldwidth = starval;
     550           0 :                         if (fieldwidth < 0)
     551             :                         {
     552           0 :                             leftjust = 1;
     553           0 :                             fieldwidth = -fieldwidth;
     554             :                         }
     555             :                     }
     556           0 :                     afterstar = false;
     557             :                 }
     558             :                 else
     559           0 :                     fmtpos = accum;
     560           0 :                 accum = 0;
     561           0 :                 goto nextch2;
     562             :             case 'l':
     563     4853812 :                 if (longflag)
     564        6378 :                     longlongflag = 1;
     565             :                 else
     566     4847434 :                     longflag = 1;
     567     4853812 :                 goto nextch2;
     568             :             case 'z':
     569             : #if SIZEOF_SIZE_T == 8
     570             : #ifdef HAVE_LONG_INT_64
     571        7278 :                 longflag = 1;
     572             : #elif defined(HAVE_LONG_LONG_INT_64)
     573             :                 longlongflag = 1;
     574             : #else
     575             : #error "Don't know how to print 64bit integers"
     576             : #endif
     577             : #else
     578             :                 /* assume size_t is same size as int */
     579             : #endif
     580        7278 :                 goto nextch2;
     581             :             case 'h':
     582             :             case '\'':
     583             :                 /* ignore these */
     584          84 :                 goto nextch2;
     585             :             case 'd':
     586             :             case 'i':
     587   120201334 :                 if (!have_star)
     588             :                 {
     589   120175324 :                     if (pointflag)
     590           0 :                         precision = accum;
     591             :                     else
     592   120175324 :                         fieldwidth = accum;
     593             :                 }
     594   120201334 :                 if (have_dollar)
     595             :                 {
     596           0 :                     if (longlongflag)
     597           0 :                         numvalue = argvalues[fmtpos].ll;
     598           0 :                     else if (longflag)
     599           0 :                         numvalue = argvalues[fmtpos].l;
     600             :                     else
     601           0 :                         numvalue = argvalues[fmtpos].i;
     602             :                 }
     603             :                 else
     604             :                 {
     605   120201334 :                     if (longlongflag)
     606        6348 :                         numvalue = va_arg(args, long long);
     607   120194986 :                     else if (longflag)
     608     4224012 :                         numvalue = va_arg(args, long);
     609             :                     else
     610   115970974 :                         numvalue = va_arg(args, int);
     611             :                 }
     612   120201342 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     613             :                        precision, pointflag, target);
     614   120201342 :                 break;
     615             :             case 'o':
     616             :             case 'u':
     617             :             case 'x':
     618             :             case 'X':
     619    85313214 :                 if (!have_star)
     620             :                 {
     621    85313214 :                     if (pointflag)
     622           0 :                         precision = accum;
     623             :                     else
     624    85313214 :                         fieldwidth = accum;
     625             :                 }
     626    85313214 :                 if (have_dollar)
     627             :                 {
     628           0 :                     if (longlongflag)
     629           0 :                         numvalue = (unsigned long long) argvalues[fmtpos].ll;
     630           0 :                     else if (longflag)
     631           0 :                         numvalue = (unsigned long) argvalues[fmtpos].l;
     632             :                     else
     633           0 :                         numvalue = (unsigned int) argvalues[fmtpos].i;
     634             :                 }
     635             :                 else
     636             :                 {
     637    85313214 :                     if (longlongflag)
     638          30 :                         numvalue = (unsigned long long) va_arg(args, long long);
     639    85313184 :                     else if (longflag)
     640      624322 :                         numvalue = (unsigned long) va_arg(args, long);
     641             :                     else
     642    84688862 :                         numvalue = (unsigned int) va_arg(args, int);
     643             :                 }
     644    85313214 :                 fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
     645             :                        precision, pointflag, target);
     646    85313214 :                 break;
     647             :             case 'c':
     648       24396 :                 if (!have_star)
     649             :                 {
     650       24396 :                     if (pointflag)
     651           0 :                         precision = accum;
     652             :                     else
     653       24396 :                         fieldwidth = accum;
     654             :                 }
     655       24396 :                 if (have_dollar)
     656           0 :                     cvalue = (unsigned char) argvalues[fmtpos].i;
     657             :                 else
     658       24396 :                     cvalue = (unsigned char) va_arg(args, int);
     659       24396 :                 fmtchar(cvalue, leftjust, fieldwidth, target);
     660       24396 :                 break;
     661             :             case 's':
     662      695296 :                 if (!have_star)
     663             :                 {
     664       29846 :                     if (pointflag)
     665           0 :                         precision = accum;
     666             :                     else
     667       29846 :                         fieldwidth = accum;
     668             :                 }
     669      695296 :                 if (have_dollar)
     670           0 :                     strvalue = argvalues[fmtpos].cptr;
     671             :                 else
     672      695296 :                     strvalue = va_arg(args, char *);
     673             :                 /* Whine if someone tries to print a NULL string */
     674             :                 Assert(strvalue != NULL);
     675      695296 :                 fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
     676             :                        target);
     677      695296 :                 break;
     678             :             case 'p':
     679             :                 /* fieldwidth/leftjust are ignored ... */
     680          46 :                 if (have_dollar)
     681           0 :                     strvalue = argvalues[fmtpos].cptr;
     682             :                 else
     683          46 :                     strvalue = va_arg(args, char *);
     684          46 :                 fmtptr((void *) strvalue, target);
     685          46 :                 break;
     686             :             case 'e':
     687             :             case 'E':
     688             :             case 'f':
     689             :             case 'g':
     690             :             case 'G':
     691     2148040 :                 if (!have_star)
     692             :                 {
     693     2128670 :                     if (pointflag)
     694     2125036 :                         precision = accum;
     695             :                     else
     696        3634 :                         fieldwidth = accum;
     697             :                 }
     698     2148040 :                 if (have_dollar)
     699           0 :                     fvalue = argvalues[fmtpos].d;
     700             :                 else
     701     2148040 :                     fvalue = va_arg(args, double);
     702     2148040 :                 fmtfloat(fvalue, ch, forcesign, leftjust,
     703             :                          fieldwidth, zpad,
     704             :                          precision, pointflag,
     705             :                          target);
     706     2148040 :                 break;
     707             :             case 'm':
     708             :                 {
     709             :                     char        errbuf[PG_STRERROR_R_BUFLEN];
     710          54 :                     const char *errm = strerror_r(save_errno,
     711             :                                                   errbuf, sizeof(errbuf));
     712             : 
     713          54 :                     dostr(errm, strlen(errm), target);
     714             :                 }
     715          54 :                 break;
     716             :             case '%':
     717         512 :                 dopr_outch('%', target);
     718         512 :                 break;
     719             :             default:
     720             : 
     721             :                 /*
     722             :                  * Anything else --- in particular, '\0' indicating end of
     723             :                  * format string --- is bogus.
     724             :                  */
     725           0 :                 goto bad_format;
     726             :         }
     727             : 
     728             :         /* Check for failure after each conversion spec */
     729   208382900 :         if (target->failed)
     730           0 :             break;
     731             :     }
     732             : 
     733   233317146 :     return;
     734             : 
     735             : bad_format:
     736           0 :     errno = EINVAL;
     737           0 :     target->failed = true;
     738             : }
     739             : 
     740             : /*
     741             :  * find_arguments(): sort out the arguments for a format spec with %n$
     742             :  *
     743             :  * If format is valid, return true and fill argvalues[i] with the value
     744             :  * for the conversion spec that has %i$ or *i$.  Else return false.
     745             :  */
     746             : static bool
     747           0 : find_arguments(const char *format, va_list args,
     748             :                PrintfArgValue *argvalues)
     749             : {
     750             :     int         ch;
     751             :     bool        afterstar;
     752             :     int         accum;
     753             :     int         longlongflag;
     754             :     int         longflag;
     755             :     int         fmtpos;
     756             :     int         i;
     757             :     int         last_dollar;
     758             :     PrintfArgType argtypes[PG_NL_ARGMAX + 1];
     759             : 
     760             :     /* Initialize to "no dollar arguments known" */
     761           0 :     last_dollar = 0;
     762           0 :     MemSet(argtypes, 0, sizeof(argtypes));
     763             : 
     764             :     /*
     765             :      * This loop must accept the same format strings as the one in dopr().
     766             :      * However, we don't need to analyze them to the same level of detail.
     767             :      *
     768             :      * Since we're only called if there's a dollar-type spec somewhere, we can
     769             :      * fail immediately if we find a non-dollar spec.  Per the C99 standard,
     770             :      * all argument references in the format string must be one or the other.
     771             :      */
     772           0 :     while (*format != '\0')
     773             :     {
     774             :         /* Locate next conversion specifier */
     775           0 :         if (*format != '%')
     776             :         {
     777             :             /* Unlike dopr, we can just quit if there's no more specifiers */
     778           0 :             format = strchr(format + 1, '%');
     779           0 :             if (format == NULL)
     780           0 :                 break;
     781             :         }
     782             : 
     783             :         /* Process conversion spec starting at *format */
     784           0 :         format++;
     785           0 :         longflag = longlongflag = 0;
     786           0 :         fmtpos = accum = 0;
     787           0 :         afterstar = false;
     788             : nextch1:
     789           0 :         ch = *format++;
     790           0 :         switch (ch)
     791             :         {
     792             :             case '-':
     793             :             case '+':
     794           0 :                 goto nextch1;
     795             :             case '0':
     796             :             case '1':
     797             :             case '2':
     798             :             case '3':
     799             :             case '4':
     800             :             case '5':
     801             :             case '6':
     802             :             case '7':
     803             :             case '8':
     804             :             case '9':
     805           0 :                 accum = accum * 10 + (ch - '0');
     806           0 :                 goto nextch1;
     807             :             case '.':
     808           0 :                 accum = 0;
     809           0 :                 goto nextch1;
     810             :             case '*':
     811           0 :                 if (afterstar)
     812           0 :                     return false;   /* previous star missing dollar */
     813           0 :                 afterstar = true;
     814           0 :                 accum = 0;
     815           0 :                 goto nextch1;
     816             :             case '$':
     817           0 :                 if (accum <= 0 || accum > PG_NL_ARGMAX)
     818           0 :                     return false;
     819           0 :                 if (afterstar)
     820             :                 {
     821           0 :                     if (argtypes[accum] &&
     822           0 :                         argtypes[accum] != ATYPE_INT)
     823           0 :                         return false;
     824           0 :                     argtypes[accum] = ATYPE_INT;
     825           0 :                     last_dollar = Max(last_dollar, accum);
     826           0 :                     afterstar = false;
     827             :                 }
     828             :                 else
     829           0 :                     fmtpos = accum;
     830           0 :                 accum = 0;
     831           0 :                 goto nextch1;
     832             :             case 'l':
     833           0 :                 if (longflag)
     834           0 :                     longlongflag = 1;
     835             :                 else
     836           0 :                     longflag = 1;
     837           0 :                 goto nextch1;
     838             :             case 'z':
     839             : #if SIZEOF_SIZE_T == 8
     840             : #ifdef HAVE_LONG_INT_64
     841           0 :                 longflag = 1;
     842             : #elif defined(HAVE_LONG_LONG_INT_64)
     843             :                 longlongflag = 1;
     844             : #else
     845             : #error "Don't know how to print 64bit integers"
     846             : #endif
     847             : #else
     848             :                 /* assume size_t is same size as int */
     849             : #endif
     850           0 :                 goto nextch1;
     851             :             case 'h':
     852             :             case '\'':
     853             :                 /* ignore these */
     854           0 :                 goto nextch1;
     855             :             case 'd':
     856             :             case 'i':
     857             :             case 'o':
     858             :             case 'u':
     859             :             case 'x':
     860             :             case 'X':
     861           0 :                 if (fmtpos)
     862             :                 {
     863             :                     PrintfArgType atype;
     864             : 
     865           0 :                     if (longlongflag)
     866           0 :                         atype = ATYPE_LONGLONG;
     867           0 :                     else if (longflag)
     868           0 :                         atype = ATYPE_LONG;
     869             :                     else
     870           0 :                         atype = ATYPE_INT;
     871           0 :                     if (argtypes[fmtpos] &&
     872           0 :                         argtypes[fmtpos] != atype)
     873           0 :                         return false;
     874           0 :                     argtypes[fmtpos] = atype;
     875           0 :                     last_dollar = Max(last_dollar, fmtpos);
     876             :                 }
     877             :                 else
     878           0 :                     return false;   /* non-dollar conversion spec */
     879           0 :                 break;
     880             :             case 'c':
     881           0 :                 if (fmtpos)
     882             :                 {
     883           0 :                     if (argtypes[fmtpos] &&
     884           0 :                         argtypes[fmtpos] != ATYPE_INT)
     885           0 :                         return false;
     886           0 :                     argtypes[fmtpos] = ATYPE_INT;
     887           0 :                     last_dollar = Max(last_dollar, fmtpos);
     888             :                 }
     889             :                 else
     890           0 :                     return false;   /* non-dollar conversion spec */
     891           0 :                 break;
     892             :             case 's':
     893             :             case 'p':
     894           0 :                 if (fmtpos)
     895             :                 {
     896           0 :                     if (argtypes[fmtpos] &&
     897           0 :                         argtypes[fmtpos] != ATYPE_CHARPTR)
     898           0 :                         return false;
     899           0 :                     argtypes[fmtpos] = ATYPE_CHARPTR;
     900           0 :                     last_dollar = Max(last_dollar, fmtpos);
     901             :                 }
     902             :                 else
     903           0 :                     return false;   /* non-dollar conversion spec */
     904           0 :                 break;
     905             :             case 'e':
     906             :             case 'E':
     907             :             case 'f':
     908             :             case 'g':
     909             :             case 'G':
     910           0 :                 if (fmtpos)
     911             :                 {
     912           0 :                     if (argtypes[fmtpos] &&
     913           0 :                         argtypes[fmtpos] != ATYPE_DOUBLE)
     914           0 :                         return false;
     915           0 :                     argtypes[fmtpos] = ATYPE_DOUBLE;
     916           0 :                     last_dollar = Max(last_dollar, fmtpos);
     917             :                 }
     918             :                 else
     919           0 :                     return false;   /* non-dollar conversion spec */
     920           0 :                 break;
     921             :             case 'm':
     922             :             case '%':
     923           0 :                 break;
     924             :             default:
     925           0 :                 return false;   /* bogus format string */
     926             :         }
     927             : 
     928             :         /*
     929             :          * If we finish the spec with afterstar still set, there's a
     930             :          * non-dollar star in there.
     931             :          */
     932           0 :         if (afterstar)
     933           0 :             return false;       /* non-dollar conversion spec */
     934             :     }
     935             : 
     936             :     /*
     937             :      * Format appears valid so far, so collect the arguments in physical
     938             :      * order.  (Since we rejected any non-dollar specs that would have
     939             :      * collected arguments, we know that dopr() hasn't collected any yet.)
     940             :      */
     941           0 :     for (i = 1; i <= last_dollar; i++)
     942             :     {
     943           0 :         switch (argtypes[i])
     944             :         {
     945             :             case ATYPE_NONE:
     946           0 :                 return false;
     947             :             case ATYPE_INT:
     948           0 :                 argvalues[i].i = va_arg(args, int);
     949           0 :                 break;
     950             :             case ATYPE_LONG:
     951           0 :                 argvalues[i].l = va_arg(args, long);
     952           0 :                 break;
     953             :             case ATYPE_LONGLONG:
     954           0 :                 argvalues[i].ll = va_arg(args, long long);
     955           0 :                 break;
     956             :             case ATYPE_DOUBLE:
     957           0 :                 argvalues[i].d = va_arg(args, double);
     958           0 :                 break;
     959             :             case ATYPE_CHARPTR:
     960           0 :                 argvalues[i].cptr = va_arg(args, char *);
     961           0 :                 break;
     962             :         }
     963             :     }
     964             : 
     965           0 :     return true;
     966             : }
     967             : 
     968             : static void
     969      695296 : fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
     970             :        int pointflag, PrintfTarget *target)
     971             : {
     972             :     int         padlen,
     973             :                 vallen;         /* amount to pad */
     974             : 
     975             :     /*
     976             :      * If a maxwidth (precision) is specified, we must not fetch more bytes
     977             :      * than that.
     978             :      */
     979      695296 :     if (pointflag)
     980       11504 :         vallen = strnlen(value, maxwidth);
     981             :     else
     982      683792 :         vallen = strlen(value);
     983             : 
     984      695296 :     padlen = compute_padlen(minlen, vallen, leftjust);
     985             : 
     986      695296 :     if (padlen > 0)
     987             :     {
     988      306668 :         dopr_outchmulti(' ', padlen, target);
     989      306668 :         padlen = 0;
     990             :     }
     991             : 
     992      695296 :     dostr(value, vallen, target);
     993             : 
     994      695296 :     trailing_pad(padlen, target);
     995      695296 : }
     996             : 
     997             : static void
     998          46 : fmtptr(void *value, PrintfTarget *target)
     999             : {
    1000             :     int         vallen;
    1001             :     char        convert[64];
    1002             : 
    1003             :     /* we rely on regular C library's sprintf to do the basic conversion */
    1004          46 :     vallen = sprintf(convert, "%p", value);
    1005          46 :     if (vallen < 0)
    1006           0 :         target->failed = true;
    1007             :     else
    1008          46 :         dostr(convert, vallen, target);
    1009          46 : }
    1010             : 
    1011             : static void
    1012   205514552 : fmtint(long long value, char type, int forcesign, int leftjust,
    1013             :        int minlen, int zpad, int precision, int pointflag,
    1014             :        PrintfTarget *target)
    1015             : {
    1016             :     unsigned long long base;
    1017             :     unsigned long long uvalue;
    1018             :     int         dosign;
    1019   205514552 :     const char *cvt = "0123456789abcdef";
    1020   205514552 :     int         signvalue = 0;
    1021             :     char        convert[64];
    1022   205514552 :     int         vallen = 0;
    1023             :     int         padlen;         /* amount to pad */
    1024             :     int         zeropad;        /* extra leading zeroes */
    1025             : 
    1026   205514552 :     switch (type)
    1027             :     {
    1028             :         case 'd':
    1029             :         case 'i':
    1030   120201338 :             base = 10;
    1031   120201338 :             dosign = 1;
    1032   120201338 :             break;
    1033             :         case 'o':
    1034         304 :             base = 8;
    1035         304 :             dosign = 0;
    1036         304 :             break;
    1037             :         case 'u':
    1038    85182322 :             base = 10;
    1039    85182322 :             dosign = 0;
    1040    85182322 :             break;
    1041             :         case 'x':
    1042       15278 :             base = 16;
    1043       15278 :             dosign = 0;
    1044       15278 :             break;
    1045             :         case 'X':
    1046      115310 :             cvt = "0123456789ABCDEF";
    1047      115310 :             base = 16;
    1048      115310 :             dosign = 0;
    1049      115310 :             break;
    1050             :         default:
    1051           0 :             return;             /* keep compiler quiet */
    1052             :     }
    1053             : 
    1054             :     /* disable MSVC warning about applying unary minus to an unsigned value */
    1055             : #ifdef _MSC_VER
    1056             : #pragma warning(push)
    1057             : #pragma warning(disable: 4146)
    1058             : #endif
    1059             :     /* Handle +/- */
    1060   205514552 :     if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
    1061    20528178 :         uvalue = -(unsigned long long) value;
    1062             :     else
    1063   184986374 :         uvalue = (unsigned long long) value;
    1064             : #ifdef _MSC_VER
    1065             : #pragma warning(pop)
    1066             : #endif
    1067             : 
    1068             :     /*
    1069             :      * SUS: the result of converting 0 with an explicit precision of 0 is no
    1070             :      * characters
    1071             :      */
    1072   205514552 :     if (value == 0 && pointflag && precision == 0)
    1073           0 :         vallen = 0;
    1074             :     else
    1075             :     {
    1076             :         /* make integer string */
    1077             :         do
    1078             :         {
    1079   360524128 :             convert[sizeof(convert) - (++vallen)] = cvt[uvalue % base];
    1080   360524128 :             uvalue = uvalue / base;
    1081   360524128 :         } while (uvalue);
    1082             :     }
    1083             : 
    1084   205514552 :     zeropad = Max(0, precision - vallen);
    1085             : 
    1086   205514552 :     padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
    1087             : 
    1088   205514560 :     leading_pad(zpad, signvalue, &padlen, target);
    1089             : 
    1090   205514550 :     if (zeropad > 0)
    1091           0 :         dopr_outchmulti('0', zeropad, target);
    1092             : 
    1093   205514550 :     dostr(convert + sizeof(convert) - vallen, vallen, target);
    1094             : 
    1095   205514558 :     trailing_pad(padlen, target);
    1096             : }
    1097             : 
    1098             : static void
    1099       24396 : fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
    1100             : {
    1101             :     int         padlen;         /* amount to pad */
    1102             : 
    1103       24396 :     padlen = compute_padlen(minlen, 1, leftjust);
    1104             : 
    1105       24396 :     if (padlen > 0)
    1106             :     {
    1107           0 :         dopr_outchmulti(' ', padlen, target);
    1108           0 :         padlen = 0;
    1109             :     }
    1110             : 
    1111       24396 :     dopr_outch(value, target);
    1112             : 
    1113       24396 :     trailing_pad(padlen, target);
    1114       24396 : }
    1115             : 
    1116             : static void
    1117     2148040 : fmtfloat(double value, char type, int forcesign, int leftjust,
    1118             :          int minlen, int zpad, int precision, int pointflag,
    1119             :          PrintfTarget *target)
    1120             : {
    1121     2148040 :     int         signvalue = 0;
    1122             :     int         prec;
    1123             :     int         vallen;
    1124             :     char        fmt[8];
    1125             :     char        convert[1024];
    1126     2148040 :     int         zeropadlen = 0; /* amount to pad with zeroes */
    1127             :     int         padlen;         /* amount to pad with spaces */
    1128             : 
    1129             :     /*
    1130             :      * We rely on the regular C library's sprintf to do the basic conversion,
    1131             :      * then handle padding considerations here.
    1132             :      *
    1133             :      * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
    1134             :      * too wildly more than that with other hardware.  In "f" format, sprintf
    1135             :      * could therefore generate at most 308 characters to the left of the
    1136             :      * decimal point; while we need to allow the precision to get as high as
    1137             :      * 308+17 to ensure that we don't truncate significant digits from very
    1138             :      * small values.  To handle both these extremes, we use a buffer of 1024
    1139             :      * bytes and limit requested precision to 350 digits; this should prevent
    1140             :      * buffer overrun even with non-IEEE math.  If the original precision
    1141             :      * request was more than 350, separately pad with zeroes.
    1142             :      *
    1143             :      * We handle infinities and NaNs specially to ensure platform-independent
    1144             :      * output.
    1145             :      */
    1146     2148040 :     if (precision < 0)           /* cover possible overflow of "accum" */
    1147           0 :         precision = 0;
    1148     2148040 :     prec = Min(precision, 350);
    1149             : 
    1150     2148040 :     if (isnan(value))
    1151             :     {
    1152           0 :         strcpy(convert, "NaN");
    1153           0 :         vallen = 3;
    1154             :         /* no zero padding, regardless of precision spec */
    1155             :     }
    1156             :     else
    1157             :     {
    1158             :         /*
    1159             :          * Handle sign (NaNs have no sign, so we don't do this in the case
    1160             :          * above).  "value < 0.0" will not be true for IEEE minus zero, so we
    1161             :          * detect that by looking for the case where value equals 0.0
    1162             :          * according to == but not according to memcmp.
    1163             :          */
    1164             :         static const double dzero = 0.0;
    1165             : 
    1166     4296080 :         if (adjust_sign((value < 0.0 ||
    1167     2630296 :                          (value == 0.0 &&
    1168      482674 :                           memcmp(&value, &dzero, sizeof(double)) != 0)),
    1169             :                         forcesign, &signvalue))
    1170         418 :             value = -value;
    1171             : 
    1172     2148040 :         if (isinf(value))
    1173             :         {
    1174           0 :             strcpy(convert, "Infinity");
    1175           0 :             vallen = 8;
    1176             :             /* no zero padding, regardless of precision spec */
    1177             :         }
    1178     2148040 :         else if (pointflag)
    1179             :         {
    1180     2144406 :             zeropadlen = precision - prec;
    1181     2144406 :             fmt[0] = '%';
    1182     2144406 :             fmt[1] = '.';
    1183     2144406 :             fmt[2] = '*';
    1184     2144406 :             fmt[3] = type;
    1185     2144406 :             fmt[4] = '\0';
    1186     2144406 :             vallen = sprintf(convert, fmt, prec, value);
    1187             :         }
    1188             :         else
    1189             :         {
    1190        3634 :             fmt[0] = '%';
    1191        3634 :             fmt[1] = type;
    1192        3634 :             fmt[2] = '\0';
    1193        3634 :             vallen = sprintf(convert, fmt, value);
    1194             :         }
    1195     2148040 :         if (vallen < 0)
    1196           0 :             goto fail;
    1197             : 
    1198             :         /*
    1199             :          * Windows, alone among our supported platforms, likes to emit
    1200             :          * three-digit exponent fields even when two digits would do.  Hack
    1201             :          * such results to look like the way everyone else does it.
    1202             :          */
    1203             : #ifdef WIN32
    1204             :         if (vallen >= 6 &&
    1205             :             convert[vallen - 5] == 'e' &&
    1206             :             convert[vallen - 3] == '0')
    1207             :         {
    1208             :             convert[vallen - 3] = convert[vallen - 2];
    1209             :             convert[vallen - 2] = convert[vallen - 1];
    1210             :             vallen--;
    1211             :         }
    1212             : #endif
    1213             :     }
    1214             : 
    1215     2148040 :     padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
    1216             : 
    1217     2148040 :     leading_pad(zpad, signvalue, &padlen, target);
    1218             : 
    1219     2148040 :     if (zeropadlen > 0)
    1220             :     {
    1221             :         /* If 'e' or 'E' format, inject zeroes before the exponent */
    1222           0 :         char       *epos = strrchr(convert, 'e');
    1223             : 
    1224           0 :         if (!epos)
    1225           0 :             epos = strrchr(convert, 'E');
    1226           0 :         if (epos)
    1227             :         {
    1228             :             /* pad before exponent */
    1229           0 :             dostr(convert, epos - convert, target);
    1230           0 :             if (zeropadlen > 0)
    1231           0 :                 dopr_outchmulti('0', zeropadlen, target);
    1232           0 :             dostr(epos, vallen - (epos - convert), target);
    1233             :         }
    1234             :         else
    1235             :         {
    1236             :             /* no exponent, pad after the digits */
    1237           0 :             dostr(convert, vallen, target);
    1238           0 :             if (zeropadlen > 0)
    1239           0 :                 dopr_outchmulti('0', zeropadlen, target);
    1240             :         }
    1241             :     }
    1242             :     else
    1243             :     {
    1244             :         /* no zero padding, just emit the number as-is */
    1245     2148040 :         dostr(convert, vallen, target);
    1246             :     }
    1247             : 
    1248     2148040 :     trailing_pad(padlen, target);
    1249     2148040 :     return;
    1250             : 
    1251             : fail:
    1252           0 :     target->failed = true;
    1253             : }
    1254             : 
    1255             : /*
    1256             :  * Nonstandard entry point to print a double value efficiently.
    1257             :  *
    1258             :  * This is approximately equivalent to strfromd(), but has an API more
    1259             :  * adapted to what float8out() wants.  The behavior is like snprintf()
    1260             :  * with a format of "%.ng", where n is the specified precision.
    1261             :  * However, the target buffer must be nonempty (i.e. count > 0), and
    1262             :  * the precision is silently bounded to a sane range.
    1263             :  */
    1264             : int
    1265      142322 : pg_strfromd(char *str, size_t count, int precision, double value)
    1266             : {
    1267             :     PrintfTarget target;
    1268      142322 :     int         signvalue = 0;
    1269             :     int         vallen;
    1270             :     char        fmt[8];
    1271             :     char        convert[64];
    1272             : 
    1273             :     /* Set up the target like pg_snprintf, but require nonempty buffer */
    1274             :     Assert(count > 0);
    1275      142322 :     target.bufstart = target.bufptr = str;
    1276      142322 :     target.bufend = str + count - 1;
    1277      142322 :     target.stream = NULL;
    1278      142322 :     target.nchars = 0;
    1279      142322 :     target.failed = false;
    1280             : 
    1281             :     /*
    1282             :      * We bound precision to a reasonable range; the combination of this and
    1283             :      * the knowledge that we're using "g" format without padding allows the
    1284             :      * convert[] buffer to be reasonably small.
    1285             :      */
    1286      142322 :     if (precision < 1)
    1287           0 :         precision = 1;
    1288      142322 :     else if (precision > 32)
    1289           0 :         precision = 32;
    1290             : 
    1291             :     /*
    1292             :      * The rest is just an inlined version of the fmtfloat() logic above,
    1293             :      * simplified using the knowledge that no padding is wanted.
    1294             :      */
    1295      142322 :     if (isnan(value))
    1296             :     {
    1297        7372 :         strcpy(convert, "NaN");
    1298        7372 :         vallen = 3;
    1299             :     }
    1300             :     else
    1301             :     {
    1302             :         static const double dzero = 0.0;
    1303             : 
    1304      250076 :         if (value < 0.0 ||
    1305      131522 :             (value == 0.0 &&
    1306       16396 :              memcmp(&value, &dzero, sizeof(double)) != 0))
    1307             :         {
    1308       19864 :             signvalue = '-';
    1309       19864 :             value = -value;
    1310             :         }
    1311             : 
    1312      134950 :         if (isinf(value))
    1313             :         {
    1314        2236 :             strcpy(convert, "Infinity");
    1315        2236 :             vallen = 8;
    1316             :         }
    1317             :         else
    1318             :         {
    1319      132714 :             fmt[0] = '%';
    1320      132714 :             fmt[1] = '.';
    1321      132714 :             fmt[2] = '*';
    1322      132714 :             fmt[3] = 'g';
    1323      132714 :             fmt[4] = '\0';
    1324      132714 :             vallen = sprintf(convert, fmt, precision, value);
    1325      132714 :             if (vallen < 0)
    1326             :             {
    1327           0 :                 target.failed = true;
    1328           0 :                 goto fail;
    1329             :             }
    1330             : 
    1331             : #ifdef WIN32
    1332             :             if (vallen >= 6 &&
    1333             :                 convert[vallen - 5] == 'e' &&
    1334             :                 convert[vallen - 3] == '0')
    1335             :             {
    1336             :                 convert[vallen - 3] = convert[vallen - 2];
    1337             :                 convert[vallen - 2] = convert[vallen - 1];
    1338             :                 vallen--;
    1339             :             }
    1340             : #endif
    1341             :         }
    1342             :     }
    1343             : 
    1344      142322 :     if (signvalue)
    1345       19864 :         dopr_outch(signvalue, &target);
    1346             : 
    1347      142322 :     dostr(convert, vallen, &target);
    1348             : 
    1349             : fail:
    1350      142322 :     *(target.bufptr) = '\0';
    1351      284644 :     return target.failed ? -1 : (target.bufptr - target.bufstart
    1352      142322 :                                  + target.nchars);
    1353             : }
    1354             : 
    1355             : 
    1356             : static void
    1357   472325894 : dostr(const char *str, int slen, PrintfTarget *target)
    1358             : {
    1359             :     /* fast path for common case of slen == 1 */
    1360   472325894 :     if (slen == 1)
    1361             :     {
    1362   180035180 :         dopr_outch(*str, target);
    1363   180035182 :         return;
    1364             :     }
    1365             : 
    1366   875914510 :     while (slen > 0)
    1367             :     {
    1368             :         int         avail;
    1369             : 
    1370   291545410 :         if (target->bufend != NULL)
    1371   290478562 :             avail = target->bufend - target->bufptr;
    1372             :         else
    1373     1066848 :             avail = slen;
    1374   291545410 :         if (avail <= 0)
    1375             :         {
    1376             :             /* buffer full, can we dump to stream? */
    1377      212426 :             if (target->stream == NULL)
    1378             :             {
    1379      212332 :                 target->nchars += slen; /* no, lose the data */
    1380      212332 :                 return;
    1381             :             }
    1382          94 :             flushbuffer(target);
    1383          98 :             continue;
    1384             :         }
    1385   291332984 :         avail = Min(avail, slen);
    1386   291332984 :         memmove(target->bufptr, str, avail);
    1387   291332984 :         target->bufptr += avail;
    1388   291332984 :         str += avail;
    1389   291332984 :         slen -= avail;
    1390             :     }
    1391             : }
    1392             : 
    1393             : static void
    1394   200940044 : dopr_outch(int c, PrintfTarget *target)
    1395             : {
    1396   200940044 :     if (target->bufend != NULL && target->bufptr >= target->bufend)
    1397             :     {
    1398             :         /* buffer full, can we dump to stream? */
    1399       45146 :         if (target->stream == NULL)
    1400             :         {
    1401       45146 :             target->nchars++;    /* no, lose the data */
    1402       45146 :             return;
    1403             :         }
    1404           0 :         flushbuffer(target);
    1405             :     }
    1406   200894900 :     *(target->bufptr++) = c;
    1407             : }
    1408             : 
    1409             : static void
    1410      778910 : dopr_outchmulti(int c, int slen, PrintfTarget *target)
    1411             : {
    1412             :     /* fast path for common case of slen == 1 */
    1413      778910 :     if (slen == 1)
    1414             :     {
    1415      331456 :         dopr_outch(c, target);
    1416      331456 :         return;
    1417             :     }
    1418             : 
    1419     1342794 :     while (slen > 0)
    1420             :     {
    1421             :         int         avail;
    1422             : 
    1423      447886 :         if (target->bufend != NULL)
    1424      442584 :             avail = target->bufend - target->bufptr;
    1425             :         else
    1426        5302 :             avail = slen;
    1427      447886 :         if (avail <= 0)
    1428             :         {
    1429             :             /* buffer full, can we dump to stream? */
    1430         218 :             if (target->stream == NULL)
    1431             :             {
    1432           0 :                 target->nchars += slen; /* no, lose the data */
    1433           0 :                 return;
    1434             :             }
    1435         218 :             flushbuffer(target);
    1436         218 :             continue;
    1437             :         }
    1438      447668 :         avail = Min(avail, slen);
    1439      447668 :         memset(target->bufptr, c, avail);
    1440      447668 :         target->bufptr += avail;
    1441      447668 :         slen -= avail;
    1442             :     }
    1443             : }
    1444             : 
    1445             : 
    1446             : static int
    1447   122349374 : adjust_sign(int is_negative, int forcesign, int *signvalue)
    1448             : {
    1449   122349374 :     if (is_negative)
    1450             :     {
    1451    20528596 :         *signvalue = '-';
    1452    20528596 :         return true;
    1453             :     }
    1454   101820778 :     else if (forcesign)
    1455          40 :         *signvalue = '+';
    1456   101820778 :     return false;
    1457             : }
    1458             : 
    1459             : 
    1460             : static int
    1461   208382292 : compute_padlen(int minlen, int vallen, int leftjust)
    1462             : {
    1463             :     int         padlen;
    1464             : 
    1465   208382292 :     padlen = minlen - vallen;
    1466   208382292 :     if (padlen < 0)
    1467   206667486 :         padlen = 0;
    1468   208382292 :     if (leftjust)
    1469      250884 :         padlen = -padlen;
    1470   208382292 :     return padlen;
    1471             : }
    1472             : 
    1473             : 
    1474             : static void
    1475   207662592 : leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
    1476             : {
    1477             :     int         maxpad;
    1478             : 
    1479   207662592 :     if (*padlen > 0 && zpad)
    1480             :     {
    1481      363144 :         if (signvalue)
    1482             :         {
    1483          52 :             dopr_outch(signvalue, target);
    1484          52 :             --(*padlen);
    1485          52 :             signvalue = 0;
    1486             :         }
    1487      363144 :         if (*padlen > 0)
    1488             :         {
    1489      363140 :             dopr_outchmulti(zpad, *padlen, target);
    1490      363140 :             *padlen = 0;
    1491             :         }
    1492             :     }
    1493   207662592 :     maxpad = (signvalue != 0);
    1494   207662592 :     if (*padlen > maxpad)
    1495             :     {
    1496        5908 :         dopr_outchmulti(' ', *padlen - maxpad, target);
    1497        5908 :         *padlen = maxpad;
    1498             :     }
    1499   207662592 :     if (signvalue)
    1500             :     {
    1501    20528584 :         dopr_outch(signvalue, target);
    1502    20528584 :         if (*padlen > 0)
    1503           0 :             --(*padlen);
    1504    20528584 :         else if (*padlen < 0)
    1505           0 :             ++(*padlen);
    1506             :     }
    1507   207662592 : }
    1508             : 
    1509             : 
    1510             : static void
    1511   208382286 : trailing_pad(int padlen, PrintfTarget *target)
    1512             : {
    1513   208382286 :     if (padlen < 0)
    1514      103194 :         dopr_outchmulti(' ', -padlen, target);
    1515   208382286 : }

Generated by: LCOV version 1.13