summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2003-10-03 18:26:14 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2003-10-03 18:26:14 +0000
commit251033186ff575cf130ec53daa996749022fbad3 (patch)
treeba5c4f571541863bea9cd8164ba3cfe40fbedbb8 /src
parent3b4c142030de5e016cb7b87a0bbcb070aab997cd (diff)
Cause PQescapeString to stop processing at a null character, rather
than generating an invalid output string. Per observation and patch from Igor Shevchenko. Further code cleanup and documentation by Tom Lane.
Diffstat (limited to 'src')
-rw-r--r--src/interfaces/libpq/fe-exec.c42
1 files changed, 21 insertions, 21 deletions
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
index 6f23fde1e40..6d159a9a35a 100644
--- a/src/interfaces/libpq/fe-exec.c
+++ b/src/interfaces/libpq/fe-exec.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.149 2003/10/02 14:47:44 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.150 2003/10/03 18:26:14 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -2143,47 +2143,47 @@ PQfreeNotify(PGnotify *notify)
}
-/* ---------------
- * Escaping arbitrary strings to get valid SQL strings/identifiers.
+/*
+ * Escaping arbitrary strings to get valid SQL literal strings.
*
* Replaces "\\" with "\\\\" and "'" with "''".
- * length is the length of the buffer pointed to by
- * from. The buffer at to must be at least 2*length + 1 characters
- * long. A terminating NUL character is written.
- * ---------------
+ *
+ * length is the length of the source string. (Note: if a terminating NUL
+ * is encountered sooner, PQescapeString stops short of "length"; the behavior
+ * is thus rather like strncpy.)
+ *
+ * For safety the buffer at "to" must be at least 2*length + 1 bytes long.
+ * A terminating NUL character is added to the output string, whether the
+ * input is NUL-terminated or not.
+ *
+ * Returns the actual length of the output (not counting the terminating NUL).
*/
-
size_t
PQescapeString(char *to, const char *from, size_t length)
{
const char *source = from;
char *target = to;
- unsigned int remaining = length;
+ size_t remaining = length;
- while (remaining > 0)
+ while (remaining > 0 && *source != '\0')
{
switch (*source)
{
case '\\':
- *target = '\\';
- target++;
- *target = '\\';
- /* target and remaining are updated below. */
+ *target++ = '\\';
+ *target++ = '\\';
break;
case '\'':
- *target = '\'';
- target++;
- *target = '\'';
- /* target and remaining are updated below. */
+ *target++ = '\'';
+ *target++ = '\'';
break;
default:
- *target = *source;
- /* target and remaining are updated below. */
+ *target++ = *source;
+ break;
}
source++;
- target++;
remaining--;
}