]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/fe-protocol3.c
libpq: have PQconnectdbParams() and PQpingParams accept "" as default
[postgresql] / src / interfaces / libpq / fe-protocol3.c
1 /*-------------------------------------------------------------------------
2  *
3  * fe-protocol3.c
4  *        functions that are specific to frontend/backend protocol version 3
5  *
6  * Portions Copyright (c) 1996-2014, 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
20 #include "libpq-fe.h"
21 #include "libpq-int.h"
22
23 #include "mb/pg_wchar.h"
24
25 #ifdef WIN32
26 #include "win32.h"
27 #else
28 #include <unistd.h>
29 #include <netinet/in.h>
30 #ifdef HAVE_NETINET_TCP_H
31 #include <netinet/tcp.h>
32 #endif
33 #include <arpa/inet.h>
34 #endif
35
36
37 /*
38  * This macro lists the backend message types that could be "long" (more
39  * than a couple of kilobytes).
40  */
41 #define VALID_LONG_MESSAGE_TYPE(id) \
42         ((id) == 'T' || (id) == 'D' || (id) == 'd' || (id) == 'V' || \
43          (id) == 'E' || (id) == 'N' || (id) == 'A')
44
45
46 static void handleSyncLoss(PGconn *conn, char id, int msgLength);
47 static int      getRowDescriptions(PGconn *conn, int msgLength);
48 static int      getParamDescriptions(PGconn *conn);
49 static int      getAnotherTuple(PGconn *conn, int msgLength);
50 static int      getParameterStatus(PGconn *conn);
51 static int      getNotify(PGconn *conn);
52 static int      getCopyStart(PGconn *conn, ExecStatusType copytype);
53 static int      getReadyForQuery(PGconn *conn);
54 static void reportErrorPosition(PQExpBuffer msg, const char *query,
55                                         int loc, int encoding);
56 static int build_startup_packet(const PGconn *conn, char *packet,
57                                          const PQEnvironmentOption *options);
58
59
60 /*
61  * parseInput: if appropriate, parse input data from backend
62  * until input is exhausted or a stopping state is reached.
63  * Note that this function will NOT attempt to read more data from the backend.
64  */
65 void
66 pqParseInput3(PGconn *conn)
67 {
68         char            id;
69         int                     msgLength;
70         int                     avail;
71
72         /*
73          * Loop to parse successive complete messages available in the buffer.
74          */
75         for (;;)
76         {
77                 /*
78                  * Try to read a message.  First get the type code and length. Return
79                  * if not enough data.
80                  */
81                 conn->inCursor = conn->inStart;
82                 if (pqGetc(&id, conn))
83                         return;
84                 if (pqGetInt(&msgLength, 4, conn))
85                         return;
86
87                 /*
88                  * Try to validate message type/length here.  A length less than 4 is
89                  * definitely broken.  Large lengths should only be believed for a few
90                  * message types.
91                  */
92                 if (msgLength < 4)
93                 {
94                         handleSyncLoss(conn, id, msgLength);
95                         return;
96                 }
97                 if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
98                 {
99                         handleSyncLoss(conn, id, msgLength);
100                         return;
101                 }
102
103                 /*
104                  * Can't process if message body isn't all here yet.
105                  */
106                 msgLength -= 4;
107                 avail = conn->inEnd - conn->inCursor;
108                 if (avail < msgLength)
109                 {
110                         /*
111                          * Before returning, enlarge the input buffer if needed to hold
112                          * the whole message.  This is better than leaving it to
113                          * pqReadData because we can avoid multiple cycles of realloc()
114                          * when the message is large; also, we can implement a reasonable
115                          * recovery strategy if we are unable to make the buffer big
116                          * enough.
117                          */
118                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
119                                                                          conn))
120                         {
121                                 /*
122                                  * XXX add some better recovery code... plan is to skip over
123                                  * the message using its length, then report an error. For the
124                                  * moment, just treat this like loss of sync (which indeed it
125                                  * might be!)
126                                  */
127                                 handleSyncLoss(conn, id, msgLength);
128                         }
129                         return;
130                 }
131
132                 /*
133                  * NOTIFY and NOTICE messages can happen in any state; always process
134                  * them right away.
135                  *
136                  * Most other messages should only be processed while in BUSY state.
137                  * (In particular, in READY state we hold off further parsing until
138                  * the application collects the current PGresult.)
139                  *
140                  * However, if the state is IDLE then we got trouble; we need to deal
141                  * with the unexpected message somehow.
142                  *
143                  * ParameterStatus ('S') messages are a special case: in IDLE state we
144                  * must process 'em (this case could happen if a new value was adopted
145                  * from config file due to SIGHUP), but otherwise we hold off until
146                  * BUSY state.
147                  */
148                 if (id == 'A')
149                 {
150                         if (getNotify(conn))
151                                 return;
152                 }
153                 else if (id == 'N')
154                 {
155                         if (pqGetErrorNotice3(conn, false))
156                                 return;
157                 }
158                 else if (conn->asyncStatus != PGASYNC_BUSY)
159                 {
160                         /* If not IDLE state, just wait ... */
161                         if (conn->asyncStatus != PGASYNC_IDLE)
162                                 return;
163
164                         /*
165                          * Unexpected message in IDLE state; need to recover somehow.
166                          * ERROR messages are displayed using the notice processor;
167                          * ParameterStatus is handled normally; anything else is just
168                          * dropped on the floor after displaying a suitable warning
169                          * notice.      (An ERROR is very possibly the backend telling us why
170                          * it is about to close the connection, so we don't want to just
171                          * discard it...)
172                          */
173                         if (id == 'E')
174                         {
175                                 if (pqGetErrorNotice3(conn, false /* treat as notice */ ))
176                                         return;
177                         }
178                         else if (id == 'S')
179                         {
180                                 if (getParameterStatus(conn))
181                                         return;
182                         }
183                         else
184                         {
185                                 pqInternalNotice(&conn->noticeHooks,
186                                                 "message type 0x%02x arrived from server while idle",
187                                                                  id);
188                                 /* Discard the unexpected message */
189                                 conn->inCursor += msgLength;
190                         }
191                 }
192                 else
193                 {
194                         /*
195                          * In BUSY state, we can process everything.
196                          */
197                         switch (id)
198                         {
199                                 case 'C':               /* command complete */
200                                         if (pqGets(&conn->workBuffer, conn))
201                                                 return;
202                                         if (conn->result == NULL)
203                                         {
204                                                 conn->result = PQmakeEmptyPGresult(conn,
205                                                                                                                    PGRES_COMMAND_OK);
206                                                 if (!conn->result)
207                                                         return;
208                                         }
209                                         strlcpy(conn->result->cmdStatus, conn->workBuffer.data,
210                                                         CMDSTATUS_LEN);
211                                         conn->asyncStatus = PGASYNC_READY;
212                                         break;
213                                 case 'E':               /* error return */
214                                         if (pqGetErrorNotice3(conn, true))
215                                                 return;
216                                         conn->asyncStatus = PGASYNC_READY;
217                                         break;
218                                 case 'Z':               /* backend is ready for new query */
219                                         if (getReadyForQuery(conn))
220                                                 return;
221                                         conn->asyncStatus = PGASYNC_IDLE;
222                                         break;
223                                 case 'I':               /* empty query */
224                                         if (conn->result == NULL)
225                                         {
226                                                 conn->result = PQmakeEmptyPGresult(conn,
227                                                                                                                    PGRES_EMPTY_QUERY);
228                                                 if (!conn->result)
229                                                         return;
230                                         }
231                                         conn->asyncStatus = PGASYNC_READY;
232                                         break;
233                                 case '1':               /* Parse Complete */
234                                         /* If we're doing PQprepare, we're done; else ignore */
235                                         if (conn->queryclass == PGQUERY_PREPARE)
236                                         {
237                                                 if (conn->result == NULL)
238                                                 {
239                                                         conn->result = PQmakeEmptyPGresult(conn,
240                                                                                                                    PGRES_COMMAND_OK);
241                                                         if (!conn->result)
242                                                                 return;
243                                                 }
244                                                 conn->asyncStatus = PGASYNC_READY;
245                                         }
246                                         break;
247                                 case '2':               /* Bind Complete */
248                                 case '3':               /* Close Complete */
249                                         /* Nothing to do for these message types */
250                                         break;
251                                 case 'S':               /* parameter status */
252                                         if (getParameterStatus(conn))
253                                                 return;
254                                         break;
255                                 case 'K':               /* secret key data from the backend */
256
257                                         /*
258                                          * This is expected only during backend startup, but it's
259                                          * just as easy to handle it as part of the main loop.
260                                          * Save the data and continue processing.
261                                          */
262                                         if (pqGetInt(&(conn->be_pid), 4, conn))
263                                                 return;
264                                         if (pqGetInt(&(conn->be_key), 4, conn))
265                                                 return;
266                                         break;
267                                 case 'T':               /* Row Description */
268                                         if (conn->result == NULL ||
269                                                 conn->queryclass == PGQUERY_DESCRIBE)
270                                         {
271                                                 /* First 'T' in a query sequence */
272                                                 if (getRowDescriptions(conn, msgLength))
273                                                         return;
274                                                 /* getRowDescriptions() moves inStart itself */
275                                                 continue;
276                                         }
277                                         else
278                                         {
279                                                 /*
280                                                  * A new 'T' message is treated as the start of
281                                                  * another PGresult.  (It is not clear that this is
282                                                  * really possible with the current backend.) We stop
283                                                  * parsing until the application accepts the current
284                                                  * result.
285                                                  */
286                                                 conn->asyncStatus = PGASYNC_READY;
287                                                 return;
288                                         }
289                                         break;
290                                 case 'n':               /* No Data */
291
292                                         /*
293                                          * NoData indicates that we will not be seeing a
294                                          * RowDescription message because the statement or portal
295                                          * inquired about doesn't return rows.
296                                          *
297                                          * If we're doing a Describe, we have to pass something
298                                          * back to the client, so set up a COMMAND_OK result,
299                                          * instead of TUPLES_OK.  Otherwise we can just ignore
300                                          * this message.
301                                          */
302                                         if (conn->queryclass == PGQUERY_DESCRIBE)
303                                         {
304                                                 if (conn->result == NULL)
305                                                 {
306                                                         conn->result = PQmakeEmptyPGresult(conn,
307                                                                                                                    PGRES_COMMAND_OK);
308                                                         if (!conn->result)
309                                                                 return;
310                                                 }
311                                                 conn->asyncStatus = PGASYNC_READY;
312                                         }
313                                         break;
314                                 case 't':               /* Parameter Description */
315                                         if (getParamDescriptions(conn))
316                                                 return;
317                                         break;
318                                 case 'D':               /* Data Row */
319                                         if (conn->result != NULL &&
320                                                 conn->result->resultStatus == PGRES_TUPLES_OK)
321                                         {
322                                                 /* Read another tuple of a normal query response */
323                                                 if (getAnotherTuple(conn, msgLength))
324                                                         return;
325                                                 /* getAnotherTuple() moves inStart itself */
326                                                 continue;
327                                         }
328                                         else if (conn->result != NULL &&
329                                                          conn->result->resultStatus == PGRES_FATAL_ERROR)
330                                         {
331                                                 /*
332                                                  * We've already choked for some reason.  Just discard
333                                                  * tuples till we get to the end of the query.
334                                                  */
335                                                 conn->inCursor += msgLength;
336                                         }
337                                         else
338                                         {
339                                                 /* Set up to report error at end of query */
340                                                 printfPQExpBuffer(&conn->errorMessage,
341                                                                                   libpq_gettext("server sent data (\"D\" message) without prior row description (\"T\" message)\n"));
342                                                 pqSaveErrorResult(conn);
343                                                 /* Discard the unexpected message */
344                                                 conn->inCursor += msgLength;
345                                         }
346                                         break;
347                                 case 'G':               /* Start Copy In */
348                                         if (getCopyStart(conn, PGRES_COPY_IN))
349                                                 return;
350                                         conn->asyncStatus = PGASYNC_COPY_IN;
351                                         break;
352                                 case 'H':               /* Start Copy Out */
353                                         if (getCopyStart(conn, PGRES_COPY_OUT))
354                                                 return;
355                                         conn->asyncStatus = PGASYNC_COPY_OUT;
356                                         conn->copy_already_done = 0;
357                                         break;
358                                 case 'W':               /* Start Copy Both */
359                                         if (getCopyStart(conn, PGRES_COPY_BOTH))
360                                                 return;
361                                         conn->asyncStatus = PGASYNC_COPY_BOTH;
362                                         conn->copy_already_done = 0;
363                                         break;
364                                 case 'd':               /* Copy Data */
365
366                                         /*
367                                          * If we see Copy Data, just silently drop it.  This would
368                                          * only occur if application exits COPY OUT mode too
369                                          * early.
370                                          */
371                                         conn->inCursor += msgLength;
372                                         break;
373                                 case 'c':               /* Copy Done */
374
375                                         /*
376                                          * If we see Copy Done, just silently drop it.  This is
377                                          * the normal case during PQendcopy.  We will keep
378                                          * swallowing data, expecting to see command-complete for
379                                          * the COPY command.
380                                          */
381                                         break;
382                                 default:
383                                         printfPQExpBuffer(&conn->errorMessage,
384                                                                           libpq_gettext(
385                                                                                                         "unexpected response from server; first received character was \"%c\"\n"),
386                                                                           id);
387                                         /* build an error result holding the error message */
388                                         pqSaveErrorResult(conn);
389                                         /* not sure if we will see more, so go to ready state */
390                                         conn->asyncStatus = PGASYNC_READY;
391                                         /* Discard the unexpected message */
392                                         conn->inCursor += msgLength;
393                                         break;
394                         }                                       /* switch on protocol character */
395                 }
396                 /* Successfully consumed this message */
397                 if (conn->inCursor == conn->inStart + 5 + msgLength)
398                 {
399                         /* Normal case: parsing agrees with specified length */
400                         conn->inStart = conn->inCursor;
401                 }
402                 else
403                 {
404                         /* Trouble --- report it */
405                         printfPQExpBuffer(&conn->errorMessage,
406                                                           libpq_gettext("message contents do not agree with length in message type \"%c\"\n"),
407                                                           id);
408                         /* build an error result holding the error message */
409                         pqSaveErrorResult(conn);
410                         conn->asyncStatus = PGASYNC_READY;
411                         /* trust the specified message length as what to skip */
412                         conn->inStart += 5 + msgLength;
413                 }
414         }
415 }
416
417 /*
418  * handleSyncLoss: clean up after loss of message-boundary sync
419  *
420  * There isn't really a lot we can do here except abandon the connection.
421  */
422 static void
423 handleSyncLoss(PGconn *conn, char id, int msgLength)
424 {
425         printfPQExpBuffer(&conn->errorMessage,
426                                           libpq_gettext(
427         "lost synchronization with server: got message type \"%c\", length %d\n"),
428                                           id, msgLength);
429         /* build an error result holding the error message */
430         pqSaveErrorResult(conn);
431         conn->asyncStatus = PGASYNC_READY;      /* drop out of GetResult wait loop */
432
433         pqDropConnection(conn);
434         conn->status = CONNECTION_BAD;          /* No more connection to backend */
435 }
436
437 /*
438  * parseInput subroutine to read a 'T' (row descriptions) message.
439  * We'll build a new PGresult structure (unless called for a Describe
440  * command for a prepared statement) containing the attribute data.
441  * Returns: 0 if processed message successfully, EOF to suspend parsing
442  * (the latter case is not actually used currently).
443  * In either case, conn->inStart has been advanced past the message.
444  */
445 static int
446 getRowDescriptions(PGconn *conn, int msgLength)
447 {
448         PGresult   *result;
449         int                     nfields;
450         const char *errmsg;
451         int                     i;
452
453         /*
454          * When doing Describe for a prepared statement, there'll already be a
455          * PGresult created by getParamDescriptions, and we should fill data into
456          * that.  Otherwise, create a new, empty PGresult.
457          */
458         if (conn->queryclass == PGQUERY_DESCRIBE)
459         {
460                 if (conn->result)
461                         result = conn->result;
462                 else
463                         result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
464         }
465         else
466                 result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
467         if (!result)
468         {
469                 errmsg = NULL;                  /* means "out of memory", see below */
470                 goto advance_and_error;
471         }
472
473         /* parseInput already read the 'T' label and message length. */
474         /* the next two bytes are the number of fields */
475         if (pqGetInt(&(result->numAttributes), 2, conn))
476         {
477                 /* We should not run out of data here, so complain */
478                 errmsg = libpq_gettext("insufficient data in \"T\" message");
479                 goto advance_and_error;
480         }
481         nfields = result->numAttributes;
482
483         /* allocate space for the attribute descriptors */
484         if (nfields > 0)
485         {
486                 result->attDescs = (PGresAttDesc *)
487                         pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
488                 if (!result->attDescs)
489                 {
490                         errmsg = NULL;          /* means "out of memory", see below */
491                         goto advance_and_error;
492                 }
493                 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
494         }
495
496         /* result->binary is true only if ALL columns are binary */
497         result->binary = (nfields > 0) ? 1 : 0;
498
499         /* get type info */
500         for (i = 0; i < nfields; i++)
501         {
502                 int                     tableid;
503                 int                     columnid;
504                 int                     typid;
505                 int                     typlen;
506                 int                     atttypmod;
507                 int                     format;
508
509                 if (pqGets(&conn->workBuffer, conn) ||
510                         pqGetInt(&tableid, 4, conn) ||
511                         pqGetInt(&columnid, 2, conn) ||
512                         pqGetInt(&typid, 4, conn) ||
513                         pqGetInt(&typlen, 2, conn) ||
514                         pqGetInt(&atttypmod, 4, conn) ||
515                         pqGetInt(&format, 2, conn))
516                 {
517                         /* We should not run out of data here, so complain */
518                         errmsg = libpq_gettext("insufficient data in \"T\" message");
519                         goto advance_and_error;
520                 }
521
522                 /*
523                  * Since pqGetInt treats 2-byte integers as unsigned, we need to
524                  * coerce these results to signed form.
525                  */
526                 columnid = (int) ((int16) columnid);
527                 typlen = (int) ((int16) typlen);
528                 format = (int) ((int16) format);
529
530                 result->attDescs[i].name = pqResultStrdup(result,
531                                                                                                   conn->workBuffer.data);
532                 if (!result->attDescs[i].name)
533                 {
534                         errmsg = NULL;          /* means "out of memory", see below */
535                         goto advance_and_error;
536                 }
537                 result->attDescs[i].tableid = tableid;
538                 result->attDescs[i].columnid = columnid;
539                 result->attDescs[i].format = format;
540                 result->attDescs[i].typid = typid;
541                 result->attDescs[i].typlen = typlen;
542                 result->attDescs[i].atttypmod = atttypmod;
543
544                 if (format != 1)
545                         result->binary = 0;
546         }
547
548         /* Sanity check that we absorbed all the data */
549         if (conn->inCursor != conn->inStart + 5 + msgLength)
550         {
551                 errmsg = libpq_gettext("extraneous data in \"T\" message");
552                 goto advance_and_error;
553         }
554
555         /* Success! */
556         conn->result = result;
557
558         /* Advance inStart to show that the "T" message has been processed. */
559         conn->inStart = conn->inCursor;
560
561         /*
562          * If we're doing a Describe, we're done, and ready to pass the result
563          * back to the client.
564          */
565         if (conn->queryclass == PGQUERY_DESCRIBE)
566         {
567                 conn->asyncStatus = PGASYNC_READY;
568                 return 0;
569         }
570
571         /*
572          * We could perform additional setup for the new result set here, but for
573          * now there's nothing else to do.
574          */
575
576         /* And we're done. */
577         return 0;
578
579 advance_and_error:
580         /* Discard unsaved result, if any */
581         if (result && result != conn->result)
582                 PQclear(result);
583
584         /* Discard the failed message by pretending we read it */
585         conn->inStart += 5 + msgLength;
586
587         /*
588          * Replace partially constructed result with an error result. First
589          * discard the old result to try to win back some memory.
590          */
591         pqClearAsyncResult(conn);
592
593         /*
594          * If preceding code didn't provide an error message, assume "out of
595          * memory" was meant.  The advantage of having this special case is that
596          * freeing the old result first greatly improves the odds that gettext()
597          * will succeed in providing a translation.
598          */
599         if (!errmsg)
600                 errmsg = libpq_gettext("out of memory for query result");
601
602         printfPQExpBuffer(&conn->errorMessage, "%s\n", errmsg);
603         pqSaveErrorResult(conn);
604
605         /*
606          * Return zero to allow input parsing to continue.      Subsequent "D"
607          * messages will be ignored until we get to end of data, since an error
608          * result is already set up.
609          */
610         return 0;
611 }
612
613 /*
614  * parseInput subroutine to read a 't' (ParameterDescription) message.
615  * We'll build a new PGresult structure containing the parameter data.
616  * Returns: 0 if completed message, EOF if not enough data yet.
617  *
618  * Note that if we run out of data, we have to release the partially
619  * constructed PGresult, and rebuild it again next time.  Fortunately,
620  * that shouldn't happen often, since 't' messages usually fit in a packet.
621  */
622 static int
623 getParamDescriptions(PGconn *conn)
624 {
625         PGresult   *result;
626         int                     nparams;
627         int                     i;
628
629         result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
630         if (!result)
631                 goto failure;
632
633         /* parseInput already read the 't' label and message length. */
634         /* the next two bytes are the number of parameters */
635         if (pqGetInt(&(result->numParameters), 2, conn))
636                 goto failure;
637         nparams = result->numParameters;
638
639         /* allocate space for the parameter descriptors */
640         if (nparams > 0)
641         {
642                 result->paramDescs = (PGresParamDesc *)
643                         pqResultAlloc(result, nparams * sizeof(PGresParamDesc), TRUE);
644                 if (!result->paramDescs)
645                         goto failure;
646                 MemSet(result->paramDescs, 0, nparams * sizeof(PGresParamDesc));
647         }
648
649         /* get parameter info */
650         for (i = 0; i < nparams; i++)
651         {
652                 int                     typid;
653
654                 if (pqGetInt(&typid, 4, conn))
655                         goto failure;
656                 result->paramDescs[i].typid = typid;
657         }
658
659         /* Success! */
660         conn->result = result;
661         return 0;
662
663 failure:
664         PQclear(result);
665         return EOF;
666 }
667
668 /*
669  * parseInput subroutine to read a 'D' (row data) message.
670  * We fill rowbuf with column pointers and then call the row processor.
671  * Returns: 0 if processed message successfully, EOF to suspend parsing
672  * (the latter case is not actually used currently).
673  * In either case, conn->inStart has been advanced past the message.
674  */
675 static int
676 getAnotherTuple(PGconn *conn, int msgLength)
677 {
678         PGresult   *result = conn->result;
679         int                     nfields = result->numAttributes;
680         const char *errmsg;
681         PGdataValue *rowbuf;
682         int                     tupnfields;             /* # fields from tuple */
683         int                     vlen;                   /* length of the current field value */
684         int                     i;
685
686         /* Get the field count and make sure it's what we expect */
687         if (pqGetInt(&tupnfields, 2, conn))
688         {
689                 /* We should not run out of data here, so complain */
690                 errmsg = libpq_gettext("insufficient data in \"D\" message");
691                 goto advance_and_error;
692         }
693
694         if (tupnfields != nfields)
695         {
696                 errmsg = libpq_gettext("unexpected field count in \"D\" message");
697                 goto advance_and_error;
698         }
699
700         /* Resize row buffer if needed */
701         rowbuf = conn->rowBuf;
702         if (nfields > conn->rowBufLen)
703         {
704                 rowbuf = (PGdataValue *) realloc(rowbuf,
705                                                                                  nfields * sizeof(PGdataValue));
706                 if (!rowbuf)
707                 {
708                         errmsg = NULL;          /* means "out of memory", see below */
709                         goto advance_and_error;
710                 }
711                 conn->rowBuf = rowbuf;
712                 conn->rowBufLen = nfields;
713         }
714
715         /* Scan the fields */
716         for (i = 0; i < nfields; i++)
717         {
718                 /* get the value length */
719                 if (pqGetInt(&vlen, 4, conn))
720                 {
721                         /* We should not run out of data here, so complain */
722                         errmsg = libpq_gettext("insufficient data in \"D\" message");
723                         goto advance_and_error;
724                 }
725                 rowbuf[i].len = vlen;
726
727                 /*
728                  * rowbuf[i].value always points to the next address in the data
729                  * buffer even if the value is NULL.  This allows row processors to
730                  * estimate data sizes more easily.
731                  */
732                 rowbuf[i].value = conn->inBuffer + conn->inCursor;
733
734                 /* Skip over the data value */
735                 if (vlen > 0)
736                 {
737                         if (pqSkipnchar(vlen, conn))
738                         {
739                                 /* We should not run out of data here, so complain */
740                                 errmsg = libpq_gettext("insufficient data in \"D\" message");
741                                 goto advance_and_error;
742                         }
743                 }
744         }
745
746         /* Sanity check that we absorbed all the data */
747         if (conn->inCursor != conn->inStart + 5 + msgLength)
748         {
749                 errmsg = libpq_gettext("extraneous data in \"D\" message");
750                 goto advance_and_error;
751         }
752
753         /* Advance inStart to show that the "D" message has been processed. */
754         conn->inStart = conn->inCursor;
755
756         /* Process the collected row */
757         errmsg = NULL;
758         if (pqRowProcessor(conn, &errmsg))
759                 return 0;                               /* normal, successful exit */
760
761         goto set_error_result;          /* pqRowProcessor failed, report it */
762
763 advance_and_error:
764         /* Discard the failed message by pretending we read it */
765         conn->inStart += 5 + msgLength;
766
767 set_error_result:
768
769         /*
770          * Replace partially constructed result with an error result. First
771          * discard the old result to try to win back some memory.
772          */
773         pqClearAsyncResult(conn);
774
775         /*
776          * If preceding code didn't provide an error message, assume "out of
777          * memory" was meant.  The advantage of having this special case is that
778          * freeing the old result first greatly improves the odds that gettext()
779          * will succeed in providing a translation.
780          */
781         if (!errmsg)
782                 errmsg = libpq_gettext("out of memory for query result");
783
784         printfPQExpBuffer(&conn->errorMessage, "%s\n", errmsg);
785         pqSaveErrorResult(conn);
786
787         /*
788          * Return zero to allow input parsing to continue.      Subsequent "D"
789          * messages will be ignored until we get to end of data, since an error
790          * result is already set up.
791          */
792         return 0;
793 }
794
795
796 /*
797  * Attempt to read an Error or Notice response message.
798  * This is possible in several places, so we break it out as a subroutine.
799  * Entry: 'E' or 'N' message type and length have already been consumed.
800  * Exit: returns 0 if successfully consumed message.
801  *               returns EOF if not enough data.
802  */
803 int
804 pqGetErrorNotice3(PGconn *conn, bool isError)
805 {
806         PGresult   *res = NULL;
807         PQExpBufferData workBuf;
808         char            id;
809         const char *val;
810         const char *querytext = NULL;
811         int                     querypos = 0;
812
813         /*
814          * Since the fields might be pretty long, we create a temporary
815          * PQExpBuffer rather than using conn->workBuffer.      workBuffer is intended
816          * for stuff that is expected to be short.      We shouldn't use
817          * conn->errorMessage either, since this might be only a notice.
818          */
819         initPQExpBuffer(&workBuf);
820
821         /*
822          * Make a PGresult to hold the accumulated fields.      We temporarily lie
823          * about the result status, so that PQmakeEmptyPGresult doesn't uselessly
824          * copy conn->errorMessage.
825          */
826         res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
827         if (!res)
828                 goto fail;
829         res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
830
831         /*
832          * Read the fields and save into res.
833          */
834         for (;;)
835         {
836                 if (pqGetc(&id, conn))
837                         goto fail;
838                 if (id == '\0')
839                         break;                          /* terminator found */
840                 if (pqGets(&workBuf, conn))
841                         goto fail;
842                 pqSaveMessageField(res, id, workBuf.data);
843         }
844
845         /*
846          * Now build the "overall" error message for PQresultErrorMessage.
847          *
848          * Also, save the SQLSTATE in conn->last_sqlstate.
849          */
850         resetPQExpBuffer(&workBuf);
851         val = PQresultErrorField(res, PG_DIAG_SEVERITY);
852         if (val)
853                 appendPQExpBuffer(&workBuf, "%s:  ", val);
854         val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
855         if (val)
856         {
857                 if (strlen(val) < sizeof(conn->last_sqlstate))
858                         strcpy(conn->last_sqlstate, val);
859                 if (conn->verbosity == PQERRORS_VERBOSE)
860                         appendPQExpBuffer(&workBuf, "%s: ", val);
861         }
862         val = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
863         if (val)
864                 appendPQExpBufferStr(&workBuf, val);
865         val = PQresultErrorField(res, PG_DIAG_STATEMENT_POSITION);
866         if (val)
867         {
868                 if (conn->verbosity != PQERRORS_TERSE && conn->last_query != NULL)
869                 {
870                         /* emit position as a syntax cursor display */
871                         querytext = conn->last_query;
872                         querypos = atoi(val);
873                 }
874                 else
875                 {
876                         /* emit position as text addition to primary message */
877                         /* translator: %s represents a digit string */
878                         appendPQExpBuffer(&workBuf, libpq_gettext(" at character %s"),
879                                                           val);
880                 }
881         }
882         else
883         {
884                 val = PQresultErrorField(res, PG_DIAG_INTERNAL_POSITION);
885                 if (val)
886                 {
887                         querytext = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
888                         if (conn->verbosity != PQERRORS_TERSE && querytext != NULL)
889                         {
890                                 /* emit position as a syntax cursor display */
891                                 querypos = atoi(val);
892                         }
893                         else
894                         {
895                                 /* emit position as text addition to primary message */
896                                 /* translator: %s represents a digit string */
897                                 appendPQExpBuffer(&workBuf, libpq_gettext(" at character %s"),
898                                                                   val);
899                         }
900                 }
901         }
902         appendPQExpBufferChar(&workBuf, '\n');
903         if (conn->verbosity != PQERRORS_TERSE)
904         {
905                 if (querytext && querypos > 0)
906                         reportErrorPosition(&workBuf, querytext, querypos,
907                                                                 conn->client_encoding);
908                 val = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
909                 if (val)
910                         appendPQExpBuffer(&workBuf, libpq_gettext("DETAIL:  %s\n"), val);
911                 val = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
912                 if (val)
913                         appendPQExpBuffer(&workBuf, libpq_gettext("HINT:  %s\n"), val);
914                 val = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
915                 if (val)
916                         appendPQExpBuffer(&workBuf, libpq_gettext("QUERY:  %s\n"), val);
917                 val = PQresultErrorField(res, PG_DIAG_CONTEXT);
918                 if (val)
919                         appendPQExpBuffer(&workBuf, libpq_gettext("CONTEXT:  %s\n"), val);
920         }
921         if (conn->verbosity == PQERRORS_VERBOSE)
922         {
923                 val = PQresultErrorField(res, PG_DIAG_SCHEMA_NAME);
924                 if (val)
925                         appendPQExpBuffer(&workBuf,
926                                                           libpq_gettext("SCHEMA NAME:  %s\n"), val);
927                 val = PQresultErrorField(res, PG_DIAG_TABLE_NAME);
928                 if (val)
929                         appendPQExpBuffer(&workBuf,
930                                                           libpq_gettext("TABLE NAME:  %s\n"), val);
931                 val = PQresultErrorField(res, PG_DIAG_COLUMN_NAME);
932                 if (val)
933                         appendPQExpBuffer(&workBuf,
934                                                           libpq_gettext("COLUMN NAME:  %s\n"), val);
935                 val = PQresultErrorField(res, PG_DIAG_DATATYPE_NAME);
936                 if (val)
937                         appendPQExpBuffer(&workBuf,
938                                                           libpq_gettext("DATATYPE NAME:  %s\n"), val);
939                 val = PQresultErrorField(res, PG_DIAG_CONSTRAINT_NAME);
940                 if (val)
941                         appendPQExpBuffer(&workBuf,
942                                                           libpq_gettext("CONSTRAINT NAME:  %s\n"), val);
943         }
944         if (conn->verbosity == PQERRORS_VERBOSE)
945         {
946                 const char *valf;
947                 const char *vall;
948
949                 valf = PQresultErrorField(res, PG_DIAG_SOURCE_FILE);
950                 vall = PQresultErrorField(res, PG_DIAG_SOURCE_LINE);
951                 val = PQresultErrorField(res, PG_DIAG_SOURCE_FUNCTION);
952                 if (val || valf || vall)
953                 {
954                         appendPQExpBufferStr(&workBuf, libpq_gettext("LOCATION:  "));
955                         if (val)
956                                 appendPQExpBuffer(&workBuf, libpq_gettext("%s, "), val);
957                         if (valf && vall)       /* unlikely we'd have just one */
958                                 appendPQExpBuffer(&workBuf, libpq_gettext("%s:%s"),
959                                                                   valf, vall);
960                         appendPQExpBufferChar(&workBuf, '\n');
961                 }
962         }
963
964         /*
965          * Either save error as current async result, or just emit the notice.
966          */
967         if (isError)
968         {
969                 res->errMsg = pqResultStrdup(res, workBuf.data);
970                 if (!res->errMsg)
971                         goto fail;
972                 pqClearAsyncResult(conn);
973                 conn->result = res;
974                 appendPQExpBufferStr(&conn->errorMessage, workBuf.data);
975         }
976         else
977         {
978                 /* We can cheat a little here and not copy the message. */
979                 res->errMsg = workBuf.data;
980                 if (res->noticeHooks.noticeRec != NULL)
981                         (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
982                 PQclear(res);
983         }
984
985         termPQExpBuffer(&workBuf);
986         return 0;
987
988 fail:
989         PQclear(res);
990         termPQExpBuffer(&workBuf);
991         return EOF;
992 }
993
994 /*
995  * Add an error-location display to the error message under construction.
996  *
997  * The cursor location is measured in logical characters; the query string
998  * is presumed to be in the specified encoding.
999  */
1000 static void
1001 reportErrorPosition(PQExpBuffer msg, const char *query, int loc, int encoding)
1002 {
1003 #define DISPLAY_SIZE    60              /* screen width limit, in screen cols */
1004 #define MIN_RIGHT_CUT   10              /* try to keep this far away from EOL */
1005
1006         char       *wquery;
1007         int                     slen,
1008                                 cno,
1009                                 i,
1010                            *qidx,
1011                            *scridx,
1012                                 qoffset,
1013                                 scroffset,
1014                                 ibeg,
1015                                 iend,
1016                                 loc_line;
1017         bool            mb_encoding,
1018                                 beg_trunc,
1019                                 end_trunc;
1020
1021         /* Convert loc from 1-based to 0-based; no-op if out of range */
1022         loc--;
1023         if (loc < 0)
1024                 return;
1025
1026         /* Need a writable copy of the query */
1027         wquery = strdup(query);
1028         if (wquery == NULL)
1029                 return;                                 /* fail silently if out of memory */
1030
1031         /*
1032          * Each character might occupy multiple physical bytes in the string, and
1033          * in some Far Eastern character sets it might take more than one screen
1034          * column as well.      We compute the starting byte offset and starting
1035          * screen column of each logical character, and store these in qidx[] and
1036          * scridx[] respectively.
1037          */
1038
1039         /* we need a safe allocation size... */
1040         slen = strlen(wquery) + 1;
1041
1042         qidx = (int *) malloc(slen * sizeof(int));
1043         if (qidx == NULL)
1044         {
1045                 free(wquery);
1046                 return;
1047         }
1048         scridx = (int *) malloc(slen * sizeof(int));
1049         if (scridx == NULL)
1050         {
1051                 free(qidx);
1052                 free(wquery);
1053                 return;
1054         }
1055
1056         /* We can optimize a bit if it's a single-byte encoding */
1057         mb_encoding = (pg_encoding_max_length(encoding) != 1);
1058
1059         /*
1060          * Within the scanning loop, cno is the current character's logical
1061          * number, qoffset is its offset in wquery, and scroffset is its starting
1062          * logical screen column (all indexed from 0).  "loc" is the logical
1063          * character number of the error location.      We scan to determine loc_line
1064          * (the 1-based line number containing loc) and ibeg/iend (first character
1065          * number and last+1 character number of the line containing loc). Note
1066          * that qidx[] and scridx[] are filled only as far as iend.
1067          */
1068         qoffset = 0;
1069         scroffset = 0;
1070         loc_line = 1;
1071         ibeg = 0;
1072         iend = -1;                                      /* -1 means not set yet */
1073
1074         for (cno = 0; wquery[qoffset] != '\0'; cno++)
1075         {
1076                 char            ch = wquery[qoffset];
1077
1078                 qidx[cno] = qoffset;
1079                 scridx[cno] = scroffset;
1080
1081                 /*
1082                  * Replace tabs with spaces in the writable copy.  (Later we might
1083                  * want to think about coping with their variable screen width, but
1084                  * not today.)
1085                  */
1086                 if (ch == '\t')
1087                         wquery[qoffset] = ' ';
1088
1089                 /*
1090                  * If end-of-line, count lines and mark positions. Each \r or \n
1091                  * counts as a line except when \r \n appear together.
1092                  */
1093                 else if (ch == '\r' || ch == '\n')
1094                 {
1095                         if (cno < loc)
1096                         {
1097                                 if (ch == '\r' ||
1098                                         cno == 0 ||
1099                                         wquery[qidx[cno - 1]] != '\r')
1100                                         loc_line++;
1101                                 /* extract beginning = last line start before loc. */
1102                                 ibeg = cno + 1;
1103                         }
1104                         else
1105                         {
1106                                 /* set extract end. */
1107                                 iend = cno;
1108                                 /* done scanning. */
1109                                 break;
1110                         }
1111                 }
1112
1113                 /* Advance */
1114                 if (mb_encoding)
1115                 {
1116                         int                     w;
1117
1118                         w = pg_encoding_dsplen(encoding, &wquery[qoffset]);
1119                         /* treat any non-tab control chars as width 1 */
1120                         if (w <= 0)
1121                                 w = 1;
1122                         scroffset += w;
1123                         qoffset += pg_encoding_mblen(encoding, &wquery[qoffset]);
1124                 }
1125                 else
1126                 {
1127                         /* We assume wide chars only exist in multibyte encodings */
1128                         scroffset++;
1129                         qoffset++;
1130                 }
1131         }
1132         /* Fix up if we didn't find an end-of-line after loc */
1133         if (iend < 0)
1134         {
1135                 iend = cno;                             /* query length in chars, +1 */
1136                 qidx[iend] = qoffset;
1137                 scridx[iend] = scroffset;
1138         }
1139
1140         /* Print only if loc is within computed query length */
1141         if (loc <= cno)
1142         {
1143                 /* If the line extracted is too long, we truncate it. */
1144                 beg_trunc = false;
1145                 end_trunc = false;
1146                 if (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1147                 {
1148                         /*
1149                          * We first truncate right if it is enough.  This code might be
1150                          * off a space or so on enforcing MIN_RIGHT_CUT if there's a wide
1151                          * character right there, but that should be okay.
1152                          */
1153                         if (scridx[ibeg] + DISPLAY_SIZE >= scridx[loc] + MIN_RIGHT_CUT)
1154                         {
1155                                 while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1156                                         iend--;
1157                                 end_trunc = true;
1158                         }
1159                         else
1160                         {
1161                                 /* Truncate right if not too close to loc. */
1162                                 while (scridx[loc] + MIN_RIGHT_CUT < scridx[iend])
1163                                 {
1164                                         iend--;
1165                                         end_trunc = true;
1166                                 }
1167
1168                                 /* Truncate left if still too long. */
1169                                 while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1170                                 {
1171                                         ibeg++;
1172                                         beg_trunc = true;
1173                                 }
1174                         }
1175                 }
1176
1177                 /* truncate working copy at desired endpoint */
1178                 wquery[qidx[iend]] = '\0';
1179
1180                 /* Begin building the finished message. */
1181                 i = msg->len;
1182                 appendPQExpBuffer(msg, libpq_gettext("LINE %d: "), loc_line);
1183                 if (beg_trunc)
1184                         appendPQExpBufferStr(msg, "...");
1185
1186                 /*
1187                  * While we have the prefix in the msg buffer, compute its screen
1188                  * width.
1189                  */
1190                 scroffset = 0;
1191                 for (; i < msg->len; i += pg_encoding_mblen(encoding, &msg->data[i]))
1192                 {
1193                         int                     w = pg_encoding_dsplen(encoding, &msg->data[i]);
1194
1195                         if (w <= 0)
1196                                 w = 1;
1197                         scroffset += w;
1198                 }
1199
1200                 /* Finish up the LINE message line. */
1201                 appendPQExpBufferStr(msg, &wquery[qidx[ibeg]]);
1202                 if (end_trunc)
1203                         appendPQExpBufferStr(msg, "...");
1204                 appendPQExpBufferChar(msg, '\n');
1205
1206                 /* Now emit the cursor marker line. */
1207                 scroffset += scridx[loc] - scridx[ibeg];
1208                 for (i = 0; i < scroffset; i++)
1209                         appendPQExpBufferChar(msg, ' ');
1210                 appendPQExpBufferChar(msg, '^');
1211                 appendPQExpBufferChar(msg, '\n');
1212         }
1213
1214         /* Clean up. */
1215         free(scridx);
1216         free(qidx);
1217         free(wquery);
1218 }
1219
1220
1221 /*
1222  * Attempt to read a ParameterStatus message.
1223  * This is possible in several places, so we break it out as a subroutine.
1224  * Entry: 'S' message type and length have already been consumed.
1225  * Exit: returns 0 if successfully consumed message.
1226  *               returns EOF if not enough data.
1227  */
1228 static int
1229 getParameterStatus(PGconn *conn)
1230 {
1231         PQExpBufferData valueBuf;
1232
1233         /* Get the parameter name */
1234         if (pqGets(&conn->workBuffer, conn))
1235                 return EOF;
1236         /* Get the parameter value (could be large) */
1237         initPQExpBuffer(&valueBuf);
1238         if (pqGets(&valueBuf, conn))
1239         {
1240                 termPQExpBuffer(&valueBuf);
1241                 return EOF;
1242         }
1243         /* And save it */
1244         pqSaveParameterStatus(conn, conn->workBuffer.data, valueBuf.data);
1245         termPQExpBuffer(&valueBuf);
1246         return 0;
1247 }
1248
1249
1250 /*
1251  * Attempt to read a Notify response message.
1252  * This is possible in several places, so we break it out as a subroutine.
1253  * Entry: 'A' message type and length have already been consumed.
1254  * Exit: returns 0 if successfully consumed Notify message.
1255  *               returns EOF if not enough data.
1256  */
1257 static int
1258 getNotify(PGconn *conn)
1259 {
1260         int                     be_pid;
1261         char       *svname;
1262         int                     nmlen;
1263         int                     extralen;
1264         PGnotify   *newNotify;
1265
1266         if (pqGetInt(&be_pid, 4, conn))
1267                 return EOF;
1268         if (pqGets(&conn->workBuffer, conn))
1269                 return EOF;
1270         /* must save name while getting extra string */
1271         svname = strdup(conn->workBuffer.data);
1272         if (!svname)
1273                 return EOF;
1274         if (pqGets(&conn->workBuffer, conn))
1275         {
1276                 free(svname);
1277                 return EOF;
1278         }
1279
1280         /*
1281          * Store the strings right after the PQnotify structure so it can all be
1282          * freed at once.  We don't use NAMEDATALEN because we don't want to tie
1283          * this interface to a specific server name length.
1284          */
1285         nmlen = strlen(svname);
1286         extralen = strlen(conn->workBuffer.data);
1287         newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + extralen + 2);
1288         if (newNotify)
1289         {
1290                 newNotify->relname = (char *) newNotify + sizeof(PGnotify);
1291                 strcpy(newNotify->relname, svname);
1292                 newNotify->extra = newNotify->relname + nmlen + 1;
1293                 strcpy(newNotify->extra, conn->workBuffer.data);
1294                 newNotify->be_pid = be_pid;
1295                 newNotify->next = NULL;
1296                 if (conn->notifyTail)
1297                         conn->notifyTail->next = newNotify;
1298                 else
1299                         conn->notifyHead = newNotify;
1300                 conn->notifyTail = newNotify;
1301         }
1302
1303         free(svname);
1304         return 0;
1305 }
1306
1307 /*
1308  * getCopyStart - process CopyInResponse, CopyOutResponse or
1309  * CopyBothResponse message
1310  *
1311  * parseInput already read the message type and length.
1312  */
1313 static int
1314 getCopyStart(PGconn *conn, ExecStatusType copytype)
1315 {
1316         PGresult   *result;
1317         int                     nfields;
1318         int                     i;
1319
1320         result = PQmakeEmptyPGresult(conn, copytype);
1321         if (!result)
1322                 goto failure;
1323
1324         if (pqGetc(&conn->copy_is_binary, conn))
1325                 goto failure;
1326         result->binary = conn->copy_is_binary;
1327         /* the next two bytes are the number of fields  */
1328         if (pqGetInt(&(result->numAttributes), 2, conn))
1329                 goto failure;
1330         nfields = result->numAttributes;
1331
1332         /* allocate space for the attribute descriptors */
1333         if (nfields > 0)
1334         {
1335                 result->attDescs = (PGresAttDesc *)
1336                         pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
1337                 if (!result->attDescs)
1338                         goto failure;
1339                 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
1340         }
1341
1342         for (i = 0; i < nfields; i++)
1343         {
1344                 int                     format;
1345
1346                 if (pqGetInt(&format, 2, conn))
1347                         goto failure;
1348
1349                 /*
1350                  * Since pqGetInt treats 2-byte integers as unsigned, we need to
1351                  * coerce these results to signed form.
1352                  */
1353                 format = (int) ((int16) format);
1354                 result->attDescs[i].format = format;
1355         }
1356
1357         /* Success! */
1358         conn->result = result;
1359         return 0;
1360
1361 failure:
1362         PQclear(result);
1363         return EOF;
1364 }
1365
1366 /*
1367  * getReadyForQuery - process ReadyForQuery message
1368  */
1369 static int
1370 getReadyForQuery(PGconn *conn)
1371 {
1372         char            xact_status;
1373
1374         if (pqGetc(&xact_status, conn))
1375                 return EOF;
1376         switch (xact_status)
1377         {
1378                 case 'I':
1379                         conn->xactStatus = PQTRANS_IDLE;
1380                         break;
1381                 case 'T':
1382                         conn->xactStatus = PQTRANS_INTRANS;
1383                         break;
1384                 case 'E':
1385                         conn->xactStatus = PQTRANS_INERROR;
1386                         break;
1387                 default:
1388                         conn->xactStatus = PQTRANS_UNKNOWN;
1389                         break;
1390         }
1391
1392         return 0;
1393 }
1394
1395 /*
1396  * getCopyDataMessage - fetch next CopyData message, process async messages
1397  *
1398  * Returns length word of CopyData message (> 0), or 0 if no complete
1399  * message available, -1 if end of copy, -2 if error.
1400  */
1401 static int
1402 getCopyDataMessage(PGconn *conn)
1403 {
1404         char            id;
1405         int                     msgLength;
1406         int                     avail;
1407
1408         for (;;)
1409         {
1410                 /*
1411                  * Do we have the next input message?  To make life simpler for async
1412                  * callers, we keep returning 0 until the next message is fully
1413                  * available, even if it is not Copy Data.
1414                  */
1415                 conn->inCursor = conn->inStart;
1416                 if (pqGetc(&id, conn))
1417                         return 0;
1418                 if (pqGetInt(&msgLength, 4, conn))
1419                         return 0;
1420                 if (msgLength < 4)
1421                 {
1422                         handleSyncLoss(conn, id, msgLength);
1423                         return -2;
1424                 }
1425                 avail = conn->inEnd - conn->inCursor;
1426                 if (avail < msgLength - 4)
1427                 {
1428                         /*
1429                          * Before returning, enlarge the input buffer if needed to hold
1430                          * the whole message.  See notes in parseInput.
1431                          */
1432                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength - 4,
1433                                                                          conn))
1434                         {
1435                                 /*
1436                                  * XXX add some better recovery code... plan is to skip over
1437                                  * the message using its length, then report an error. For the
1438                                  * moment, just treat this like loss of sync (which indeed it
1439                                  * might be!)
1440                                  */
1441                                 handleSyncLoss(conn, id, msgLength);
1442                                 return -2;
1443                         }
1444                         return 0;
1445                 }
1446
1447                 /*
1448                  * If it's a legitimate async message type, process it.  (NOTIFY
1449                  * messages are not currently possible here, but we handle them for
1450                  * completeness.)  Otherwise, if it's anything except Copy Data,
1451                  * report end-of-copy.
1452                  */
1453                 switch (id)
1454                 {
1455                         case 'A':                       /* NOTIFY */
1456                                 if (getNotify(conn))
1457                                         return 0;
1458                                 break;
1459                         case 'N':                       /* NOTICE */
1460                                 if (pqGetErrorNotice3(conn, false))
1461                                         return 0;
1462                                 break;
1463                         case 'S':                       /* ParameterStatus */
1464                                 if (getParameterStatus(conn))
1465                                         return 0;
1466                                 break;
1467                         case 'd':                       /* Copy Data, pass it back to caller */
1468                                 return msgLength;
1469                         case 'c':
1470
1471                                 /*
1472                                  * If this is a CopyDone message, exit COPY_OUT mode and let
1473                                  * caller read status with PQgetResult().  If we're in
1474                                  * COPY_BOTH mode, return to COPY_IN mode.
1475                                  */
1476                                 if (conn->asyncStatus == PGASYNC_COPY_BOTH)
1477                                         conn->asyncStatus = PGASYNC_COPY_IN;
1478                                 else
1479                                         conn->asyncStatus = PGASYNC_BUSY;
1480                                 return -1;
1481                         default:                        /* treat as end of copy */
1482
1483                                 /*
1484                                  * Any other message terminates either COPY_IN or COPY_BOTH
1485                                  * mode.
1486                                  */
1487                                 conn->asyncStatus = PGASYNC_BUSY;
1488                                 return -1;
1489                 }
1490
1491                 /* Drop the processed message and loop around for another */
1492                 conn->inStart = conn->inCursor;
1493         }
1494 }
1495
1496 /*
1497  * PQgetCopyData - read a row of data from the backend during COPY OUT
1498  * or COPY BOTH
1499  *
1500  * If successful, sets *buffer to point to a malloc'd row of data, and
1501  * returns row length (always > 0) as result.
1502  * Returns 0 if no row available yet (only possible if async is true),
1503  * -1 if end of copy (consult PQgetResult), or -2 if error (consult
1504  * PQerrorMessage).
1505  */
1506 int
1507 pqGetCopyData3(PGconn *conn, char **buffer, int async)
1508 {
1509         int                     msgLength;
1510
1511         for (;;)
1512         {
1513                 /*
1514                  * Collect the next input message.      To make life simpler for async
1515                  * callers, we keep returning 0 until the next message is fully
1516                  * available, even if it is not Copy Data.
1517                  */
1518                 msgLength = getCopyDataMessage(conn);
1519                 if (msgLength < 0)
1520                         return msgLength;       /* end-of-copy or error */
1521                 if (msgLength == 0)
1522                 {
1523                         /* Don't block if async read requested */
1524                         if (async)
1525                                 return 0;
1526                         /* Need to load more data */
1527                         if (pqWait(TRUE, FALSE, conn) ||
1528                                 pqReadData(conn) < 0)
1529                                 return -2;
1530                         continue;
1531                 }
1532
1533                 /*
1534                  * Drop zero-length messages (shouldn't happen anyway).  Otherwise
1535                  * pass the data back to the caller.
1536                  */
1537                 msgLength -= 4;
1538                 if (msgLength > 0)
1539                 {
1540                         *buffer = (char *) malloc(msgLength + 1);
1541                         if (*buffer == NULL)
1542                         {
1543                                 printfPQExpBuffer(&conn->errorMessage,
1544                                                                   libpq_gettext("out of memory\n"));
1545                                 return -2;
1546                         }
1547                         memcpy(*buffer, &conn->inBuffer[conn->inCursor], msgLength);
1548                         (*buffer)[msgLength] = '\0';            /* Add terminating null */
1549
1550                         /* Mark message consumed */
1551                         conn->inStart = conn->inCursor + msgLength;
1552
1553                         return msgLength;
1554                 }
1555
1556                 /* Empty, so drop it and loop around for another */
1557                 conn->inStart = conn->inCursor;
1558         }
1559 }
1560
1561 /*
1562  * PQgetline - gets a newline-terminated string from the backend.
1563  *
1564  * See fe-exec.c for documentation.
1565  */
1566 int
1567 pqGetline3(PGconn *conn, char *s, int maxlen)
1568 {
1569         int                     status;
1570
1571         if (conn->sock == PGINVALID_SOCKET ||
1572                 (conn->asyncStatus != PGASYNC_COPY_OUT &&
1573                  conn->asyncStatus != PGASYNC_COPY_BOTH) ||
1574                 conn->copy_is_binary)
1575         {
1576                 printfPQExpBuffer(&conn->errorMessage,
1577                                           libpq_gettext("PQgetline: not doing text COPY OUT\n"));
1578                 *s = '\0';
1579                 return EOF;
1580         }
1581
1582         while ((status = PQgetlineAsync(conn, s, maxlen - 1)) == 0)
1583         {
1584                 /* need to load more data */
1585                 if (pqWait(TRUE, FALSE, conn) ||
1586                         pqReadData(conn) < 0)
1587                 {
1588                         *s = '\0';
1589                         return EOF;
1590                 }
1591         }
1592
1593         if (status < 0)
1594         {
1595                 /* End of copy detected; gin up old-style terminator */
1596                 strcpy(s, "\\.");
1597                 return 0;
1598         }
1599
1600         /* Add null terminator, and strip trailing \n if present */
1601         if (s[status - 1] == '\n')
1602         {
1603                 s[status - 1] = '\0';
1604                 return 0;
1605         }
1606         else
1607         {
1608                 s[status] = '\0';
1609                 return 1;
1610         }
1611 }
1612
1613 /*
1614  * PQgetlineAsync - gets a COPY data row without blocking.
1615  *
1616  * See fe-exec.c for documentation.
1617  */
1618 int
1619 pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
1620 {
1621         int                     msgLength;
1622         int                     avail;
1623
1624         if (conn->asyncStatus != PGASYNC_COPY_OUT
1625                 && conn->asyncStatus != PGASYNC_COPY_BOTH)
1626                 return -1;                              /* we are not doing a copy... */
1627
1628         /*
1629          * Recognize the next input message.  To make life simpler for async
1630          * callers, we keep returning 0 until the next message is fully available
1631          * even if it is not Copy Data.  This should keep PQendcopy from blocking.
1632          * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.)
1633          */
1634         msgLength = getCopyDataMessage(conn);
1635         if (msgLength < 0)
1636                 return -1;                              /* end-of-copy or error */
1637         if (msgLength == 0)
1638                 return 0;                               /* no data yet */
1639
1640         /*
1641          * Move data from libpq's buffer to the caller's.  In the case where a
1642          * prior call found the caller's buffer too small, we use
1643          * conn->copy_already_done to remember how much of the row was already
1644          * returned to the caller.
1645          */
1646         conn->inCursor += conn->copy_already_done;
1647         avail = msgLength - 4 - conn->copy_already_done;
1648         if (avail <= bufsize)
1649         {
1650                 /* Able to consume the whole message */
1651                 memcpy(buffer, &conn->inBuffer[conn->inCursor], avail);
1652                 /* Mark message consumed */
1653                 conn->inStart = conn->inCursor + avail;
1654                 /* Reset state for next time */
1655                 conn->copy_already_done = 0;
1656                 return avail;
1657         }
1658         else
1659         {
1660                 /* We must return a partial message */
1661                 memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize);
1662                 /* The message is NOT consumed from libpq's buffer */
1663                 conn->copy_already_done += bufsize;
1664                 return bufsize;
1665         }
1666 }
1667
1668 /*
1669  * PQendcopy
1670  *
1671  * See fe-exec.c for documentation.
1672  */
1673 int
1674 pqEndcopy3(PGconn *conn)
1675 {
1676         PGresult   *result;
1677
1678         if (conn->asyncStatus != PGASYNC_COPY_IN &&
1679                 conn->asyncStatus != PGASYNC_COPY_OUT &&
1680                 conn->asyncStatus != PGASYNC_COPY_BOTH)
1681         {
1682                 printfPQExpBuffer(&conn->errorMessage,
1683                                                   libpq_gettext("no COPY in progress\n"));
1684                 return 1;
1685         }
1686
1687         /* Send the CopyDone message if needed */
1688         if (conn->asyncStatus == PGASYNC_COPY_IN ||
1689                 conn->asyncStatus == PGASYNC_COPY_BOTH)
1690         {
1691                 if (pqPutMsgStart('c', false, conn) < 0 ||
1692                         pqPutMsgEnd(conn) < 0)
1693                         return 1;
1694
1695                 /*
1696                  * If we sent the COPY command in extended-query mode, we must issue a
1697                  * Sync as well.
1698                  */
1699                 if (conn->queryclass != PGQUERY_SIMPLE)
1700                 {
1701                         if (pqPutMsgStart('S', false, conn) < 0 ||
1702                                 pqPutMsgEnd(conn) < 0)
1703                                 return 1;
1704                 }
1705         }
1706
1707         /*
1708          * make sure no data is waiting to be sent, abort if we are non-blocking
1709          * and the flush fails
1710          */
1711         if (pqFlush(conn) && pqIsnonblocking(conn))
1712                 return 1;
1713
1714         /* Return to active duty */
1715         conn->asyncStatus = PGASYNC_BUSY;
1716         resetPQExpBuffer(&conn->errorMessage);
1717
1718         /*
1719          * Non blocking connections may have to abort at this point.  If everyone
1720          * played the game there should be no problem, but in error scenarios the
1721          * expected messages may not have arrived yet.  (We are assuming that the
1722          * backend's packetizing will ensure that CommandComplete arrives along
1723          * with the CopyDone; are there corner cases where that doesn't happen?)
1724          */
1725         if (pqIsnonblocking(conn) && PQisBusy(conn))
1726                 return 1;
1727
1728         /* Wait for the completion response */
1729         result = PQgetResult(conn);
1730
1731         /* Expecting a successful result */
1732         if (result && result->resultStatus == PGRES_COMMAND_OK)
1733         {
1734                 PQclear(result);
1735                 return 0;
1736         }
1737
1738         /*
1739          * Trouble. For backwards-compatibility reasons, we issue the error
1740          * message as if it were a notice (would be nice to get rid of this
1741          * silliness, but too many apps probably don't handle errors from
1742          * PQendcopy reasonably).  Note that the app can still obtain the error
1743          * status from the PGconn object.
1744          */
1745         if (conn->errorMessage.len > 0)
1746         {
1747                 /* We have to strip the trailing newline ... pain in neck... */
1748                 char            svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
1749
1750                 if (svLast == '\n')
1751                         conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
1752                 pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
1753                 conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
1754         }
1755
1756         PQclear(result);
1757
1758         return 1;
1759 }
1760
1761
1762 /*
1763  * PQfn - Send a function call to the POSTGRES backend.
1764  *
1765  * See fe-exec.c for documentation.
1766  */
1767 PGresult *
1768 pqFunctionCall3(PGconn *conn, Oid fnid,
1769                                 int *result_buf, int *actual_result_len,
1770                                 int result_is_int,
1771                                 const PQArgBlock *args, int nargs)
1772 {
1773         bool            needInput = false;
1774         ExecStatusType status = PGRES_FATAL_ERROR;
1775         char            id;
1776         int                     msgLength;
1777         int                     avail;
1778         int                     i;
1779
1780         /* PQfn already validated connection state */
1781
1782         if (pqPutMsgStart('F', false, conn) < 0 ||      /* function call msg */
1783                 pqPutInt(fnid, 4, conn) < 0 ||  /* function id */
1784                 pqPutInt(1, 2, conn) < 0 ||             /* # of format codes */
1785                 pqPutInt(1, 2, conn) < 0 ||             /* format code: BINARY */
1786                 pqPutInt(nargs, 2, conn) < 0)   /* # of args */
1787         {
1788                 pqHandleSendFailure(conn);
1789                 return NULL;
1790         }
1791
1792         for (i = 0; i < nargs; ++i)
1793         {                                                       /* len.int4 + contents     */
1794                 if (pqPutInt(args[i].len, 4, conn))
1795                 {
1796                         pqHandleSendFailure(conn);
1797                         return NULL;
1798                 }
1799                 if (args[i].len == -1)
1800                         continue;                       /* it's NULL */
1801
1802                 if (args[i].isint)
1803                 {
1804                         if (pqPutInt(args[i].u.integer, args[i].len, conn))
1805                         {
1806                                 pqHandleSendFailure(conn);
1807                                 return NULL;
1808                         }
1809                 }
1810                 else
1811                 {
1812                         if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
1813                         {
1814                                 pqHandleSendFailure(conn);
1815                                 return NULL;
1816                         }
1817                 }
1818         }
1819
1820         if (pqPutInt(1, 2, conn) < 0)           /* result format code: BINARY */
1821         {
1822                 pqHandleSendFailure(conn);
1823                 return NULL;
1824         }
1825
1826         if (pqPutMsgEnd(conn) < 0 ||
1827                 pqFlush(conn))
1828         {
1829                 pqHandleSendFailure(conn);
1830                 return NULL;
1831         }
1832
1833         for (;;)
1834         {
1835                 if (needInput)
1836                 {
1837                         /* Wait for some data to arrive (or for the channel to close) */
1838                         if (pqWait(TRUE, FALSE, conn) ||
1839                                 pqReadData(conn) < 0)
1840                                 break;
1841                 }
1842
1843                 /*
1844                  * Scan the message. If we run out of data, loop around to try again.
1845                  */
1846                 needInput = true;
1847
1848                 conn->inCursor = conn->inStart;
1849                 if (pqGetc(&id, conn))
1850                         continue;
1851                 if (pqGetInt(&msgLength, 4, conn))
1852                         continue;
1853
1854                 /*
1855                  * Try to validate message type/length here.  A length less than 4 is
1856                  * definitely broken.  Large lengths should only be believed for a few
1857                  * message types.
1858                  */
1859                 if (msgLength < 4)
1860                 {
1861                         handleSyncLoss(conn, id, msgLength);
1862                         break;
1863                 }
1864                 if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
1865                 {
1866                         handleSyncLoss(conn, id, msgLength);
1867                         break;
1868                 }
1869
1870                 /*
1871                  * Can't process if message body isn't all here yet.
1872                  */
1873                 msgLength -= 4;
1874                 avail = conn->inEnd - conn->inCursor;
1875                 if (avail < msgLength)
1876                 {
1877                         /*
1878                          * Before looping, enlarge the input buffer if needed to hold the
1879                          * whole message.  See notes in parseInput.
1880                          */
1881                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
1882                                                                          conn))
1883                         {
1884                                 /*
1885                                  * XXX add some better recovery code... plan is to skip over
1886                                  * the message using its length, then report an error. For the
1887                                  * moment, just treat this like loss of sync (which indeed it
1888                                  * might be!)
1889                                  */
1890                                 handleSyncLoss(conn, id, msgLength);
1891                                 break;
1892                         }
1893                         continue;
1894                 }
1895
1896                 /*
1897                  * We should see V or E response to the command, but might get N
1898                  * and/or A notices first. We also need to swallow the final Z before
1899                  * returning.
1900                  */
1901                 switch (id)
1902                 {
1903                         case 'V':                       /* function result */
1904                                 if (pqGetInt(actual_result_len, 4, conn))
1905                                         continue;
1906                                 if (*actual_result_len != -1)
1907                                 {
1908                                         if (result_is_int)
1909                                         {
1910                                                 if (pqGetInt(result_buf, *actual_result_len, conn))
1911                                                         continue;
1912                                         }
1913                                         else
1914                                         {
1915                                                 if (pqGetnchar((char *) result_buf,
1916                                                                            *actual_result_len,
1917                                                                            conn))
1918                                                         continue;
1919                                         }
1920                                 }
1921                                 /* correctly finished function result message */
1922                                 status = PGRES_COMMAND_OK;
1923                                 break;
1924                         case 'E':                       /* error return */
1925                                 if (pqGetErrorNotice3(conn, true))
1926                                         continue;
1927                                 status = PGRES_FATAL_ERROR;
1928                                 break;
1929                         case 'A':                       /* notify message */
1930                                 /* handle notify and go back to processing return values */
1931                                 if (getNotify(conn))
1932                                         continue;
1933                                 break;
1934                         case 'N':                       /* notice */
1935                                 /* handle notice and go back to processing return values */
1936                                 if (pqGetErrorNotice3(conn, false))
1937                                         continue;
1938                                 break;
1939                         case 'Z':                       /* backend is ready for new query */
1940                                 if (getReadyForQuery(conn))
1941                                         continue;
1942                                 /* consume the message and exit */
1943                                 conn->inStart += 5 + msgLength;
1944                                 /* if we saved a result object (probably an error), use it */
1945                                 if (conn->result)
1946                                         return pqPrepareAsyncResult(conn);
1947                                 return PQmakeEmptyPGresult(conn, status);
1948                         case 'S':                       /* parameter status */
1949                                 if (getParameterStatus(conn))
1950                                         continue;
1951                                 break;
1952                         default:
1953                                 /* The backend violates the protocol. */
1954                                 printfPQExpBuffer(&conn->errorMessage,
1955                                                                   libpq_gettext("protocol error: id=0x%x\n"),
1956                                                                   id);
1957                                 pqSaveErrorResult(conn);
1958                                 /* trust the specified message length as what to skip */
1959                                 conn->inStart += 5 + msgLength;
1960                                 return pqPrepareAsyncResult(conn);
1961                 }
1962                 /* Completed this message, keep going */
1963                 /* trust the specified message length as what to skip */
1964                 conn->inStart += 5 + msgLength;
1965                 needInput = false;
1966         }
1967
1968         /*
1969          * We fall out of the loop only upon failing to read data.
1970          * conn->errorMessage has been set by pqWait or pqReadData. We want to
1971          * append it to any already-received error message.
1972          */
1973         pqSaveErrorResult(conn);
1974         return pqPrepareAsyncResult(conn);
1975 }
1976
1977
1978 /*
1979  * Construct startup packet
1980  *
1981  * Returns a malloc'd packet buffer, or NULL if out of memory
1982  */
1983 char *
1984 pqBuildStartupPacket3(PGconn *conn, int *packetlen,
1985                                           const PQEnvironmentOption *options)
1986 {
1987         char       *startpacket;
1988
1989         *packetlen = build_startup_packet(conn, NULL, options);
1990         startpacket = (char *) malloc(*packetlen);
1991         if (!startpacket)
1992                 return NULL;
1993         *packetlen = build_startup_packet(conn, startpacket, options);
1994         return startpacket;
1995 }
1996
1997 /*
1998  * Build a startup packet given a filled-in PGconn structure.
1999  *
2000  * We need to figure out how much space is needed, then fill it in.
2001  * To avoid duplicate logic, this routine is called twice: the first time
2002  * (with packet == NULL) just counts the space needed, the second time
2003  * (with packet == allocated space) fills it in.  Return value is the number
2004  * of bytes used.
2005  */
2006 static int
2007 build_startup_packet(const PGconn *conn, char *packet,
2008                                          const PQEnvironmentOption *options)
2009 {
2010         int                     packet_len = 0;
2011         const PQEnvironmentOption *next_eo;
2012         const char *val;
2013
2014         /* Protocol version comes first. */
2015         if (packet)
2016         {
2017                 ProtocolVersion pv = htonl(conn->pversion);
2018
2019                 memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
2020         }
2021         packet_len += sizeof(ProtocolVersion);
2022
2023         /* Add user name, database name, options */
2024
2025 #define ADD_STARTUP_OPTION(optname, optval) \
2026         do { \
2027                 if (packet) \
2028                         strcpy(packet + packet_len, optname); \
2029                 packet_len += strlen(optname) + 1; \
2030                 if (packet) \
2031                         strcpy(packet + packet_len, optval); \
2032                 packet_len += strlen(optval) + 1; \
2033         } while(0)
2034
2035         if (conn->pguser && conn->pguser[0])
2036                 ADD_STARTUP_OPTION("user", conn->pguser);
2037         if (conn->dbName && conn->dbName[0])
2038                 ADD_STARTUP_OPTION("database", conn->dbName);
2039         if (conn->replication && conn->replication[0])
2040                 ADD_STARTUP_OPTION("replication", conn->replication);
2041         if (conn->pgoptions && conn->pgoptions[0])
2042                 ADD_STARTUP_OPTION("options", conn->pgoptions);
2043         if (conn->send_appname)
2044         {
2045                 /* Use appname if present, otherwise use fallback */
2046                 val = conn->appname ? conn->appname : conn->fbappname;
2047                 if (val && val[0])
2048                         ADD_STARTUP_OPTION("application_name", val);
2049         }
2050
2051         if (conn->client_encoding_initial && conn->client_encoding_initial[0])
2052                 ADD_STARTUP_OPTION("client_encoding", conn->client_encoding_initial);
2053
2054         /* Add any environment-driven GUC settings needed */
2055         for (next_eo = options; next_eo->envName; next_eo++)
2056         {
2057                 if ((val = getenv(next_eo->envName)) != NULL)
2058                 {
2059                         if (pg_strcasecmp(val, "default") != 0)
2060                                 ADD_STARTUP_OPTION(next_eo->pgName, val);
2061                 }
2062         }
2063
2064         /* Add trailing terminator */
2065         if (packet)
2066                 packet[packet_len] = '\0';
2067         packet_len++;
2068
2069         return packet_len;
2070 }