1 /*-------------------------------------------------------------------------
2  *
3  * fe-protocol3.c
4  *	  functions that are specific to frontend/backend protocol version 3
5  *
6  * Portions Copyright (c) 1996-2019, 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
pqParseInput3(PGconn * conn)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
handleSyncLoss(PGconn * conn,char id,int msgLength)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
getRowDescriptions(PGconn * conn,int msgLength)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
getParamDescriptions(PGconn * conn,int msgLength)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
getAnotherTuple(PGconn * conn,int msgLength)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
pqGetErrorNotice3(PGconn * conn,bool isError)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
pqBuildErrorMessage3(PQExpBuffer msg,const PGresult * res,PGVerbosity verbosity,PGContextVisibility show_context)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 
989 	if (verbosity == PQERRORS_SQLSTATE)
990 	{
991 		/*
992 		 * If we have a SQLSTATE, print that and nothing else.  If not (which
993 		 * shouldn't happen for server-generated errors, but might possibly
994 		 * happen for libpq-generated ones), fall back to TERSE format, as
995 		 * that seems better than printing nothing at all.
996 		 */
997 		val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
998 		if (val)
999 		{
1000 			appendPQExpBuffer(msg, "%s\n", val);
1001 			return;
1002 		}
1003 		verbosity = PQERRORS_TERSE;
1004 	}
1005 
1006 	if (verbosity == PQERRORS_VERBOSE)
1007 	{
1008 		val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
1009 		if (val)
1010 			appendPQExpBuffer(msg, "%s: ", val);
1011 	}
1012 	val = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
1013 	if (val)
1014 		appendPQExpBufferStr(msg, val);
1015 	val = PQresultErrorField(res, PG_DIAG_STATEMENT_POSITION);
1016 	if (val)
1017 	{
1018 		if (verbosity != PQERRORS_TERSE && res->errQuery != NULL)
1019 		{
1020 			/* emit position as a syntax cursor display */
1021 			querytext = res->errQuery;
1022 			querypos = atoi(val);
1023 		}
1024 		else
1025 		{
1026 			/* emit position as text addition to primary message */
1027 			/* translator: %s represents a digit string */
1028 			appendPQExpBuffer(msg, libpq_gettext(" at character %s"),
1029 							  val);
1030 		}
1031 	}
1032 	else
1033 	{
1034 		val = PQresultErrorField(res, PG_DIAG_INTERNAL_POSITION);
1035 		if (val)
1036 		{
1037 			querytext = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
1038 			if (verbosity != PQERRORS_TERSE && querytext != NULL)
1039 			{
1040 				/* emit position as a syntax cursor display */
1041 				querypos = atoi(val);
1042 			}
1043 			else
1044 			{
1045 				/* emit position as text addition to primary message */
1046 				/* translator: %s represents a digit string */
1047 				appendPQExpBuffer(msg, libpq_gettext(" at character %s"),
1048 								  val);
1049 			}
1050 		}
1051 	}
1052 	appendPQExpBufferChar(msg, '\n');
1053 	if (verbosity != PQERRORS_TERSE)
1054 	{
1055 		if (querytext && querypos > 0)
1056 			reportErrorPosition(msg, querytext, querypos,
1057 								res->client_encoding);
1058 		val = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
1059 		if (val)
1060 			appendPQExpBuffer(msg, libpq_gettext("DETAIL:  %s\n"), val);
1061 		val = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
1062 		if (val)
1063 			appendPQExpBuffer(msg, libpq_gettext("HINT:  %s\n"), val);
1064 		val = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
1065 		if (val)
1066 			appendPQExpBuffer(msg, libpq_gettext("QUERY:  %s\n"), val);
1067 		if (show_context == PQSHOW_CONTEXT_ALWAYS ||
1068 			(show_context == PQSHOW_CONTEXT_ERRORS &&
1069 			 res->resultStatus == PGRES_FATAL_ERROR))
1070 		{
1071 			val = PQresultErrorField(res, PG_DIAG_CONTEXT);
1072 			if (val)
1073 				appendPQExpBuffer(msg, libpq_gettext("CONTEXT:  %s\n"),
1074 								  val);
1075 		}
1076 	}
1077 	if (verbosity == PQERRORS_VERBOSE)
1078 	{
1079 		val = PQresultErrorField(res, PG_DIAG_SCHEMA_NAME);
1080 		if (val)
1081 			appendPQExpBuffer(msg,
1082 							  libpq_gettext("SCHEMA NAME:  %s\n"), val);
1083 		val = PQresultErrorField(res, PG_DIAG_TABLE_NAME);
1084 		if (val)
1085 			appendPQExpBuffer(msg,
1086 							  libpq_gettext("TABLE NAME:  %s\n"), val);
1087 		val = PQresultErrorField(res, PG_DIAG_COLUMN_NAME);
1088 		if (val)
1089 			appendPQExpBuffer(msg,
1090 							  libpq_gettext("COLUMN NAME:  %s\n"), val);
1091 		val = PQresultErrorField(res, PG_DIAG_DATATYPE_NAME);
1092 		if (val)
1093 			appendPQExpBuffer(msg,
1094 							  libpq_gettext("DATATYPE NAME:  %s\n"), val);
1095 		val = PQresultErrorField(res, PG_DIAG_CONSTRAINT_NAME);
1096 		if (val)
1097 			appendPQExpBuffer(msg,
1098 							  libpq_gettext("CONSTRAINT NAME:  %s\n"), val);
1099 	}
1100 	if (verbosity == PQERRORS_VERBOSE)
1101 	{
1102 		const char *valf;
1103 		const char *vall;
1104 
1105 		valf = PQresultErrorField(res, PG_DIAG_SOURCE_FILE);
1106 		vall = PQresultErrorField(res, PG_DIAG_SOURCE_LINE);
1107 		val = PQresultErrorField(res, PG_DIAG_SOURCE_FUNCTION);
1108 		if (val || valf || vall)
1109 		{
1110 			appendPQExpBufferStr(msg, libpq_gettext("LOCATION:  "));
1111 			if (val)
1112 				appendPQExpBuffer(msg, libpq_gettext("%s, "), val);
1113 			if (valf && vall)	/* unlikely we'd have just one */
1114 				appendPQExpBuffer(msg, libpq_gettext("%s:%s"),
1115 								  valf, vall);
1116 			appendPQExpBufferChar(msg, '\n');
1117 		}
1118 	}
1119 }
1120 
1121 /*
1122  * Add an error-location display to the error message under construction.
1123  *
1124  * The cursor location is measured in logical characters; the query string
1125  * is presumed to be in the specified encoding.
1126  */
1127 static void
reportErrorPosition(PQExpBuffer msg,const char * query,int loc,int encoding)1128 reportErrorPosition(PQExpBuffer msg, const char *query, int loc, int encoding)
1129 {
1130 #define DISPLAY_SIZE	60		/* screen width limit, in screen cols */
1131 #define MIN_RIGHT_CUT	10		/* try to keep this far away from EOL */
1132 
1133 	char	   *wquery;
1134 	int			slen,
1135 				cno,
1136 				i,
1137 			   *qidx,
1138 			   *scridx,
1139 				qoffset,
1140 				scroffset,
1141 				ibeg,
1142 				iend,
1143 				loc_line;
1144 	bool		mb_encoding,
1145 				beg_trunc,
1146 				end_trunc;
1147 
1148 	/* Convert loc from 1-based to 0-based; no-op if out of range */
1149 	loc--;
1150 	if (loc < 0)
1151 		return;
1152 
1153 	/* Need a writable copy of the query */
1154 	wquery = strdup(query);
1155 	if (wquery == NULL)
1156 		return;					/* fail silently if out of memory */
1157 
1158 	/*
1159 	 * Each character might occupy multiple physical bytes in the string, and
1160 	 * in some Far Eastern character sets it might take more than one screen
1161 	 * column as well.  We compute the starting byte offset and starting
1162 	 * screen column of each logical character, and store these in qidx[] and
1163 	 * scridx[] respectively.
1164 	 */
1165 
1166 	/* we need a safe allocation size... */
1167 	slen = strlen(wquery) + 1;
1168 
1169 	qidx = (int *) malloc(slen * sizeof(int));
1170 	if (qidx == NULL)
1171 	{
1172 		free(wquery);
1173 		return;
1174 	}
1175 	scridx = (int *) malloc(slen * sizeof(int));
1176 	if (scridx == NULL)
1177 	{
1178 		free(qidx);
1179 		free(wquery);
1180 		return;
1181 	}
1182 
1183 	/* We can optimize a bit if it's a single-byte encoding */
1184 	mb_encoding = (pg_encoding_max_length(encoding) != 1);
1185 
1186 	/*
1187 	 * Within the scanning loop, cno is the current character's logical
1188 	 * number, qoffset is its offset in wquery, and scroffset is its starting
1189 	 * logical screen column (all indexed from 0).  "loc" is the logical
1190 	 * character number of the error location.  We scan to determine loc_line
1191 	 * (the 1-based line number containing loc) and ibeg/iend (first character
1192 	 * number and last+1 character number of the line containing loc). Note
1193 	 * that qidx[] and scridx[] are filled only as far as iend.
1194 	 */
1195 	qoffset = 0;
1196 	scroffset = 0;
1197 	loc_line = 1;
1198 	ibeg = 0;
1199 	iend = -1;					/* -1 means not set yet */
1200 
1201 	for (cno = 0; wquery[qoffset] != '\0'; cno++)
1202 	{
1203 		char		ch = wquery[qoffset];
1204 
1205 		qidx[cno] = qoffset;
1206 		scridx[cno] = scroffset;
1207 
1208 		/*
1209 		 * Replace tabs with spaces in the writable copy.  (Later we might
1210 		 * want to think about coping with their variable screen width, but
1211 		 * not today.)
1212 		 */
1213 		if (ch == '\t')
1214 			wquery[qoffset] = ' ';
1215 
1216 		/*
1217 		 * If end-of-line, count lines and mark positions. Each \r or \n
1218 		 * counts as a line except when \r \n appear together.
1219 		 */
1220 		else if (ch == '\r' || ch == '\n')
1221 		{
1222 			if (cno < loc)
1223 			{
1224 				if (ch == '\r' ||
1225 					cno == 0 ||
1226 					wquery[qidx[cno - 1]] != '\r')
1227 					loc_line++;
1228 				/* extract beginning = last line start before loc. */
1229 				ibeg = cno + 1;
1230 			}
1231 			else
1232 			{
1233 				/* set extract end. */
1234 				iend = cno;
1235 				/* done scanning. */
1236 				break;
1237 			}
1238 		}
1239 
1240 		/* Advance */
1241 		if (mb_encoding)
1242 		{
1243 			int			w;
1244 
1245 			w = pg_encoding_dsplen(encoding, &wquery[qoffset]);
1246 			/* treat any non-tab control chars as width 1 */
1247 			if (w <= 0)
1248 				w = 1;
1249 			scroffset += w;
1250 			qoffset += PQmblenBounded(&wquery[qoffset], encoding);
1251 		}
1252 		else
1253 		{
1254 			/* We assume wide chars only exist in multibyte encodings */
1255 			scroffset++;
1256 			qoffset++;
1257 		}
1258 	}
1259 	/* Fix up if we didn't find an end-of-line after loc */
1260 	if (iend < 0)
1261 	{
1262 		iend = cno;				/* query length in chars, +1 */
1263 		qidx[iend] = qoffset;
1264 		scridx[iend] = scroffset;
1265 	}
1266 
1267 	/* Print only if loc is within computed query length */
1268 	if (loc <= cno)
1269 	{
1270 		/* If the line extracted is too long, we truncate it. */
1271 		beg_trunc = false;
1272 		end_trunc = false;
1273 		if (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1274 		{
1275 			/*
1276 			 * We first truncate right if it is enough.  This code might be
1277 			 * off a space or so on enforcing MIN_RIGHT_CUT if there's a wide
1278 			 * character right there, but that should be okay.
1279 			 */
1280 			if (scridx[ibeg] + DISPLAY_SIZE >= scridx[loc] + MIN_RIGHT_CUT)
1281 			{
1282 				while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1283 					iend--;
1284 				end_trunc = true;
1285 			}
1286 			else
1287 			{
1288 				/* Truncate right if not too close to loc. */
1289 				while (scridx[loc] + MIN_RIGHT_CUT < scridx[iend])
1290 				{
1291 					iend--;
1292 					end_trunc = true;
1293 				}
1294 
1295 				/* Truncate left if still too long. */
1296 				while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1297 				{
1298 					ibeg++;
1299 					beg_trunc = true;
1300 				}
1301 			}
1302 		}
1303 
1304 		/* truncate working copy at desired endpoint */
1305 		wquery[qidx[iend]] = '\0';
1306 
1307 		/* Begin building the finished message. */
1308 		i = msg->len;
1309 		appendPQExpBuffer(msg, libpq_gettext("LINE %d: "), loc_line);
1310 		if (beg_trunc)
1311 			appendPQExpBufferStr(msg, "...");
1312 
1313 		/*
1314 		 * While we have the prefix in the msg buffer, compute its screen
1315 		 * width.
1316 		 */
1317 		scroffset = 0;
1318 		for (; i < msg->len; i += PQmblenBounded(&msg->data[i], encoding))
1319 		{
1320 			int			w = pg_encoding_dsplen(encoding, &msg->data[i]);
1321 
1322 			if (w <= 0)
1323 				w = 1;
1324 			scroffset += w;
1325 		}
1326 
1327 		/* Finish up the LINE message line. */
1328 		appendPQExpBufferStr(msg, &wquery[qidx[ibeg]]);
1329 		if (end_trunc)
1330 			appendPQExpBufferStr(msg, "...");
1331 		appendPQExpBufferChar(msg, '\n');
1332 
1333 		/* Now emit the cursor marker line. */
1334 		scroffset += scridx[loc] - scridx[ibeg];
1335 		for (i = 0; i < scroffset; i++)
1336 			appendPQExpBufferChar(msg, ' ');
1337 		appendPQExpBufferChar(msg, '^');
1338 		appendPQExpBufferChar(msg, '\n');
1339 	}
1340 
1341 	/* Clean up. */
1342 	free(scridx);
1343 	free(qidx);
1344 	free(wquery);
1345 }
1346 
1347 
1348 /*
1349  * Attempt to read a ParameterStatus message.
1350  * This is possible in several places, so we break it out as a subroutine.
1351  * Entry: 'S' message type and length have already been consumed.
1352  * Exit: returns 0 if successfully consumed message.
1353  *		 returns EOF if not enough data.
1354  */
1355 static int
getParameterStatus(PGconn * conn)1356 getParameterStatus(PGconn *conn)
1357 {
1358 	PQExpBufferData valueBuf;
1359 
1360 	/* Get the parameter name */
1361 	if (pqGets(&conn->workBuffer, conn))
1362 		return EOF;
1363 	/* Get the parameter value (could be large) */
1364 	initPQExpBuffer(&valueBuf);
1365 	if (pqGets(&valueBuf, conn))
1366 	{
1367 		termPQExpBuffer(&valueBuf);
1368 		return EOF;
1369 	}
1370 	/* And save it */
1371 	pqSaveParameterStatus(conn, conn->workBuffer.data, valueBuf.data);
1372 	termPQExpBuffer(&valueBuf);
1373 	return 0;
1374 }
1375 
1376 
1377 /*
1378  * Attempt to read a Notify response message.
1379  * This is possible in several places, so we break it out as a subroutine.
1380  * Entry: 'A' message type and length have already been consumed.
1381  * Exit: returns 0 if successfully consumed Notify message.
1382  *		 returns EOF if not enough data.
1383  */
1384 static int
getNotify(PGconn * conn)1385 getNotify(PGconn *conn)
1386 {
1387 	int			be_pid;
1388 	char	   *svname;
1389 	int			nmlen;
1390 	int			extralen;
1391 	PGnotify   *newNotify;
1392 
1393 	if (pqGetInt(&be_pid, 4, conn))
1394 		return EOF;
1395 	if (pqGets(&conn->workBuffer, conn))
1396 		return EOF;
1397 	/* must save name while getting extra string */
1398 	svname = strdup(conn->workBuffer.data);
1399 	if (!svname)
1400 		return EOF;
1401 	if (pqGets(&conn->workBuffer, conn))
1402 	{
1403 		free(svname);
1404 		return EOF;
1405 	}
1406 
1407 	/*
1408 	 * Store the strings right after the PQnotify structure so it can all be
1409 	 * freed at once.  We don't use NAMEDATALEN because we don't want to tie
1410 	 * this interface to a specific server name length.
1411 	 */
1412 	nmlen = strlen(svname);
1413 	extralen = strlen(conn->workBuffer.data);
1414 	newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + extralen + 2);
1415 	if (newNotify)
1416 	{
1417 		newNotify->relname = (char *) newNotify + sizeof(PGnotify);
1418 		strcpy(newNotify->relname, svname);
1419 		newNotify->extra = newNotify->relname + nmlen + 1;
1420 		strcpy(newNotify->extra, conn->workBuffer.data);
1421 		newNotify->be_pid = be_pid;
1422 		newNotify->next = NULL;
1423 		if (conn->notifyTail)
1424 			conn->notifyTail->next = newNotify;
1425 		else
1426 			conn->notifyHead = newNotify;
1427 		conn->notifyTail = newNotify;
1428 	}
1429 
1430 	free(svname);
1431 	return 0;
1432 }
1433 
1434 /*
1435  * getCopyStart - process CopyInResponse, CopyOutResponse or
1436  * CopyBothResponse message
1437  *
1438  * parseInput already read the message type and length.
1439  */
1440 static int
getCopyStart(PGconn * conn,ExecStatusType copytype)1441 getCopyStart(PGconn *conn, ExecStatusType copytype)
1442 {
1443 	PGresult   *result;
1444 	int			nfields;
1445 	int			i;
1446 
1447 	result = PQmakeEmptyPGresult(conn, copytype);
1448 	if (!result)
1449 		goto failure;
1450 
1451 	if (pqGetc(&conn->copy_is_binary, conn))
1452 		goto failure;
1453 	result->binary = conn->copy_is_binary;
1454 	/* the next two bytes are the number of fields	*/
1455 	if (pqGetInt(&(result->numAttributes), 2, conn))
1456 		goto failure;
1457 	nfields = result->numAttributes;
1458 
1459 	/* allocate space for the attribute descriptors */
1460 	if (nfields > 0)
1461 	{
1462 		result->attDescs = (PGresAttDesc *)
1463 			pqResultAlloc(result, nfields * sizeof(PGresAttDesc), true);
1464 		if (!result->attDescs)
1465 			goto failure;
1466 		MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
1467 	}
1468 
1469 	for (i = 0; i < nfields; i++)
1470 	{
1471 		int			format;
1472 
1473 		if (pqGetInt(&format, 2, conn))
1474 			goto failure;
1475 
1476 		/*
1477 		 * Since pqGetInt treats 2-byte integers as unsigned, we need to
1478 		 * coerce these results to signed form.
1479 		 */
1480 		format = (int) ((int16) format);
1481 		result->attDescs[i].format = format;
1482 	}
1483 
1484 	/* Success! */
1485 	conn->result = result;
1486 	return 0;
1487 
1488 failure:
1489 	PQclear(result);
1490 	return EOF;
1491 }
1492 
1493 /*
1494  * getReadyForQuery - process ReadyForQuery message
1495  */
1496 static int
getReadyForQuery(PGconn * conn)1497 getReadyForQuery(PGconn *conn)
1498 {
1499 	char		xact_status;
1500 
1501 	if (pqGetc(&xact_status, conn))
1502 		return EOF;
1503 	switch (xact_status)
1504 	{
1505 		case 'I':
1506 			conn->xactStatus = PQTRANS_IDLE;
1507 			break;
1508 		case 'T':
1509 			conn->xactStatus = PQTRANS_INTRANS;
1510 			break;
1511 		case 'E':
1512 			conn->xactStatus = PQTRANS_INERROR;
1513 			break;
1514 		default:
1515 			conn->xactStatus = PQTRANS_UNKNOWN;
1516 			break;
1517 	}
1518 
1519 	return 0;
1520 }
1521 
1522 /*
1523  * getCopyDataMessage - fetch next CopyData message, process async messages
1524  *
1525  * Returns length word of CopyData message (> 0), or 0 if no complete
1526  * message available, -1 if end of copy, -2 if error.
1527  */
1528 static int
getCopyDataMessage(PGconn * conn)1529 getCopyDataMessage(PGconn *conn)
1530 {
1531 	char		id;
1532 	int			msgLength;
1533 	int			avail;
1534 
1535 	for (;;)
1536 	{
1537 		/*
1538 		 * Do we have the next input message?  To make life simpler for async
1539 		 * callers, we keep returning 0 until the next message is fully
1540 		 * available, even if it is not Copy Data.
1541 		 */
1542 		conn->inCursor = conn->inStart;
1543 		if (pqGetc(&id, conn))
1544 			return 0;
1545 		if (pqGetInt(&msgLength, 4, conn))
1546 			return 0;
1547 		if (msgLength < 4)
1548 		{
1549 			handleSyncLoss(conn, id, msgLength);
1550 			return -2;
1551 		}
1552 		avail = conn->inEnd - conn->inCursor;
1553 		if (avail < msgLength - 4)
1554 		{
1555 			/*
1556 			 * Before returning, enlarge the input buffer if needed to hold
1557 			 * the whole message.  See notes in parseInput.
1558 			 */
1559 			if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength - 4,
1560 									 conn))
1561 			{
1562 				/*
1563 				 * XXX add some better recovery code... plan is to skip over
1564 				 * the message using its length, then report an error. For the
1565 				 * moment, just treat this like loss of sync (which indeed it
1566 				 * might be!)
1567 				 */
1568 				handleSyncLoss(conn, id, msgLength);
1569 				return -2;
1570 			}
1571 			return 0;
1572 		}
1573 
1574 		/*
1575 		 * If it's a legitimate async message type, process it.  (NOTIFY
1576 		 * messages are not currently possible here, but we handle them for
1577 		 * completeness.)  Otherwise, if it's anything except Copy Data,
1578 		 * report end-of-copy.
1579 		 */
1580 		switch (id)
1581 		{
1582 			case 'A':			/* NOTIFY */
1583 				if (getNotify(conn))
1584 					return 0;
1585 				break;
1586 			case 'N':			/* NOTICE */
1587 				if (pqGetErrorNotice3(conn, false))
1588 					return 0;
1589 				break;
1590 			case 'S':			/* ParameterStatus */
1591 				if (getParameterStatus(conn))
1592 					return 0;
1593 				break;
1594 			case 'd':			/* Copy Data, pass it back to caller */
1595 				return msgLength;
1596 			case 'c':
1597 
1598 				/*
1599 				 * If this is a CopyDone message, exit COPY_OUT mode and let
1600 				 * caller read status with PQgetResult().  If we're in
1601 				 * COPY_BOTH mode, return to COPY_IN mode.
1602 				 */
1603 				if (conn->asyncStatus == PGASYNC_COPY_BOTH)
1604 					conn->asyncStatus = PGASYNC_COPY_IN;
1605 				else
1606 					conn->asyncStatus = PGASYNC_BUSY;
1607 				return -1;
1608 			default:			/* treat as end of copy */
1609 
1610 				/*
1611 				 * Any other message terminates either COPY_IN or COPY_BOTH
1612 				 * mode.
1613 				 */
1614 				conn->asyncStatus = PGASYNC_BUSY;
1615 				return -1;
1616 		}
1617 
1618 		/* Drop the processed message and loop around for another */
1619 		conn->inStart = conn->inCursor;
1620 	}
1621 }
1622 
1623 /*
1624  * PQgetCopyData - read a row of data from the backend during COPY OUT
1625  * or COPY BOTH
1626  *
1627  * If successful, sets *buffer to point to a malloc'd row of data, and
1628  * returns row length (always > 0) as result.
1629  * Returns 0 if no row available yet (only possible if async is true),
1630  * -1 if end of copy (consult PQgetResult), or -2 if error (consult
1631  * PQerrorMessage).
1632  */
1633 int
pqGetCopyData3(PGconn * conn,char ** buffer,int async)1634 pqGetCopyData3(PGconn *conn, char **buffer, int async)
1635 {
1636 	int			msgLength;
1637 
1638 	for (;;)
1639 	{
1640 		/*
1641 		 * Collect the next input message.  To make life simpler for async
1642 		 * callers, we keep returning 0 until the next message is fully
1643 		 * available, even if it is not Copy Data.
1644 		 */
1645 		msgLength = getCopyDataMessage(conn);
1646 		if (msgLength < 0)
1647 			return msgLength;	/* end-of-copy or error */
1648 		if (msgLength == 0)
1649 		{
1650 			/* Don't block if async read requested */
1651 			if (async)
1652 				return 0;
1653 			/* Need to load more data */
1654 			if (pqWait(true, false, conn) ||
1655 				pqReadData(conn) < 0)
1656 				return -2;
1657 			continue;
1658 		}
1659 
1660 		/*
1661 		 * Drop zero-length messages (shouldn't happen anyway).  Otherwise
1662 		 * pass the data back to the caller.
1663 		 */
1664 		msgLength -= 4;
1665 		if (msgLength > 0)
1666 		{
1667 			*buffer = (char *) malloc(msgLength + 1);
1668 			if (*buffer == NULL)
1669 			{
1670 				printfPQExpBuffer(&conn->errorMessage,
1671 								  libpq_gettext("out of memory\n"));
1672 				return -2;
1673 			}
1674 			memcpy(*buffer, &conn->inBuffer[conn->inCursor], msgLength);
1675 			(*buffer)[msgLength] = '\0';	/* Add terminating null */
1676 
1677 			/* Mark message consumed */
1678 			conn->inStart = conn->inCursor + msgLength;
1679 
1680 			return msgLength;
1681 		}
1682 
1683 		/* Empty, so drop it and loop around for another */
1684 		conn->inStart = conn->inCursor;
1685 	}
1686 }
1687 
1688 /*
1689  * PQgetline - gets a newline-terminated string from the backend.
1690  *
1691  * See fe-exec.c for documentation.
1692  */
1693 int
pqGetline3(PGconn * conn,char * s,int maxlen)1694 pqGetline3(PGconn *conn, char *s, int maxlen)
1695 {
1696 	int			status;
1697 
1698 	if (conn->sock == PGINVALID_SOCKET ||
1699 		(conn->asyncStatus != PGASYNC_COPY_OUT &&
1700 		 conn->asyncStatus != PGASYNC_COPY_BOTH) ||
1701 		conn->copy_is_binary)
1702 	{
1703 		printfPQExpBuffer(&conn->errorMessage,
1704 						  libpq_gettext("PQgetline: not doing text COPY OUT\n"));
1705 		*s = '\0';
1706 		return EOF;
1707 	}
1708 
1709 	while ((status = PQgetlineAsync(conn, s, maxlen - 1)) == 0)
1710 	{
1711 		/* need to load more data */
1712 		if (pqWait(true, false, conn) ||
1713 			pqReadData(conn) < 0)
1714 		{
1715 			*s = '\0';
1716 			return EOF;
1717 		}
1718 	}
1719 
1720 	if (status < 0)
1721 	{
1722 		/* End of copy detected; gin up old-style terminator */
1723 		strcpy(s, "\\.");
1724 		return 0;
1725 	}
1726 
1727 	/* Add null terminator, and strip trailing \n if present */
1728 	if (s[status - 1] == '\n')
1729 	{
1730 		s[status - 1] = '\0';
1731 		return 0;
1732 	}
1733 	else
1734 	{
1735 		s[status] = '\0';
1736 		return 1;
1737 	}
1738 }
1739 
1740 /*
1741  * PQgetlineAsync - gets a COPY data row without blocking.
1742  *
1743  * See fe-exec.c for documentation.
1744  */
1745 int
pqGetlineAsync3(PGconn * conn,char * buffer,int bufsize)1746 pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
1747 {
1748 	int			msgLength;
1749 	int			avail;
1750 
1751 	if (conn->asyncStatus != PGASYNC_COPY_OUT
1752 		&& conn->asyncStatus != PGASYNC_COPY_BOTH)
1753 		return -1;				/* we are not doing a copy... */
1754 
1755 	/*
1756 	 * Recognize the next input message.  To make life simpler for async
1757 	 * callers, we keep returning 0 until the next message is fully available
1758 	 * even if it is not Copy Data.  This should keep PQendcopy from blocking.
1759 	 * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.)
1760 	 */
1761 	msgLength = getCopyDataMessage(conn);
1762 	if (msgLength < 0)
1763 		return -1;				/* end-of-copy or error */
1764 	if (msgLength == 0)
1765 		return 0;				/* no data yet */
1766 
1767 	/*
1768 	 * Move data from libpq's buffer to the caller's.  In the case where a
1769 	 * prior call found the caller's buffer too small, we use
1770 	 * conn->copy_already_done to remember how much of the row was already
1771 	 * returned to the caller.
1772 	 */
1773 	conn->inCursor += conn->copy_already_done;
1774 	avail = msgLength - 4 - conn->copy_already_done;
1775 	if (avail <= bufsize)
1776 	{
1777 		/* Able to consume the whole message */
1778 		memcpy(buffer, &conn->inBuffer[conn->inCursor], avail);
1779 		/* Mark message consumed */
1780 		conn->inStart = conn->inCursor + avail;
1781 		/* Reset state for next time */
1782 		conn->copy_already_done = 0;
1783 		return avail;
1784 	}
1785 	else
1786 	{
1787 		/* We must return a partial message */
1788 		memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize);
1789 		/* The message is NOT consumed from libpq's buffer */
1790 		conn->copy_already_done += bufsize;
1791 		return bufsize;
1792 	}
1793 }
1794 
1795 /*
1796  * PQendcopy
1797  *
1798  * See fe-exec.c for documentation.
1799  */
1800 int
pqEndcopy3(PGconn * conn)1801 pqEndcopy3(PGconn *conn)
1802 {
1803 	PGresult   *result;
1804 
1805 	if (conn->asyncStatus != PGASYNC_COPY_IN &&
1806 		conn->asyncStatus != PGASYNC_COPY_OUT &&
1807 		conn->asyncStatus != PGASYNC_COPY_BOTH)
1808 	{
1809 		printfPQExpBuffer(&conn->errorMessage,
1810 						  libpq_gettext("no COPY in progress\n"));
1811 		return 1;
1812 	}
1813 
1814 	/* Send the CopyDone message if needed */
1815 	if (conn->asyncStatus == PGASYNC_COPY_IN ||
1816 		conn->asyncStatus == PGASYNC_COPY_BOTH)
1817 	{
1818 		if (pqPutMsgStart('c', false, conn) < 0 ||
1819 			pqPutMsgEnd(conn) < 0)
1820 			return 1;
1821 
1822 		/*
1823 		 * If we sent the COPY command in extended-query mode, we must issue a
1824 		 * Sync as well.
1825 		 */
1826 		if (conn->queryclass != PGQUERY_SIMPLE)
1827 		{
1828 			if (pqPutMsgStart('S', false, conn) < 0 ||
1829 				pqPutMsgEnd(conn) < 0)
1830 				return 1;
1831 		}
1832 	}
1833 
1834 	/*
1835 	 * make sure no data is waiting to be sent, abort if we are non-blocking
1836 	 * and the flush fails
1837 	 */
1838 	if (pqFlush(conn) && pqIsnonblocking(conn))
1839 		return 1;
1840 
1841 	/* Return to active duty */
1842 	conn->asyncStatus = PGASYNC_BUSY;
1843 	resetPQExpBuffer(&conn->errorMessage);
1844 
1845 	/*
1846 	 * Non blocking connections may have to abort at this point.  If everyone
1847 	 * played the game there should be no problem, but in error scenarios the
1848 	 * expected messages may not have arrived yet.  (We are assuming that the
1849 	 * backend's packetizing will ensure that CommandComplete arrives along
1850 	 * with the CopyDone; are there corner cases where that doesn't happen?)
1851 	 */
1852 	if (pqIsnonblocking(conn) && PQisBusy(conn))
1853 		return 1;
1854 
1855 	/* Wait for the completion response */
1856 	result = PQgetResult(conn);
1857 
1858 	/* Expecting a successful result */
1859 	if (result && result->resultStatus == PGRES_COMMAND_OK)
1860 	{
1861 		PQclear(result);
1862 		return 0;
1863 	}
1864 
1865 	/*
1866 	 * Trouble. For backwards-compatibility reasons, we issue the error
1867 	 * message as if it were a notice (would be nice to get rid of this
1868 	 * silliness, but too many apps probably don't handle errors from
1869 	 * PQendcopy reasonably).  Note that the app can still obtain the error
1870 	 * status from the PGconn object.
1871 	 */
1872 	if (conn->errorMessage.len > 0)
1873 	{
1874 		/* We have to strip the trailing newline ... pain in neck... */
1875 		char		svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
1876 
1877 		if (svLast == '\n')
1878 			conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
1879 		pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
1880 		conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
1881 	}
1882 
1883 	PQclear(result);
1884 
1885 	return 1;
1886 }
1887 
1888 
1889 /*
1890  * PQfn - Send a function call to the POSTGRES backend.
1891  *
1892  * See fe-exec.c for documentation.
1893  */
1894 PGresult *
pqFunctionCall3(PGconn * conn,Oid fnid,int * result_buf,int * actual_result_len,int result_is_int,const PQArgBlock * args,int nargs)1895 pqFunctionCall3(PGconn *conn, Oid fnid,
1896 				int *result_buf, int *actual_result_len,
1897 				int result_is_int,
1898 				const PQArgBlock *args, int nargs)
1899 {
1900 	bool		needInput = false;
1901 	ExecStatusType status = PGRES_FATAL_ERROR;
1902 	char		id;
1903 	int			msgLength;
1904 	int			avail;
1905 	int			i;
1906 
1907 	/* PQfn already validated connection state */
1908 
1909 	if (pqPutMsgStart('F', false, conn) < 0 ||	/* function call msg */
1910 		pqPutInt(fnid, 4, conn) < 0 ||	/* function id */
1911 		pqPutInt(1, 2, conn) < 0 || /* # of format codes */
1912 		pqPutInt(1, 2, conn) < 0 || /* format code: BINARY */
1913 		pqPutInt(nargs, 2, conn) < 0)	/* # of args */
1914 	{
1915 		/* error message should be set up already */
1916 		return NULL;
1917 	}
1918 
1919 	for (i = 0; i < nargs; ++i)
1920 	{							/* len.int4 + contents	   */
1921 		if (pqPutInt(args[i].len, 4, conn))
1922 			return NULL;
1923 		if (args[i].len == -1)
1924 			continue;			/* it's NULL */
1925 
1926 		if (args[i].isint)
1927 		{
1928 			if (pqPutInt(args[i].u.integer, args[i].len, conn))
1929 				return NULL;
1930 		}
1931 		else
1932 		{
1933 			if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
1934 				return NULL;
1935 		}
1936 	}
1937 
1938 	if (pqPutInt(1, 2, conn) < 0)	/* result format code: BINARY */
1939 		return NULL;
1940 
1941 	if (pqPutMsgEnd(conn) < 0 ||
1942 		pqFlush(conn))
1943 		return NULL;
1944 
1945 	for (;;)
1946 	{
1947 		if (needInput)
1948 		{
1949 			/* Wait for some data to arrive (or for the channel to close) */
1950 			if (pqWait(true, false, conn) ||
1951 				pqReadData(conn) < 0)
1952 				break;
1953 		}
1954 
1955 		/*
1956 		 * Scan the message. If we run out of data, loop around to try again.
1957 		 */
1958 		needInput = true;
1959 
1960 		conn->inCursor = conn->inStart;
1961 		if (pqGetc(&id, conn))
1962 			continue;
1963 		if (pqGetInt(&msgLength, 4, conn))
1964 			continue;
1965 
1966 		/*
1967 		 * Try to validate message type/length here.  A length less than 4 is
1968 		 * definitely broken.  Large lengths should only be believed for a few
1969 		 * message types.
1970 		 */
1971 		if (msgLength < 4)
1972 		{
1973 			handleSyncLoss(conn, id, msgLength);
1974 			break;
1975 		}
1976 		if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
1977 		{
1978 			handleSyncLoss(conn, id, msgLength);
1979 			break;
1980 		}
1981 
1982 		/*
1983 		 * Can't process if message body isn't all here yet.
1984 		 */
1985 		msgLength -= 4;
1986 		avail = conn->inEnd - conn->inCursor;
1987 		if (avail < msgLength)
1988 		{
1989 			/*
1990 			 * Before looping, enlarge the input buffer if needed to hold the
1991 			 * whole message.  See notes in parseInput.
1992 			 */
1993 			if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
1994 									 conn))
1995 			{
1996 				/*
1997 				 * XXX add some better recovery code... plan is to skip over
1998 				 * the message using its length, then report an error. For the
1999 				 * moment, just treat this like loss of sync (which indeed it
2000 				 * might be!)
2001 				 */
2002 				handleSyncLoss(conn, id, msgLength);
2003 				break;
2004 			}
2005 			continue;
2006 		}
2007 
2008 		/*
2009 		 * We should see V or E response to the command, but might get N
2010 		 * and/or A notices first. We also need to swallow the final Z before
2011 		 * returning.
2012 		 */
2013 		switch (id)
2014 		{
2015 			case 'V':			/* function result */
2016 				if (pqGetInt(actual_result_len, 4, conn))
2017 					continue;
2018 				if (*actual_result_len != -1)
2019 				{
2020 					if (result_is_int)
2021 					{
2022 						if (pqGetInt(result_buf, *actual_result_len, conn))
2023 							continue;
2024 					}
2025 					else
2026 					{
2027 						if (pqGetnchar((char *) result_buf,
2028 									   *actual_result_len,
2029 									   conn))
2030 							continue;
2031 					}
2032 				}
2033 				/* correctly finished function result message */
2034 				status = PGRES_COMMAND_OK;
2035 				break;
2036 			case 'E':			/* error return */
2037 				if (pqGetErrorNotice3(conn, true))
2038 					continue;
2039 				status = PGRES_FATAL_ERROR;
2040 				break;
2041 			case 'A':			/* notify message */
2042 				/* handle notify and go back to processing return values */
2043 				if (getNotify(conn))
2044 					continue;
2045 				break;
2046 			case 'N':			/* notice */
2047 				/* handle notice and go back to processing return values */
2048 				if (pqGetErrorNotice3(conn, false))
2049 					continue;
2050 				break;
2051 			case 'Z':			/* backend is ready for new query */
2052 				if (getReadyForQuery(conn))
2053 					continue;
2054 				/* consume the message and exit */
2055 				conn->inStart += 5 + msgLength;
2056 				/* if we saved a result object (probably an error), use it */
2057 				if (conn->result)
2058 					return pqPrepareAsyncResult(conn);
2059 				return PQmakeEmptyPGresult(conn, status);
2060 			case 'S':			/* parameter status */
2061 				if (getParameterStatus(conn))
2062 					continue;
2063 				break;
2064 			default:
2065 				/* The backend violates the protocol. */
2066 				printfPQExpBuffer(&conn->errorMessage,
2067 								  libpq_gettext("protocol error: id=0x%x\n"),
2068 								  id);
2069 				pqSaveErrorResult(conn);
2070 				/* trust the specified message length as what to skip */
2071 				conn->inStart += 5 + msgLength;
2072 				return pqPrepareAsyncResult(conn);
2073 		}
2074 		/* Completed this message, keep going */
2075 		/* trust the specified message length as what to skip */
2076 		conn->inStart += 5 + msgLength;
2077 		needInput = false;
2078 	}
2079 
2080 	/*
2081 	 * We fall out of the loop only upon failing to read data.
2082 	 * conn->errorMessage has been set by pqWait or pqReadData. We want to
2083 	 * append it to any already-received error message.
2084 	 */
2085 	pqSaveErrorResult(conn);
2086 	return pqPrepareAsyncResult(conn);
2087 }
2088 
2089 
2090 /*
2091  * Construct startup packet
2092  *
2093  * Returns a malloc'd packet buffer, or NULL if out of memory
2094  */
2095 char *
pqBuildStartupPacket3(PGconn * conn,int * packetlen,const PQEnvironmentOption * options)2096 pqBuildStartupPacket3(PGconn *conn, int *packetlen,
2097 					  const PQEnvironmentOption *options)
2098 {
2099 	char	   *startpacket;
2100 
2101 	*packetlen = build_startup_packet(conn, NULL, options);
2102 	startpacket = (char *) malloc(*packetlen);
2103 	if (!startpacket)
2104 		return NULL;
2105 	*packetlen = build_startup_packet(conn, startpacket, options);
2106 	return startpacket;
2107 }
2108 
2109 /*
2110  * Build a startup packet given a filled-in PGconn structure.
2111  *
2112  * We need to figure out how much space is needed, then fill it in.
2113  * To avoid duplicate logic, this routine is called twice: the first time
2114  * (with packet == NULL) just counts the space needed, the second time
2115  * (with packet == allocated space) fills it in.  Return value is the number
2116  * of bytes used.
2117  */
2118 static int
build_startup_packet(const PGconn * conn,char * packet,const PQEnvironmentOption * options)2119 build_startup_packet(const PGconn *conn, char *packet,
2120 					 const PQEnvironmentOption *options)
2121 {
2122 	int			packet_len = 0;
2123 	const PQEnvironmentOption *next_eo;
2124 	const char *val;
2125 
2126 	/* Protocol version comes first. */
2127 	if (packet)
2128 	{
2129 		ProtocolVersion pv = pg_hton32(conn->pversion);
2130 
2131 		memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
2132 	}
2133 	packet_len += sizeof(ProtocolVersion);
2134 
2135 	/* Add user name, database name, options */
2136 
2137 #define ADD_STARTUP_OPTION(optname, optval) \
2138 	do { \
2139 		if (packet) \
2140 			strcpy(packet + packet_len, optname); \
2141 		packet_len += strlen(optname) + 1; \
2142 		if (packet) \
2143 			strcpy(packet + packet_len, optval); \
2144 		packet_len += strlen(optval) + 1; \
2145 	} while(0)
2146 
2147 	if (conn->pguser && conn->pguser[0])
2148 		ADD_STARTUP_OPTION("user", conn->pguser);
2149 	if (conn->dbName && conn->dbName[0])
2150 		ADD_STARTUP_OPTION("database", conn->dbName);
2151 	if (conn->replication && conn->replication[0])
2152 		ADD_STARTUP_OPTION("replication", conn->replication);
2153 	if (conn->pgoptions && conn->pgoptions[0])
2154 		ADD_STARTUP_OPTION("options", conn->pgoptions);
2155 	if (conn->send_appname)
2156 	{
2157 		/* Use appname if present, otherwise use fallback */
2158 		val = conn->appname ? conn->appname : conn->fbappname;
2159 		if (val && val[0])
2160 			ADD_STARTUP_OPTION("application_name", val);
2161 	}
2162 
2163 	if (conn->client_encoding_initial && conn->client_encoding_initial[0])
2164 		ADD_STARTUP_OPTION("client_encoding", conn->client_encoding_initial);
2165 
2166 	/* Add any environment-driven GUC settings needed */
2167 	for (next_eo = options; next_eo->envName; next_eo++)
2168 	{
2169 		if ((val = getenv(next_eo->envName)) != NULL)
2170 		{
2171 			if (pg_strcasecmp(val, "default") != 0)
2172 				ADD_STARTUP_OPTION(next_eo->pgName, val);
2173 		}
2174 	}
2175 
2176 	/* Add trailing terminator */
2177 	if (packet)
2178 		packet[packet_len] = '\0';
2179 	packet_len++;
2180 
2181 	return packet_len;
2182 }
2183