Skip to content

Commit 130e372

Browse files
committed
Minor improvements to stringinfo package to make it more
robust, since it's about to get used much more heavily.
1 parent f4add18 commit 130e372

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

src/backend/commands/explain.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright (c) 1994-5, Regents of the University of California
66
*
7-
* $Id: explain.c,v 1.45 1999/08/16 23:47:23 tgl Exp $
7+
* $Id: explain.c,v 1.46 1999/08/31 01:28:28 tgl Exp $
88
*
99
*/
1010

@@ -31,6 +31,9 @@ static char *Explain_PlanToString(Plan *plan, ExplainState *es);
3131
static void printLongNotice(const char *header, const char *message);
3232
static void ExplainOneQuery(Query *query, bool verbose, CommandDest dest);
3333

34+
/* Convert a null string pointer into "<>" */
35+
#define stringStringInfo(s) (((s) == NULL) ? "<>" : (s))
36+
3437

3538
/*
3639
* ExplainQuery -

src/backend/lib/stringinfo.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $Id: stringinfo.c,v 1.20 1999/07/17 20:16:59 momjian Exp $
11+
* $Id: stringinfo.c,v 1.21 1999/08/31 01:28:25 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,7 +17,6 @@
1717
#include "postgres.h"
1818
#include "lib/stringinfo.h"
1919

20-
#ifdef NOT_USED
2120
/*
2221
* makeStringInfo
2322
*
@@ -36,7 +35,6 @@ makeStringInfo(void)
3635

3736
return res;
3837
}
39-
#endif
4038

4139
/*
4240
* initStringInfo
@@ -49,7 +47,7 @@ initStringInfo(StringInfo str)
4947
{
5048
int size = 256; /* initial default buffer size */
5149

52-
str->data = palloc(size);
50+
str->data = (char *) palloc(size);
5351
if (str->data == NULL)
5452
elog(ERROR,
5553
"initStringInfo: Out of memory (%d bytes requested)", size);
@@ -68,7 +66,6 @@ static void
6866
enlargeStringInfo(StringInfo str, int needed)
6967
{
7068
int newlen;
71-
char *newdata;
7269

7370
needed += str->len + 1; /* total space required now */
7471
if (needed <= str->maxlen)
@@ -84,15 +81,11 @@ enlargeStringInfo(StringInfo str, int needed)
8481
while (needed > newlen)
8582
newlen = 2 * newlen;
8683

87-
newdata = palloc(newlen);
88-
if (newdata == NULL)
84+
str->data = (char *) repalloc(str->data, newlen);
85+
if (str->data == NULL)
8986
elog(ERROR,
90-
"enlargeStringInfo: Out of memory (%d bytes requested)", newlen);
87+
"enlargeStringInfo: Out of memory (%d bytes requested)", newlen);
9188

92-
/* OK, transfer data into new buffer, and release old buffer */
93-
memcpy(newdata, str->data, str->len + 1);
94-
pfree(str->data);
95-
str->data = newdata;
9689
str->maxlen = newlen;
9790
}
9891

@@ -103,29 +96,41 @@ enlargeStringInfo(StringInfo str, int needed)
10396
* and append it to whatever is already in str. More space is allocated
10497
* to str if necessary. This is sort of like a combination of sprintf and
10598
* strcat.
106-
*
107-
* CAUTION: the current implementation has a 1K limit on the amount of text
108-
* generated in a single call (not on the total string length).
10999
*/
110100
void
111101
appendStringInfo(StringInfo str, const char *fmt,...)
112102
{
113103
va_list args;
114-
char buffer[1024];
115-
int buflen;
104+
int avail,
105+
nprinted;
116106

117107
Assert(str != NULL);
118108

119-
va_start(args, fmt);
120-
buflen = vsnprintf(buffer, sizeof(buffer), fmt, args);
121-
va_end(args);
122-
123-
/* Make more room if needed */
124-
enlargeStringInfo(str, buflen);
125-
126-
/* OK, append the data, including the trailing null */
127-
memcpy(str->data + str->len, buffer, buflen + 1);
128-
str->len += buflen;
109+
for (;;)
110+
{
111+
/*----------
112+
* Try to format the given string into the available space;
113+
* but if there's hardly any space, don't bother trying,
114+
* just fall through to enlarge the buffer first.
115+
*----------
116+
*/
117+
avail = str->maxlen - str->len - 1;
118+
if (avail > 16)
119+
{
120+
va_start(args, fmt);
121+
nprinted = vsnprintf(str->data + str->len, avail,
122+
fmt, args);
123+
va_end(args);
124+
if (nprinted < avail-1)
125+
{
126+
/* Success. Note nprinted does not include trailing null. */
127+
str->len += nprinted;
128+
break;
129+
}
130+
}
131+
/* Double the buffer size and try again. */
132+
enlargeStringInfo(str, str->maxlen);
133+
}
129134
}
130135

