libpq: Add suppress argument to pqTraceOutputNchar
authorAlvaro Herrera <[email protected]>
Fri, 9 Aug 2024 00:35:12 +0000 (20:35 -0400)
committerAlvaro Herrera <[email protected]>
Fri, 9 Aug 2024 00:35:12 +0000 (20:35 -0400)
In future commits we're going to trace authentication related messages.
Some of these messages contain challenge bytes as part of a
challenge-response flow.  Since these bytes are different for every
connection, we want to normalize them when the PQTRACE_REGRESS_MODE
trace flag is set.  This commit modifies pqTraceOutputNchar to take a
suppress argument, which makes it possible to do so.

Author: Jelte Fennema-Nio <[email protected]>
Discussion: https://postgr.es/m/CAGECzQSoPHtZ4xe0raJ6FYSEiPPS+YWXBhOGo+Y1YecLgknF3g@mail.gmail.com

src/interfaces/libpq/fe-trace.c

index d7a61ec9cc1720d4fa2bbd330220ee54b445cffb..c479ea19c442b16d02ff1cecd40c4005b8f6d4fe 100644 (file)
@@ -158,6 +158,8 @@ pqTraceOutputInt32(FILE *pfdebug, const char *data, int *cursor, bool suppress)
 
 /*
  *   pqTraceOutputString: output a string message to the log
+ *
+ * If 'suppress' is true, print a literal "SSSS" instead of the actual string.
  */
 static void
 pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
@@ -183,14 +185,23 @@ pqTraceOutputString(FILE *pfdebug, const char *data, int *cursor, bool suppress)
 
 /*
  * pqTraceOutputNchar: output a string of exactly len bytes message to the log
+ *
+ * If 'suppress' is true, print a literal 'BBBB' instead of the actual bytes.
  */
 static void
-pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
+pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor, bool suppress)
 {
    int         i,
                next;           /* first char not yet printed */
    const char *v = data + *cursor;
 
+   if (suppress)
+   {
+       fprintf(pfdebug, " 'BBBB'");
+       *cursor += len;
+       return;
+   }
+
    fprintf(pfdebug, " \'");
 
    for (next = i = 0; i < len; ++i)
@@ -246,7 +257,7 @@ pqTraceOutput_Bind(FILE *f, const char *message, int *cursor)
        nbytes = pqTraceOutputInt32(f, message, cursor, false);
        if (nbytes == -1)
            continue;
-       pqTraceOutputNchar(f, nbytes, message, cursor);
+       pqTraceOutputNchar(f, nbytes, message, cursor, false);
    }
 
    nparams = pqTraceOutputInt16(f, message, cursor);
@@ -283,7 +294,7 @@ pqTraceOutput_DataRow(FILE *f, const char *message, int *cursor)
        len = pqTraceOutputInt32(f, message, cursor, false);
        if (len == -1)
            continue;
-       pqTraceOutputNchar(f, len, message, cursor);
+       pqTraceOutputNchar(f, len, message, cursor, false);
    }
 }
 
@@ -363,7 +374,7 @@ pqTraceOutput_FunctionCall(FILE *f, const char *message, int *cursor, bool regre
        nbytes = pqTraceOutputInt32(f, message, cursor, false);
        if (nbytes == -1)
            continue;
-       pqTraceOutputNchar(f, nbytes, message, cursor);
+       pqTraceOutputNchar(f, nbytes, message, cursor, false);
    }
 
    pqTraceOutputInt16(f, message, cursor);
@@ -487,7 +498,7 @@ pqTraceOutput_FunctionCallResponse(FILE *f, const char *message, int *cursor)
    fprintf(f, "FunctionCallResponse\t");
    len = pqTraceOutputInt32(f, message, cursor, false);
    if (len != -1)
-       pqTraceOutputNchar(f, len, message, cursor);
+       pqTraceOutputNchar(f, len, message, cursor, false);
 }
 
 static void