Skip to content

Commit 594bee4

Browse files
committed
Portability fix for old SunOS releases: realloc(NULL, ...)
doesn't work there.
1 parent 9fc69f5 commit 594bee4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/interfaces/libpq/fe-exec.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.69 1998/10/01 01:40:21 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.69.2.1 1998/11/29 01:54:34 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -214,17 +214,23 @@ addTuple(PGresult *res, PGresAttValue *tup)
214214
*
215215
* We can use realloc because shallow copying of the structure is
216216
* okay. Note that the first time through, res->tuples is NULL.
217-
* realloc is supposed to do the right thing in that case. Also,
218-
* on failure realloc is supposed to return NULL without damaging
217+
* While ANSI says that realloc() should act like malloc() in that
218+
* case, some old C libraries (like SunOS 4.1.x) coredump instead.
219+
* On failure realloc is supposed to return NULL without damaging
219220
* the existing allocation.
220221
* Note that the positions beyond res->ntups are garbage, not
221222
* necessarily NULL.
222223
*/
223224
int newSize = res->tupArrSize + TUPARR_GROW_BY;
224-
PGresAttValue ** newTuples = (PGresAttValue **)
225-
realloc(res->tuples, newSize * sizeof(PGresAttValue *));
225+
PGresAttValue ** newTuples;
226+
if (res->tuples == NULL)
227+
newTuples = (PGresAttValue **)
228+
malloc(newSize * sizeof(PGresAttValue *));
229+
else
230+
newTuples = (PGresAttValue **)
231+
realloc(res->tuples, newSize * sizeof(PGresAttValue *));
226232
if (! newTuples)
227-
return FALSE; /* realloc failed */
233+
return FALSE; /* malloc or realloc failed */
228234
res->tupArrSize = newSize;
229235
res->tuples = newTuples;
230236
}

0 commit comments

Comments
 (0)