|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * psprintf.c |
| 4 | + *sprintf into an allocated-on-demand buffer |
| 5 | + * |
| 6 | + * |
| 7 | + * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group |
| 8 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 9 | + * |
| 10 | + * |
| 11 | + * IDENTIFICATION |
| 12 | + * src/common/psprintf.c |
| 13 | + * |
| 14 | + *------------------------------------------------------------------------- |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndefFRONTEND |
| 18 | +#include"postgres.h" |
| 19 | +#else |
| 20 | +#include"postgres_fe.h" |
| 21 | +#endif |
| 22 | + |
| 23 | +#include"utils/memutils.h" |
| 24 | + |
| 25 | + |
| 26 | +staticsize_tpvsnprintf(char*buf,size_tlen,constchar*fmt,va_listargs) |
| 27 | +__attribute__((format(PG_PRINTF_ATTRIBUTE,3,0))); |
| 28 | + |
| 29 | + |
| 30 | +/* |
| 31 | + * psprintf |
| 32 | + * |
| 33 | + * Format text data under the control of fmt (an sprintf-style format string) |
| 34 | + * and return it in an allocated-on-demand buffer.The buffer is allocated |
| 35 | + * with palloc in the backend, or malloc in frontend builds. Caller is |
| 36 | + * responsible to free the buffer when no longer needed, if appropriate. |
| 37 | + * |
| 38 | + * Errors are not returned to the caller, but are reported via elog(ERROR) |
| 39 | + * in the backend, or printf-to-stderr-and-exit() in frontend builds. |
| 40 | + * One should therefore think twice about using this in libpq. |
| 41 | + */ |
| 42 | +char* |
| 43 | +psprintf(constchar*fmt,...) |
| 44 | +{ |
| 45 | +size_tlen=128;/* initial assumption about buffer size */ |
| 46 | + |
| 47 | +for (;;) |
| 48 | +{ |
| 49 | +char*result; |
| 50 | +va_listargs; |
| 51 | + |
| 52 | +/* |
| 53 | + * Allocate result buffer.Note that in frontend this maps to malloc |
| 54 | + * with exit-on-error. |
| 55 | + */ |
| 56 | +result= (char*)palloc(len); |
| 57 | + |
| 58 | +/* Try to format the data. */ |
| 59 | +va_start(args,fmt); |
| 60 | +len=pvsnprintf(result,len,fmt,args); |
| 61 | +va_end(args); |
| 62 | + |
| 63 | +if (len==0) |
| 64 | +returnresult;/* success */ |
| 65 | + |
| 66 | +/* Release buffer and loop around to try again with larger len. */ |
| 67 | +pfree(result); |
| 68 | +} |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | + * pvsnprintf |
| 73 | + * |
| 74 | + * Attempt to format text data under the control of fmt (an sprintf-style |
| 75 | + * format string) and insert it into buf (which has length len). |
| 76 | + * |
| 77 | + * If successful, return zero.If there's not enough space in buf, return |
| 78 | + * an estimate of the buffer size needed to succeed (this *must* be more |
| 79 | + * than "len", else psprintf might loop infinitely). |
| 80 | + * Other error cases do not return. |
| 81 | + * |
| 82 | + * XXX This API is ugly, but there seems no alternative given the C spec's |
| 83 | + * restrictions on what can portably be done with va_list arguments: you have |
| 84 | + * to redo va_start before you can rescan the argument list, and we can't do |
| 85 | + * that from here. |
| 86 | + */ |
| 87 | +staticsize_t |
| 88 | +pvsnprintf(char*buf,size_tlen,constchar*fmt,va_listargs) |
| 89 | +{ |
| 90 | +intnprinted; |
| 91 | + |
| 92 | +Assert(len>0); |
| 93 | + |
| 94 | +errno=0; |
| 95 | + |
| 96 | +/* |
| 97 | + * Assert check here is to catch buggy vsnprintf that overruns the |
| 98 | + * specified buffer length. Solaris 7 in 64-bit mode is an example of a |
| 99 | + * platform with such a bug. |
| 100 | + */ |
| 101 | +#ifdefUSE_ASSERT_CHECKING |
| 102 | +buf[len-1]='\0'; |
| 103 | +#endif |
| 104 | + |
| 105 | +nprinted=vsnprintf(buf,len,fmt,args); |
| 106 | + |
| 107 | +Assert(buf[len-1]=='\0'); |
| 108 | + |
| 109 | +/* |
| 110 | + * If vsnprintf reports an error other than ENOMEM, fail. The possible |
| 111 | + * causes of this are not user-facing errors, so elog should be enough. |
| 112 | + */ |
| 113 | +if (nprinted<0&&errno!=0&&errno!=ENOMEM) |
| 114 | +{ |
| 115 | +#ifndefFRONTEND |
| 116 | +elog(ERROR,"vsnprintf failed: %m"); |
| 117 | +#else |
| 118 | +fprintf(stderr,"vsnprintf failed: %s\n",strerror(errno)); |
| 119 | +exit(EXIT_FAILURE); |
| 120 | +#endif |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * Note: some versions of vsnprintf return the number of chars actually |
| 125 | + * stored, not the total space needed as C99 specifies. And at least one |
| 126 | + * returns -1 on failure. Be conservative about believing whether the |
| 127 | + * print worked. |
| 128 | + */ |
| 129 | +if (nprinted >=0&& (size_t)nprinted<len-1) |
| 130 | +{ |
| 131 | +/* Success. Note nprinted does not include trailing null. */ |
| 132 | +return0; |
| 133 | +} |
| 134 | + |
| 135 | +if (nprinted >=0&& (size_t)nprinted>len) |
| 136 | +{ |
| 137 | +/* |
| 138 | + * This appears to be a C99-compliant vsnprintf, so believe its |
| 139 | + * estimate of the required space.(If it's wrong, this code will |
| 140 | + * still work, but may loop multiple times.) Note that the space |
| 141 | + * needed should be only nprinted+1 bytes, but we'd better allocate |
| 142 | + * one more than that so that the test above will succeed next time. |
| 143 | + * |
| 144 | + * In the corner case where the required space just barely overflows, |
| 145 | + * fall through so that we'll error out below (possibly after looping). |
| 146 | + */ |
| 147 | +if ((size_t)nprinted <=MaxAllocSize-2) |
| 148 | +returnnprinted+2; |
| 149 | +} |
| 150 | + |
| 151 | +/* |
| 152 | + * Buffer overrun, and we don't know how much space is needed. Estimate |
| 153 | + * twice the previous buffer size.If this would overflow, choke.We use |
| 154 | + * a palloc-oriented overflow limit even when in frontend. |
| 155 | + */ |
| 156 | +if (len>MaxAllocSize /2) |
| 157 | +{ |
| 158 | +#ifndefFRONTEND |
| 159 | +ereport(ERROR, |
| 160 | +(errcode(ERRCODE_OUT_OF_MEMORY), |
| 161 | +errmsg("out of memory"))); |
| 162 | +#else |
| 163 | +fprintf(stderr,_("out of memory\n")); |
| 164 | +exit(EXIT_FAILURE); |
| 165 | +#endif |
| 166 | +} |
| 167 | + |
| 168 | +returnlen*2; |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +/* |
| 173 | + * XXX this is going away shortly. |
| 174 | + */ |
| 175 | +#ifdefFRONTEND |
| 176 | +int |
| 177 | +pg_asprintf(char**ret,constchar*fmt, ...) |
| 178 | +{ |
| 179 | +size_tlen=128;/* initial assumption about buffer size */ |
| 180 | + |
| 181 | +for (;;) |
| 182 | +{ |
| 183 | +char*result; |
| 184 | +va_listargs; |
| 185 | + |
| 186 | +/* |
| 187 | + * Allocate result buffer.Note that in frontend this maps to malloc |
| 188 | + * with exit-on-error. |
| 189 | + */ |
| 190 | +result= (char*)palloc(len); |
| 191 | + |
| 192 | +/* Try to format the data. */ |
| 193 | +va_start(args,fmt); |
| 194 | +len=pvsnprintf(result,len,fmt,args); |
| 195 | +va_end(args); |
| 196 | + |
| 197 | +if (len==0) |
| 198 | +{ |
| 199 | +*ret=result; |
| 200 | +return0; |
| 201 | +} |
| 202 | + |
| 203 | +/* Release buffer and loop around to try again with larger len. */ |
| 204 | +pfree(result); |
| 205 | +} |
| 206 | +} |
| 207 | +#endif |