4
4
*
5
5
* StringInfo provides an indefinitely-extensible string data type.
6
6
* It can be used to buffer either ordinary C strings (null-terminated text)
7
- * or arbitrary binary data. All storage is allocated with palloc() and
8
- * friends.
7
+ * or arbitrary binary data. All storage is allocated with palloc().
9
8
*
10
9
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
11
10
* Portions Copyright (c) 1994, Regents of the University of California
@@ -37,29 +36,11 @@ makeStringInfo(void)
37
36
return res ;
38
37
}
39
38
40
- /*
41
- * makeLongStringInfo
42
- *
43
- * Same as makeStringInfo, for larger strings.
44
- */
45
- StringInfo
46
- makeLongStringInfo (void )
47
- {
48
- StringInfo res ;
49
-
50
- res = (StringInfo ) palloc (sizeof (StringInfoData ));
51
-
52
- initLongStringInfo (res );
53
-
54
- return res ;
55
- }
56
-
57
-
58
39
/*
59
40
* initStringInfo
60
41
*
61
42
* Initialize a StringInfoData struct (with previously undefined contents)
62
- * to describe an empty string; don't enable long strings yet .
43
+ * to describe an empty string.
63
44
*/
64
45
void
65
46
initStringInfo (StringInfo str )
@@ -68,22 +49,9 @@ initStringInfo(StringInfo str)
68
49
69
50
str -> data = (char * ) palloc (size );
70
51
str -> maxlen = size ;
71
- str -> long_ok = false;
72
52
resetStringInfo (str );
73
53
}
74
54
75
- /*
76
- * initLongStringInfo
77
- *
78
- * Same as initStringInfo, plus enable long strings.
79
- */
80
- void
81
- initLongStringInfo (StringInfo str )
82
- {
83
- initStringInfo (str );
84
- str -> long_ok = true;
85
- }
86
-
87
55
/*
88
56
* resetStringInfo
89
57
*
@@ -174,7 +142,7 @@ appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
174
142
/*
175
143
* Return pvsnprintf's estimate of the space needed. (Although this is
176
144
* given as a size_t, we know it will fit in int because it's not more
177
- * than either MaxAllocSize or half an int's width .)
145
+ * than MaxAllocSize.)
178
146
*/
179
147
return (int ) nprinted ;
180
148
}
@@ -276,25 +244,15 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
276
244
void
277
245
enlargeStringInfo (StringInfo str , int needed )
278
246
{
279
- Size newlen ;
280
- Size limit ;
281
-
282
- /*
283
- * Determine the upper size limit. Because of overflow concerns outside
284
- * of this module, we limit ourselves to 4-byte signed integer range,
285
- * even for "long_ok" strings.
286
- */
287
- limit = str -> long_ok ?
288
- (((Size ) 1 ) << (sizeof (int32 ) * 8 - 1 )) - 1 :
289
- MaxAllocSize ;
247
+ int newlen ;
290
248
291
249
/*
292
250
* Guard against out-of-range "needed" values. Without this, we can get
293
251
* an overflow or infinite loop in the following.
294
252
*/
295
253
if (needed < 0 ) /* should not happen */
296
254
elog (ERROR , "invalid string enlargement request size: %d" , needed );
297
- if (((Size ) needed ) >= (limit - (Size ) str -> len ))
255
+ if (((Size ) needed ) >= (MaxAllocSize - (Size ) str -> len ))
298
256
ereport (ERROR ,
299
257
(errcode (ERRCODE_PROGRAM_LIMIT_EXCEEDED ),
300
258
errmsg ("out of memory" ),
@@ -303,7 +261,7 @@ enlargeStringInfo(StringInfo str, int needed)
303
261
304
262
needed += str -> len + 1 ; /* total space required now */
305
263
306
- /* Because of the above test, we now have needed <= limit */
264
+ /* Because of the above test, we now have needed <= MaxAllocSize */
307
265
308
266
if (needed <= str -> maxlen )
309
267
return ; /* got enough space already */
@@ -313,20 +271,19 @@ enlargeStringInfo(StringInfo str, int needed)
313
271
* for efficiency, double the buffer size each time it overflows.
314
272
* Actually, we might need to more than double it if 'needed' is big...
315
273
*/
316
- newlen = 2 * ( Size ) str -> maxlen ;
317
- while (( Size ) needed > newlen )
274
+ newlen = 2 * str -> maxlen ;
275
+ while (needed > newlen )
318
276
newlen = 2 * newlen ;
319
277
320
278
/*
321
- * Clamp to the limit in case we went past it. (We used to depend on
322
- * limit <= INT32_MAX/2, to avoid overflow in the loop above; we no longer
323
- * depend on that, but if "needed" and str->maxlen ever become wider, we
324
- * will need similar caution here.) We will still have newlen >= needed.
279
+ * Clamp to MaxAllocSize in case we went past it. Note we are assuming
280
+ * here that MaxAllocSize <= INT_MAX/2, else the above loop could
281
+ * overflow. We will still have newlen >= needed.
325
282
*/
326
- if (newlen > limit )
327
- newlen = limit ;
283
+ if (newlen > ( int ) MaxAllocSize )
284
+ newlen = ( int ) MaxAllocSize ;
328
285
329
- str -> data = (char * ) repalloc_huge (str -> data , newlen );
286
+ str -> data = (char * ) repalloc (str -> data , newlen );
330
287
331
288
str -> maxlen = newlen ;
332
289
}
0 commit comments