Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1cee06a

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 25a4a77 commit 1cee06a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/backend/utils/adt/varbit.c

Lines changed: 11 additions & 6 deletions
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-
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.54 2007/06/15 20:56:51 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.55 2007/08/21 02:40:06 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -537,20 +537,25 @@ varbit_out(PG_FUNCTION_ARGS)
537537
result = (char *) palloc(len + 1);
538538
sp = VARBITS(s);
539539
r = result;
540-
for (i = 0; i < len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
540+
for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
541541
{
542+
/* print full bytes */
542543
x = *sp;
543544
for (k = 0; k < BITS_PER_BYTE; k++)
544545
{
545546
*r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
546547
x <<= 1;
547548
}
548549
}
549-
x = *sp;
550-
for (k = i; k < len; k++)
550+
if (i < len)
551551
{
552-
*r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
553-
x <<= 1;
552+
/* print the last partial byte */
553+
x = *sp;
554+
for (k = i; k < len; k++)
555+
{
556+
*r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
557+
x <<= 1;
558+
}
554559
}
555560
*r = '\0';
556561

0 commit comments

Comments
 (0)