mirror of
https://github.com/postgres/postgres.git
synced 2025-08-27 07:42:10 +03:00
Adjust the behavior of the PQExpBuffer code to make it have well-defined
results (ie, an empty "broken" buffer) if memory overrun occurs anywhere along the way to filling the buffer. The previous coding would just silently discard portions of the intended buffer contents, as exhibited in trouble report from Sam Mason. Also, tweak psql's main loop to correctly detect and report such overruns. There's probably much more that should be done in this line, but this is a start.
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.h,v 1.19 2008/01/01 19:46:00 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.h,v 1.20 2008/11/26 00:26:23 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -35,6 +35,10 @@
|
||||
* string size (including the terminating '\0' char) that we can
|
||||
* currently store in 'data' without having to reallocate
|
||||
* more space. We must always have maxlen > len.
|
||||
*
|
||||
* An exception occurs if we failed to allocate enough memory for the string
|
||||
* buffer. In that case data points to a statically allocated empty string,
|
||||
* and len = maxlen = 0.
|
||||
*-------------------------
|
||||
*/
|
||||
typedef struct PQExpBufferData
|
||||
@@ -46,6 +50,15 @@ typedef struct PQExpBufferData
|
||||
|
||||
typedef PQExpBufferData *PQExpBuffer;
|
||||
|
||||
/*------------------------
|
||||
* Test for a broken (out of memory) PQExpBuffer.
|
||||
* When a buffer is "broken", all operations except resetting or deleting it
|
||||
* are no-ops.
|
||||
*------------------------
|
||||
*/
|
||||
#define PQExpBufferBroken(str) \
|
||||
(!(str) || (str)->maxlen == 0)
|
||||
|
||||
/*------------------------
|
||||
* Initial size of the data buffer in a PQExpBuffer.
|
||||
* NB: this must be large enough to hold error messages that might
|
||||
@@ -103,6 +116,8 @@ extern void termPQExpBuffer(PQExpBuffer str);
|
||||
/*------------------------
|
||||
* resetPQExpBuffer
|
||||
* Reset a PQExpBuffer to empty
|
||||
*
|
||||
* Note: if possible, a "broken" PQExpBuffer is returned to normal.
|
||||
*/
|
||||
extern void resetPQExpBuffer(PQExpBuffer str);
|
||||
|
||||
@@ -111,7 +126,8 @@ extern void resetPQExpBuffer(PQExpBuffer str);
|
||||
* Make sure there is enough space for 'needed' more bytes in the buffer
|
||||
* ('needed' does not include the terminating null).
|
||||
*
|
||||
* Returns 1 if OK, 0 if failed to enlarge buffer.
|
||||
* Returns 1 if OK, 0 if failed to enlarge buffer. (In the latter case
|
||||
* the buffer is left in "broken" state.)
|
||||
*/
|
||||
extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed);
|
||||
|
||||
|
Reference in New Issue
Block a user