Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2510331

Browse files
committed
Cause PQescapeString to stop processing at a null character, rather
than generating an invalid output string. Per observation and patchfrom Igor Shevchenko. Further code cleanup and documentation byTom Lane.
1 parent3b4c142 commit2510331

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.137 2003/09/20 20:12:05 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.138 2003/10/03 18:26:14 tgl Exp $
33
-->
44

55
<chapter id="libpq">
@@ -1972,10 +1972,13 @@ size_t PQescapeString (char *to, const char *from, size_t length);
19721972

19731973
<para>
19741974
The parameter <parameter>from</> points to the first character of the string
1975-
that
1976-
is to be escaped, and the <parameter>length</> parameter gives the
1977-
number of characters in this string. (A terminating zero byte is
1978-
neither necessary nor counted.) <parameter>to</> shall point to a
1975+
that is to be escaped, and the <parameter>length</> parameter gives the
1976+
number of characters in this string. A terminating zero byte is not
1977+
required, and should not be counted in <parameter>length</>. (If
1978+
a terminating zero byte is found before <parameter>length</> bytes are
1979+
processed, <function>PQescapeString</> stops at the zero; the behavior
1980+
is thus rather like <function>strncpy</>.)
1981+
<parameter>to</> shall point to a
19791982
buffer that is able to hold at least one more character than twice
19801983
the value of <parameter>length</>, otherwise the behavior is
19811984
undefined. A call to <function>PQescapeString</> writes an escaped

‎src/interfaces/libpq/fe-exec.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.149 2003/10/02 14:47:44 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.150 2003/10/03 18:26:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2143,47 +2143,47 @@ PQfreeNotify(PGnotify *notify)
21432143
}
21442144

21452145

2146-
/* ---------------
2147-
* Escaping arbitrary strings to get valid SQL strings/identifiers.
2146+
/*
2147+
* Escaping arbitrary strings to get valid SQLliteralstrings.
21482148
*
21492149
* Replaces "\\" with "\\\\" and "'" with "''".
2150-
* length is the length of the buffer pointed to by
2151-
* from. The buffer at to must be at least 2*length + 1 characters
2152-
* long. A terminating NUL character is written.
2153-
* ---------------
2150+
*
2151+
* length is the length of the source string. (Note: if a terminating NUL
2152+
* is encountered sooner, PQescapeString stops short of "length"; the behavior
2153+
* is thus rather like strncpy.)
2154+
*
2155+
* For safety the buffer at "to" must be at least 2*length + 1 bytes long.
2156+
* A terminating NUL character is added to the output string, whether the
2157+
* input is NUL-terminated or not.
2158+
*
2159+
* Returns the actual length of the output (not counting the terminating NUL).
21542160
*/
2155-
21562161
size_t
21572162
PQescapeString(char*to,constchar*from,size_tlength)
21582163
{
21592164
constchar*source=from;
21602165
char*target=to;
2161-
unsignedintremaining=length;
2166+
size_tremaining=length;
21622167

2163-
while (remaining>0)
2168+
while (remaining>0&&*source!='\0')
21642169
{
21652170
switch (*source)
21662171
{
21672172
case'\\':
2168-
*target='\\';
2169-
target++;
2170-
*target='\\';
2171-
/* target and remaining are updated below. */
2173+
*target++='\\';
2174+
*target++='\\';
21722175
break;
21732176

21742177
case'\'':
2175-
*target='\'';
2176-
target++;
2177-
*target='\'';
2178-
/* target and remaining are updated below. */
2178+
*target++='\'';
2179+
*target++='\'';
21792180
break;
21802181

21812182
default:
2182-
*target=*source;
2183-
/* target and remaining are updated below. */
2183+
*target++=*source;
2184+
break;
21842185
}
21852186
source++;
2186-
target++;
21872187
remaining--;
21882188
}
21892189

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp