LCOV - differential code coverage report
Current view: top level - src/interfaces/libpq - fe-protocol3.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 65.1 % 921 600 42 66 170 43 73 350 177 201 348 4 9
Current Date: 2023-04-08 17:13:01 Functions: 81.0 % 21 17 1 3 17 4 17
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (120,180] days: 0.0 % 42 0 42
Legend: Lines: hit not hit (240..) days: 68.3 % 879 600 66 170 43 73 350 177 201 327
Function coverage date bins:
(120,180] days: 0.0 % 1 0 1
(240..) days: 41.5 % 41 17 3 17 4 17

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

Generated by: LCOV version v1.16-55-g56c0a2a