Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include VARHDRSZ when allocating return value #20

Merged
merged 1 commit into from Sep 6, 2019

Conversation

JaredReisinger
Copy link
Contributor

@JaredReisinger JaredReisinger commented Sep 6, 2019

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 Sep 6, 2019
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.

2 participants