Fix reading of BitString nodes
authorPeter Eisentraut <[email protected]>
Sat, 24 Sep 2022 22:10:52 +0000 (18:10 -0400)
committerPeter Eisentraut <[email protected]>
Sat, 24 Sep 2022 22:10:52 +0000 (18:10 -0400)
The node tokenizer went out of its way to store BitString node values
without the leading 'b'.  But everything else in the system stores the
leading 'b'.  This would break if a BitString node is
read-printed-read.

Also, the node tokenizer didn't know that BitString node tokens could
also start with 'x'.

Reviewed-by: Tom Lane <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/4159834.1657405226@sss.pgh.pa.us

src/backend/nodes/read.c

index a9cb81b1290d9b14b901ef0e0bd91cef4c119728..fe84f140eefe8a95ef9228849af36865ab4b9ec2 100644 (file)
@@ -288,7 +288,7 @@ nodeTokenType(const char *token, int length)
        retval = T_Boolean;
    else if (*token == '"' && length > 1 && token[length - 1] == '"')
        retval = T_String;
-   else if (*token == 'b')
+   else if (*token == 'b' || *token == 'x')
        retval = T_BitString;
    else
        retval = OTHER_TOKEN;
@@ -471,11 +471,10 @@ nodeRead(const char *token, int tok_len)
            break;
        case T_BitString:
            {
-               char       *val = palloc(tok_len);
+               char       *val = palloc(tok_len + 1);
 
-               /* skip leading 'b' */
-               memcpy(val, token + 1, tok_len - 1);
-               val[tok_len - 1] = '\0';
+               memcpy(val, token, tok_len);
+               val[tok_len] = '\0';
                result = (Node *) makeBitString(val);
                break;
            }