Skip to content

Commit ffaaaf9

Browse files
committed
Fix potential access-off-the-end-of-memory in varbit_out(): it fetched the
byte after the last full byte of the bit array, regardless of whether that byte was part of the valid data or not. Found by buildfarm testing. Thanks to Stefan Kaltenbrunner for nailing down the cause.
1 parent 99fa5f4 commit ffaaaf9

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/backend/utils/adt/varbit.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.26 2002/09/18 21:35:23 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.26.2.1 2007/08/21 02:40:40 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -390,20 +390,25 @@ varbit_out(PG_FUNCTION_ARGS)
390390
result = (char *) palloc(len + 1);
391391
sp = VARBITS(s);
392392
r = result;
393-
for (i = 0; i < len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
393+
for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
394394
{
395+
/* print full bytes */
395396
x = *sp;
396397
for (k = 0; k < BITS_PER_BYTE; k++)
397398
{
398399
*r++ = (x & BITHIGH) ? '1' : '0';
399400
x <<= 1;
400401
}
401402
}
402-
x = *sp;
403-
for (k = i; k < len; k++)
403+
if (i < len)
404404
{
405-
*r++ = (x & BITHIGH) ? '1' : '0';
406-
x <<= 1;
405+
/* print the last partial byte */
406+
x = *sp;
407+
for (k = i; k < len; k++)
408+
{
409+
*r++ = (x & BITHIGH) ? '1' : '0';
410+
x <<= 1;
411+
}
407412
}
408413
*r = '\0';
409414

0 commit comments

Comments
 (0)