From 39a5e0c6b5559a0fb9a6c376e493e4ee4bc980fb Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Fri, 26 Mar 2021 20:04:53 -0300
Subject: [PATCH v30 5/7] pass *cursor to printing routines

---
 src/interfaces/libpq/libpq-trace.c | 309 +++++++++++++----------------
 1 file changed, 134 insertions(+), 175 deletions(-)

diff --git a/src/interfaces/libpq/libpq-trace.c b/src/interfaces/libpq/libpq-trace.c
index 94f28f04a8..06d8d981a3 100644
--- a/src/interfaces/libpq/libpq-trace.c
+++ b/src/interfaces/libpq/libpq-trace.c
@@ -106,7 +106,7 @@ pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
 		fprintf(pfdebug, " \\x%02x", *v);
 	else
 		fprintf(pfdebug, " %c", *v);
-	++*cursor;
+	*cursor += 1;
 }
 
 /*
@@ -195,142 +195,125 @@ pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
 
 /* Authentication */
 static void
-pqTraceOutputR(FILE *f, const char *message)
+pqTraceOutputR(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "Authentication\t");
-	pqTraceOutputInt32(f, message, &cursor);
+	pqTraceOutputInt32(f, message, cursor);
 }
 
 /* BackendKeyData */
 static void
-pqTraceOutputK(FILE *f, const char *message)
+pqTraceOutputK(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "BackendKeyData\t");
-	pqTraceOutputInt32(f, message, &cursor);
-	pqTraceOutputInt32(f, message, &cursor);
+	pqTraceOutputInt32(f, message, cursor);
+	pqTraceOutputInt32(f, message, cursor);
 }
 
 /* Bind */
 static void
-pqTraceOutputB(FILE *f, const char *message, int end)
+pqTraceOutputB(FILE *f, const char *message, int *cursor)
 {
-	int cursor = 0;
 	int nparams;
-	int nbytes;
-	int i;
 
 	fprintf(f, "Bind\t");
-	pqTraceOutputString(f, message, &cursor);
-	pqTraceOutputString(f, message, &cursor);
-	nparams = pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputString(f, message, cursor);
+	pqTraceOutputString(f, message, cursor);
+	nparams = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nparams; i++)
-		pqTraceOutputInt16(f, message, &cursor);
+	for (int i = 0; i < nparams; i++)
+		pqTraceOutputInt16(f, message, cursor);
 
-	nparams = pqTraceOutputInt16(f, message, &cursor);
+	nparams = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nparams; i++)
+	for (int i = 0; i < nparams; i++)
 	{
-		nbytes = pqTraceOutputInt32(f, message, &cursor);
+		int nbytes;
+
+		nbytes = pqTraceOutputInt32(f, message, cursor);
 		if (nbytes == -1)
 			continue;
-		pqTraceOutputNchar(f, nbytes, message, &cursor);
+		pqTraceOutputNchar(f, nbytes, message, cursor);
 	}
 
-	nparams = pqTraceOutputInt16(f, message, &cursor);
-	for (i = 0; i < nparams; i++)
-		pqTraceOutputInt16(f, message, &cursor);
+	nparams = pqTraceOutputInt16(f, message, cursor);
+	for (int i = 0; i < nparams; i++)
+		pqTraceOutputInt16(f, message, cursor);
 }
 
 /* Close(F) or CommandComplete(B) */
 static void
-pqTraceOutputC(FILE *f, bool toServer, const char *message, int end)
+pqTraceOutputC(FILE *f, bool toServer, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	if (toServer)
 	{
 		fprintf(f, "Close\t");
-		pqTraceOutputByte1(f, message, &cursor);
-		pqTraceOutputString(f, message, &cursor);
+		pqTraceOutputByte1(f, message, cursor);
+		pqTraceOutputString(f, message, cursor);
 	}
 	else
 	{
 		fprintf(f, "CommandComplete\t");
-		pqTraceOutputString(f, message, &cursor);
+		pqTraceOutputString(f, message, cursor);
 	}
 }
 
 /* CopyFail */
 static void
-pqTraceOutputf(FILE *f, const char *message, int end)
+pqTraceOutputf(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "CopyFail\t");
-	pqTraceOutputString(f, message, &cursor);
+	pqTraceOutputString(f, message, cursor);
 }
 
 /* CopyInResponse */
 static void
-pqTraceOutputG(FILE *f, const char *message, int end)
+pqTraceOutputG(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int	nfields;
-	int	i;
 
 	fprintf(f, "CopyInResponse\t");
-	pqTraceOutputByte1(f, message, &cursor);
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputByte1(f, message, cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
-		pqTraceOutputInt16(f, message, &cursor);
+	for (int i = 0; i < nfields; i++)
+		pqTraceOutputInt16(f, message, cursor);
 }
 
 /* CopyOutResponse */
 static void
-pqTraceOutputH(FILE *f, const char *message, int end)
+pqTraceOutputH(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int	nfields;
-	int	i;
 
 	fprintf(f, "CopyOutResponse\t");
-	pqTraceOutputByte1(f, message, &cursor);
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputByte1(f, message, cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
-		pqTraceOutputInt16(f, message, &cursor);
+	for (int i = 0; i < nfields; i++)
+		pqTraceOutputInt16(f, message, cursor);
 }
 
 /* CopyBothResponse */
 static void
-pqTraceOutputW(FILE *f, const char *message, int end)
+pqTraceOutputW(FILE *f, const char *message, int *cursor, int length)
 {
-	int	cursor = 0;
-
 	fprintf(f, "CopyBothResponse\t");
-	pqTraceOutputByte1(f, message, &cursor);
+	pqTraceOutputByte1(f, message, cursor);
 
-	while (end > cursor)
-		pqTraceOutputInt16(f, message, &cursor);
+	while (length > *cursor)
+		pqTraceOutputInt16(f, message, cursor);
 }
 
 /* Describe(F) or DataRow(B) */
 static void
-pqTraceOutputD(FILE *f, bool toServer, const char *message, int end)
+pqTraceOutputD(FILE *f, bool toServer, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	if (toServer)
 	{
 		fprintf(f, "Describe\t");
-		pqTraceOutputByte1(f, message, &cursor);
-		pqTraceOutputString(f, message, &cursor);
+		pqTraceOutputByte1(f, message, cursor);
+		pqTraceOutputString(f, message, cursor);
 	}
 	else
 	{
@@ -339,206 +322,183 @@ pqTraceOutputD(FILE *f, bool toServer, const char *message, int end)
 		int		i;
 
 		fprintf(f, "DataRow\t");
-		nfields = pqTraceOutputInt16(f, message, &cursor);
+		nfields = pqTraceOutputInt16(f, message, cursor);
 		for (i = 0; i < nfields; i++)
 		{
-			len = pqTraceOutputInt32(f, message, &cursor);
+			len = pqTraceOutputInt32(f, message, cursor);
 			if (len == -1)
 				continue;
-			pqTraceOutputNchar(f, len, message, &cursor);
+			pqTraceOutputNchar(f, len, message, cursor);
 		}
 	}
 }
 
 /* Execute(F) or ErrorResponse(B) */
 static void
-pqTraceOutputE(FILE *f, bool toServer, const char *message, int end)
+pqTraceOutputE(FILE *f, bool toServer, const char *message, int *cursor, int length)
 {
-	int	cursor = 0;
-
 	if (toServer)
 	{
 		fprintf(f, "Execute\t");
-		pqTraceOutputString(f, message, &cursor);
-		pqTraceOutputInt32(f, message, &cursor);
+		pqTraceOutputString(f, message, cursor);
+		pqTraceOutputInt32(f, message, cursor);
 	}
 	else
 	{
 		fprintf(f, "ErrorResponse\t");
-		while (end > cursor)
+		for (;;)
 		{
-			pqTraceOutputByte1(f, message, &cursor);
-			if (message[cursor] == '\0')
-				continue;
-			pqTraceOutputString(f, message, &cursor);
+			pqTraceOutputByte1(f, message, cursor);
+			if (message[*cursor - 1] == '\0')
+				break;
+			pqTraceOutputString(f, message, cursor);
 		}
 	}
 }
 
 /* FunctionCall */
 static void
-pqTraceOutputF(FILE *f, const char *message)
+pqTraceOutputF(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int nfields;
 	int nbytes;
-	int	i;
 
 	fprintf(f, "FunctionCall\t");
-	pqTraceOutputInt32(f, message, &cursor);
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputInt32(f, message, cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
-		pqTraceOutputInt16(f, message, &cursor);
+	for (int i = 0; i < nfields; i++)
+		pqTraceOutputInt16(f, message, cursor);
 
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
+	for (int i = 0; i < nfields; i++)
 	{
-		nbytes = pqTraceOutputInt32(f, message, &cursor);
+		nbytes = pqTraceOutputInt32(f, message, cursor);
 		if (nbytes == -1)
 			continue;
-		pqTraceOutputNchar(f, nbytes, message, &cursor);
+		pqTraceOutputNchar(f, nbytes, message, cursor);
 	}
 
-	pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputInt16(f, message, cursor);
 }
 
 /* FunctionCallResponse */
 static void
-pqTraceOutputV(FILE *f, const char *message)
+pqTraceOutputV(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int		len;
 
 	fprintf(f, "FunctionCallResponse\t");
-	len = pqTraceOutputInt32(f, message, &cursor);
+	len = pqTraceOutputInt32(f, message, cursor);
 	if (len != -1)
-		pqTraceOutputNchar(f, len, message, &cursor);
+		pqTraceOutputNchar(f, len, message, cursor);
 }
 
 /* NegotiateProtocolVersion */
 static void
-pqTraceOutputv(FILE *f, const char *message)
+pqTraceOutputv(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "NegotiateProtocolVersion\t");
-	pqTraceOutputInt32(f, message, &cursor);
-	pqTraceOutputInt32(f, message, &cursor);
+	pqTraceOutputInt32(f, message, cursor);
+	pqTraceOutputInt32(f, message, cursor);
 }
 
 /* NoticeResponse */
 static void
-pqTraceOutputN(FILE *f, const char *message, int end)
+pqTraceOutputN(FILE *f, const char *message, int *cursor, int length)
 {
-	int	cursor = 0;
-
 	fprintf(f, "NoticeResponse\t");
-	while (end > cursor)
+	for (;;)
 	{
-		pqTraceOutputByte1(f, message, &cursor);
-		if (message[cursor] == '\0')
-			continue;
-		pqTraceOutputString(f, message, &cursor);
+		pqTraceOutputByte1(f, message, cursor);
+		if (message[*cursor - 1] == '\0')
+			break;
+		pqTraceOutputString(f, message, cursor);
 	}
 }
 
 /* NotificationResponse */
 static void
-pqTraceOutputA(FILE *f, const char *message, int end)
+pqTraceOutputA(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "NotificationResponse\t");
-	pqTraceOutputInt32(f, message, &cursor);
-	pqTraceOutputString(f, message, &cursor);
-	pqTraceOutputString(f, message, &cursor);
+	pqTraceOutputInt32(f, message, cursor);
+	pqTraceOutputString(f, message, cursor);
+	pqTraceOutputString(f, message, cursor);
 }
 
 /* ParameterDescription */
 static void
-pqTraceOutputt(FILE *f, const char *message)
+pqTraceOutputt(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int	nfields;
-	int	i;
 
 	fprintf(f, "ParameterDescription\t");
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
-		pqTraceOutputInt32(f, message, &cursor);
+	for (int i = 0; i < nfields; i++)
+		pqTraceOutputInt32(f, message, cursor);
 }
 
 /* ParameterStatus */
 static void
-pqTraceOutputS(FILE *f, const char *message, int end)
+pqTraceOutputS(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "ParameterStatus\t");
-	pqTraceOutputString(f, message, &cursor);
-	pqTraceOutputString(f, message, &cursor);
+	pqTraceOutputString(f, message, cursor);
+	pqTraceOutputString(f, message, cursor);
 }
 
 /* Parse */
 static void
