Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 523adeb

Browse files
committed
Teach PQcmdTuples() that a COPY command tag might contain a row count,
and tighten up its sanity checking of the tag as a safety measure. Volkan Yazici.
1 parent 502e9ae commit 523adeb

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

doc/src/sgml/libpq.sgml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.204 2006/03/01 00:23:21 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.205 2006/03/03 20:57:32 tgl Exp $
33
-->
44

55
<chapter id="libpq">
@@ -2127,12 +2127,13 @@ char *PQcmdTuples(PGresult *res);
21272127
affected by the <acronym>SQL</> statement that generated the
21282128
<structname>PGresult</>. This function can only be used
21292129
following the execution of an <command>INSERT</>,
2130-
<command>UPDATE</>, <command>DELETE</>, <command>MOVE</>, or
2131-
<command>FETCH</> statement, or an <command>EXECUTE</> of a
2132-
prepared query that contains a <command>INSERT</>,
2130+
<command>UPDATE</>, <command>DELETE</>, <command>MOVE</>,
2131+
<command>FETCH</>, or <command>COPY</> statement,
2132+
or an <command>EXECUTE</> of a
2133+
prepared query that contains an <command>INSERT</>,
21332134
<command>UPDATE</>, or <command>DELETE</> statement. If the
21342135
command that generated the <structname>PGresult</> was
2135-
anything else, <function>PQcmdTuples</> returns the empty
2136+
anything else, <function>PQcmdTuples</> returns an empty
21362137
string. The caller should not free the return value
21372138
directly. It will be freed when the associated
21382139
<structname>PGresult</> handle is passed to

src/interfaces/libpq/fe-exec.c

+26-19
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.179 2006/01/25 20:44:32 tgl Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.180 2006/03/03 20:57:32 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515
#include "postgres_fe.h"
1616

17-
#include <errno.h>
1817
#include <ctype.h>
1918
#include <fcntl.h>
2019

@@ -2168,49 +2167,57 @@ PQoidValue(const PGresult *res)
21682167

21692168
/*
21702169
* PQcmdTuples -
2171-
* If the last command was an INSERT/UPDATE/DELETE/MOVE/FETCH, return a
2172-
* string containing the number of inserted/affected tuples. If not,
2170+
* If the last command was INSERT/UPDATE/DELETE/MOVE/FETCH/COPY, return
2171+
* a string containing the number of inserted/affected tuples. If not,
21732172
* return "".
21742173
*
21752174
* XXX: this should probably return an int
21762175
*/
21772176
char *
21782177
PQcmdTuples(PGresult *res)
21792178
{
2180-
char *p;
2179+
char *p, *c;
21812180

21822181
if (!res)
21832182
return "";
21842183

21852184
if (strncmp(res->cmdStatus, "INSERT ", 7) == 0)
21862185
{
2187-
p = res->cmdStatus + 6;
2188-
p++;
2189-
/* INSERT: skip oid */
2190-
while (*p != ' ' && *p)
2186+
p = res->cmdStatus + 7;
2187+
/* INSERT: skip oid and space */
2188+
while (*p && *p != ' ')
21912189
p++;
2190+
if (*p == 0)
2191+
goto interpret_error; /* no space? */
2192+
p++;
21922193
}
21932194
else if (strncmp(res->cmdStatus, "DELETE ", 7) == 0 ||
21942195
strncmp(res->cmdStatus, "UPDATE ", 7) == 0)
2195-
p = res->cmdStatus + 6;
2196+
p = res->cmdStatus + 7;
21962197
else if (strncmp(res->cmdStatus, "FETCH ", 6) == 0)
2198+
p = res->cmdStatus + 6;
2199+
else if (strncmp(res->cmdStatus, "MOVE ", 5) == 0 ||
2200+
strncmp(res->cmdStatus, "COPY ", 5) == 0)
21972201
p = res->cmdStatus + 5;
2198-
else if (strncmp(res->cmdStatus, "MOVE ", 5) == 0)
2199-
p = res->cmdStatus + 4;
22002202
else
22012203
return "";
22022204

2203-
p++;
2204-
2205-
if (*p == 0)
2205+
/* check that we have an integer (at least one digit, nothing else) */
2206+
for (c = p; *c; c++)
22062207
{
2207-
pqInternalNotice(&res->noticeHooks,
2208-
"could not interpret result from server: %s",
2209-
res->cmdStatus);
2210-
return "";
2208+
if (!isdigit((unsigned char) *c))
2209+
goto interpret_error;
22112210
}
2211+
if (c == p)
2212+
goto interpret_error;
22122213

22132214
return p;
2215+
2216+
interpret_error:
2217+
pqInternalNotice(&res->noticeHooks,
2218+
"could not interpret result from server: %s",
2219+
res->cmdStatus);
2220+
return "";
22142221
}
22152222

22162223
/*

0 commit comments

Comments
 (0)