SQL_C_BINARY data buffer uses NULL terminator, but realloc() may be missed #145
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
We get spurious heap corruption.
Diagnosis
After a steep learning curve in such matters, we finally found a way to pinpoint it using
valgrind(the key was using the redzone, so it would not immediately corrupt everything):It outputs these sections (snippets).
EDIT: installed Debug symbol, now we can even better pinpoint it:
Due to the sequence of ODBC calls, and the exact size of the buffer (3576), we knew it was the BYTEA column in our row:
Because we use bound buffers that are smaller than that, and we haven't yet come to the point of calling
SQLGetData(), we know this must be an internal buffer that already knows the size of the BYTEA data. Therefore it couldn't be one of our buffers being too small.It follows that this looks like the extra NULL terminator byte that so plagues the C world. And sure, the code says:
psqlodbc/convert.c
Lines 1058 to 1066 in 6e99e75
Proposed fix
It seems that immediately after this, the
len_for_wcs_termis not included in the comparison to assess the need ofrealloc(). If by chance the buffer was already exactlyneedbuflenbytes large, it would not be realloc'd.psqlodbc/convert.c
Lines 1067 to 1073 in 6e99e75
Therefore this PR intends to fix that.
We were unable to test this due to time constraints (this is currently impeding our rollout, we have our hands full 🤕!)Help in setting up a psqlodbc compilation and local installation and deployment is welcome.