-pqTraceOutputP(FILE *f, const char *message, int end)
+pqTraceOutputP(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int nparams;
-	int i;
 
 	fprintf(f, "Parse\t");
-	pqTraceOutputString(f, message, &cursor);
-	pqTraceOutputString(f, message, &cursor);
-	nparams = pqTraceOutputInt16(f, message, &cursor);
+	pqTraceOutputString(f, message, cursor);
+	pqTraceOutputString(f, message, cursor);
+	nparams = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nparams; i++)
-		pqTraceOutputInt32(f, message, &cursor);
+	for (int i = 0; i < nparams; i++)
+		pqTraceOutputInt32(f, message, cursor);
 }
 
 /* Query */
 static void
-pqTraceOutputQ(FILE *f, const char *message, int end)
+pqTraceOutputQ(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "Query\t");
-	pqTraceOutputString(f, message, &cursor);
+	pqTraceOutputString(f, message, cursor);
 }
 
 /* ReadyForQuery */
 static void
-pqTraceOutputZ(FILE *f, const char *message)
+pqTraceOutputZ(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
-
 	fprintf(f, "ReadyForQuery\t");
-	pqTraceOutputByte1(f, message, &cursor);
+	pqTraceOutputByte1(f, message, cursor);
 }
 
 /* RowDescription */
 static void
