From f15d01cb5dbdc44276f82178759586023896a3b7 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 13 Feb 2024 12:18:25 -0500 Subject: Use a safer outfuncs/readfuncs representation for BitStrings. For a long time, our outfuncs.c code has supposed that the string contents of a BitString node could just be printed literally with no concern for quoting/escaping. Now, that's okay if the string literal contains only valid binary or hex digits ... but our lexer doesn't check that, preferring to let bitin() be the sole authority on what's valid. So we could have raw parse trees that contain incorrect BitString literals, and that can result in failures when WRITE_READ_PARSE_PLAN_TREES debugging is enabled. Fix by using outToken() to print the string field, and debackslash() to read it. This results in a change in the emitted representation only in cases that would have failed before, and don't represent valid SQL in the first place. Between that and the fact that we don't store raw parse trees in the catalogs, I judge this safe to apply without a catversion bump. Per bug #18340 from Alexander Lakhin. Back-patch to v16; before that, we lacked readfuncs support for BitString nodes, so that the problem was only cosmetic. Discussion: https://postgr.es/m/18340-4aa1ae6ed4121912@postgresql.org --- src/backend/nodes/outfuncs.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/backend/nodes/outfuncs.c') diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 955286513d2..e56392e6f9d 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -680,8 +680,13 @@ _outString(StringInfo str, const String *node) static void _outBitString(StringInfo str, const BitString *node) { - /* internal representation already has leading 'b' */ - appendStringInfoString(str, node->bsval); + /* + * The lexer will always produce a string starting with 'b' or 'x'. There + * might be characters following that that need escaping, but outToken + * won't escape the 'b' or 'x'. This is relied on by nodeTokenType. + */ + Assert(node->bsval[0] == 'b' || node->bsval[0] == 'x'); + outToken(str, node->bsval); } static void -- cgit v1.2.3