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