@@ -176,6 +176,9 @@ decode_varbyte(unsigned char **ptr)
176176 * 'maxsize' bytes in size. The number items in the returned segment is
177177 * returned in *nwritten. If it's not equal to nipd, not all the items fit
178178 * in 'maxsize', and only the first *nwritten were encoded.
179+ *
180+ * The allocated size of the returned struct is short-aligned, and the padding
181+ * byte at the end, if any, is zero.
179182 */
180183GinPostingList *
181184ginCompressPostingList (const ItemPointer ipd ,int nipd ,int maxsize ,
@@ -188,9 +191,12 @@ ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize,
188191unsignedchar * ptr ;
189192unsignedchar * endptr ;
190193
194+ maxsize = SHORTALIGN_DOWN (maxsize );
195+
191196result = palloc (maxsize );
192197
193198maxbytes = maxsize - offsetof(GinPostingList ,bytes );
199+ Assert (maxbytes > 0 );
194200
195201/* Store the first special item */
196202result -> first = ipd [0 ];
@@ -228,6 +234,13 @@ ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize,
228234}
229235result -> nbytes = ptr - result -> bytes ;
230236
237+ /*
238+ * If we wrote an odd number of bytes, zero out the padding byte at the
239+ * end.
240+ */
241+ if (result -> nbytes != SHORTALIGN (result -> nbytes ))
242+ result -> bytes [result -> nbytes ]= 0 ;
243+
231244if (nwritten )
232245* nwritten = totalpacked ;
233246