YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

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