-pqTraceOutputT(FILE *f, const char *message, int end)
+pqTraceOutputT(FILE *f, const char *message, int *cursor)
 {
-	int	cursor = 0;
 	int nfields;
-	int	i;
 
 	fprintf(f, "RowDescription\t");
-	nfields = pqTraceOutputInt16(f, message, &cursor);
+	nfields = pqTraceOutputInt16(f, message, cursor);
 
-	for (i = 0; i < nfields; i++)
+	for (int i = 0; i < nfields; i++)
 	{
-		pqTraceOutputString(f, message, &cursor);
-		pqTraceOutputInt32(f, message, &cursor);
-		pqTraceOutputInt16(f, message, &cursor);
-		pqTraceOutputInt32(f, message, &cursor);
-		pqTraceOutputInt16(f, message, &cursor);
-		pqTraceOutputInt32(f, message, &cursor);
-		pqTraceOutputInt16(f, message, &cursor);
+		pqTraceOutputString(f, message, cursor);
+		pqTraceOutputInt32(f, message, cursor);
+		pqTraceOutputInt16(f, message, cursor);
+		pqTraceOutputInt32(f, message, cursor);
+		pqTraceOutputInt16(f, message, cursor);
+		pqTraceOutputInt32(f, message, cursor);
+		pqTraceOutputInt16(f, message, cursor);
 	}
 }
 
@@ -550,95 +510,94 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
 	int			length;
 	char	   *prefix = toServer ? "F" : "B";
 	int			LogCursor = 0;
-	int			LogEnd;
 
 	if ((conn->traceFlags & PQTRACE_SUPPRESS_TIMESTAMPS) == 0)
+	{
 		pqTraceFormatTimestamp(timestr, sizeof(timestr));
-	else
-		timestr[0] = '\0';
+		fprintf(conn->Pfdebug, "%s\t", timestr);
+	}
 
 	id = message[LogCursor++];
 
