From 005292954be8270e1eff3828e0418c7c090c8254 Mon Sep 17 00:00:00 2001 From: Anthonin Bonnefoy Date: Tue, 7 Jul 2026 08:15:38 +0200 Subject: Print cancel key as a hex string in trace Currently, cancel key is printed with pqTraceOutputNchar, which print printable char as char, and the rest as hex. As cancel key is just random bytes, there's not much meaning to print them as chars. This patch introduces a new pqTraceOutputNbyte to print bytes using only hex. --- src/interfaces/libpq/fe-trace.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/interfaces/libpq/fe-trace.c b/src/interfaces/libpq/fe-trace.c index 2901fa5b451..741be09bdd3 100644 --- a/src/interfaces/libpq/fe-trace.c +++ b/src/interfaces/libpq/fe-trace.c @@ -223,6 +223,33 @@ pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor, bool s *cursor += len; } +/* + * pqTraceOutputNbyte: output bytes in hexadecimal of exactly len bytes to the log + * + * If 'suppress' is true, print a literal 'BBBB' instead of the actual bytes. + */ +static void +pqTraceOutputNbyte(FILE *pfdebug, int len, const char *data, int *cursor, bool suppress) +{ + int i; + const char *v = data + *cursor; + + if (suppress) + { + fprintf(pfdebug, " 'BBBB'"); + *cursor += len; + return; + } + + fprintf(pfdebug, " \'"); + + for (i = 0; i < len; ++i) + fprintf(pfdebug, "\\x%02x", (unsigned char) v[i]); + + fprintf(pfdebug, "\'"); + *cursor += len; +} + /* * Output functions by protocol message type */ @@ -457,7 +484,7 @@ pqTraceOutput_BackendKeyData(FILE *f, const char *message, int *cursor, int leng { fprintf(f, "BackendKeyData\t"); pqTraceOutputInt32(f, message, cursor, regress); - pqTraceOutputNchar(f, length - *cursor + 1, message, cursor, regress); + pqTraceOutputNbyte(f, length - *cursor + 1, message, cursor, regress); } static void @@ -878,7 +905,7 @@ pqTraceOutputNoTypeByteMessage(PGconn *conn, const char *message) pqTraceOutputInt16(conn->Pfdebug, message, &logCursor); pqTraceOutputInt16(conn->Pfdebug, message, &logCursor); pqTraceOutputInt32(conn->Pfdebug, message, &logCursor, regress); - pqTraceOutputNchar(conn->Pfdebug, length - logCursor, message, + pqTraceOutputNbyte(conn->Pfdebug, length - logCursor, message, &logCursor, regress); } else if (version == NEGOTIATE_SSL_CODE) -- 2.54.0