iCyberon / pg_hashids

Short unique id generator for PostgreSQL, using hashids
MIT License
280 stars 24 forks source link

Include VARHDRSZ when allocating return value #20

Closed JaredReisinger closed 5 years ago

JaredReisinger commented 5 years ago

According to the PostgreSQL documentation, the VARHDRSZ header needs to be included in the allocation for the return value. Without this, the allocations are 4 bytes short (which explains the results described in issue #19), writing the data flies off the end of the allocated space (by 4 bytes), and is then overwritten by subsequent allocations.

From the docs:

char buffer[40]; /* our source data */
...
text *destination = (text *) palloc(VARHDRSZ + 40);
SET_VARSIZE(destination, VARHDRSZ + 40);
memcpy(destination->data, buffer, 40);

(Notice the VARHDRSZ + 40 in both the palloc() and the SET_VARSIZE() calls. While the existing code had + VARHDRSZ in the VARSIZE() call, it was missing from palloc().

Fixes #19, and probably #15.

Signed-off-by: Jared Reisinger jaredreisinger@hotmail.com

iCyberon commented 5 years ago

Awesome! thanks for the work @JaredReisinger.