libpq: Fix PQtrace() format for non-printable characters
authorMichael Paquier <michael@paquier.xyz>
Wed, 3 Sep 2025 03:54:32 +0000 (12:54 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 3 Sep 2025 03:54:32 +0000 (12:54 +0900)
PQtrace() was generating its output for non-printable characters without
casting the characters printed with unsigned char, leading to some extra
"\xffffff" generated in the output due to the fact that char may be
signed.

Oversights introduced by commit 198b3716dba6, so backpatch down to v14.

Author: Ran Benita <ran@unusedvar.com>
Discussion: https://postgr.es/m/a3383211-4539-459b-9d51-95c736ef08e0@app.fastmail.com
Backpatch-through: 14

src/interfaces/libpq/fe-trace.c

index 5d68cf2eb3b5452edb35650a335851f14c9637cf..a985de5fc86282ba72dc0f6ba4de66949e6ac5f1 100644 (file)
@@ -112,7 +112,7 @@ pqTraceOutputByte1(FILE *pfdebug, const char *data, int *cursor)
     * that completes ErrorResponse and NoticeResponse messages.
     */
    if (!isprint((unsigned char) *v))
-       fprintf(pfdebug, " \\x%02x", *v);
+       fprintf(pfdebug, " \\x%02x", (unsigned char) *v);
    else
        fprintf(pfdebug, " %c", *v);
    *cursor += 1;
@@ -200,7 +200,7 @@ pqTraceOutputNchar(FILE *pfdebug, int len, const char *data, int *cursor)
        else
        {
            fwrite(v + next, 1, i - next, pfdebug);
-           fprintf(pfdebug, "\\x%02x", v[i]);
+           fprintf(pfdebug, "\\x%02x", (unsigned char) v[i]);
            next = i + 1;
        }
    }