Skip to content

Include VARHDRSZ when allocating return value#20

Merged
iCyberon merged 1 commit intoiCyberon:masterfrom
JaredReisinger:fix-corrupted-returns
Sep 6, 2019
Merged

Include VARHDRSZ when allocating return value#20
iCyberon merged 1 commit intoiCyberon:masterfrom
JaredReisinger:fix-corrupted-returns

Conversation

@JaredReisinger
Copy link
Contributor

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

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 describe in issue iCyberon#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 iCyberon#19, and probably iCyberon#15.

Signed-off-by: Jared Reisinger <jaredreisinger@hotmail.com>
@iCyberon
Copy link
Owner

iCyberon commented Sep 6, 2019

Awesome! thanks for the work @JaredReisinger.

@iCyberon iCyberon merged commit 4a32358 into iCyberon:master Sep 6, 2019
@JaredReisinger JaredReisinger deleted the fix-corrupted-returns branch September 6, 2019 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent results when calling id_encode multiple times in same query

2 participants