diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c index ec5a5bebd0f82a45d7f2f2b45f93f3ffb30f6b5f..7d0ff139f5cc624309f032bc0e538afa5317a3ff 100644 --- a/src/backend/utils/adt/varbit.c +++ b/src/backend/utils/adt/varbit.c @@ -9,7 +9,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.58 2009/01/01 17:23:50 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.59 2009/12/12 19:24:35 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1346,7 +1346,12 @@ bitfromint4(PG_FUNCTION_ARGS) /* store first fractional byte */ if (destbitsleft > srcbitsleft) { - *r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK); + int val = (int) (a >> (destbitsleft - 8)); + + /* Force sign-fill in case the compiler implements >> as zero-fill */ + if (a < 0) + val |= (-1) << (srcbitsleft + 8 - destbitsleft); + *r++ = (bits8) (val & BITMASK); destbitsleft -= 8; } /* Now srcbitsleft and destbitsleft are the same, need not track both */ @@ -1425,7 +1430,12 @@ bitfromint8(PG_FUNCTION_ARGS) /* store first fractional byte */ if (destbitsleft > srcbitsleft) { - *r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK); + int val = (int) (a >> (destbitsleft - 8)); + + /* Force sign-fill in case the compiler implements >> as zero-fill */ + if (a < 0) + val |= (-1) << (srcbitsleft + 8 - destbitsleft); + *r++ = (bits8) (val & BITMASK); destbitsleft -= 8; } /* Now srcbitsleft and destbitsleft are the same, need not track both */