131136
/*------------------------

src/backend/nodes/outfuncs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: outfuncs.c,v 1.94 1999/08/21 03:48:58 tgl Exp $
8+
* $Id: outfuncs.c,v 1.95 1999/08/31 01:28:32 tgl Exp $
99
*
1010
* NOTES
1111
* Every (plan) node in POSTGRES has an associated "out" routine which
@@ -42,6 +42,10 @@
4242
static void _outDatum(StringInfo str, Datum value, Oid type);
4343
static void _outNode(StringInfo str, void *obj);
4444

45+
/* Convert a null string pointer into "<>" */
46+
#define stringStringInfo(s) (((s) == NULL) ? "<>" : (s))
47+
48+
4549
/*
4650
* _outIntList -
4751
* converts a List of integers

src/backend/port/snprintf.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ typedef unsigned long ulong_long;
7474
* causing nast effects.
7575
**************************************************************/
7676

77-
/*static char _id[] = "$Id: snprintf.c,v 1.25 1999/07/17 20:17:28 momjian Exp $";*/
77+
/*static char _id[] = "$Id: snprintf.c,v 1.26 1999/08/31 01:28:37 tgl Exp $";*/
7878
static char *end;
7979
static int SnprfOverflow;
8080

@@ -98,14 +98,14 @@ snprintf(char *str, size_t count, const char *fmt,...)
9898
int
9999
vsnprintf(char *str, size_t count, const char *fmt, va_list args)
100100
{
101-
str[0] = 0;
101+
str[0] = '\0';
102102
end = str + count - 1;
103103
SnprfOverflow = 0;
104104
dopr(str, fmt, args);
105105
if (count > 0)
106-
end[0] = 0;
106+
end[0] = '\0';
107107
if (SnprfOverflow)
108-
elog(NOTICE, "vsnprintf overflow, len = %d, str = %s",
108+
elog(DEBUG, "vsnprintf overflow, len = %d, str = %s",
109109
count, str);
110110
return strlen(str);
111111
}
@@ -152,6 +152,7 @@ dopr(char *buffer, const char *format, va_list args)
152152
{
153153
case 0:
154154
dostr("**end of format**", 0);
155+
*output = '\0';
155156
return;
156157
case '-':
157158
ljust = 1;
@@ -287,7 +288,7 @@ dopr(char *buffer, const char *format, va_list args)
287288
break;
288289
}
289290
}
290-
*output = 0;
291+
*output = '\0';
291292
}
292293

293294
static void

src/include/lib/stringinfo.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $Id: stringinfo.h,v 1.13 1999/05/26 12:56:27 momjian Exp $
12+
* $Id: stringinfo.h,v 1.14 1999/08/31 01:28:21 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -60,13 +60,11 @@ typedef StringInfoData *StringInfo;
6060
*-------------------------
6161
*/
6262

63-
#ifdef NOT_USED
6463
/*------------------------
6564
* makeStringInfo
6665
* Create an empty 'StringInfoData' & return a pointer to it.
6766
*/
6867
extern StringInfo makeStringInfo(void);
69-
#endif
7068

7169
/*------------------------
7270
* initStringInfo
@@ -81,8 +79,6 @@ extern void initStringInfo(StringInfo str);
8179
* and append it to whatever is already in str. More space is allocated
8280
* to str if necessary. This is sort of like a combination of sprintf and
8381
* strcat.
84-
* CAUTION: the current implementation has a 1K limit on the amount of text
85-
* generated in a single call (not on the total string length).
8682
*/
8783
extern void appendStringInfo(StringInfo str, const char *fmt,...);
8884

@@ -101,11 +97,4 @@ extern void appendStringInfoChar(StringInfo str, char ch);
10197
extern void appendBinaryStringInfo(StringInfo str,
10298
const char *data, int datalen);
10399

104-
/*------------------------
105-
* stringStringInfo
106-
* Return the string itself or "<>" if it is NULL.
107-
* This is just a convenience macro used by many callers of appendStringInfo.
108-
*/
109-
#define stringStringInfo(s) (((s) == NULL) ? "<>" : (s))
110-
111100
#endif /* STRINGINFO_H */

0 commit comments

Comments
 (0)