-	memcpy(&length, message + LogCursor , 4);
+	memcpy(&length, message + LogCursor, 4);
 	length = (int) pg_ntoh32(length);
 	LogCursor += 4;
-	LogEnd = length - 4;
 
-	fprintf(conn->Pfdebug, "%s\t%s\t%d\t", timestr, prefix, length);
+	fprintf(conn->Pfdebug, "%s\t%d\t", prefix, length);
 
 	switch(id)
 	{
 		case 'R':	/* Authentication */
-			pqTraceOutputR(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputR(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'K':	/* secret key data from the backend */
-			pqTraceOutputK(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputK(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'B':	/* Bind */
-			pqTraceOutputB(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputB(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'C':	/* Close(F) or Command Complete(B) */
-			pqTraceOutputC(conn->Pfdebug, toServer, message + LogCursor, LogEnd);
+			pqTraceOutputC(conn->Pfdebug, toServer, message, &LogCursor);
 			break;
 		case 'd':	/* Copy Data */
 			/* Drop COPY data to reduce the overhead of logging. */
 			break;
 		case 'f':	/* Copy Fail */
-			pqTraceOutputf(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputf(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'G':	/* Start Copy In */
-			pqTraceOutputG(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputG(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'H':	/* Flush(F) or Start Copy Out(B) */
 			if (!toServer)
-				pqTraceOutputH(conn->Pfdebug, message + LogCursor, LogEnd);
+				pqTraceOutputH(conn->Pfdebug, message, &LogCursor);
 			else
 				fprintf(conn->Pfdebug, "Flush");
 			break;
 		case 'W':	/* Start Copy Both */
-			pqTraceOutputW(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputW(conn->Pfdebug, message, &LogCursor, length);
 			break;
 		case 'D':	/* Describe(F) or Data Row(B) */
-			pqTraceOutputD(conn->Pfdebug, toServer, message + LogCursor, LogEnd);
+			pqTraceOutputD(conn->Pfdebug, toServer, message, &LogCursor);
 			break;
 		case 'E':	/* Execute(F) or Error Response(B) */
-			pqTraceOutputE(conn->Pfdebug, toServer, message + LogCursor, LogEnd);
+			pqTraceOutputE(conn->Pfdebug, toServer, message, &LogCursor, length);
 			break;
 		case 'F':	/* Function Call */
-			pqTraceOutputF(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputF(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'V':	/* Function Call response */
-			pqTraceOutputV(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputV(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'v':	/* Negotiate Protocol Version */
-			pqTraceOutputv(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputv(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'N':	/* Notice Response */
-			pqTraceOutputN(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputN(conn->Pfdebug, message, &LogCursor, length);
 			break;
 		case 'A':	/* Notification Response */
-			pqTraceOutputA(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputA(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 't':	/* Parameter Description */
-			pqTraceOutputt(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputt(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'S':	/* Parameter Status(B) or Sync(F) */
 			if (!toServer)
-				pqTraceOutputS(conn->Pfdebug, message + LogCursor, LogEnd);
+				pqTraceOutputS(conn->Pfdebug, message, &LogCursor);
 			else
 				fprintf(conn->Pfdebug, "Sync");
 			break;
 		case 'P':	/* Parse */
-			pqTraceOutputP(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputP(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'Q':	/* Query */
-			pqTraceOutputQ(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputQ(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'Z':	/* Ready For Query */
-			pqTraceOutputZ(conn->Pfdebug, message + LogCursor);
+			pqTraceOutputZ(conn->Pfdebug, message, &LogCursor);
 			break;
 		case 'T':	/* Row Description */
-			pqTraceOutputT(conn->Pfdebug, message + LogCursor, LogEnd);
+			pqTraceOutputT(conn->Pfdebug, message, &LogCursor);
 			break;
 		case '2':	/* Bind Complete */
 			fprintf(conn->Pfdebug, "BindComplete");
-- 
2.20.1

