Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * fe-protocol3.c
4 : : * functions that are specific to frontend/backend protocol version 3
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/interfaces/libpq/fe-protocol3.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres_fe.h"
16 : :
17 : : #include <ctype.h>
18 : : #include <fcntl.h>
19 : : #include <limits.h>
20 : :
21 : : #ifdef WIN32
22 : : #include "win32.h"
23 : : #else
24 : : #include <unistd.h>
25 : : #include <netinet/tcp.h>
26 : : #endif
27 : :
28 : : #include "common/int.h"
29 : : #include "libpq-fe.h"
30 : : #include "libpq-int.h"
31 : : #include "mb/pg_wchar.h"
32 : : #include "port/pg_bswap.h"
33 : :
34 : : /*
35 : : * This macro lists the backend message types that could be "long" (more
36 : : * than a couple of kilobytes).
37 : : */
38 : : #define VALID_LONG_MESSAGE_TYPE(id) \
39 : : ((id) == PqMsg_CopyData || \
40 : : (id) == PqMsg_DataRow || \
41 : : (id) == PqMsg_ErrorResponse || \
42 : : (id) == PqMsg_FunctionCallResponse || \
43 : : (id) == PqMsg_NoticeResponse || \
44 : : (id) == PqMsg_NotificationResponse || \
45 : : (id) == PqMsg_RowDescription)
46 : :
47 : :
48 : : static void handleFatalError(PGconn *conn);
49 : : static void handleSyncLoss(PGconn *conn, char id, int msgLength);
50 : : static int getRowDescriptions(PGconn *conn, int msgLength);
51 : : static int getParamDescriptions(PGconn *conn, int msgLength);
52 : : static int getAnotherTuple(PGconn *conn, int msgLength);
53 : : static int getParameterStatus(PGconn *conn);
54 : : static int getBackendKeyData(PGconn *conn, int msgLength);
55 : : static int getNotify(PGconn *conn);
56 : : static int getCopyStart(PGconn *conn, ExecStatusType copytype);
57 : : static int getReadyForQuery(PGconn *conn);
58 : : static void reportErrorPosition(PQExpBuffer msg, const char *query,
59 : : int loc, int encoding);
60 : : static size_t build_startup_packet(const PGconn *conn, char *packet,
61 : : const PQEnvironmentOption *options);
62 : :
63 : :
64 : : /*
65 : : * parseInput: if appropriate, parse input data from backend
66 : : * until input is exhausted or a stopping state is reached.
67 : : * Note that this function will NOT attempt to read more data from the backend.
68 : : */
69 : : void
8392 tgl@sss.pgh.pa.us 70 :CBC 2202528 : pqParseInput3(PGconn *conn)
71 : : {
72 : : char id;
73 : : int msgLength;
74 : : int avail;
75 : :
76 : : /*
77 : : * Loop to parse successive complete messages available in the buffer.
78 : : */
79 : : for (;;)
80 : : {
81 : : /*
82 : : * Try to read a message. First get the type code and length. Return
83 : : * if not enough data.
84 : : */
85 : 8586327 : conn->inCursor = conn->inStart;
86 [ + + ]: 8586327 : if (pqGetc(&id, conn))
87 : 1638637 : return;
88 [ + + ]: 6947690 : if (pqGetInt(&msgLength, 4, conn))
89 : 2331 : return;
90 : :
91 : : /*
92 : : * Try to validate message type/length here. A length less than 4 is
93 : : * definitely broken. Large lengths should only be believed for a few
94 : : * message types.
95 : : */
96 [ - + ]: 6945359 : if (msgLength < 4)
97 : : {
8392 tgl@sss.pgh.pa.us 98 :UBC 0 : handleSyncLoss(conn, id, msgLength);
99 : 0 : return;
100 : : }
8189 tgl@sss.pgh.pa.us 101 :CBC 6945359 : if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
[ + + + +
+ + - + -
- - - - -
- - ]
102 : : {
8392 tgl@sss.pgh.pa.us 103 :UBC 0 : handleSyncLoss(conn, id, msgLength);
104 : 0 : return;
105 : : }
106 : :
107 : : /*
108 : : * Can't process if message body isn't all here yet.
109 : : */
8392 tgl@sss.pgh.pa.us 110 :CBC 6945359 : msgLength -= 4;
111 : 6945359 : avail = conn->inEnd - conn->inCursor;
112 [ + + ]: 6945359 : if (avail < msgLength)
113 : : {
114 : : /*
115 : : * Before returning, enlarge the input buffer if needed to hold
116 : : * the whole message. This is better than leaving it to
117 : : * pqReadData because we can avoid multiple cycles of realloc()
118 : : * when the message is large; also, we can implement a reasonable
119 : : * recovery strategy if we are unable to make the buffer big
120 : : * enough.
121 : : */
6575 122 [ - + ]: 76006 : if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
123 : : conn))
124 : : {
125 : : /*
126 : : * Abandon the connection. There's not much else we can
127 : : * safely do; we can't just ignore the message or we could
128 : : * miss important changes to the connection state.
129 : : * pqCheckInBufferSpace() already reported the error.
130 : : */
281 heikki.linnakangas@i 131 :UBC 0 : handleFatalError(conn);
132 : : }
8392 tgl@sss.pgh.pa.us 133 :CBC 76006 : return;
134 : : }
135 : :
136 : : /*
137 : : * NOTIFY and NOTICE messages can happen in any state; always process
138 : : * them right away.
139 : : *
140 : : * Most other messages should only be processed while in BUSY state.
141 : : * (In particular, in READY state we hold off further parsing until
142 : : * the application collects the current PGresult.)
143 : : *
144 : : * However, if the state is IDLE then we got trouble; we need to deal
145 : : * with the unexpected message somehow.
146 : : *
147 : : * ParameterStatus ('S') messages are a special case: in IDLE state we
148 : : * must process 'em (this case could happen if a new value was adopted
149 : : * from config file due to SIGHUP), but otherwise we hold off until
150 : : * BUSY state.
151 : : */
1012 nathan@postgresql.or 152 [ + + ]: 6869353 : if (id == PqMsg_NotificationResponse)
153 : : {
8392 tgl@sss.pgh.pa.us 154 [ - + ]: 53 : if (getNotify(conn))
8392 tgl@sss.pgh.pa.us 155 :UBC 0 : return;
156 : : }
1012 nathan@postgresql.or 157 [ + + ]:CBC 6869300 : else if (id == PqMsg_NoticeResponse)
158 : : {
8392 tgl@sss.pgh.pa.us 159 [ - + ]: 17462 : if (pqGetErrorNotice3(conn, false))
8392 tgl@sss.pgh.pa.us 160 :UBC 0 : return;
161 : : }
8392 tgl@sss.pgh.pa.us 162 [ + + ]:CBC 6851838 : else if (conn->asyncStatus != PGASYNC_BUSY)
163 : : {
164 : : /* If not IDLE state, just wait ... */
165 [ + - ]: 485554 : if (conn->asyncStatus != PGASYNC_IDLE)
166 : 485554 : return;
167 : :
168 : : /*
169 : : * Unexpected message in IDLE state; need to recover somehow.
170 : : * ERROR messages are handled using the notice processor;
171 : : * ParameterStatus is handled normally; anything else is just
172 : : * dropped on the floor after displaying a suitable warning
173 : : * notice. (An ERROR is very possibly the backend telling us why
174 : : * it is about to close the connection, so we don't want to just
175 : : * discard it...)
176 : : */
1012 nathan@postgresql.or 177 [ # # ]:UBC 0 : if (id == PqMsg_ErrorResponse)
178 : : {
8335 bruce@momjian.us 179 [ # # ]: 0 : if (pqGetErrorNotice3(conn, false /* treat as notice */ ))
8392 tgl@sss.pgh.pa.us 180 : 0 : return;
181 : : }
1012 nathan@postgresql.or 182 [ # # ]: 0 : else if (id == PqMsg_ParameterStatus)
183 : : {
8392 tgl@sss.pgh.pa.us 184 [ # # ]: 0 : if (getParameterStatus(conn))
185 : 0 : return;
186 : : }
187 : : else
188 : : {
189 : : /* Any other case is unexpected and we summarily skip it */
8377 190 : 0 : pqInternalNotice(&conn->noticeHooks,
191 : : "message type 0x%02x arrived from server while idle",
192 : : id);
193 : : /* Discard the unexpected message */
8392 194 : 0 : conn->inCursor += msgLength;
195 : : }
196 : : }
197 : : else
198 : : {
199 : : /*
200 : : * In BUSY state, we can process everything.
201 : : */
8392 tgl@sss.pgh.pa.us 202 :CBC 6366284 : switch (id)
[ + + + +
+ + + + +
+ + + + +
+ + + +
- ]
203 : : {
1012 nathan@postgresql.or 204 : 401947 : case PqMsg_CommandComplete:
8392 tgl@sss.pgh.pa.us 205 [ - + ]: 401947 : if (pqGets(&conn->workBuffer, conn))
8392 tgl@sss.pgh.pa.us 206 :UBC 0 : return;
1500 tgl@sss.pgh.pa.us 207 [ + + + - ]:CBC 401947 : if (!pgHavePendingResult(conn))
208 : : {
8392 209 : 199544 : conn->result = PQmakeEmptyPGresult(conn,
210 : : PGRES_COMMAND_OK);
7657 neilc@samurai.com 211 [ - + ]: 199544 : if (!conn->result)
212 : : {
1292 peter@eisentraut.org 213 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
3980 heikki.linnakangas@i 214 : 0 : pqSaveErrorResult(conn);
215 : : }
216 : : }
3980 heikki.linnakangas@i 217 [ + - ]:CBC 401947 : if (conn->result)
218 : 401947 : strlcpy(conn->result->cmdStatus, conn->workBuffer.data,
219 : : CMDSTATUS_LEN);
8392 tgl@sss.pgh.pa.us 220 : 401947 : conn->asyncStatus = PGASYNC_READY;
221 : 401947 : break;
1012 nathan@postgresql.or 222 : 31257 : case PqMsg_ErrorResponse:
8392 tgl@sss.pgh.pa.us 223 [ - + ]: 31257 : if (pqGetErrorNotice3(conn, true))
8392 tgl@sss.pgh.pa.us 224 :UBC 0 : return;
8392 tgl@sss.pgh.pa.us 225 :CBC 31257 : conn->asyncStatus = PGASYNC_READY;
226 : 31257 : break;
1012 nathan@postgresql.or 227 : 427257 : case PqMsg_ReadyForQuery:
8379 tgl@sss.pgh.pa.us 228 [ - + ]: 427257 : if (getReadyForQuery(conn))
8392 tgl@sss.pgh.pa.us 229 :UBC 0 : return;
1902 alvherre@alvh.no-ip. 230 [ + + ]:CBC 427257 : if (conn->pipelineStatus != PQ_PIPELINE_OFF)
231 : : {
232 : 329 : conn->result = PQmakeEmptyPGresult(conn,
233 : : PGRES_PIPELINE_SYNC);
234 [ - + ]: 329 : if (!conn->result)
235 : : {
1292 peter@eisentraut.org 236 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
1902 alvherre@alvh.no-ip. 237 : 0 : pqSaveErrorResult(conn);
238 : : }
239 : : else
240 : : {
1902 alvherre@alvh.no-ip. 241 :CBC 329 : conn->pipelineStatus = PQ_PIPELINE_ON;
242 : 329 : conn->asyncStatus = PGASYNC_READY;
243 : : }
244 : : }
245 : : else
246 : : {
247 : : /* Advance the command queue and set us idle */
907 248 : 426928 : pqCommandQueueAdvance(conn, true, false);
1902 249 : 426928 : conn->asyncStatus = PGASYNC_IDLE;
250 : : }
8392 tgl@sss.pgh.pa.us 251 : 427257 : break;
1012 nathan@postgresql.or 252 : 1238 : case PqMsg_EmptyQueryResponse:
1500 tgl@sss.pgh.pa.us 253 [ + - + - ]: 1238 : if (!pgHavePendingResult(conn))
254 : : {
8392 255 : 1238 : conn->result = PQmakeEmptyPGresult(conn,
256 : : PGRES_EMPTY_QUERY);
7657 neilc@samurai.com 257 [ - + ]: 1238 : if (!conn->result)
258 : : {
1292 peter@eisentraut.org 259 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
3980 heikki.linnakangas@i 260 : 0 : pqSaveErrorResult(conn);
261 : : }
262 : : }
8392 tgl@sss.pgh.pa.us 263 :CBC 1238 : conn->asyncStatus = PGASYNC_READY;
264 : 1238 : break;
1012 nathan@postgresql.or 265 : 4429 : case PqMsg_ParseComplete:
266 : : /* If we're doing PQprepare, we're done; else ignore */
1902 alvherre@alvh.no-ip. 267 [ + - ]: 4429 : if (conn->cmd_queue_head &&
268 [ + + ]: 4429 : conn->cmd_queue_head->queryclass == PGQUERY_PREPARE)
269 : : {
1500 tgl@sss.pgh.pa.us 270 [ + - + - ]: 1408 : if (!pgHavePendingResult(conn))
271 : : {
7894 272 : 1408 : conn->result = PQmakeEmptyPGresult(conn,
273 : : PGRES_COMMAND_OK);
7657 neilc@samurai.com 274 [ - + ]: 1408 : if (!conn->result)
275 : : {
1292 peter@eisentraut.org 276 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
3980 heikki.linnakangas@i 277 : 0 : pqSaveErrorResult(conn);
278 : : }
279 : : }
7894 tgl@sss.pgh.pa.us 280 :CBC 1408 : conn->asyncStatus = PGASYNC_READY;
281 : : }
282 : 4429 : break;
1012 nathan@postgresql.or 283 : 10128 : case PqMsg_BindComplete:
284 : : /* Nothing to do for this message type */
1061 michael@paquier.xyz 285 : 10128 : break;
1012 nathan@postgresql.or 286 : 21 : case PqMsg_CloseComplete:
287 : : /* If we're doing PQsendClose, we're done; else ignore */
1061 michael@paquier.xyz 288 [ + - ]: 21 : if (conn->cmd_queue_head &&
289 [ + - ]: 21 : conn->cmd_queue_head->queryclass == PGQUERY_CLOSE)
290 : : {
291 [ + - + - ]: 21 : if (!pgHavePendingResult(conn))
292 : : {
293 : 21 : conn->result = PQmakeEmptyPGresult(conn,
294 : : PGRES_COMMAND_OK);
295 [ - + ]: 21 : if (!conn->result)
296 : : {
1061 michael@paquier.xyz 297 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
298 : 0 : pqSaveErrorResult(conn);
299 : : }
300 : : }
1061 michael@paquier.xyz 301 :CBC 21 : conn->asyncStatus = PGASYNC_READY;
302 : : }
8379 tgl@sss.pgh.pa.us 303 : 21 : break;
1012 nathan@postgresql.or 304 : 236990 : case PqMsg_ParameterStatus:
8392 tgl@sss.pgh.pa.us 305 [ - + ]: 236990 : if (getParameterStatus(conn))
8392 tgl@sss.pgh.pa.us 306 :UBC 0 : return;
8392 tgl@sss.pgh.pa.us 307 :CBC 236990 : break;
1012 nathan@postgresql.or 308 : 15178 : case PqMsg_BackendKeyData:
309 : :
310 : : /*
311 : : * This is expected only during backend startup, but it's
312 : : * just as easy to handle it as part of the main loop.
313 : : * Save the data and continue processing.
314 : : */
423 heikki.linnakangas@i 315 [ - + ]: 15178 : if (getBackendKeyData(conn, msgLength))
8392 tgl@sss.pgh.pa.us 316 :UBC 0 : return;
8392 tgl@sss.pgh.pa.us 317 :CBC 15178 : break;
1012 nathan@postgresql.or 318 : 207468 : case PqMsg_RowDescription:
1562 tgl@sss.pgh.pa.us 319 [ + - ]: 207468 : if (conn->error_result ||
320 [ + + ]: 207468 : (conn->result != NULL &&
321 [ - + ]: 50 : conn->result->resultStatus == PGRES_FATAL_ERROR))
322 : : {
323 : : /*
324 : : * We've already choked for some reason. Just discard
325 : : * the data till we get to the end of the query.
326 : : */
3820 heikki.linnakangas@i 327 :UBC 0 : conn->inCursor += msgLength;
328 : : }
3820 heikki.linnakangas@i 329 [ + + ]:CBC 207468 : else if (conn->result == NULL ||
1902 alvherre@alvh.no-ip. 330 [ + - ]: 50 : (conn->cmd_queue_head &&
331 [ + - ]: 50 : conn->cmd_queue_head->queryclass == PGQUERY_DESCRIBE))
332 : : {
333 : : /* First 'T' in a query sequence */
5169 tgl@sss.pgh.pa.us 334 [ - + ]: 207468 : if (getRowDescriptions(conn, msgLength))
8392 tgl@sss.pgh.pa.us 335 :UBC 0 : return;
336 : : }
337 : : else
338 : : {
339 : : /*
340 : : * A new 'T' message is treated as the start of
341 : : * another PGresult. (It is not clear that this is
342 : : * really possible with the current backend.) We stop
343 : : * parsing until the application accepts the current
344 : : * result.
345 : : */
346 : 0 : conn->asyncStatus = PGASYNC_READY;
347 : 0 : return;
348 : : }
8392 tgl@sss.pgh.pa.us 349 :CBC 207468 : break;
1012 nathan@postgresql.or 350 : 5105 : case PqMsg_NoData:
351 : :
352 : : /*
353 : : * NoData indicates that we will not be seeing a
354 : : * RowDescription message because the statement or portal
355 : : * inquired about doesn't return rows.
356 : : *
357 : : * If we're doing a Describe, we have to pass something
358 : : * back to the client, so set up a COMMAND_OK result,
359 : : * instead of PGRES_TUPLES_OK. Otherwise we can just
360 : : * ignore this message.
361 : : */
1902 alvherre@alvh.no-ip. 362 [ + - ]: 5105 : if (conn->cmd_queue_head &&
363 [ + + ]: 5105 : conn->cmd_queue_head->queryclass == PGQUERY_DESCRIBE)
364 : : {
1500 tgl@sss.pgh.pa.us 365 [ - + - - ]: 8 : if (!pgHavePendingResult(conn))
366 : : {
6350 tgl@sss.pgh.pa.us 367 :UBC 0 : conn->result = PQmakeEmptyPGresult(conn,
368 : : PGRES_COMMAND_OK);
369 [ # # ]: 0 : if (!conn->result)
370 : : {
1292 peter@eisentraut.org 371 : 0 : libpq_append_conn_error(conn, "out of memory");
3980 heikki.linnakangas@i 372 : 0 : pqSaveErrorResult(conn);
373 : : }
374 : : }
7225 tgl@sss.pgh.pa.us 375 :CBC 8 : conn->asyncStatus = PGASYNC_READY;
376 : : }
377 : 5105 : break;
1012 nathan@postgresql.or 378 : 58 : case PqMsg_ParameterDescription:
3820 heikki.linnakangas@i 379 [ - + ]: 58 : if (getParamDescriptions(conn, msgLength))
7225 tgl@sss.pgh.pa.us 380 :UBC 0 : return;
1906 tgl@sss.pgh.pa.us 381 :CBC 58 : break;
1012 nathan@postgresql.or 382 : 5012616 : case PqMsg_DataRow:
8392 tgl@sss.pgh.pa.us 383 [ + - ]: 5012616 : if (conn->result != NULL &&
784 384 [ + + ]: 5012616 : (conn->result->resultStatus == PGRES_TUPLES_OK ||
385 [ + - ]: 123 : conn->result->resultStatus == PGRES_TUPLES_CHUNK))
386 : : {
387 : : /* Read another tuple of a normal query response */
8392 388 [ - + ]: 5012616 : if (getAnotherTuple(conn, msgLength))
8392 tgl@sss.pgh.pa.us 389 :UBC 0 : return;
390 : : }
1562 391 [ # # ]: 0 : else if (conn->error_result ||
392 [ # # ]: 0 : (conn->result != NULL &&
393 [ # # ]: 0 : conn->result->resultStatus == PGRES_FATAL_ERROR))
394 : : {
395 : : /*
396 : : * We've already choked for some reason. Just discard
397 : : * tuples till we get to the end of the query.
398 : : */
8392 399 : 0 : conn->inCursor += msgLength;
400 : : }
401 : : else
402 : : {
403 : : /* Set up to report error at end of query */
1292 peter@eisentraut.org 404 : 0 : libpq_append_conn_error(conn, "server sent data (\"D\" message) without prior row description (\"T\" message)");
8392 tgl@sss.pgh.pa.us 405 : 0 : pqSaveErrorResult(conn);
406 : : /* Discard the unexpected message */
407 : 0 : conn->inCursor += msgLength;
408 : : }
8392 tgl@sss.pgh.pa.us 409 :CBC 5012616 : break;
1012 nathan@postgresql.or 410 : 709 : case PqMsg_CopyInResponse:
8379 tgl@sss.pgh.pa.us 411 [ - + ]: 709 : if (getCopyStart(conn, PGRES_COPY_IN))
8392 tgl@sss.pgh.pa.us 412 :UBC 0 : return;
8392 tgl@sss.pgh.pa.us 413 :CBC 709 : conn->asyncStatus = PGASYNC_COPY_IN;
414 : 709 : break;
1012 nathan@postgresql.or 415 : 5363 : case PqMsg_CopyOutResponse:
8379 tgl@sss.pgh.pa.us 416 [ - + ]: 5363 : if (getCopyStart(conn, PGRES_COPY_OUT))
8392 tgl@sss.pgh.pa.us 417 :UBC 0 : return;
8392 tgl@sss.pgh.pa.us 418 :CBC 5363 : conn->asyncStatus = PGASYNC_COPY_OUT;
419 : 5363 : conn->copy_already_done = 0;
420 : 5363 : break;
1012 nathan@postgresql.or 421 : 783 : case PqMsg_CopyBothResponse:
5649 rhaas@postgresql.org 422 [ - + ]: 783 : if (getCopyStart(conn, PGRES_COPY_BOTH))
5649 rhaas@postgresql.org 423 :UBC 0 : return;
5649 rhaas@postgresql.org 424 :CBC 783 : conn->asyncStatus = PGASYNC_COPY_BOTH;
425 : 783 : conn->copy_already_done = 0;
426 : 783 : break;
1012 nathan@postgresql.or 427 : 7 : case PqMsg_CopyData:
428 : :
429 : : /*
430 : : * If we see Copy Data, just silently drop it. This would
431 : : * only occur if application exits COPY OUT mode too
432 : : * early.
433 : : */
8392 tgl@sss.pgh.pa.us 434 : 7 : conn->inCursor += msgLength;
435 : 7 : break;
1012 nathan@postgresql.or 436 : 5730 : case PqMsg_CopyDone:
437 : :
438 : : /*
439 : : * If we see Copy Done, just silently drop it. This is
440 : : * the normal case during PQendcopy. We will keep
441 : : * swallowing data, expecting to see command-complete for
442 : : * the COPY command.
443 : : */
8392 tgl@sss.pgh.pa.us 444 : 5730 : break;
8392 tgl@sss.pgh.pa.us 445 :UBC 0 : default:
1292 peter@eisentraut.org 446 : 0 : libpq_append_conn_error(conn, "unexpected response from server; first received character was \"%c\"", id);
447 : : /* build an error result holding the error message */
8392 tgl@sss.pgh.pa.us 448 : 0 : pqSaveErrorResult(conn);
449 : : /* not sure if we will see more, so go to ready state */
450 : 0 : conn->asyncStatus = PGASYNC_READY;
451 : : /* Discard the unexpected message */
452 : 0 : conn->inCursor += msgLength;
453 : 0 : break;
454 : : } /* switch on protocol character */
455 : : }
456 : : /* Successfully consumed this message */
8392 tgl@sss.pgh.pa.us 457 [ + - ]:CBC 6383799 : if (conn->inCursor == conn->inStart + 5 + msgLength)
458 : : {
459 : : /* Normal case: parsing agrees with specified length */
652 alvherre@alvh.no-ip. 460 : 6383799 : pqParseDone(conn, conn->inCursor);
461 : : }
281 heikki.linnakangas@i 462 [ # # # # ]:UBC 0 : else if (conn->error_result && conn->status == CONNECTION_BAD)
463 : : {
464 : : /* The connection was abandoned and we already reported it */
465 : 0 : return;
466 : : }
467 : : else
468 : : {
469 : : /* Trouble --- report it */
1292 peter@eisentraut.org 470 : 0 : libpq_append_conn_error(conn, "message contents do not agree with length in message type \"%c\"", id);
471 : : /* build an error result holding the error message */
8392 tgl@sss.pgh.pa.us 472 : 0 : pqSaveErrorResult(conn);
473 : 0 : conn->asyncStatus = PGASYNC_READY;
474 : : /* trust the specified message length as what to skip */
475 : 0 : conn->inStart += 5 + msgLength;
476 : : }
477 : : }
478 : : }
479 : :
480 : : /*
481 : : * handleFatalError: clean up after a nonrecoverable error
482 : : *
483 : : * This is for errors where we need to abandon the connection. The caller has
484 : : * already saved the error message in conn->errorMessage.
485 : : */
486 : : static void
281 heikki.linnakangas@i 487 : 0 : handleFatalError(PGconn *conn)
488 : : {
489 : : /* build an error result holding the error message */
8392 tgl@sss.pgh.pa.us 490 : 0 : pqSaveErrorResult(conn);
1902 alvherre@alvh.no-ip. 491 : 0 : conn->asyncStatus = PGASYNC_READY; /* drop out of PQgetResult wait loop */
492 : : /* flush input data since we're giving up on processing it */
3852 tgl@sss.pgh.pa.us 493 : 0 : pqDropConnection(conn, true);
3265 494 : 0 : conn->status = CONNECTION_BAD; /* No more connection to backend */
8392 495 : 0 : }
496 : :
497 : : /*
498 : : * handleSyncLoss: clean up after loss of message-boundary sync
499 : : *
500 : : * There isn't really a lot we can do here except abandon the connection.
501 : : */
502 : : static void
281 heikki.linnakangas@i 503 : 0 : handleSyncLoss(PGconn *conn, char id, int msgLength)
504 : : {
505 : 0 : libpq_append_conn_error(conn, "lost synchronization with server: got message type \"%c\", length %d",
506 : : id, msgLength);
507 : 0 : handleFatalError(conn);
508 : 0 : }
509 : :
510 : : /*
511 : : * parseInput subroutine to read a 'T' (row descriptions) message.
512 : : * We'll build a new PGresult structure (unless called for a Describe
513 : : * command for a prepared statement) containing the attribute data.
514 : : * Returns: 0 if processed message successfully, EOF to suspend parsing
515 : : * (the latter case is not actually used currently).
516 : : */
517 : : static int
5169 tgl@sss.pgh.pa.us 518 :CBC 207468 : getRowDescriptions(PGconn *conn, int msgLength)
519 : : {
520 : : PGresult *result;
521 : : int nfields;
522 : : const char *errmsg;
523 : : int i;
524 : :
525 : : /*
526 : : * When doing Describe for a prepared statement, there'll already be a
527 : : * PGresult created by getParamDescriptions, and we should fill data into
528 : : * that. Otherwise, create a new, empty PGresult.
529 : : */
1902 alvherre@alvh.no-ip. 530 [ + - ]: 207468 : if (!conn->cmd_queue_head ||
531 [ + - ]: 207468 : (conn->cmd_queue_head &&
532 [ + + ]: 207468 : conn->cmd_queue_head->queryclass == PGQUERY_DESCRIBE))
533 : : {
7225 tgl@sss.pgh.pa.us 534 [ + + ]: 51 : if (conn->result)
535 : 50 : result = conn->result;
536 : : else
537 : 1 : result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
538 : : }
539 : : else
540 : 207417 : result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
7657 neilc@samurai.com 541 [ - + ]: 207468 : if (!result)
542 : : {
5169 tgl@sss.pgh.pa.us 543 :UBC 0 : errmsg = NULL; /* means "out of memory", see below */
544 : 0 : goto advance_and_error;
545 : : }
546 : :
547 : : /* parseInput already read the 'T' label and message length. */
548 : : /* the next two bytes are the number of fields */
8392 tgl@sss.pgh.pa.us 549 [ - + ]:CBC 207468 : if (pqGetInt(&(result->numAttributes), 2, conn))
550 : : {
551 : : /* We should not run out of data here, so complain */
5169 tgl@sss.pgh.pa.us 552 :UBC 0 : errmsg = libpq_gettext("insufficient data in \"T\" message");
553 : 0 : goto advance_and_error;
554 : : }
8392 tgl@sss.pgh.pa.us 555 :CBC 207468 : nfields = result->numAttributes;
556 : :
557 : : /* allocate space for the attribute descriptors */
558 [ + + ]: 207468 : if (nfields > 0)
559 : : {
560 : 207239 : result->attDescs = (PGresAttDesc *)
3209 peter_e@gmx.net 561 : 207239 : pqResultAlloc(result, nfields * sizeof(PGresAttDesc), true);
7657 neilc@samurai.com 562 [ - + ]: 207239 : if (!result->attDescs)
563 : : {
5169 tgl@sss.pgh.pa.us 564 :UBC 0 : errmsg = NULL; /* means "out of memory", see below */
565 : 0 : goto advance_and_error;
566 : : }
7657 neilc@samurai.com 567 [ + - + - :CBC 2822979 : MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
+ - + + +
+ ]
568 : : }
569 : :
570 : : /* result->binary is true only if ALL columns are binary */
8379 tgl@sss.pgh.pa.us 571 : 207468 : result->binary = (nfields > 0) ? 1 : 0;
572 : :
573 : : /* get type info */
8392 574 [ + + ]: 872813 : for (i = 0; i < nfields; i++)
575 : : {
576 : : int tableid;
577 : : int columnid;
578 : : int typid;
579 : : int typlen;
580 : : int atttypmod;
581 : : int format;
582 : :
583 [ + - + - ]: 1330690 : if (pqGets(&conn->workBuffer, conn) ||
584 [ + - ]: 1330690 : pqGetInt(&tableid, 4, conn) ||
585 [ + - ]: 1330690 : pqGetInt(&columnid, 2, conn) ||
586 [ + - ]: 1330690 : pqGetInt(&typid, 4, conn) ||
587 [ + - ]: 1330690 : pqGetInt(&typlen, 2, conn) ||
588 [ - + ]: 1330690 : pqGetInt(&atttypmod, 4, conn) ||
589 : 665345 : pqGetInt(&format, 2, conn))
590 : : {
591 : : /* We should not run out of data here, so complain */
5169 tgl@sss.pgh.pa.us 592 :UBC 0 : errmsg = libpq_gettext("insufficient data in \"T\" message");
593 : 0 : goto advance_and_error;
594 : : }
595 : :
596 : : /*
597 : : * Since pqGetInt treats 2-byte integers as unsigned, we need to
598 : : * coerce these results to signed form.
599 : : */
8392 tgl@sss.pgh.pa.us 600 :CBC 665345 : columnid = (int) ((int16) columnid);
601 : 665345 : typlen = (int) ((int16) typlen);
602 : 665345 : format = (int) ((int16) format);
603 : :
604 : 1330690 : result->attDescs[i].name = pqResultStrdup(result,
605 : 665345 : conn->workBuffer.data);
7657 neilc@samurai.com 606 [ - + ]: 665345 : if (!result->attDescs[i].name)
607 : : {
5169 tgl@sss.pgh.pa.us 608 :UBC 0 : errmsg = NULL; /* means "out of memory", see below */
609 : 0 : goto advance_and_error;
610 : : }
8379 tgl@sss.pgh.pa.us 611 :CBC 665345 : result->attDescs[i].tableid = tableid;
612 : 665345 : result->attDescs[i].columnid = columnid;
613 : 665345 : result->attDescs[i].format = format;
8392 614 : 665345 : result->attDescs[i].typid = typid;
615 : 665345 : result->attDescs[i].typlen = typlen;
616 : 665345 : result->attDescs[i].atttypmod = atttypmod;
617 : :
8379 618 [ + + ]: 665345 : if (format != 1)
619 : 665306 : result->binary = 0;
620 : : }
621 : :
622 : : /* Success! */
8392 623 : 207468 : conn->result = result;
624 : :
625 : : /*
626 : : * If we're doing a Describe, we're done, and ready to pass the result
627 : : * back to the client.
628 : : */
1902 alvherre@alvh.no-ip. 629 [ + - ]: 207468 : if ((!conn->cmd_queue_head) ||
630 [ + - ]: 207468 : (conn->cmd_queue_head &&
631 [ + + ]: 207468 : conn->cmd_queue_head->queryclass == PGQUERY_DESCRIBE))
632 : : {
5169 tgl@sss.pgh.pa.us 633 : 51 : conn->asyncStatus = PGASYNC_READY;
634 : 51 : return 0;
635 : : }
636 : :
637 : : /*
638 : : * We could perform additional setup for the new result set here, but for
639 : : * now there's nothing else to do.
640 : : */
641 : :
642 : : /* And we're done. */
5049 643 : 207417 : return 0;
644 : :
5169 tgl@sss.pgh.pa.us 645 :UBC 0 : advance_and_error:
646 : : /* Discard unsaved result, if any */
647 [ # # # # ]: 0 : if (result && result != conn->result)
7225 648 : 0 : PQclear(result);
649 : :
650 : : /*
651 : : * Replace partially constructed result with an error result. First
652 : : * discard the old result to try to win back some memory.
653 : : */
5169 654 : 0 : pqClearAsyncResult(conn);
655 : :
656 : : /*
657 : : * If preceding code didn't provide an error message, assume "out of
658 : : * memory" was meant. The advantage of having this special case is that
659 : : * freeing the old result first greatly improves the odds that gettext()
660 : : * will succeed in providing a translation.
661 : : */
662 [ # # ]: 0 : if (!errmsg)
663 : 0 : errmsg = libpq_gettext("out of memory for query result");
664 : :
1965 665 : 0 : appendPQExpBuffer(&conn->errorMessage, "%s\n", errmsg);
5169 666 : 0 : pqSaveErrorResult(conn);
667 : :
668 : : /*
669 : : * Show the message as fully consumed, else pqParseInput3 will overwrite
670 : : * our error with a complaint about that.
671 : : */
1906 672 : 0 : conn->inCursor = conn->inStart + 5 + msgLength;
673 : :
674 : : /*
675 : : * Return zero to allow input parsing to continue. Subsequent "D"
676 : : * messages will be ignored until we get to end of data, since an error
677 : : * result is already set up.
678 : : */
5169 679 : 0 : return 0;
680 : : }
681 : :
682 : : /*
683 : : * parseInput subroutine to read a 't' (ParameterDescription) message.
684 : : * We'll build a new PGresult structure containing the parameter data.
685 : : * Returns: 0 if processed message successfully, EOF to suspend parsing
686 : : * (the latter case is not actually used currently).
687 : : */
688 : : static int
3820 heikki.linnakangas@i 689 :CBC 58 : getParamDescriptions(PGconn *conn, int msgLength)
690 : : {
691 : : PGresult *result;
3711 tgl@sss.pgh.pa.us 692 : 58 : const char *errmsg = NULL; /* means "out of memory", see below */
693 : : int nparams;
694 : : int i;
695 : :
7225 696 : 58 : result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
697 [ - + ]: 58 : if (!result)
3820 heikki.linnakangas@i 698 :UBC 0 : goto advance_and_error;
699 : :
700 : : /* parseInput already read the 't' label and message length. */
701 : : /* the next two bytes are the number of parameters */
7225 tgl@sss.pgh.pa.us 702 [ - + ]:CBC 58 : if (pqGetInt(&(result->numParameters), 2, conn))
3820 heikki.linnakangas@i 703 :UBC 0 : goto not_enough_data;
7225 tgl@sss.pgh.pa.us 704 :CBC 58 : nparams = result->numParameters;
705 : :
706 : : /* allocate space for the parameter descriptors */
707 [ + + ]: 58 : if (nparams > 0)
708 : : {
709 : 5 : result->paramDescs = (PGresParamDesc *)
3209 peter_e@gmx.net 710 : 5 : pqResultAlloc(result, nparams * sizeof(PGresParamDesc), true);
7225 tgl@sss.pgh.pa.us 711 [ - + ]: 5 : if (!result->paramDescs)
3820 heikki.linnakangas@i 712 :UBC 0 : goto advance_and_error;
7225 tgl@sss.pgh.pa.us 713 [ + - + + :CBC 9 : MemSet(result->paramDescs, 0, nparams * sizeof(PGresParamDesc));
+ - + - +
+ ]
714 : : }
715 : :
716 : : /* get parameter info */
717 [ + + ]: 67 : for (i = 0; i < nparams; i++)
718 : : {
719 : : int typid;
720 : :
721 [ - + ]: 9 : if (pqGetInt(&typid, 4, conn))
3820 heikki.linnakangas@i 722 :UBC 0 : goto not_enough_data;
7225 tgl@sss.pgh.pa.us 723 :CBC 9 : result->paramDescs[i].typid = typid;
724 : : }
725 : :
726 : : /* Success! */
727 : 58 : conn->result = result;
728 : :
729 : 58 : return 0;
730 : :
3820 heikki.linnakangas@i 731 :UBC 0 : not_enough_data:
1906 tgl@sss.pgh.pa.us 732 : 0 : errmsg = libpq_gettext("insufficient data in \"t\" message");
733 : :
3820 heikki.linnakangas@i 734 : 0 : advance_and_error:
735 : : /* Discard unsaved result, if any */
736 [ # # # # ]: 0 : if (result && result != conn->result)
737 : 0 : PQclear(result);
738 : :
739 : : /*
740 : : * Replace partially constructed result with an error result. First
741 : : * discard the old result to try to win back some memory.
742 : : */
743 : 0 : pqClearAsyncResult(conn);
744 : :
745 : : /*
746 : : * If preceding code didn't provide an error message, assume "out of
747 : : * memory" was meant. The advantage of having this special case is that
748 : : * freeing the old result first greatly improves the odds that gettext()
749 : : * will succeed in providing a translation.
750 : : */
751 [ # # ]: 0 : if (!errmsg)
752 : 0 : errmsg = libpq_gettext("out of memory");
1965 tgl@sss.pgh.pa.us 753 : 0 : appendPQExpBuffer(&conn->errorMessage, "%s\n", errmsg);
3820 heikki.linnakangas@i 754 : 0 : pqSaveErrorResult(conn);
755 : :
756 : : /*
757 : : * Show the message as fully consumed, else pqParseInput3 will overwrite
758 : : * our error with a complaint about that.
759 : : */
1906 tgl@sss.pgh.pa.us 760 : 0 : conn->inCursor = conn->inStart + 5 + msgLength;
761 : :
762 : : /*
763 : : * Return zero to allow input parsing to continue. Essentially, we've
764 : : * replaced the COMMAND_OK result with an error result, but since this
765 : : * doesn't affect the protocol state, it's fine.
766 : : */
3820 heikki.linnakangas@i 767 : 0 : return 0;
768 : : }
769 : :
770 : : /*
771 : : * parseInput subroutine to read a 'D' (row data) message.
772 : : * We fill rowbuf with column pointers and then call the row processor.
773 : : * Returns: 0 if processed message successfully, EOF to suspend parsing
774 : : * (the latter case is not actually used currently).
775 : : */
776 : : static int
8392 tgl@sss.pgh.pa.us 777 :CBC 5012616 : getAnotherTuple(PGconn *conn, int msgLength)
778 : : {
779 : 5012616 : PGresult *result = conn->result;
780 : 5012616 : int nfields = result->numAttributes;
781 : : const char *errmsg;
782 : : PGdataValue *rowbuf;
783 : : int tupnfields; /* # fields from tuple */
784 : : int vlen; /* length of the current field value */
785 : : int i;
786 : :
787 : : /* Get the field count and make sure it's what we expect */
788 [ - + ]: 5012616 : if (pqGetInt(&tupnfields, 2, conn))
789 : : {
790 : : /* We should not run out of data here, so complain */
5169 tgl@sss.pgh.pa.us 791 :UBC 0 : errmsg = libpq_gettext("insufficient data in \"D\" message");
792 : 0 : goto advance_and_error;
793 : : }
794 : :
8392 tgl@sss.pgh.pa.us 795 [ - + ]:CBC 5012616 : if (tupnfields != nfields)
796 : : {
5169 tgl@sss.pgh.pa.us 797 :UBC 0 : errmsg = libpq_gettext("unexpected field count in \"D\" message");
798 : 0 : goto advance_and_error;
799 : : }
800 : :
801 : : /* Resize row buffer if needed */
5169 tgl@sss.pgh.pa.us 802 :CBC 5012616 : rowbuf = conn->rowBuf;
803 [ + + ]: 5012616 : if (nfields > conn->rowBufLen)
804 : : {
805 : 286 : rowbuf = (PGdataValue *) realloc(rowbuf,
806 : : nfields * sizeof(PGdataValue));
807 [ - + ]: 286 : if (!rowbuf)
808 : : {
5169 tgl@sss.pgh.pa.us 809 :UBC 0 : errmsg = NULL; /* means "out of memory", see below */
810 : 0 : goto advance_and_error;
811 : : }
5169 tgl@sss.pgh.pa.us 812 :CBC 286 : conn->rowBuf = rowbuf;
813 : 286 : conn->rowBufLen = nfields;
814 : : }
815 : :
816 : : /* Scan the fields */
8392 817 [ + + ]: 31230320 : for (i = 0; i < nfields; i++)
818 : : {
819 : : /* get the value length */
820 [ - + ]: 26217704 : if (pqGetInt(&vlen, 4, conn))
821 : : {
822 : : /* We should not run out of data here, so complain */
5169 tgl@sss.pgh.pa.us 823 :UBC 0 : errmsg = libpq_gettext("insufficient data in \"D\" message");
824 : 0 : goto advance_and_error;
825 : : }
5169 tgl@sss.pgh.pa.us 826 :CBC 26217704 : rowbuf[i].len = vlen;
827 : :
828 : : /*
829 : : * rowbuf[i].value always points to the next address in the data
830 : : * buffer even if the value is NULL. This allows row processors to
831 : : * estimate data sizes more easily.
832 : : */
833 : 26217704 : rowbuf[i].value = conn->inBuffer + conn->inCursor;
834 : :
835 : : /* Skip over the data value */
8392 836 [ + + ]: 26217704 : if (vlen > 0)
837 : : {
5169 838 [ - + ]: 24672864 : if (pqSkipnchar(vlen, conn))
839 : : {
840 : : /* We should not run out of data here, so complain */
5169 tgl@sss.pgh.pa.us 841 :UBC 0 : errmsg = libpq_gettext("insufficient data in \"D\" message");
842 : 0 : goto advance_and_error;
843 : : }
844 : : }
845 : : }
846 : :
847 : : /* Process the collected row */
5169 tgl@sss.pgh.pa.us 848 :CBC 5012616 : errmsg = NULL;
5049 849 [ + - ]: 5012616 : if (pqRowProcessor(conn, &errmsg))
850 : 5012616 : return 0; /* normal, successful exit */
851 : :
852 : : /* pqRowProcessor failed, fall through to report it */
853 : :
5169 tgl@sss.pgh.pa.us 854 :UBC 0 : advance_and_error:
855 : :
856 : : /*
857 : : * Replace partially constructed result with an error result. First
858 : : * discard the old result to try to win back some memory.
859 : : */
8392 860 : 0 : pqClearAsyncResult(conn);
861 : :
862 : : /*
863 : : * If preceding code didn't provide an error message, assume "out of
864 : : * memory" was meant. The advantage of having this special case is that
865 : : * freeing the old result first greatly improves the odds that gettext()
866 : : * will succeed in providing a translation.
867 : : */
5169 868 [ # # ]: 0 : if (!errmsg)
869 : 0 : errmsg = libpq_gettext("out of memory for query result");
870 : :
1965 871 : 0 : appendPQExpBuffer(&conn->errorMessage, "%s\n", errmsg);
8392 872 : 0 : pqSaveErrorResult(conn);
873 : :
874 : : /*
875 : : * Show the message as fully consumed, else pqParseInput3 will overwrite
876 : : * our error with a complaint about that.
877 : : */
1906 878 : 0 : conn->inCursor = conn->inStart + 5 + msgLength;
879 : :
880 : : /*
881 : : * Return zero to allow input parsing to continue. Subsequent "D"
882 : : * messages will be ignored until we get to end of data, since an error
883 : : * result is already set up.
884 : : */
8392 885 : 0 : return 0;
886 : : }
887 : :
888 : :
889 : : /*
890 : : * Attempt to read an Error or Notice response message.
891 : : * This is possible in several places, so we break it out as a subroutine.
892 : : *
893 : : * Entry: 'E' or 'N' message type and length have already been consumed.
894 : : * Exit: returns 0 if successfully consumed message.
895 : : * returns EOF if not enough data.
896 : : */
897 : : int
8392 tgl@sss.pgh.pa.us 898 :CBC 49147 : pqGetErrorNotice3(PGconn *conn, bool isError)
899 : : {
7657 neilc@samurai.com 900 : 49147 : PGresult *res = NULL;
3709 tgl@sss.pgh.pa.us 901 : 49147 : bool have_position = false;
902 : : PQExpBufferData workBuf;
903 : : char id;
904 : :
905 : : /* If in pipeline mode, set error indicator for it */
1902 alvherre@alvh.no-ip. 906 [ + + + + ]: 49147 : if (isError && conn->pipelineStatus != PQ_PIPELINE_OFF)
907 : 62 : conn->pipelineStatus = PQ_PIPELINE_ABORTED;
908 : :
909 : : /*
910 : : * If this is an error message, pre-emptively clear any incomplete query
911 : : * result we may have. We'd just throw it away below anyway, and
912 : : * releasing it before collecting the error might avoid out-of-memory.
913 : : */
2969 tgl@sss.pgh.pa.us 914 [ + + ]: 49147 : if (isError)
915 : 31650 : pqClearAsyncResult(conn);
916 : :
917 : : /*
918 : : * Since the fields might be pretty long, we create a temporary
919 : : * PQExpBuffer rather than using conn->workBuffer. workBuffer is intended
920 : : * for stuff that is expected to be short. We shouldn't use
921 : : * conn->errorMessage either, since this might be only a notice.
922 : : */
8392 923 : 49147 : initPQExpBuffer(&workBuf);
924 : :
925 : : /*
926 : : * Make a PGresult to hold the accumulated fields. We temporarily lie
927 : : * about the result status, so that PQmakeEmptyPGresult doesn't uselessly
928 : : * copy conn->errorMessage.
929 : : *
930 : : * NB: This allocation can fail, if you run out of memory. The rest of the
931 : : * function handles that gracefully, and we still try to set the error
932 : : * message as the connection's error message.
933 : : */
7657 neilc@samurai.com 934 : 49147 : res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
3980 heikki.linnakangas@i 935 [ + - ]: 49147 : if (res)
936 [ + + ]: 49147 : res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
937 : :
938 : : /*
939 : : * Read the fields and save into res.
940 : : *
941 : : * While at it, save the SQLSTATE in conn->last_sqlstate, and note whether
942 : : * we saw a PG_DIAG_STATEMENT_POSITION field.
943 : : */
944 : : for (;;)
945 : : {
8392 tgl@sss.pgh.pa.us 946 [ - + ]: 434534 : if (pqGetc(&id, conn))
8392 tgl@sss.pgh.pa.us 947 :UBC 0 : goto fail;
8392 tgl@sss.pgh.pa.us 948 [ + + ]:CBC 434534 : if (id == '\0')
949 : 49147 : break; /* terminator found */
950 [ - + ]: 385387 : if (pqGets(&workBuf, conn))
8392 tgl@sss.pgh.pa.us 951 :UBC 0 : goto fail;
8379 tgl@sss.pgh.pa.us 952 :CBC 385387 : pqSaveMessageField(res, id, workBuf.data);
3709 953 [ + + ]: 385387 : if (id == PG_DIAG_SQLSTATE)
954 : 49147 : strlcpy(conn->last_sqlstate, workBuf.data,
955 : : sizeof(conn->last_sqlstate));
956 [ + + ]: 336240 : else if (id == PG_DIAG_STATEMENT_POSITION)
957 : 7986 : have_position = true;
958 : : }
959 : :
960 : : /*
961 : : * Save the active query text, if any, into res as well; but only if we
962 : : * might need it for an error cursor display, which is only true if there
963 : : * is a PG_DIAG_STATEMENT_POSITION field.
964 : : */
1902 alvherre@alvh.no-ip. 965 [ + + + - : 49147 : if (have_position && res && conn->cmd_queue_head && conn->cmd_queue_head->query)
+ - + - ]
966 : 7986 : res->errQuery = pqResultStrdup(res, conn->cmd_queue_head->query);
967 : :
968 : : /*
969 : : * Now build the "overall" error message for PQresultErrorMessage.
970 : : */
8392 tgl@sss.pgh.pa.us 971 : 49147 : resetPQExpBuffer(&workBuf);
3709 972 : 49147 : pqBuildErrorMessage3(&workBuf, res, conn->verbosity, conn->show_context);
973 : :
974 : : /*
975 : : * Either save error as current async result, or just emit the notice.
976 : : */
977 [ + + ]: 49147 : if (isError)
978 : : {
2969 979 : 31650 : pqClearAsyncResult(conn); /* redundant, but be safe */
1562 980 [ + - ]: 31650 : if (res)
981 : : {
982 : 31650 : pqSetResultError(res, &workBuf, 0);
983 : 31650 : conn->result = res;
984 : : }
985 : : else
986 : : {
987 : : /* Fall back to using the internal-error processing paths */
1562 tgl@sss.pgh.pa.us 988 :UBC 0 : conn->error_result = true;
989 : : }
990 : :
3709 tgl@sss.pgh.pa.us 991 [ - + ]:CBC 31650 : if (PQExpBufferDataBroken(workBuf))
1292 peter@eisentraut.org 992 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
993 : : else
3709 tgl@sss.pgh.pa.us 994 :CBC 31650 : appendPQExpBufferStr(&conn->errorMessage, workBuf.data);
995 : : }
996 : : else
997 : : {
998 : : /* if we couldn't allocate the result set, just discard the NOTICE */
999 [ + - ]: 17497 : if (res)
1000 : : {
1001 : : /*
1002 : : * We can cheat a little here and not copy the message. But if we
1003 : : * were unlucky enough to run out of memory while filling workBuf,
1004 : : * insert "out of memory", as in pqSetResultError.
1005 : : */
1766 1006 [ - + ]: 17497 : if (PQExpBufferDataBroken(workBuf))
1766 tgl@sss.pgh.pa.us 1007 :UBC 0 : res->errMsg = libpq_gettext("out of memory\n");
1008 : : else
1766 tgl@sss.pgh.pa.us 1009 :CBC 17497 : res->errMsg = workBuf.data;
3709 1010 [ + - ]: 17497 : if (res->noticeHooks.noticeRec != NULL)
3187 peter_e@gmx.net 1011 : 17497 : res->noticeHooks.noticeRec(res->noticeHooks.noticeRecArg, res);
3709 tgl@sss.pgh.pa.us 1012 : 17497 : PQclear(res);
1013 : : }
1014 : : }
1015 : :
1016 : 49147 : termPQExpBuffer(&workBuf);
1017 : 49147 : return 0;
1018 : :
3709 tgl@sss.pgh.pa.us 1019 :UBC 0 : fail:
1020 : 0 : PQclear(res);
1021 : 0 : termPQExpBuffer(&workBuf);
1022 : 0 : return EOF;
1023 : : }
1024 : :
1025 : : /*
1026 : : * Construct an error message from the fields in the given PGresult,
1027 : : * appending it to the contents of "msg".
1028 : : */
1029 : : void
3709 tgl@sss.pgh.pa.us 1030 :CBC 49150 : pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res,
1031 : : PGVerbosity verbosity, PGContextVisibility show_context)
1032 : : {
1033 : : const char *val;
1034 : 49150 : const char *querytext = NULL;
1035 : 49150 : int querypos = 0;
1036 : :
1037 : : /* If we couldn't allocate a PGresult, just say "out of memory" */
1038 [ - + ]: 49150 : if (res == NULL)
1039 : : {
2522 drowley@postgresql.o 1040 :UBC 0 : appendPQExpBufferStr(msg, libpq_gettext("out of memory\n"));
3709 tgl@sss.pgh.pa.us 1041 : 0 : return;
1042 : : }
1043 : :
1044 : : /*
1045 : : * If we don't have any broken-down fields, just return the base message.
1046 : : * This mainly applies if we're given a libpq-generated error result.
1047 : : */
3709 tgl@sss.pgh.pa.us 1048 [ - + ]:CBC 49150 : if (res->errFields == NULL)
1049 : : {
3709 tgl@sss.pgh.pa.us 1050 [ # # # # ]:UBC 0 : if (res->errMsg && res->errMsg[0])
1051 : 0 : appendPQExpBufferStr(msg, res->errMsg);
1052 : : else
2522 drowley@postgresql.o 1053 : 0 : appendPQExpBufferStr(msg, libpq_gettext("no error message available\n"));
3709 tgl@sss.pgh.pa.us 1054 : 0 : return;
1055 : : }
1056 : :
1057 : : /* Else build error message from relevant fields */
8312 peter_e@gmx.net 1058 :CBC 49150 : val = PQresultErrorField(res, PG_DIAG_SEVERITY);
8379 tgl@sss.pgh.pa.us 1059 [ + - ]: 49150 : if (val)
3709 1060 : 49150 : appendPQExpBuffer(msg, "%s: ", val);
1061 : :
2613 1062 [ + + ]: 49150 : if (verbosity == PQERRORS_SQLSTATE)
1063 : : {
1064 : : /*
1065 : : * If we have a SQLSTATE, print that and nothing else. If not (which
1066 : : * shouldn't happen for server-generated errors, but might possibly
1067 : : * happen for libpq-generated ones), fall back to TERSE format, as
1068 : : * that seems better than printing nothing at all.
1069 : : */
1070 : 43 : val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
1071 [ + - ]: 43 : if (val)
1072 : : {
1073 : 43 : appendPQExpBuffer(msg, "%s\n", val);
1074 : 43 : return;
1075 : : }
2613 tgl@sss.pgh.pa.us 1076 :UBC 0 : verbosity = PQERRORS_TERSE;
1077 : : }
1078 : :
3709 tgl@sss.pgh.pa.us 1079 [ + + ]:CBC 49107 : if (verbosity == PQERRORS_VERBOSE)
1080 : : {
1081 : 3 : val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
1082 [ + - ]: 3 : if (val)
1083 : 3 : appendPQExpBuffer(msg, "%s: ", val);
1084 : : }
8312 peter_e@gmx.net 1085 : 49107 : val = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
8379 tgl@sss.pgh.pa.us 1086 [ + - ]: 49107 : if (val)
3709 1087 : 49107 : appendPQExpBufferStr(msg, val);
8312 peter_e@gmx.net 1088 : 49107 : val = PQresultErrorField(res, PG_DIAG_STATEMENT_POSITION);
8379 tgl@sss.pgh.pa.us 1089 [ + + ]: 49107 : if (val)
1090 : : {
3709 1091 [ + + + - ]: 7985 : if (verbosity != PQERRORS_TERSE && res->errQuery != NULL)
1092 : : {
1093 : : /* emit position as a syntax cursor display */
1094 : 7981 : querytext = res->errQuery;
7382 1095 : 7981 : querypos = atoi(val);
1096 : : }
1097 : : else
1098 : : {
1099 : : /* emit position as text addition to primary message */
1100 : : /* translator: %s represents a digit string */
3709 1101 : 4 : appendPQExpBuffer(msg, libpq_gettext(" at character %s"),
1102 : : val);
1103 : : }
1104 : : }
1105 : : else
1106 : : {
8105 1107 : 41122 : val = PQresultErrorField(res, PG_DIAG_INTERNAL_POSITION);
1108 [ + + ]: 41122 : if (val)
1109 : : {
7382 1110 : 57 : querytext = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
3709 1111 [ + - + - ]: 57 : if (verbosity != PQERRORS_TERSE && querytext != NULL)
1112 : : {
1113 : : /* emit position as a syntax cursor display */
7382 1114 : 57 : querypos = atoi(val);
1115 : : }
1116 : : else
1117 : : {
1118 : : /* emit position as text addition to primary message */
1119 : : /* translator: %s represents a digit string */
3709 tgl@sss.pgh.pa.us 1120 :UBC 0 : appendPQExpBuffer(msg, libpq_gettext(" at character %s"),
1121 : : val);
1122 : : }
1123 : : }
1124 : : }
3709 tgl@sss.pgh.pa.us 1125 :CBC 49107 : appendPQExpBufferChar(msg, '\n');
1126 [ + + ]: 49107 : if (verbosity != PQERRORS_TERSE)
1127 : : {
7382 1128 [ + + + - ]: 48790 : if (querytext && querypos > 0)
3709 1129 : 8038 : reportErrorPosition(msg, querytext, querypos,
1130 : 8038 : res->client_encoding);
8312 peter_e@gmx.net 1131 : 48790 : val = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
8379 tgl@sss.pgh.pa.us 1132 [ + + ]: 48790 : if (val)
3709 1133 : 8157 : appendPQExpBuffer(msg, libpq_gettext("DETAIL: %s\n"), val);
8312 peter_e@gmx.net 1134 : 48790 : val = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
8379 tgl@sss.pgh.pa.us 1135 [ + + ]: 48790 : if (val)
3709 1136 : 3199 : appendPQExpBuffer(msg, libpq_gettext("HINT: %s\n"), val);
8105 1137 : 48790 : val = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
1138 [ + + ]: 48790 : if (val)
3709 1139 : 57 : appendPQExpBuffer(msg, libpq_gettext("QUERY: %s\n"), val);
1140 [ + + + + ]: 48790 : if (show_context == PQSHOW_CONTEXT_ALWAYS ||
1141 : 48579 : (show_context == PQSHOW_CONTEXT_ERRORS &&
1142 [ + + ]: 48579 : res->resultStatus == PGRES_FATAL_ERROR))
1143 : : {
3920 1144 : 31586 : val = PQresultErrorField(res, PG_DIAG_CONTEXT);
1145 [ + + ]: 31586 : if (val)
3709 1146 : 1592 : appendPQExpBuffer(msg, libpq_gettext("CONTEXT: %s\n"),
1147 : : val);
1148 : : }
1149 : : }
1150 [ + + ]: 49107 : if (verbosity == PQERRORS_VERBOSE)
1151 : : {
4869 1152 : 3 : val = PQresultErrorField(res, PG_DIAG_SCHEMA_NAME);
1153 [ - + ]: 3 : if (val)
3709 tgl@sss.pgh.pa.us 1154 :UBC 0 : appendPQExpBuffer(msg,
4869 1155 : 0 : libpq_gettext("SCHEMA NAME: %s\n"), val);
4869 tgl@sss.pgh.pa.us 1156 :CBC 3 : val = PQresultErrorField(res, PG_DIAG_TABLE_NAME);
1157 [ - + ]: 3 : if (val)
3709 tgl@sss.pgh.pa.us 1158 :UBC 0 : appendPQExpBuffer(msg,
4869 1159 : 0 : libpq_gettext("TABLE NAME: %s\n"), val);
4869 tgl@sss.pgh.pa.us 1160 :CBC 3 : val = PQresultErrorField(res, PG_DIAG_COLUMN_NAME);
1161 [ - + ]: 3 : if (val)
3709 tgl@sss.pgh.pa.us 1162 :UBC 0 : appendPQExpBuffer(msg,
4869 1163 : 0 : libpq_gettext("COLUMN NAME: %s\n"), val);
4869 tgl@sss.pgh.pa.us 1164 :CBC 3 : val = PQresultErrorField(res, PG_DIAG_DATATYPE_NAME);
1165 [ - + ]: 3 : if (val)
3709 tgl@sss.pgh.pa.us 1166 :UBC 0 : appendPQExpBuffer(msg,
4869 1167 : 0 : libpq_gettext("DATATYPE NAME: %s\n"), val);
4869 tgl@sss.pgh.pa.us 1168 :CBC 3 : val = PQresultErrorField(res, PG_DIAG_CONSTRAINT_NAME);
1169 [ - + ]: 3 : if (val)
3709 tgl@sss.pgh.pa.us 1170 :UBC 0 : appendPQExpBuffer(msg,
4869 1171 : 0 : libpq_gettext("CONSTRAINT NAME: %s\n"), val);
1172 : : }
3709 tgl@sss.pgh.pa.us 1173 [ + + ]:CBC 49107 : if (verbosity == PQERRORS_VERBOSE)
1174 : : {
1175 : : const char *valf;
1176 : : const char *vall;
1177 : :
8312 peter_e@gmx.net 1178 : 3 : valf = PQresultErrorField(res, PG_DIAG_SOURCE_FILE);
1179 : 3 : vall = PQresultErrorField(res, PG_DIAG_SOURCE_LINE);
1180 : 3 : val = PQresultErrorField(res, PG_DIAG_SOURCE_FUNCTION);
8379 tgl@sss.pgh.pa.us 1181 [ - + - - : 3 : if (val || valf || vall)
- - ]
1182 : : {
3709 1183 : 3 : appendPQExpBufferStr(msg, libpq_gettext("LOCATION: "));
8379 1184 [ + - ]: 3 : if (val)
3709 1185 : 3 : appendPQExpBuffer(msg, libpq_gettext("%s, "), val);
8379 1186 [ + - + - ]: 3 : if (valf && vall) /* unlikely we'd have just one */
3709 1187 : 3 : appendPQExpBuffer(msg, libpq_gettext("%s:%s"),
1188 : : valf, vall);
1189 : 3 : appendPQExpBufferChar(msg, '\n');
1190 : : }
1191 : : }
1192 : : }
1193 : :
1194 : : /*
1195 : : * Add an error-location display to the error message under construction.
1196 : : *
1197 : : * The cursor location is measured in logical characters; the query string
1198 : : * is presumed to be in the specified encoding.
1199 : : */
1200 : : static void
7382 1201 : 8038 : reportErrorPosition(PQExpBuffer msg, const char *query, int loc, int encoding)
1202 : : {
1203 : : #define DISPLAY_SIZE 60 /* screen width limit, in screen cols */
1204 : : #define MIN_RIGHT_CUT 10 /* try to keep this far away from EOL */
1205 : :
1206 : : char *wquery;
1207 : : int slen,
1208 : : cno,
1209 : : i,
1210 : : *qidx,
1211 : : *scridx,
1212 : : qoffset,
1213 : : scroffset,
1214 : : ibeg,
1215 : : iend,
1216 : : loc_line;
1217 : : bool mb_encoding,
1218 : : beg_trunc,
1219 : : end_trunc;
1220 : :
1221 : : /* Convert loc from 1-based to 0-based; no-op if out of range */
7181 1222 : 8038 : loc--;
1223 [ - + ]: 8038 : if (loc < 0)
7181 tgl@sss.pgh.pa.us 1224 :UBC 0 : return;
1225 : :
1226 : : /* Need a writable copy of the query */
7382 tgl@sss.pgh.pa.us 1227 :CBC 8038 : wquery = strdup(query);
1228 [ - + ]: 8038 : if (wquery == NULL)
7382 tgl@sss.pgh.pa.us 1229 :UBC 0 : return; /* fail silently if out of memory */
1230 : :
1231 : : /*
1232 : : * Each character might occupy multiple physical bytes in the string, and
1233 : : * in some Far Eastern character sets it might take more than one screen
1234 : : * column as well. We compute the starting byte offset and starting
1235 : : * screen column of each logical character, and store these in qidx[] and
1236 : : * scridx[] respectively.
1237 : : */
1238 : :
1239 : : /*
1240 : : * We need a safe allocation size.
1241 : : *
1242 : : * The only caller of reportErrorPosition() is pqBuildErrorMessage3(); it
1243 : : * gets its query from either a PQresultErrorField() or a PGcmdQueueEntry,
1244 : : * both of which must have fit into conn->inBuffer/outBuffer. So slen fits
1245 : : * inside an int, but we can't assume that (slen * sizeof(int)) fits
1246 : : * inside a size_t.
1247 : : */
7181 tgl@sss.pgh.pa.us 1248 :CBC 8038 : slen = strlen(wquery) + 1;
201 jchampion@postgresql 1249 [ - + ]: 8038 : if (slen > SIZE_MAX / sizeof(int))
1250 : : {
201 jchampion@postgresql 1251 :UBC 0 : free(wquery);
1252 : 0 : return;
1253 : : }
1254 : :
7382 tgl@sss.pgh.pa.us 1255 :CBC 8038 : qidx = (int *) malloc(slen * sizeof(int));
1256 [ - + ]: 8038 : if (qidx == NULL)
1257 : : {
7382 tgl@sss.pgh.pa.us 1258 :UBC 0 : free(wquery);
1259 : 0 : return;
1260 : : }
7382 tgl@sss.pgh.pa.us 1261 :CBC 8038 : scridx = (int *) malloc(slen * sizeof(int));
1262 [ - + ]: 8038 : if (scridx == NULL)
1263 : : {
7382 tgl@sss.pgh.pa.us 1264 :UBC 0 : free(qidx);
1265 : 0 : free(wquery);
1266 : 0 : return;
1267 : : }
1268 : :
1269 : : /* We can optimize a bit if it's a single-byte encoding */
7181 tgl@sss.pgh.pa.us 1270 :CBC 8038 : mb_encoding = (pg_encoding_max_length(encoding) != 1);
1271 : :
1272 : : /*
1273 : : * Within the scanning loop, cno is the current character's logical
1274 : : * number, qoffset is its offset in wquery, and scroffset is its starting
1275 : : * logical screen column (all indexed from 0). "loc" is the logical
1276 : : * character number of the error location. We scan to determine loc_line
1277 : : * (the 1-based line number containing loc) and ibeg/iend (first character
1278 : : * number and last+1 character number of the line containing loc). Note
1279 : : * that qidx[] and scridx[] are filled only as far as iend.
1280 : : */
7382 1281 : 8038 : qoffset = 0;
1282 : 8038 : scroffset = 0;
7181 1283 : 8038 : loc_line = 1;
1284 : 8038 : ibeg = 0;
1285 : 8038 : iend = -1; /* -1 means not set yet */
1286 : :
1287 [ + + ]: 475139 : for (cno = 0; wquery[qoffset] != '\0'; cno++)
1288 : : {
7178 bruce@momjian.us 1289 : 468107 : char ch = wquery[qoffset];
1290 : :
7181 tgl@sss.pgh.pa.us 1291 : 468107 : qidx[cno] = qoffset;
1292 : 468107 : scridx[cno] = scroffset;
1293 : :
1294 : : /*
1295 : : * Replace tabs with spaces in the writable copy. (Later we might
1296 : : * want to think about coping with their variable screen width, but
1297 : : * not today.)
1298 : : */
1299 [ + + ]: 468107 : if (ch == '\t')
1300 : 652 : wquery[qoffset] = ' ';
1301 : :
1302 : : /*
1303 : : * If end-of-line, count lines and mark positions. Each \r or \n
1304 : : * counts as a line except when \r \n appear together.
1305 : : */
1306 [ + - + + ]: 467455 : else if (ch == '\r' || ch == '\n')
1307 : : {
1308 [ + + ]: 3327 : if (cno < loc)
1309 : : {
1310 [ + - + + ]: 2321 : if (ch == '\r' ||
1311 : 2317 : cno == 0 ||
1312 [ + - ]: 2317 : wquery[qidx[cno - 1]] != '\r')
1313 : 2321 : loc_line++;
1314 : : /* extract beginning = last line start before loc. */
1315 : 2321 : ibeg = cno + 1;
1316 : : }
1317 : : else
1318 : : {
1319 : : /* set extract end. */
1320 : 1006 : iend = cno;
1321 : : /* done scanning. */
1322 : 1006 : break;
1323 : : }
1324 : : }
1325 : :
1326 : : /* Advance */
1327 [ + + ]: 467101 : if (mb_encoding)
1328 : : {
1329 : : int w;
1330 : :
1331 : 466913 : w = pg_encoding_dsplen(encoding, &wquery[qoffset]);
1332 : : /* treat any non-tab control chars as width 1 */
1333 [ + + ]: 466913 : if (w <= 0)
1334 : 2321 : w = 1;
1335 : 466913 : scroffset += w;
1818 1336 : 466913 : qoffset += PQmblenBounded(&wquery[qoffset], encoding);
1337 : : }
1338 : : else
1339 : : {
1340 : : /* We assume wide chars only exist in multibyte encodings */
7181 1341 : 188 : scroffset++;
1342 : 188 : qoffset++;
1343 : : }
1344 : : }
1345 : : /* Fix up if we didn't find an end-of-line after loc */
1346 [ + + ]: 8038 : if (iend < 0)
1347 : : {
1348 : 7032 : iend = cno; /* query length in chars, +1 */
1349 : 7032 : qidx[iend] = qoffset;
1350 : 7032 : scridx[iend] = scroffset;
1351 : : }
1352 : :
1353 : : /* Print only if loc is within computed query length */
1354 [ + + ]: 8038 : if (loc <= cno)
1355 : : {
1356 : : /* If the line extracted is too long, we truncate it. */
7382 1357 : 8026 : beg_trunc = false;
1358 : 8026 : end_trunc = false;
1359 [ + + ]: 8026 : if (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1360 : : {
1361 : : /*
1362 : : * We first truncate right if it is enough. This code might be
1363 : : * off a space or so on enforcing MIN_RIGHT_CUT if there's a wide
1364 : : * character right there, but that should be okay.
1365 : : */
1366 [ + + ]: 2340 : if (scridx[ibeg] + DISPLAY_SIZE >= scridx[loc] + MIN_RIGHT_CUT)
1367 : : {
1368 [ + + ]: 25403 : while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1369 : 23976 : iend--;
1370 : 1427 : end_trunc = true;
1371 : : }
1372 : : else
1373 : : {
1374 : : /* Truncate right if not too close to loc. */
1375 [ + + ]: 11773 : while (scridx[loc] + MIN_RIGHT_CUT < scridx[iend])
1376 : : {
1377 : 10860 : iend--;
1378 : 10860 : end_trunc = true;
1379 : : }
1380 : :
1381 : : /* Truncate left if still too long. */
1382 [ + + ]: 19521 : while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1383 : : {
1384 : 18608 : ibeg++;
1385 : 18608 : beg_trunc = true;
1386 : : }
1387 : : }
1388 : : }
1389 : :
1390 : : /* truncate working copy at desired endpoint */
1391 : 8026 : wquery[qidx[iend]] = '\0';
1392 : :
1393 : : /* Begin building the finished message. */
1394 : 8026 : i = msg->len;
1395 : 8026 : appendPQExpBuffer(msg, libpq_gettext("LINE %d: "), loc_line);
1396 [ + + ]: 8026 : if (beg_trunc)
1397 : 913 : appendPQExpBufferStr(msg, "...");
1398 : :
1399 : : /*
1400 : : * While we have the prefix in the msg buffer, compute its screen
1401 : : * width.
1402 : : */
1403 : 8026 : scroffset = 0;
1818 1404 [ + + ]: 74985 : for (; i < msg->len; i += PQmblenBounded(&msg->data[i], encoding))
1405 : : {
7178 bruce@momjian.us 1406 : 66959 : int w = pg_encoding_dsplen(encoding, &msg->data[i]);
1407 : :
7382 tgl@sss.pgh.pa.us 1408 [ - + ]: 66959 : if (w <= 0)
7382 tgl@sss.pgh.pa.us 1409 :UBC 0 : w = 1;
7382 tgl@sss.pgh.pa.us 1410 :CBC 66959 : scroffset += w;
1411 : : }
1412 : :
1413 : : /* Finish up the LINE message line. */
1414 : 8026 : appendPQExpBufferStr(msg, &wquery[qidx[ibeg]]);
1415 [ + + ]: 8026 : if (end_trunc)
1416 : 2094 : appendPQExpBufferStr(msg, "...");
1417 : 8026 : appendPQExpBufferChar(msg, '\n');
1418 : :
1419 : : /* Now emit the cursor marker line. */
1420 : 8026 : scroffset += scridx[loc] - scridx[ibeg];
1421 [ + + ]: 259624 : for (i = 0; i < scroffset; i++)
1422 : 251598 : appendPQExpBufferChar(msg, ' ');
1423 : 8026 : appendPQExpBufferChar(msg, '^');
1424 : 8026 : appendPQExpBufferChar(msg, '\n');
1425 : : }
1426 : :
1427 : : /* Clean up. */
1428 : 8038 : free(scridx);
1429 : 8038 : free(qidx);
1430 : 8038 : free(wquery);
1431 : : }
1432 : :
1433 : :
1434 : : /*
1435 : : * Attempt to read a NegotiateProtocolVersion message. Sets conn->pversion
1436 : : * to the version that's negotiated by the server.
1437 : : *
1438 : : * Entry: 'v' message type and length have already been consumed.
1439 : : * Exit: returns 0 if successfully consumed message.
1440 : : * returns 1 on failure. The error message is filled in.
1441 : : */
1442 : : int
1290 peter@eisentraut.org 1443 :GBC 15607 : pqGetNegotiateProtocolVersion3(PGconn *conn)
1444 : : {
1445 : : int their_version;
1446 : : int num;
1447 : : bool found_test_protocol_negotiation;
1448 : : bool expect_test_protocol_negotiation;
1449 : :
1450 : : /*
1451 : : * During 19beta only, if protocol grease is in use, assume that it's the
1452 : : * cause of any invalid messages encountered below. We'll print extra
1453 : : * information for the end user in that case.
1454 : : */
96 jchampion@postgresql 1455 :GNC 15607 : bool need_grease_info = (conn->max_pversion == PG_PROTOCOL_GREASE);
1456 : :
423 heikki.linnakangas@i 1457 [ - + ]:GBC 15607 : if (pqGetInt(&their_version, 4, conn) != 0)
423 heikki.linnakangas@i 1458 :UBC 0 : goto eof;
1459 : :
1290 peter@eisentraut.org 1460 [ - + ]:GBC 15607 : if (pqGetInt(&num, 4, conn) != 0)
423 heikki.linnakangas@i 1461 :UBC 0 : goto eof;
1462 : :
1463 : : /*
1464 : : * Check the protocol version.
1465 : : *
1466 : : * PG_PROTOCOL_GREASE is intentionally unsupported and reserved. It's
1467 : : * higher than any real version, so check for that first, to get the most
1468 : : * specific error message. Then check the upper and lower bounds.
1469 : : */
113 jchampion@postgresql 1470 [ - + ]:GNC 15607 : if (their_version == PG_PROTOCOL_GREASE)
1471 : : {
113 jchampion@postgresql 1472 :UNC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server requested \"grease\" protocol version 3.9999");
1473 : 0 : goto failure;
1474 : : }
1475 : :
423 heikki.linnakangas@i 1476 [ - + ]:GBC 15607 : if (their_version > conn->pversion)
1477 : : {
423 heikki.linnakangas@i 1478 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server requested downgrade to a higher-numbered version");
1479 : 0 : goto failure;
1480 : : }
1481 : :
423 heikki.linnakangas@i 1482 [ - + ]:GBC 15607 : if (their_version < PG_PROTOCOL(3, 0))
1483 : : {
423 heikki.linnakangas@i 1484 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server requested downgrade to pre-3.0 protocol version");
1485 : 0 : goto failure;
1486 : : }
1487 : :
1488 : : /* 3.1 never existed, we went straight from 3.0 to 3.2 */
127 jchampion@postgresql 1489 [ - + ]:GNC 15607 : if (their_version == PG_PROTOCOL_RESERVED_31)
1490 : : {
338 peter@eisentraut.org 1491 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server requested downgrade to non-existent 3.1 protocol version");
423 heikki.linnakangas@i 1492 : 0 : goto failure;
1493 : : }
1494 : :
423 heikki.linnakangas@i 1495 [ - + ]:GBC 15607 : if (num < 0)
1496 : : {
423 heikki.linnakangas@i 1497 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server reported negative number of unsupported parameters");
1498 : 0 : goto failure;
1499 : : }
1500 : :
423 heikki.linnakangas@i 1501 [ - + - - ]:GBC 15607 : if (their_version == conn->pversion && num == 0)
1502 : : {
423 heikki.linnakangas@i 1503 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server negotiated but asks for no changes");
1504 : 0 : goto failure;
1505 : : }
1506 : :
423 heikki.linnakangas@i 1507 [ - + ]:GBC 15607 : if (their_version < conn->min_pversion)
1508 : : {
338 peter@eisentraut.org 1509 :UBC 0 : libpq_append_conn_error(conn, "server only supports protocol version %d.%d, but \"%s\" was set to %d.%d",
1510 : : PG_PROTOCOL_MAJOR(their_version),
1511 : : PG_PROTOCOL_MINOR(their_version),
1512 : : "min_protocol_version",
423 heikki.linnakangas@i 1513 : 0 : PG_PROTOCOL_MAJOR(conn->min_pversion),
1514 : 0 : PG_PROTOCOL_MINOR(conn->min_pversion));
1515 : :
96 jchampion@postgresql 1516 :UNC 0 : need_grease_info = false; /* this is valid server behavior */
423 heikki.linnakangas@i 1517 :UBC 0 : goto failure;
1518 : : }
1519 : :
1520 : : /* the version is acceptable */
423 heikki.linnakangas@i 1521 :GBC 15607 : conn->pversion = their_version;
1522 : :
1523 : : /*
1524 : : * Check that all expected unsupported parameters are reported by the
1525 : : * server.
1526 : : */
96 jchampion@postgresql 1527 :GNC 15607 : found_test_protocol_negotiation = false;
1528 : 15607 : expect_test_protocol_negotiation = (conn->max_pversion == PG_PROTOCOL_GREASE);
1529 : :
423 heikki.linnakangas@i 1530 [ + + ]:GBC 31214 : for (int i = 0; i < num; i++)
1531 : : {
1532 [ - + ]: 15607 : if (pqGets(&conn->workBuffer, conn))
1533 : : {
423 heikki.linnakangas@i 1534 :UBC 0 : goto eof;
1535 : : }
423 heikki.linnakangas@i 1536 [ - + ]:GBC 15607 : if (strncmp(conn->workBuffer.data, "_pq_.", 5) != 0)
1537 : : {
338 peter@eisentraut.org 1538 :UBC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server reported unsupported parameter name without a \"%s\" prefix (\"%s\")", "_pq_.", conn->workBuffer.data);
423 heikki.linnakangas@i 1539 : 0 : goto failure;
1540 : : }
1541 : :
1542 : : /* Check if this is the expected test parameter */
96 jchampion@postgresql 1543 [ + - ]:GNC 15607 : if (expect_test_protocol_negotiation &&
1544 [ + - ]: 15607 : strcmp(conn->workBuffer.data, "_pq_.test_protocol_negotiation") == 0)
1545 : : {
1546 : 15607 : found_test_protocol_negotiation = true;
1547 : : }
1548 : : else
1549 : : {
96 jchampion@postgresql 1550 :UNC 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: server reported an unsupported parameter that was not requested (\"%s\")",
1551 : : conn->workBuffer.data);
1552 : 0 : goto failure;
1553 : : }
1554 : : }
1555 : :
1556 : : /*
1557 : : * If we requested protocol grease, the server must report
1558 : : * _pq_.test_protocol_negotiation as unsupported. This ensures
1559 : : * comprehensive NegotiateProtocolVersion implementation.
1560 : : */
96 jchampion@postgresql 1561 [ + - - + ]:GNC 15607 : if (expect_test_protocol_negotiation && !found_test_protocol_negotiation)
1562 : : {
1 jchampion@postgresql 1563 :UNC 0 : libpq_append_conn_error(conn, "server did not report the unsupported \"%s\" parameter in its protocol negotiation message",
1564 : : "_pq_.test_protocol_negotiation");
423 heikki.linnakangas@i 1565 :UBC 0 : goto failure;
1566 : : }
1567 : :
1290 peter@eisentraut.org 1568 :GBC 15607 : return 0;
1569 : :
423 heikki.linnakangas@i 1570 :UBC 0 : eof:
406 michael@paquier.xyz 1571 : 0 : libpq_append_conn_error(conn, "received invalid protocol negotiation message: message too short");
423 heikki.linnakangas@i 1572 : 0 : failure:
96 jchampion@postgresql 1573 [ # # ]:UNC 0 : if (need_grease_info)
1574 : 0 : libpq_append_grease_info(conn);
423 heikki.linnakangas@i 1575 :UBC 0 : conn->asyncStatus = PGASYNC_READY;
1576 : 0 : pqSaveErrorResult(conn);
1577 : 0 : return 1;
1578 : : }
1579 : :
1580 : :
1581 : : /*
1582 : : * Attempt to read a ParameterStatus message.
1583 : : * This is possible in several places, so we break it out as a subroutine.
1584 : : *
1585 : : * Entry: 'S' message type and length have already been consumed.
1586 : : * Exit: returns 0 if successfully consumed message.
1587 : : * returns EOF if not enough data.
1588 : : */
1589 : : static int
8392 tgl@sss.pgh.pa.us 1590 :CBC 236990 : getParameterStatus(PGconn *conn)
1591 : : {
1592 : : PQExpBufferData valueBuf;
1593 : :
1594 : : /* Get the parameter name */
1595 [ - + ]: 236990 : if (pqGets(&conn->workBuffer, conn))
8392 tgl@sss.pgh.pa.us 1596 :UBC 0 : return EOF;
1597 : : /* Get the parameter value (could be large) */
8392 tgl@sss.pgh.pa.us 1598 :CBC 236990 : initPQExpBuffer(&valueBuf);
1599 [ - + ]: 236990 : if (pqGets(&valueBuf, conn))
1600 : : {
8392 tgl@sss.pgh.pa.us 1601 :UBC 0 : termPQExpBuffer(&valueBuf);
1602 : 0 : return EOF;
1603 : : }
1604 : : /* And save it */
281 heikki.linnakangas@i 1605 [ - + ]:CBC 236990 : if (!pqSaveParameterStatus(conn, conn->workBuffer.data, valueBuf.data))
1606 : : {
281 heikki.linnakangas@i 1607 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
1608 : 0 : handleFatalError(conn);
1609 : : }
8392 tgl@sss.pgh.pa.us 1610 :CBC 236990 : termPQExpBuffer(&valueBuf);
1611 : 236990 : return 0;
1612 : : }
1613 : :
1614 : : /*
1615 : : * parseInput subroutine to read a BackendKeyData message.
1616 : : * Entry: 'K' message type and length have already been consumed.
1617 : : * Exit: returns 0 if successfully consumed message.
1618 : : * returns EOF if not enough data.
1619 : : */
1620 : : static int
423 heikki.linnakangas@i 1621 : 15178 : getBackendKeyData(PGconn *conn, int msgLength)
1622 : : {
1623 : : int cancel_key_len;
1624 : :
1625 [ - + ]: 15178 : if (conn->be_cancel_key)
1626 : : {
423 heikki.linnakangas@i 1627 :UBC 0 : free(conn->be_cancel_key);
1628 : 0 : conn->be_cancel_key = NULL;
1629 : 0 : conn->be_cancel_key_len = 0;
1630 : : }
1631 : :
423 heikki.linnakangas@i 1632 [ - + ]:CBC 15178 : if (pqGetInt(&(conn->be_pid), 4, conn))
423 heikki.linnakangas@i 1633 :UBC 0 : return EOF;
1634 : :
423 heikki.linnakangas@i 1635 :CBC 15178 : cancel_key_len = 5 + msgLength - (conn->inCursor - conn->inStart);
1636 : :
281 1637 [ + + - + ]: 15178 : if (cancel_key_len != 4 && conn->pversion == PG_PROTOCOL(3, 0))
1638 : : {
281 heikki.linnakangas@i 1639 :UBC 0 : libpq_append_conn_error(conn, "received invalid BackendKeyData message: cancel key with length %d not allowed in protocol version 3.0 (must be 4 bytes)", cancel_key_len);
1640 : 0 : handleFatalError(conn);
1641 : 0 : return 0;
1642 : : }
1643 : :
281 heikki.linnakangas@i 1644 [ - + ]:CBC 15178 : if (cancel_key_len < 4)
1645 : : {
281 heikki.linnakangas@i 1646 :UBC 0 : libpq_append_conn_error(conn, "received invalid BackendKeyData message: cancel key with length %d is too short (minimum 4 bytes)", cancel_key_len);
1647 : 0 : handleFatalError(conn);
1648 : 0 : return 0;
1649 : : }
1650 : :
281 heikki.linnakangas@i 1651 [ - + ]:CBC 15178 : if (cancel_key_len > 256)
1652 : : {
281 heikki.linnakangas@i 1653 :UBC 0 : libpq_append_conn_error(conn, "received invalid BackendKeyData message: cancel key with length %d is too long (maximum 256 bytes)", cancel_key_len);
1654 : 0 : handleFatalError(conn);
1655 : 0 : return 0;
1656 : : }
1657 : :
423 heikki.linnakangas@i 1658 :CBC 15178 : conn->be_cancel_key = malloc(cancel_key_len);
1659 [ - + ]: 15178 : if (conn->be_cancel_key == NULL)
1660 : : {
423 heikki.linnakangas@i 1661 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
281 1662 : 0 : handleFatalError(conn);
1663 : 0 : return 0;
1664 : : }
423 heikki.linnakangas@i 1665 [ - + ]:CBC 15178 : if (pqGetnchar(conn->be_cancel_key, cancel_key_len, conn))
1666 : : {
423 heikki.linnakangas@i 1667 :UBC 0 : free(conn->be_cancel_key);
1668 : 0 : conn->be_cancel_key = NULL;
1669 : 0 : return EOF;
1670 : : }
423 heikki.linnakangas@i 1671 :CBC 15178 : conn->be_cancel_key_len = cancel_key_len;
1672 : 15178 : return 0;
1673 : : }
1674 : :
1675 : :
1676 : : /*
1677 : : * Attempt to read a Notify response message.
1678 : : * This is possible in several places, so we break it out as a subroutine.
1679 : : *
1680 : : * Entry: 'A' message type and length have already been consumed.
1681 : : * Exit: returns 0 if successfully consumed Notify message.
1682 : : * returns EOF if not enough data.
1683 : : */
1684 : : static int
8392 tgl@sss.pgh.pa.us 1685 : 53 : getNotify(PGconn *conn)
1686 : : {
1687 : : int be_pid;
1688 : : char *svname;
1689 : : int nmlen;
1690 : : int extralen;
1691 : : PGnotify *newNotify;
1692 : :
1693 [ - + ]: 53 : if (pqGetInt(&be_pid, 4, conn))
8392 tgl@sss.pgh.pa.us 1694 :UBC 0 : return EOF;
8392 tgl@sss.pgh.pa.us 1695 [ - + ]:CBC 53 : if (pqGets(&conn->workBuffer, conn))
8392 tgl@sss.pgh.pa.us 1696 :UBC 0 : return EOF;
1697 : : /* must save name while getting extra string */
8379 tgl@sss.pgh.pa.us 1698 :CBC 53 : svname = strdup(conn->workBuffer.data);
1699 [ - + ]: 53 : if (!svname)
1700 : : {
1701 : : /*
1702 : : * Notify messages can arrive at any state, so we cannot associate the
1703 : : * error with any particular query. There's no way to return back an
1704 : : * "async error", so the best we can do is drop the connection. That
1705 : : * seems better than silently ignoring the notification.
1706 : : */
281 heikki.linnakangas@i 1707 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
1708 : 0 : handleFatalError(conn);
1709 : 0 : return 0;
1710 : : }
8379 tgl@sss.pgh.pa.us 1711 [ - + ]:CBC 53 : if (pqGets(&conn->workBuffer, conn))
1712 : : {
281 heikki.linnakangas@i 1713 :UBC 0 : free(svname);
8379 tgl@sss.pgh.pa.us 1714 : 0 : return EOF;
1715 : : }
1716 : :
1717 : : /*
1718 : : * Store the strings right after the PGnotify structure so it can all be
1719 : : * freed at once. We don't use NAMEDATALEN because we don't want to tie
1720 : : * this interface to a specific server name length.
1721 : : */
8379 tgl@sss.pgh.pa.us 1722 :CBC 53 : nmlen = strlen(svname);
1723 : 53 : extralen = strlen(conn->workBuffer.data);
1724 : 53 : newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + extralen + 2);
281 heikki.linnakangas@i 1725 [ - + ]: 53 : if (!newNotify)
1726 : : {
281 heikki.linnakangas@i 1727 :UBC 0 : free(svname);
1728 : 0 : libpq_append_conn_error(conn, "out of memory");
1729 : 0 : handleFatalError(conn);
1730 : 0 : return 0;
1731 : : }
1732 : :
281 heikki.linnakangas@i 1733 :CBC 53 : newNotify->relname = (char *) newNotify + sizeof(PGnotify);
1734 : 53 : strcpy(newNotify->relname, svname);
1735 : 53 : newNotify->extra = newNotify->relname + nmlen + 1;
1736 : 53 : strcpy(newNotify->extra, conn->workBuffer.data);
1737 : 53 : newNotify->be_pid = be_pid;
1738 : 53 : newNotify->next = NULL;
1739 [ + + ]: 53 : if (conn->notifyTail)
1740 : 29 : conn->notifyTail->next = newNotify;
1741 : : else
1742 : 24 : conn->notifyHead = newNotify;
1743 : 53 : conn->notifyTail = newNotify;
1744 : :
8379 tgl@sss.pgh.pa.us 1745 : 53 : free(svname);
1746 : 53 : return 0;
1747 : : }
1748 : :
1749 : : /*
1750 : : * getCopyStart - process CopyInResponse, CopyOutResponse or
1751 : : * CopyBothResponse message
1752 : : *
1753 : : * parseInput already read the message type and length.
1754 : : */
1755 : : static int
1756 : 6855 : getCopyStart(PGconn *conn, ExecStatusType copytype)
1757 : : {
1758 : : PGresult *result;
1759 : : int nfields;
1760 : : int i;
1761 : :
1762 : 6855 : result = PQmakeEmptyPGresult(conn, copytype);
7657 neilc@samurai.com 1763 [ - + ]: 6855 : if (!result)
7657 neilc@samurai.com 1764 :UBC 0 : goto failure;
1765 : :
8379 tgl@sss.pgh.pa.us 1766 [ - + ]:CBC 6855 : if (pqGetc(&conn->copy_is_binary, conn))
7657 neilc@samurai.com 1767 :UBC 0 : goto failure;
8379 tgl@sss.pgh.pa.us 1768 :CBC 6855 : result->binary = conn->copy_is_binary;
1769 : : /* the next two bytes are the number of fields */
1770 [ - + ]: 6855 : if (pqGetInt(&(result->numAttributes), 2, conn))
7657 neilc@samurai.com 1771 :UBC 0 : goto failure;
8379 tgl@sss.pgh.pa.us 1772 :CBC 6855 : nfields = result->numAttributes;
1773 : :
1774 : : /* allocate space for the attribute descriptors */
1775 [ + + ]: 6855 : if (nfields > 0)
1776 : : {
1777 : 5742 : result->attDescs = (PGresAttDesc *)
3209 peter_e@gmx.net 1778 : 5742 : pqResultAlloc(result, nfields * sizeof(PGresAttDesc), true);
7657 neilc@samurai.com 1779 [ - + ]: 5742 : if (!result->attDescs)
7657 neilc@samurai.com 1780 :UBC 0 : goto failure;
7657 neilc@samurai.com 1781 [ + - + - :CBC 62478 : MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
+ - + + +
+ ]
1782 : : }
1783 : :
8379 tgl@sss.pgh.pa.us 1784 [ + + ]: 25845 : for (i = 0; i < nfields; i++)
1785 : : {
1786 : : int format;
1787 : :
1788 [ - + ]: 18990 : if (pqGetInt(&format, 2, conn))
7657 neilc@samurai.com 1789 :UBC 0 : goto failure;
1790 : :
1791 : : /*
1792 : : * Since pqGetInt treats 2-byte integers as unsigned, we need to
1793 : : * coerce these results to signed form.
1794 : : */
8379 tgl@sss.pgh.pa.us 1795 :CBC 18990 : format = (int) ((int16) format);
1796 : 18990 : result->attDescs[i].format = format;
1797 : : }
1798 : :
1799 : : /* Success! */
1800 : 6855 : conn->result = result;
8392 1801 : 6855 : return 0;
1802 : :
7657 neilc@samurai.com 1803 :UBC 0 : failure:
1804 : 0 : PQclear(result);
1805 : 0 : return EOF;
1806 : : }
1807 : :
1808 : : /*
1809 : : * getReadyForQuery - process ReadyForQuery message
1810 : : */
1811 : : static int
8379 tgl@sss.pgh.pa.us 1812 :CBC 428598 : getReadyForQuery(PGconn *conn)
1813 : : {
1814 : : char xact_status;
1815 : :
1816 [ - + ]: 428598 : if (pqGetc(&xact_status, conn))
8379 tgl@sss.pgh.pa.us 1817 :UBC 0 : return EOF;
8379 tgl@sss.pgh.pa.us 1818 [ + + + - ]:CBC 428598 : switch (xact_status)
1819 : : {
1820 : 328955 : case 'I':
1821 : 328955 : conn->xactStatus = PQTRANS_IDLE;
1822 : 328955 : break;
1823 : 98440 : case 'T':
1824 : 98440 : conn->xactStatus = PQTRANS_INTRANS;
1825 : 98440 : break;
1826 : 1203 : case 'E':
1827 : 1203 : conn->xactStatus = PQTRANS_INERROR;
1828 : 1203 : break;
8379 tgl@sss.pgh.pa.us 1829 :UBC 0 : default:
1830 : 0 : conn->xactStatus = PQTRANS_UNKNOWN;
1831 : 0 : break;
1832 : : }
1833 : :
8379 tgl@sss.pgh.pa.us 1834 :CBC 428598 : return 0;
1835 : : }
1836 : :
1837 : : /*
1838 : : * getCopyDataMessage - fetch next CopyData message, process async messages
1839 : : *
1840 : : * Returns length word of CopyData message (> 0), or 0 if no complete
1841 : : * message available, -1 if end of copy, -2 if error.
1842 : : */
1843 : : static int
6711 1844 : 3064242 : getCopyDataMessage(PGconn *conn)
1845 : : {
1846 : : char id;
1847 : : int msgLength;
1848 : : int avail;
1849 : :
1850 : : for (;;)
1851 : : {
1852 : : /*
1853 : : * Do we have the next input message? To make life simpler for async
1854 : : * callers, we keep returning 0 until the next message is fully
1855 : : * available, even if it is not Copy Data.
1856 : : */
8379 1857 : 3064277 : conn->inCursor = conn->inStart;
1858 [ + + ]: 3064277 : if (pqGetc(&id, conn))
6711 1859 : 248308 : return 0;
8379 1860 [ + + ]: 2815969 : if (pqGetInt(&msgLength, 4, conn))
6711 1861 : 761 : return 0;
1862 [ - + ]: 2815208 : if (msgLength < 4)
1863 : : {
6711 tgl@sss.pgh.pa.us 1864 :UBC 0 : handleSyncLoss(conn, id, msgLength);
1865 : 0 : return -2;
1866 : : }
8379 tgl@sss.pgh.pa.us 1867 :CBC 2815208 : avail = conn->inEnd - conn->inCursor;
1868 [ + + ]: 2815208 : if (avail < msgLength - 4)
1869 : : {
1870 : : /*
1871 : : * Before returning, enlarge the input buffer if needed to hold
1872 : : * the whole message. See notes in parseInput.
1873 : : */
6575 1874 [ - + ]: 239521 : if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength - 4,
1875 : : conn))
1876 : : {
1877 : : /*
1878 : : * Abandon the connection. There's not much else we can
1879 : : * safely do; we can't just ignore the message or we could
1880 : : * miss important changes to the connection state.
1881 : : * pqCheckInBufferSpace() already reported the error.
1882 : : */
281 heikki.linnakangas@i 1883 :UBC 0 : handleFatalError(conn);
6708 tgl@sss.pgh.pa.us 1884 : 0 : return -2;
1885 : : }
6711 tgl@sss.pgh.pa.us 1886 :CBC 239521 : return 0;
1887 : : }
1888 : :
1889 : : /*
1890 : : * If it's a legitimate async message type, process it. (NOTIFY
1891 : : * messages are not currently possible here, but we handle them for
1892 : : * completeness.) Otherwise, if it's anything except Copy Data,
1893 : : * report end-of-copy.
1894 : : */
1895 [ - + - + : 2575687 : switch (id)
+ + ]
1896 : : {
1012 nathan@postgresql.or 1897 :UBC 0 : case PqMsg_NotificationResponse:
6711 tgl@sss.pgh.pa.us 1898 [ # # ]: 0 : if (getNotify(conn))
1899 : 0 : return 0;
1900 : 0 : break;
1012 nathan@postgresql.or 1901 :CBC 35 : case PqMsg_NoticeResponse:
6711 tgl@sss.pgh.pa.us 1902 [ - + ]: 35 : if (pqGetErrorNotice3(conn, false))
6711 tgl@sss.pgh.pa.us 1903 :UBC 0 : return 0;
6711 tgl@sss.pgh.pa.us 1904 :CBC 35 : break;
1012 nathan@postgresql.or 1905 :UBC 0 : case PqMsg_ParameterStatus:
6711 tgl@sss.pgh.pa.us 1906 [ # # ]: 0 : if (getParameterStatus(conn))
1907 : 0 : return 0;
1908 : 0 : break;
1012 nathan@postgresql.or 1909 :CBC 2570060 : case PqMsg_CopyData:
6711 tgl@sss.pgh.pa.us 1910 : 2570060 : return msgLength;
1012 nathan@postgresql.or 1911 : 5532 : case PqMsg_CopyDone:
1912 : :
1913 : : /*
1914 : : * If this is a CopyDone message, exit COPY_OUT mode and let
1915 : : * caller read status with PQgetResult(). If we're in
1916 : : * COPY_BOTH mode, return to COPY_IN mode.
1917 : : */
4779 rhaas@postgresql.org 1918 [ + + ]: 5532 : if (conn->asyncStatus == PGASYNC_COPY_BOTH)
1919 : 14 : conn->asyncStatus = PGASYNC_COPY_IN;
1920 : : else
1921 : 5518 : conn->asyncStatus = PGASYNC_BUSY;
1922 : 5532 : return -1;
6711 tgl@sss.pgh.pa.us 1923 : 60 : default: /* treat as end of copy */
1924 : :
1925 : : /*
1926 : : * Any other message terminates either COPY_IN or COPY_BOTH
1927 : : * mode.
1928 : : */
4779 rhaas@postgresql.org 1929 : 60 : conn->asyncStatus = PGASYNC_BUSY;
6711 tgl@sss.pgh.pa.us 1930 : 60 : return -1;
1931 : : }
1932 : :
1933 : : /* Drop the processed message and loop around for another */
652 alvherre@alvh.no-ip. 1934 : 35 : pqParseDone(conn, conn->inCursor);
1935 : : }
1936 : : }
1937 : :
1938 : : /*
1939 : : * PQgetCopyData - read a row of data from the backend during COPY OUT
1940 : : * or COPY BOTH
1941 : : *
1942 : : * If successful, sets *buffer to point to a malloc'd row of data, and
1943 : : * returns row length (always > 0) as result.
1944 : : * Returns 0 if no row available yet (only possible if async is true),
1945 : : * -1 if end of copy (consult PQgetResult), or -2 if error (consult
1946 : : * PQerrorMessage).
1947 : : */
1948 : : int
6711 tgl@sss.pgh.pa.us 1949 : 2936782 : pqGetCopyData3(PGconn *conn, char **buffer, int async)
1950 : : {
1951 : : int msgLength;
1952 : :
1953 : : for (;;)
1954 : : {
1955 : : /*
1956 : : * Collect the next input message. To make life simpler for async
1957 : : * callers, we keep returning 0 until the next message is fully
1958 : : * available, even if it is not Copy Data.
1959 : : */
1960 : 3064242 : msgLength = getCopyDataMessage(conn);
1961 [ + + ]: 3064242 : if (msgLength < 0)
6197 bruce@momjian.us 1962 : 5592 : return msgLength; /* end-of-copy or error */
6711 tgl@sss.pgh.pa.us 1963 [ + + ]: 3058650 : if (msgLength == 0)
1964 : : {
1965 : : /* Don't block if async read requested */
1966 [ + + ]: 488590 : if (async)
1967 : 361130 : return 0;
1968 : : /* Need to load more data */
3209 peter_e@gmx.net 1969 [ + - - + ]: 254920 : if (pqWait(true, false, conn) ||
6711 tgl@sss.pgh.pa.us 1970 : 127460 : pqReadData(conn) < 0)
6711 tgl@sss.pgh.pa.us 1971 :UBC 0 : return -2;
6711 tgl@sss.pgh.pa.us 1972 :CBC 127460 : continue;
1973 : : }
1974 : :
1975 : : /*
1976 : : * Drop zero-length messages (shouldn't happen anyway). Otherwise
1977 : : * pass the data back to the caller.
1978 : : */
8379 1979 : 2570060 : msgLength -= 4;
1980 [ + - ]: 2570060 : if (msgLength > 0)
1981 : : {
1982 : 2570060 : *buffer = (char *) malloc(msgLength + 1);
1983 [ - + ]: 2570060 : if (*buffer == NULL)
1984 : : {
1292 peter@eisentraut.org 1985 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
8379 tgl@sss.pgh.pa.us 1986 : 0 : return -2;
1987 : : }
8379 tgl@sss.pgh.pa.us 1988 :CBC 2570060 : memcpy(*buffer, &conn->inBuffer[conn->inCursor], msgLength);
3265 1989 : 2570060 : (*buffer)[msgLength] = '\0'; /* Add terminating null */
1990 : :
1991 : : /* Mark message consumed */
652 alvherre@alvh.no-ip. 1992 : 2570060 : pqParseDone(conn, conn->inCursor + msgLength);
1993 : :
8379 tgl@sss.pgh.pa.us 1994 : 2570060 : return msgLength;
1995 : : }
1996 : :
1997 : : /* Empty, so drop it and loop around for another */
652 alvherre@alvh.no-ip. 1998 :UBC 0 : pqParseDone(conn, conn->inCursor);
1999 : : }
2000 : : }
2001 : :
2002 : : /*
2003 : : * PQgetline - gets a newline-terminated string from the backend.
2004 : : *
2005 : : * See fe-exec.c for documentation.
2006 : : */
2007 : : int
8392 tgl@sss.pgh.pa.us 2008 : 0 : pqGetline3(PGconn *conn, char *s, int maxlen)
2009 : : {
2010 : : int status;
2011 : :
4427 bruce@momjian.us 2012 [ # # ]: 0 : if (conn->sock == PGINVALID_SOCKET ||
4782 rhaas@postgresql.org 2013 [ # # ]: 0 : (conn->asyncStatus != PGASYNC_COPY_OUT &&
2014 [ # # ]: 0 : conn->asyncStatus != PGASYNC_COPY_BOTH) ||
8392 tgl@sss.pgh.pa.us 2015 [ # # ]: 0 : conn->copy_is_binary)
2016 : : {
1292 peter@eisentraut.org 2017 : 0 : libpq_append_conn_error(conn, "PQgetline: not doing text COPY OUT");
8392 tgl@sss.pgh.pa.us 2018 : 0 : *s = '\0';
2019 : 0 : return EOF;
2020 : : }
2021 : :
8335 bruce@momjian.us 2022 [ # # ]: 0 : while ((status = PQgetlineAsync(conn, s, maxlen - 1)) == 0)
2023 : : {
2024 : : /* need to load more data */
3209 peter_e@gmx.net 2025 [ # # # # ]: 0 : if (pqWait(true, false, conn) ||
8392 tgl@sss.pgh.pa.us 2026 : 0 : pqReadData(conn) < 0)
2027 : : {
2028 : 0 : *s = '\0';
2029 : 0 : return EOF;
2030 : : }
2031 : : }
2032 : :
2033 [ # # ]: 0 : if (status < 0)
2034 : : {
2035 : : /* End of copy detected; gin up old-style terminator */
2036 : 0 : strcpy(s, "\\.");
2037 : 0 : return 0;
2038 : : }
2039 : :
2040 : : /* Add null terminator, and strip trailing \n if present */
8335 bruce@momjian.us 2041 [ # # ]: 0 : if (s[status - 1] == '\n')
2042 : : {
2043 : 0 : s[status - 1] = '\0';
8392 tgl@sss.pgh.pa.us 2044 : 0 : return 0;
2045 : : }
2046 : : else
2047 : : {
2048 : 0 : s[status] = '\0';
2049 : 0 : return 1;
2050 : : }
2051 : : }
2052 : :
2053 : : /*
2054 : : * PQgetlineAsync - gets a COPY data row without blocking.
2055 : : *
2056 : : * See fe-exec.c for documentation.
2057 : : */
2058 : : int
2059 : 0 : pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
2060 : : {
2061 : : int msgLength;
2062 : : int avail;
2063 : :
4782 rhaas@postgresql.org 2064 [ # # ]: 0 : if (conn->asyncStatus != PGASYNC_COPY_OUT
2065 [ # # ]: 0 : && conn->asyncStatus != PGASYNC_COPY_BOTH)
8392 tgl@sss.pgh.pa.us 2066 : 0 : return -1; /* we are not doing a copy... */
2067 : :
2068 : : /*
2069 : : * Recognize the next input message. To make life simpler for async
2070 : : * callers, we keep returning 0 until the next message is fully available
2071 : : * even if it is not Copy Data. This should keep PQendcopy from blocking.
2072 : : * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.)
2073 : : */
6711 2074 : 0 : msgLength = getCopyDataMessage(conn);
2075 [ # # ]: 0 : if (msgLength < 0)
2076 : 0 : return -1; /* end-of-copy or error */
2077 [ # # ]: 0 : if (msgLength == 0)
2078 : 0 : return 0; /* no data yet */
2079 : :
2080 : : /*
2081 : : * Move data from libpq's buffer to the caller's. In the case where a
2082 : : * prior call found the caller's buffer too small, we use
2083 : : * conn->copy_already_done to remember how much of the row was already
2084 : : * returned to the caller.
2085 : : */
8392 2086 : 0 : conn->inCursor += conn->copy_already_done;
2087 : 0 : avail = msgLength - 4 - conn->copy_already_done;
2088 [ # # ]: 0 : if (avail <= bufsize)
2089 : : {
2090 : : /* Able to consume the whole message */
2091 : 0 : memcpy(buffer, &conn->inBuffer[conn->inCursor], avail);
2092 : : /* Mark message consumed */
2093 : 0 : conn->inStart = conn->inCursor + avail;
2094 : : /* Reset state for next time */
2095 : 0 : conn->copy_already_done = 0;
2096 : 0 : return avail;
2097 : : }
2098 : : else
2099 : : {
2100 : : /* We must return a partial message */
2101 : 0 : memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize);
2102 : : /* The message is NOT consumed from libpq's buffer */
2103 : 0 : conn->copy_already_done += bufsize;
2104 : 0 : return bufsize;
2105 : : }
2106 : : }
2107 : :
2108 : : /*
2109 : : * PQendcopy
2110 : : *
2111 : : * See fe-exec.c for documentation.
2112 : : */
2113 : : int
8392 tgl@sss.pgh.pa.us 2114 :CBC 205 : pqEndcopy3(PGconn *conn)
2115 : : {
2116 : : PGresult *result;
2117 : :
2118 [ + + ]: 205 : if (conn->asyncStatus != PGASYNC_COPY_IN &&
4782 rhaas@postgresql.org 2119 [ - + ]: 198 : conn->asyncStatus != PGASYNC_COPY_OUT &&
4782 rhaas@postgresql.org 2120 [ # # ]:UBC 0 : conn->asyncStatus != PGASYNC_COPY_BOTH)
2121 : : {
1292 peter@eisentraut.org 2122 : 0 : libpq_append_conn_error(conn, "no COPY in progress");
8392 tgl@sss.pgh.pa.us 2123 : 0 : return 1;
2124 : : }
2125 : :
2126 : : /* Send the CopyDone message if needed */
4782 rhaas@postgresql.org 2127 [ + + ]:CBC 205 : if (conn->asyncStatus == PGASYNC_COPY_IN ||
2128 [ - + ]: 198 : conn->asyncStatus == PGASYNC_COPY_BOTH)
2129 : : {
1012 nathan@postgresql.or 2130 [ + - - + ]: 14 : if (pqPutMsgStart(PqMsg_CopyDone, conn) < 0 ||
8392 tgl@sss.pgh.pa.us 2131 : 7 : pqPutMsgEnd(conn) < 0)
8392 tgl@sss.pgh.pa.us 2132 :UBC 0 : return 1;
2133 : :
2134 : : /*
2135 : : * If we sent the COPY command in extended-query mode, we must issue a
2136 : : * Sync as well.
2137 : : */
1902 alvherre@alvh.no-ip. 2138 [ + - ]:CBC 7 : if (conn->cmd_queue_head &&
2139 [ - + ]: 7 : conn->cmd_queue_head->queryclass != PGQUERY_SIMPLE)
2140 : : {
1012 nathan@postgresql.or 2141 [ # # # # ]:UBC 0 : if (pqPutMsgStart(PqMsg_Sync, conn) < 0 ||
8326 tgl@sss.pgh.pa.us 2142 : 0 : pqPutMsgEnd(conn) < 0)
2143 : 0 : return 1;
2144 : : }
2145 : : }
2146 : :
2147 : : /*
2148 : : * make sure no data is waiting to be sent, abort if we are non-blocking
2149 : : * and the flush fails
2150 : : */
8392 tgl@sss.pgh.pa.us 2151 [ - + - - ]:CBC 205 : if (pqFlush(conn) && pqIsnonblocking(conn))
7444 neilc@samurai.com 2152 :UBC 0 : return 1;
2153 : :
2154 : : /* Return to active duty */
8392 tgl@sss.pgh.pa.us 2155 :CBC 205 : conn->asyncStatus = PGASYNC_BUSY;
2156 : :
2157 : : /*
2158 : : * Non blocking connections may have to abort at this point. If everyone
2159 : : * played the game there should be no problem, but in error scenarios the
2160 : : * expected messages may not have arrived yet. (We are assuming that the
2161 : : * backend's packetizing will ensure that CommandComplete arrives along
2162 : : * with the CopyDone; are there corner cases where that doesn't happen?)
2163 : : */
2164 [ - + - - ]: 205 : if (pqIsnonblocking(conn) && PQisBusy(conn))
7444 neilc@samurai.com 2165 :UBC 0 : return 1;
2166 : :
2167 : : /* Wait for the completion response */
8392 tgl@sss.pgh.pa.us 2168 :CBC 205 : result = PQgetResult(conn);
2169 : :
2170 : : /* Expecting a successful result */
2171 [ + - + - ]: 205 : if (result && result->resultStatus == PGRES_COMMAND_OK)
2172 : : {
2173 : 205 : PQclear(result);
2174 : 205 : return 0;
2175 : : }
2176 : :
2177 : : /*
2178 : : * Trouble. For backwards-compatibility reasons, we issue the error
2179 : : * message as if it were a notice (would be nice to get rid of this
2180 : : * silliness, but too many apps probably don't handle errors from
2181 : : * PQendcopy reasonably). Note that the app can still obtain the error
2182 : : * status from the PGconn object.
2183 : : */
8392 tgl@sss.pgh.pa.us 2184 [ # # ]:UBC 0 : if (conn->errorMessage.len > 0)
2185 : : {
2186 : : /* We have to strip the trailing newline ... pain in neck... */
8335 bruce@momjian.us 2187 : 0 : char svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
2188 : :
8379 tgl@sss.pgh.pa.us 2189 [ # # ]: 0 : if (svLast == '\n')
8335 bruce@momjian.us 2190 : 0 : conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
8377 tgl@sss.pgh.pa.us 2191 : 0 : pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
8335 bruce@momjian.us 2192 : 0 : conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
2193 : : }
2194 : :
8392 tgl@sss.pgh.pa.us 2195 : 0 : PQclear(result);
2196 : :
2197 : 0 : return 1;
2198 : : }
2199 : :
2200 : :
2201 : : /*
2202 : : * PQfn - Send a function call to the POSTGRES backend.
2203 : : *
2204 : : * See fe-exec.c for documentation.
2205 : : */
2206 : : PGresult *
8392 tgl@sss.pgh.pa.us 2207 :CBC 1341 : pqFunctionCall3(PGconn *conn, Oid fnid,
2208 : : int *result_buf, int buf_size, int *actual_result_len,
2209 : : int result_is_int,
2210 : : const PQArgBlock *args, int nargs)
2211 : : {
2212 : 1341 : bool needInput = false;
2213 : 1341 : ExecStatusType status = PGRES_FATAL_ERROR;
2214 : : char id;
2215 : : int msgLength;
2216 : : int avail;
2217 : : int i;
2218 : :
2219 : : /* already validated by PQfn */
1902 alvherre@alvh.no-ip. 2220 [ - + ]: 1341 : Assert(conn->pipelineStatus == PQ_PIPELINE_OFF);
2221 : :
2222 : : /* PQfn already validated connection state */
2223 : :
1012 nathan@postgresql.or 2224 [ + - + - ]: 2682 : if (pqPutMsgStart(PqMsg_FunctionCall, conn) < 0 ||
8335 bruce@momjian.us 2225 [ + - ]: 2682 : pqPutInt(fnid, 4, conn) < 0 || /* function id */
3265 tgl@sss.pgh.pa.us 2226 [ + - ]: 2682 : pqPutInt(1, 2, conn) < 0 || /* # of format codes */
2227 [ - + ]: 2682 : pqPutInt(1, 2, conn) < 0 || /* format code: BINARY */
8335 bruce@momjian.us 2228 : 1341 : pqPutInt(nargs, 2, conn) < 0) /* # of args */
2229 : : {
2230 : : /* error message should be set up already */
8392 tgl@sss.pgh.pa.us 2231 :UBC 0 : return NULL;
2232 : : }
2233 : :
8392 tgl@sss.pgh.pa.us 2234 [ + + ]:CBC 3901 : for (i = 0; i < nargs; ++i)
2235 : : { /* len.int4 + contents */
2236 [ - + ]: 2560 : if (pqPutInt(args[i].len, 4, conn))
8392 tgl@sss.pgh.pa.us 2237 :UBC 0 : return NULL;
8392 tgl@sss.pgh.pa.us 2238 [ - + ]:CBC 2560 : if (args[i].len == -1)
8392 tgl@sss.pgh.pa.us 2239 :UBC 0 : continue; /* it's NULL */
2240 : :
8392 tgl@sss.pgh.pa.us 2241 [ + + ]:CBC 2560 : if (args[i].isint)
2242 : : {
2243 [ - + ]: 1903 : if (pqPutInt(args[i].u.integer, args[i].len, conn))
8392 tgl@sss.pgh.pa.us 2244 :UBC 0 : return NULL;
2245 : : }
2246 : : else
2247 : : {
387 heikki.linnakangas@i 2248 [ - + ]:CBC 657 : if (pqPutnchar(args[i].u.ptr, args[i].len, conn))
8392 tgl@sss.pgh.pa.us 2249 :UBC 0 : return NULL;
2250 : : }
2251 : : }
2252 : :
3265 tgl@sss.pgh.pa.us 2253 [ - + ]:CBC 1341 : if (pqPutInt(1, 2, conn) < 0) /* result format code: BINARY */
8392 tgl@sss.pgh.pa.us 2254 :UBC 0 : return NULL;
2255 : :
8392 tgl@sss.pgh.pa.us 2256 [ + - + - ]:CBC 2682 : if (pqPutMsgEnd(conn) < 0 ||
2257 : 1341 : pqFlush(conn))
8392 tgl@sss.pgh.pa.us 2258 :UBC 0 : return NULL;
2259 : :
2260 : : for (;;)
2261 : : {
8392 tgl@sss.pgh.pa.us 2262 [ + + ]:CBC 4152 : if (needInput)
2263 : : {
2264 : : /* Wait for some data to arrive (or for the channel to close) */
3209 peter_e@gmx.net 2265 [ + - + - ]: 2940 : if (pqWait(true, false, conn) ||
8392 tgl@sss.pgh.pa.us 2266 : 1470 : pqReadData(conn) < 0)
2267 : : break;
2268 : : }
2269 : :
2270 : : /*
2271 : : * Scan the message. If we run out of data, loop around to try again.
2272 : : */
2273 : 4152 : needInput = true;
2274 : :
2275 : 4152 : conn->inCursor = conn->inStart;
2276 [ + + ]: 4152 : if (pqGetc(&id, conn))
2277 : 1341 : continue;
2278 [ - + ]: 2811 : if (pqGetInt(&msgLength, 4, conn))
8392 tgl@sss.pgh.pa.us 2279 :UBC 0 : continue;
2280 : :
2281 : : /*
2282 : : * Try to validate message type/length here. A length less than 4 is
2283 : : * definitely broken. Large lengths should only be believed for a few
2284 : : * message types.
2285 : : */
8392 tgl@sss.pgh.pa.us 2286 [ - + ]:CBC 2811 : if (msgLength < 4)
2287 : : {
8392 tgl@sss.pgh.pa.us 2288 :UBC 0 : handleSyncLoss(conn, id, msgLength);
2289 : 0 : break;
2290 : : }
8189 tgl@sss.pgh.pa.us 2291 :CBC 2811 : if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
[ - + - -
- - - - -
- - - - -
- - ]
2292 : : {
8392 tgl@sss.pgh.pa.us 2293 :UBC 0 : handleSyncLoss(conn, id, msgLength);
2294 : 0 : break;
2295 : : }
2296 : :
2297 : : /*
2298 : : * Can't process if message body isn't all here yet.
2299 : : */
8392 tgl@sss.pgh.pa.us 2300 :CBC 2811 : msgLength -= 4;
2301 : 2811 : avail = conn->inEnd - conn->inCursor;
2302 [ + + ]: 2811 : if (avail < msgLength)
2303 : : {
2304 : : /*
2305 : : * Before looping, enlarge the input buffer if needed to hold the
2306 : : * whole message. See notes in parseInput.
2307 : : */
6575 2308 [ - + ]: 129 : if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
2309 : : conn))
2310 : : {
2311 : : /*
2312 : : * Abandon the connection. There's not much else we can
2313 : : * safely do; we can't just ignore the message or we could
2314 : : * miss important changes to the connection state.
2315 : : * pqCheckInBufferSpace() already reported the error.
2316 : : */
281 heikki.linnakangas@i 2317 :UBC 0 : handleFatalError(conn);
8392 tgl@sss.pgh.pa.us 2318 : 0 : break;
2319 : : }
8392 tgl@sss.pgh.pa.us 2320 :CBC 129 : continue;
2321 : : }
2322 : :
2323 : : /*
2324 : : * We should see V or E response to the command, but might get N
2325 : : * and/or A notices first. We also need to swallow the final Z before
2326 : : * returning.
2327 : : */
2328 [ + - - - : 2682 : switch (id)
+ - - ]
2329 : : {
278 nathan@postgresql.or 2330 :GNC 1341 : case PqMsg_FunctionCallResponse:
8392 tgl@sss.pgh.pa.us 2331 [ - + ]:CBC 1341 : if (pqGetInt(actual_result_len, 4, conn))
8392 tgl@sss.pgh.pa.us 2332 :UBC 0 : continue;
8392 tgl@sss.pgh.pa.us 2333 [ + - ]:CBC 1341 : if (*actual_result_len != -1)
2334 : : {
2335 [ + + ]: 1341 : if (result_is_int)
2336 : : {
2337 [ - + ]: 876 : if (pqGetInt(result_buf, *actual_result_len, conn))
8392 tgl@sss.pgh.pa.us 2338 :UBC 0 : continue;
2339 : : }
2340 : : else
2341 : : {
2342 : : /*
2343 : : * If the server returned too much data for the
2344 : : * buffer, something fishy is going on. Abandon ship.
2345 : : */
19 nathan@postgresql.or 2346 [ + - - + ]:CBC 465 : if (buf_size != -1 && *actual_result_len > buf_size)
2347 : : {
19 nathan@postgresql.or 2348 :UBC 0 : libpq_append_conn_error(conn, "server returned too much data");
2349 : 0 : handleFatalError(conn);
2350 : 0 : return pqPrepareAsyncResult(conn);
2351 : : }
2352 : :
387 heikki.linnakangas@i 2353 [ - + ]:CBC 465 : if (pqGetnchar(result_buf,
8392 tgl@sss.pgh.pa.us 2354 : 465 : *actual_result_len,
2355 : : conn))
8392 tgl@sss.pgh.pa.us 2356 :UBC 0 : continue;
2357 : : }
2358 : : }
2359 : : /* correctly finished function result message */
8392 tgl@sss.pgh.pa.us 2360 :CBC 1341 : status = PGRES_COMMAND_OK;
2361 : 1341 : break;
278 nathan@postgresql.or 2362 :UNC 0 : case PqMsg_ErrorResponse:
8392 tgl@sss.pgh.pa.us 2363 [ # # ]:UBC 0 : if (pqGetErrorNotice3(conn, true))
2364 : 0 : continue;
2365 : 0 : status = PGRES_FATAL_ERROR;
2366 : 0 : break;
278 nathan@postgresql.or 2367 :UNC 0 : case PqMsg_NotificationResponse:
2368 : : /* handle notify and go back to processing return values */
8392 tgl@sss.pgh.pa.us 2369 [ # # ]:UBC 0 : if (getNotify(conn))
2370 : 0 : continue;
2371 : 0 : break;
278 nathan@postgresql.or 2372 :UNC 0 : case PqMsg_NoticeResponse:
2373 : : /* handle notice and go back to processing return values */
8392 tgl@sss.pgh.pa.us 2374 [ # # ]:UBC 0 : if (pqGetErrorNotice3(conn, false))
2375 : 0 : continue;
2376 : 0 : break;
278 nathan@postgresql.or 2377 :GNC 1341 : case PqMsg_ReadyForQuery:
8379 tgl@sss.pgh.pa.us 2378 [ - + ]:CBC 1341 : if (getReadyForQuery(conn))
8392 tgl@sss.pgh.pa.us 2379 :UBC 0 : continue;
2380 : :
2381 : : /* consume the message */
652 alvherre@alvh.no-ip. 2382 :CBC 1341 : pqParseDone(conn, conn->inStart + 5 + msgLength);
2383 : :
2384 : : /*
2385 : : * If we already have a result object (probably an error), use
2386 : : * that. Otherwise, if we saw a function result message,
2387 : : * report COMMAND_OK. Otherwise, the backend violated the
2388 : : * protocol, so complain.
2389 : : */
1500 tgl@sss.pgh.pa.us 2390 [ + - + - ]: 1341 : if (!pgHavePendingResult(conn))
2391 : : {
1562 2392 [ + - ]: 1341 : if (status == PGRES_COMMAND_OK)
2393 : : {
2394 : 1341 : conn->result = PQmakeEmptyPGresult(conn, status);
2395 [ - + ]: 1341 : if (!conn->result)
2396 : : {
1292 peter@eisentraut.org 2397 :UBC 0 : libpq_append_conn_error(conn, "out of memory");
1562 tgl@sss.pgh.pa.us 2398 : 0 : pqSaveErrorResult(conn);
2399 : : }
2400 : : }
2401 : : else
2402 : : {
1292 peter@eisentraut.org 2403 : 0 : libpq_append_conn_error(conn, "protocol error: no function result");
1562 tgl@sss.pgh.pa.us 2404 : 0 : pqSaveErrorResult(conn);
2405 : : }
2406 : : }
2407 : : /* and we're out */
1562 tgl@sss.pgh.pa.us 2408 :CBC 1341 : return pqPrepareAsyncResult(conn);
278 nathan@postgresql.or 2409 :UNC 0 : case PqMsg_ParameterStatus:
8392 tgl@sss.pgh.pa.us 2410 [ # # ]:UBC 0 : if (getParameterStatus(conn))
2411 : 0 : continue;
2412 : 0 : break;
2413 : 0 : default:
2414 : : /* The backend violates the protocol. */
1292 peter@eisentraut.org 2415 : 0 : libpq_append_conn_error(conn, "protocol error: id=0x%x", id);
8392 tgl@sss.pgh.pa.us 2416 : 0 : pqSaveErrorResult(conn);
2417 : :
2418 : : /*
2419 : : * We can't call parsing done due to the protocol violation
2420 : : * (so message tracing wouldn't work), but trust the specified
2421 : : * message length as what to skip.
2422 : : */
2423 : 0 : conn->inStart += 5 + msgLength;
2424 : 0 : return pqPrepareAsyncResult(conn);
2425 : : }
2426 : :
2427 : : /* Completed parsing this message, keep going */
652 alvherre@alvh.no-ip. 2428 :CBC 1341 : pqParseDone(conn, conn->inStart + 5 + msgLength);
8392 tgl@sss.pgh.pa.us 2429 : 1341 : needInput = false;
2430 : : }
2431 : :
2432 : : /*
2433 : : * We fall out of the loop only upon failing to read data.
2434 : : * conn->errorMessage has been set by pqWait or pqReadData. We want to
2435 : : * append it to any already-received error message.
2436 : : */
8392 tgl@sss.pgh.pa.us 2437 :UBC 0 : pqSaveErrorResult(conn);
2438 : 0 : return pqPrepareAsyncResult(conn);
2439 : : }
2440 : :
2441 : :
2442 : : /*
2443 : : * Construct startup packet
2444 : : *
2445 : : * Returns a malloc'd packet buffer, or NULL if out of memory
2446 : : */
2447 : : char *
8392 tgl@sss.pgh.pa.us 2448 :CBC 15709 : pqBuildStartupPacket3(PGconn *conn, int *packetlen,
2449 : : const PQEnvironmentOption *options)
2450 : : {
2451 : : char *startpacket;
2452 : : size_t len;
2453 : :
201 jchampion@postgresql 2454 : 15709 : len = build_startup_packet(conn, NULL, options);
2455 [ + - - + ]: 15709 : if (len == 0 || len > INT_MAX)
201 jchampion@postgresql 2456 :UBC 0 : return NULL;
2457 : :
201 jchampion@postgresql 2458 :CBC 15709 : *packetlen = len;
8392 tgl@sss.pgh.pa.us 2459 : 15709 : startpacket = (char *) malloc(*packetlen);
2460 [ - + ]: 15709 : if (!startpacket)
8392 tgl@sss.pgh.pa.us 2461 :UBC 0 : return NULL;
2462 : :
201 jchampion@postgresql 2463 :CBC 15709 : len = build_startup_packet(conn, startpacket, options);
2464 [ - + ]: 15709 : Assert(*packetlen == len);
2465 : :
8392 tgl@sss.pgh.pa.us 2466 : 15709 : return startpacket;
2467 : : }
2468 : :
2469 : : /*
2470 : : * Build a startup packet given a filled-in PGconn structure.
2471 : : *
2472 : : * We need to figure out how much space is needed, then fill it in.
2473 : : * To avoid duplicate logic, this routine is called twice: the first time
2474 : : * (with packet == NULL) just counts the space needed, the second time
2475 : : * (with packet == allocated space) fills it in. Return value is the number
2476 : : * of bytes used, or zero in the unlikely event of size_t overflow.
2477 : : */
2478 : : static size_t
2479 : 31418 : build_startup_packet(const PGconn *conn, char *packet,
2480 : : const PQEnvironmentOption *options)
2481 : : {
201 jchampion@postgresql 2482 : 31418 : size_t packet_len = 0;
2483 : : const PQEnvironmentOption *next_eo;
2484 : : const char *val;
2485 : :
2486 : : /* Protocol version comes first. */
8392 tgl@sss.pgh.pa.us 2487 [ + + ]: 31418 : if (packet)
2488 : : {
3163 andres@anarazel.de 2489 : 15709 : ProtocolVersion pv = pg_hton32(conn->pversion);
2490 : :
8392 tgl@sss.pgh.pa.us 2491 : 15709 : memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
2492 : : }
2493 : 31418 : packet_len += sizeof(ProtocolVersion);
2494 : :
2495 : : /* Add user name, database name, options */
2496 : :
2497 : : #define ADD_STARTUP_OPTION(optname, optval) \
2498 : : do { \
2499 : : if (packet) \
2500 : : strcpy(packet + packet_len, optname); \
2501 : : if (pg_add_size_overflow(packet_len, strlen(optname) + 1, &packet_len)) \
2502 : : return 0; \
2503 : : if (packet) \
2504 : : strcpy(packet + packet_len, optval); \
2505 : : if (pg_add_size_overflow(packet_len, strlen(optval) + 1, &packet_len)) \
2506 : : return 0; \
2507 : : } while(0)
2508 : :
2509 [ + - + - ]: 31418 : if (conn->pguser && conn->pguser[0])
6023 2510 [ + + - + : 31418 : ADD_STARTUP_OPTION("user", conn->pguser);
+ + - + ]
8392 2511 [ + - + - ]: 31418 : if (conn->dbName && conn->dbName[0])
6023 2512 [ + + - + : 31418 : ADD_STARTUP_OPTION("database", conn->dbName);
+ + - + ]
5876 magnus@hagander.net 2513 [ + + + + ]: 31418 : if (conn->replication && conn->replication[0])
5979 heikki.linnakangas@i 2514 [ + + - + : 3360 : ADD_STARTUP_OPTION("replication", conn->replication);
+ + - + ]
8392 tgl@sss.pgh.pa.us 2515 [ + - + + ]: 31418 : if (conn->pgoptions && conn->pgoptions[0])
6023 2516 [ + + - + : 8934 : ADD_STARTUP_OPTION("options", conn->pgoptions);
+ + - + ]
2517 [ + - ]: 31418 : if (conn->send_appname)
2518 : : {
2519 : : /* Use appname if present, otherwise use fallback */
2520 [ + + ]: 31418 : val = conn->appname ? conn->appname : conn->fbappname;
2521 [ + + + - ]: 31418 : if (val && val[0])
2522 [ + + - + : 31404 : ADD_STARTUP_OPTION("application_name", val);
+ + - + ]
2523 : : }
2524 : :
5579 peter_e@gmx.net 2525 [ + + + - ]: 31418 : if (conn->client_encoding_initial && conn->client_encoding_initial[0])
2526 [ + + - + : 1840 : ADD_STARTUP_OPTION("client_encoding", conn->client_encoding_initial);
+ + - + ]
2527 : :
2528 : : /*
2529 : : * Add the test_protocol_negotiation option when greasing, to test that
2530 : : * servers properly report unsupported protocol options in addition to
2531 : : * unsupported minor versions.
2532 : : */
96 jchampion@postgresql 2533 [ + + ]:GNC 31418 : if (conn->pversion == PG_PROTOCOL_GREASE)
2534 [ + + - + : 31238 : ADD_STARTUP_OPTION("_pq_.test_protocol_negotiation", "");
+ + - + ]
2535 : :
2536 : : /* Add any environment-driven GUC settings needed */
8392 tgl@sss.pgh.pa.us 2537 [ + + ]:CBC 125672 : for (next_eo = options; next_eo->envName; next_eo++)
2538 : : {
2539 [ + + ]: 94254 : if ((val = getenv(next_eo->envName)) != NULL)
2540 : : {
8058 2541 [ + - ]: 10948 : if (pg_strcasecmp(val, "default") != 0)
6023 2542 [ + + - + : 10948 : ADD_STARTUP_OPTION(next_eo->pgName, val);
+ + - + ]
2543 : : }
2544 : : }
2545 : :
2546 : : /* Add trailing terminator */
8392 2547 [ + + ]: 31418 : if (packet)
2548 : 15709 : packet[packet_len] = '\0';
187 jchampion@postgresql 2549 [ - + ]: 31418 : if (pg_add_size_overflow(packet_len, 1, &packet_len))
201 jchampion@postgresql 2550 :UBC 0 : return 0;
2551 : :
8392 tgl@sss.pgh.pa.us 2552 :CBC 31418 : return packet_len;
2553 : : }
|