| The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group Paperback (6"x9"), 478 pages ISBN 9781906966065 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
1.3.4 Escaping Strings for Inclusion in SQL Commands
PQescapeLiteral-
char *PQescapeLiteral(PGconn *conn, const char *str, size_t length);
PQescapeLiteralescapes a string for use within an SQL command. This is useful when inserting data values as literal constants in SQL commands. Certain characters (such as quotes and backslashes) must be escaped to prevent them from being interpreted specially by the SQL parser.PQescapeLiteralperforms this operation.PQescapeLiteralreturns an escaped version of thestrparameter in memory allocated withmalloc(). This memory should be freed usingPQfreemem()when the result is no longer needed. A terminating zero byte is not required, and should not be counted inlength. (If a terminating zero byte is found beforelengthbytes are processed,PQescapeLiteralstops at the zero; the behavior is thus rather likestrncpy.) The return string has all special characters replaced so that they can be properly processed by the PostgreSQL string literal parser. A terminating zero byte is also added. The single quotes that must surround PostgreSQL string literals are included in the result string. On error,PQescapeLiteralreturnsNULLand a suitable message is stored in theconnobject.
Note that it is not necessary nor correct to do escaping when a data value is passed as a separate parameter inTip: It is especially important to do proper escaping when handling strings that were received from an untrustworthy source. Otherwise there is a security risk: you are vulnerable to “SQL injection” attacks wherein unwanted SQL commands are fed to your database.
PQexecParamsor its sibling routines. PQescapeIdentifier-
char *PQescapeIdentifier(PGconn *conn, const char *str, size_t length);
PQescapeIdentifierescapes a string for use as an SQL identifier, such as a table, column, or function name. This is useful when a user-supplied identifier might contain special characters that would otherwise not be interpreted as part of the identifier by the SQL parser, or when the identifier might contain upper case characters whose case should be preserved.PQescapeIdentifierreturns a version of thestrparameter escaped as an SQL identifier in memory allocated withmalloc(). This memory must be freed usingPQfreemem()when the result is no longer needed. A terminating zero byte is not required, and should not be counted inlength. (If a terminating zero byte is found beforelengthbytes are processed,PQescapeIdentifierstops at the zero; the behavior is thus rather likestrncpy.) The return string has all special characters replaced so that it will be properly processed as an SQL identifier. A terminating zero byte is also added. The return string will also be surrounded by double quotes. On error,PQescapeIdentifierreturnsNULLand a suitable message is stored in theconnobject.Tip: As with string literals, to prevent SQL injection attacks, SQL identifiers must be escaped when they are received from an untrustworthy source.
PQescapeStringConn-
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error);PQescapeStringConnescapes string literals, much likePQescapeLiteral. UnlikePQescapeLiteral, the caller is responsible for providing an appropriately sized buffer. Furthermore,PQescapeStringConndoes not generate the single quotes that must surround PostgreSQL string literals; they should be provided in the SQL command that the result is inserted into. The parameterfrompoints to the first character of the string that is to be escaped, and thelengthparameter gives the number of bytes in this string. A terminating zero byte is not required, and should not be counted inlength. (If a terminating zero byte is found beforelengthbytes are processed,PQescapeStringConnstops at the zero; the behavior is thus rather likestrncpy.)toshall point to a buffer that is able to hold at least one more byte than twice the value oflength, otherwise the behavior is undefined. Behavior is likewise undefined if thetoandfromstrings overlap. If theerrorparameter is notNULL, then*erroris set to zero on success, nonzero on error. Presently the only possible error conditions involve invalid multibyte encoding in the source string. The output string is still generated on error, but it can be expected that the server will reject it as malformed. On error, a suitable message is stored in theconnobject, whether or noterrorisNULL.PQescapeStringConnreturns the number of bytes written toto, not including the terminating zero byte. PQescapeString-
size_t PQescapeString (char *to, const char *from, size_t length);
PQescapeStringis an older, deprecated version ofPQescapeStringConn; the difference is that it does not takeconnorerrorparameters. Because of this, it cannot adjust its behavior depending on the connection properties (such as character encoding) and therefore it might give the wrong results. Also, it has no way to report error conditions.PQescapeStringcan be used safely in single-threaded client programs that work with only one PostgreSQL connection at a time (in this case it can find out what it needs to know “behind the scenes”). In other contexts it is a security hazard and should be avoided in favor ofPQescapeStringConn. PQescapeByteaConn-
Escapes binary data for use within an SQL command with the type
bytea. As withPQescapeStringConn, this is only used when inserting data directly into an SQL command string.unsigned char *PQescapeByteaConn(PGconn *conn, const unsigned char *from, size_t from_length, size_t *to_length);Certain byte values must be escaped (but all byte values can be escaped) when used as part of abytealiteral in an SQL statement. In general, to escape a byte, it is converted into the three digit octal number equal to the octet value, and preceded by usually two backslashes. The single quote (') and backslash (\) characters have special alternative escape sequences. See Volume 1A: 6.4 Binary Data Types for more information.PQescapeByteaConnperforms this operation, escaping only the minimally required bytes. Thefromparameter points to the first byte of the string that is to be escaped, and thefrom_lengthparameter gives the number of bytes in this binary string. (A terminating zero byte is neither necessary nor counted.) Theto_lengthparameter points to a variable that will hold the resultant escaped string length. This result string length includes the terminating zero byte of the result.PQescapeByteaConnreturns an escaped version of thefromparameter binary string in memory allocated withmalloc(). This memory should be freed usingPQfreemem()when the result is no longer needed. The return string has all special characters replaced so that they can be properly processed by the PostgreSQL string literal parser, and thebyteainput function. A terminating zero byte is also added. The single quotes that must surround PostgreSQL string literals are not part of the result string. On error, a null pointer is returned, and a suitable error message is stored in theconnobject. Currently, the only possible error is insufficient memory for the result string. PQescapeBytea-
PQescapeByteais an older, deprecated version ofPQescapeByteaConn.unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length, size_t *to_length);The only difference fromPQescapeByteaConnis thatPQescapeByteadoes not take aPGconnparameter. Because of this, it cannot adjust its behavior depending on the connection properties (in particular, whether standard-conforming strings are enabled) and therefore it might give the wrong results. Also, it has no way to return an error message on failure.PQescapeByteacan be used safely in single-threaded client programs that work with only one PostgreSQL connection at a time (in this case it can find out what it needs to know “behind the scenes”). In other contexts it is a security hazard and should be avoided in favor ofPQescapeByteaConn. PQunescapeBytea-
Converts a string representation of binary data into binary data--the reverse of
PQescapeBytea. This is needed when retrievingbyteadata in text format, but not when retrieving it in binary format.unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length);
Thefromparameter points to a string such as might be returned byPQgetvaluewhen applied to abyteacolumn.PQunescapeByteaconverts this string representation into its binary representation. It returns a pointer to a buffer allocated withmalloc(), orNULLon error, and puts the size of the buffer into_length. The result must be freed usingPQfreememwhen it is no longer needed. This conversion is not exactly the inverse ofPQescapeBytea, because the string is not expected to be “escaped” when received fromPQgetvalue. In particular this means there is no need for string quoting considerations, and so no need for aPGconnparameter.
| ISBN 9781906966065 | The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide | See the print edition |