@@ -176,6 +176,9 @@ decode_varbyte(unsigned char **ptr)
176
176
* 'maxsize' bytes in size. The number items in the returned segment is
177
177
* returned in *nwritten. If it's not equal to nipd, not all the items fit
178
178
* 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.
179
182
*/
180
183
GinPostingList *
181
184
ginCompressPostingList (const ItemPointer ipd ,int nipd ,int maxsize ,
@@ -188,9 +191,12 @@ ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize,
188
191
unsignedchar * ptr ;
189
192
unsignedchar * endptr ;
190
193
194
+ maxsize = SHORTALIGN_DOWN (maxsize );
195
+
191
196
result = palloc (maxsize );
192
197
193
198
maxbytes = maxsize - offsetof(GinPostingList ,bytes );
199
+ Assert (maxbytes > 0 );
194
200
195
201
/* Store the first special item */
196
202
result -> first = ipd [0 ];
@@ -228,6 +234,13 @@ ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize,
228
234
}
229
235
result -> nbytes = ptr - result -> bytes ;
230
236
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
+
231
244
if (nwritten )
232
245
* nwritten = totalpacked ;
233
246