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

Should escapeStringConn explicitly look for a zero byte and error? #21

Open
brandon-leapyear opened this issue Apr 28, 2022 · 1 comment

Comments

@brandon-leapyear
Copy link

Just got bitten by a property test that tried to save a string with a zero byte in it to the database + got it silently truncated. I know that's the documented behavior in libpq, but it seems less than ideal in Haskell, a language that can contain zero bytes in strings unlike C.

Or perhaps it should be documented in escapeStringConn to look at the libpq docs for edge cases? In this case, should the postgresql-simple library throw an error? I'd understand postgresql-libpq not wanting to deviate from libpq behavior, but it seems like a good thing to catch unexpected behavior in postgresql-simple (I only bring it up because it seems like the authors of the two libraries are the same)

@phadej
Copy link
Collaborator

phadej commented Dec 28, 2022

Note that VARCHAR and TEXT fields cannot contain null bytes in PostgreSQL.

The characters that can be stored in any of these data types are determined by the database character set, which is selected when the database is created. Regardless of the specific character set, the character with code zero (sometimes called NUL) cannot be stored. For more information refer to Section 24.3.

I really don't know what's the best option. One option is to throw an exception if there is zero byte in the argument to escapeStringConn as AFAIK it won't work anyway, but then we'll traverse most bytestrings for no reason.
Probably we just should document the truncation on zero byte, and outsource the problem to postgresql-simple (not that I have any idea atm, how to elegantly solve it there. ToField is expected to succeed for "all" inputs).

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

No branches or pull requests

2 participants