pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Improve error handling in test modules: test_extensible, test_bitmapset
6+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-09-10 34c11958b6  test_bitmapset: Improve handling of incorrect inputs
  2026-09-10 7ac0c2d058  test_extensible: Improve handling of incorrect inputs
* Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-06 10:22  Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Zsolt Parragi @ 2026-09-06 10:22 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Michael Paquier <michael@paquier.xyz>; Gregory Burd <greg@burd.me>; aleksander@tigerdata.com

Hello

The recently added test extensible module has a small oversight: it
lacks input validation, which made it easy to crash the backend with
invalid input with the test module loaded. While this doesn't seem
like a critical issue for a test module, the commit message also
mentions that this could be a template for extension developers, so I
think it is worth fixing.

There's a similar issue in test_bitmapset: that's an older commit, but
it shows the same issue.

I attached fixes to both as 0001/0002.

Attachments: download all (2 files, .tar.gz)

0002-test_bitmapset-Decode-arguments-with-readBitmapset.patch (application/octet-stream, 5.7K) download From 60c2405ea935ed58b1ed1f2031400a374333748a Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <zsolt.parragi@percona.com> Date: Sat, 5 Sep 2026 21:42:26 +0000 Subject: [PATCH 2/2] test_bitmapset: Decode arguments with readBitmapset() The SQL wrappers of this module decoded their text arguments with the generic stringToNode(), casting whatever came out to a Bitmapset. stringToNode() assumes its input to be valid: the read routines of readfuncs.c fetch tokens with pg_strtok() and feed them to atoi() without checking for NULL, so any other node type name, like '{QUERY}', crashes the backend on the first missing field. Strings for other node types that do read fine, like '42' or '(i 1 2)', were then misinterpreted as a Bitmapset. The functions are executable by any user, so this was reachable without any privileges. To fix, decode with readBitmapset(), the reader used where a Bitmapset is known to be expected, which checks every token it reads and rejects anything else. "<>", the representation of an empty set written by nodeToString(), is still accepted. Oversight in commit 00c3d87a5ca. --- .../expected/test_bitmapset.out | 29 ++++++++++++++++++ .../test_bitmapset/sql/test_bitmapset.sql | 13 ++++++++ .../modules/test_bitmapset/test_bitmapset.c | 30 +++++++++++++++++-- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/test/modules/test_bitmapset/expected/test_bitmapset.out b/src/test/modules/test_bitmapset/expected/test_bitmapset.out index f8f5cf5e7f0..ab98cbd448b 100644 --- a/src/test/modules/test_bitmapset/expected/test_bitmapset.out +++ b/src/test/modules/test_bitmapset/expected/test_bitmapset.out @@ -1656,4 +1656,33 @@ SELECT test_random_offset_operations(NULL, 1000, 0, 1024) AS result; 1000 (1 row) +-- malformed inputs, rejected before reaching the read routines of other +-- node types +SELECT test_bms_num_members('{QUERY}'); -- error +ERROR: unrecognized token: "{" +SELECT test_bms_copy('{VAR}'); -- error +ERROR: unrecognized token: "{" +SELECT test_bms_num_members('42'); -- error +ERROR: unrecognized token: "42" +SELECT test_bms_num_members('(i 1 2)'); -- error +ERROR: unrecognized token: "i" +SELECT test_bms_num_members('(b 1'); -- error +ERROR: unterminated Bitmapset structure +SELECT test_bms_num_members('(b x)'); -- error +ERROR: unrecognized integer: "x" +SELECT test_bms_num_members(''); -- error +ERROR: incomplete Bitmapset structure +-- empty set, as written by nodeToString() +SELECT test_bms_num_members('<>') AS result; + result +-------- + 0 +(1 row) + +SELECT test_bms_copy('<>') AS result; + result +-------- + <> +(1 row) + DROP EXTENSION test_bitmapset; diff --git a/src/test/modules/test_bitmapset/sql/test_bitmapset.sql b/src/test/modules/test_bitmapset/sql/test_bitmapset.sql index d44cda114a4..4f3cb7bfc6d 100644 --- a/src/test/modules/test_bitmapset/sql/test_bitmapset.sql +++ b/src/test/modules/test_bitmapset/sql/test_bitmapset.sql @@ -426,4 +426,17 @@ SELECT test_random_operations(NULL, 10000, 0, 81920) > 0 AS result; -- perform some random tests on bms_offset_members() SELECT test_random_offset_operations(NULL, 1000, 0, 1024) AS result; +-- malformed inputs, rejected before reaching the read routines of other +-- node types +SELECT test_bms_num_members('{QUERY}'); -- error +SELECT test_bms_copy('{VAR}'); -- error +SELECT test_bms_num_members('42'); -- error +SELECT test_bms_num_members('(i 1 2)'); -- error +SELECT test_bms_num_members('(b 1'); -- error +SELECT test_bms_num_members('(b x)'); -- error +SELECT test_bms_num_members(''); -- error +-- empty set, as written by nodeToString() +SELECT test_bms_num_members('<>') AS result; +SELECT test_bms_copy('<>') AS result; + DROP EXTENSION test_bitmapset; diff --git a/src/test/modules/test_bitmapset/test_bitmapset.c b/src/test/modules/test_bitmapset/test_bitmapset.c index af8c664949e..9123c5efd7a 100644 --- a/src/test/modules/test_bitmapset/test_bitmapset.c +++ b/src/test/modules/test_bitmapset/test_bitmapset.c @@ -24,6 +24,7 @@ #include "nodes/bitmapset.h" #include "nodes/nodes.h" #include "nodes/pg_list.h" +#include "nodes/readfuncs.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/timestamp.h" @@ -86,16 +87,39 @@ PG_FUNCTION_INFO_V1(test_random_offset_operations); #expr, __FILE__, __LINE__); \ } while (0) -/* Encode/Decode to/from TEXT and Bitmapset */ +/* Encode a Bitmapset into its serialized representation */ #define BITMAPSET_TO_TEXT(bms) cstring_to_text(nodeToString(bms)) -#define TEXT_TO_BITMAPSET(str) ((Bitmapset *) stringToNode(text_to_cstring(str))) + +/* + * Decode a Bitmapset from its serialized representation, either "<>" for an + * empty set or "(b member ...)". + * + * This goes through readBitmapset(), the reader for contexts where a + * Bitmapset is known to be expected, rather than through the generic + * stringToNode(). The latter assumes its input to be valid, and the read + * routines of readfuncs.c it dispatches to for other node types do not check + * for missing tokens, so a string naming any of those could crash the + * backend. readBitmapset() checks everything it reads. + */ +static Bitmapset * +text_to_bitmapset(text *txt) +{ + char *str = text_to_cstring(txt); + ReadNodeContext ctx = {.str = str}; + + /* nodeToString() writes an empty set as "<>" */ + if (strcmp(str, "<>") == 0) + return NULL; + + return readBitmapset(&ctx); +} /* * Helper macro to fetch text parameters as Bitmapsets. SQL-NULL means empty * set. */ #define PG_ARG_GETBITMAPSET(n) \ - (PG_ARGISNULL(n) ? NULL : TEXT_TO_BITMAPSET(PG_GETARG_TEXT_PP(n))) + (PG_ARGISNULL(n) ? NULL : text_to_bitmapset(PG_GETARG_TEXT_PP(n))) /* * Helper macro to handle converting sets back to text, returning the -- 2.55.0
0001-test_extensible-Reject-other-node-types-before-calli.patch (application/octet-stream, 5.5K) download From 0bf4a47d9d8754108de965ac64a27efb4530a92d Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <zsolt.parragi@percona.com> Date: Sat, 5 Sep 2026 21:34:56 +0000 Subject: [PATCH 1/2] test_extensible: Reject other node types before calling stringToNode() The SQL wrappers of this module decode their text argument with stringToNode(), and only then check that the result is a TestExtNode. stringToNode() assumes its input to be valid, though: the read routines of readfuncs.c fetch tokens with pg_strtok() and feed them to atoi() without checking for NULL, so any other node type name, like '{QUERY}' or '{VAR}', crashes the backend on the first missing field. The functions are executable by any user, so this was reachable without any privileges. To fix, look at the leading tokens of the string before handing it to stringToNode(), and reject anything but an EXTENSIBLENODE named TestExtNode, so that only the nodeRead callback of this module, which does check for missing tokens, ever sees the rest of the string. Oversight in commit 0e944fe3579. --- .../expected/test_extensible.out | 11 ++++++ .../test_extensible/sql/test_extensible.sql | 7 ++++ .../modules/test_extensible/test_extensible.c | 39 ++++++++++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/test/modules/test_extensible/expected/test_extensible.out b/src/test/modules/test_extensible/expected/test_extensible.out index 1fa3b6d2c55..091bdafaa79 100644 --- a/src/test/modules/test_extensible/expected/test_extensible.out +++ b/src/test/modules/test_extensible/expected/test_extensible.out @@ -91,6 +91,17 @@ SELECT test_ext_node_equal(test_ext_node_make('1234'::oid, 2), -- correct node type, missing field SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename TestExtNode}'); ERROR: unexpected end of "TestExtNode" +-- other node types, rejected before reaching their core read routines +SELECT test_ext_node_get_relid('{QUERY}'); +ERROR: argument is not a serialized "TestExtNode" +SELECT test_ext_node_copy('{VAR}'); +ERROR: argument is not a serialized "TestExtNode" +-- extensible node of another kind +SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename NoSuchExtNode}'); +ERROR: argument is not a serialized "TestExtNode" +-- not a node string at all +SELECT test_ext_node_get_relid('42'); +ERROR: argument is not a serialized "TestExtNode" -- CustomScan tests CREATE TABLE test_extensible_tbl (id integer, val text); INSERT INTO test_extensible_tbl VALUES (1, 'one'), (2, 'two'), (3, 'three'); diff --git a/src/test/modules/test_extensible/sql/test_extensible.sql b/src/test/modules/test_extensible/sql/test_extensible.sql index b23bd1cd087..347c747a6bd 100644 --- a/src/test/modules/test_extensible/sql/test_extensible.sql +++ b/src/test/modules/test_extensible/sql/test_extensible.sql @@ -33,6 +33,13 @@ SELECT test_ext_node_equal(test_ext_node_make('1234'::oid, 2), -- correct node type, missing field SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename TestExtNode}'); +-- other node types, rejected before reaching their core read routines +SELECT test_ext_node_get_relid('{QUERY}'); +SELECT test_ext_node_copy('{VAR}'); +-- extensible node of another kind +SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename NoSuchExtNode}'); +-- not a node string at all +SELECT test_ext_node_get_relid('42'); -- CustomScan tests CREATE TABLE test_extensible_tbl (id integer, val text); diff --git a/src/test/modules/test_extensible/test_extensible.c b/src/test/modules/test_extensible/test_extensible.c index 2a1344f28d5..8ecb171111e 100644 --- a/src/test/modules/test_extensible/test_extensible.c +++ b/src/test/modules/test_extensible/test_extensible.c @@ -463,22 +463,49 @@ test_get_custom_scan_methods(PG_FUNCTION_ARGS) } /* - * Decode a TestExtNode via stringToNode(), rejecting a string describing - * some other kind of node instead of misinterpreting it as one of ours. + * Check that the next token of the string being read is the expected one. + */ +static bool +test_ext_node_expect_token(ReadNodeContext *ctx, const char *expected) +{ + int length; + const char *token = pg_strtok(ctx, &length); + + return token != NULL && length == (int) strlen(expected) && + memcmp(token, expected, length) == 0; +} + +/* + * Decode a TestExtNode via stringToNode(). + * + * stringToNode() assumes its input to be valid, and the read routines of + * readfuncs.c it dispatches to do not check for missing tokens, so handing + * it a string naming any other node type could crash the backend. Hence, + * look at the leading tokens first and refuse anything that would not end + * up in our own nodeRead callback, the only one hardened against arbitrary + * input. */ static TestExtNode * text_to_test_ext_node(text *txt) { - Node *node = stringToNode(text_to_cstring(txt)); + char *str = text_to_cstring(txt); + ReadNodeContext ctx = {.str = str}; + TestExtNode *tnode; - if (node == NULL || !IsA(node, ExtensibleNode) || - strcmp(((ExtensibleNode *) node)->extnodename, TEST_EXT_NODE_NAME) != 0) + if (!test_ext_node_expect_token(&ctx, "{") || + !test_ext_node_expect_token(&ctx, "EXTENSIBLENODE") || + !test_ext_node_expect_token(&ctx, ":extnodename") || + !test_ext_node_expect_token(&ctx, TEST_EXT_NODE_NAME)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument is not a serialized \"%s\"", TEST_EXT_NODE_NAME))); - return (TestExtNode *) node; + tnode = (TestExtNode *) stringToNode(str); + Assert(IsA(tnode, ExtensibleNode) && + strcmp(tnode->base.extnodename, TEST_EXT_NODE_NAME) == 0); + + return tnode; } /* -- 2.55.0
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-08 07:14  Michael Paquier <michael@paquier.xyz>
  parent: Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Michael Paquier @ 2026-09-08 07:14 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Gregory Burd <greg@burd.me>; aleksander@tigerdata.com

On Sun, Sep 06, 2026 at 11:22:58AM +0100, Zsolt Parragi wrote:
> The recently added test extensible module has a small oversight: it
> lacks input validation, which made it easy to crash the backend with
> invalid input with the test module loaded. While this doesn't seem
> like a critical issue for a test module, the commit message also
> mentions that this could be a template for extension developers, so I
> think it is worth fixing.

+static bool
+test_ext_node_expect_token(ReadNodeContext *ctx, const char *expected)
+{
+	int			length;
+	const char *token = pg_strtok(ctx, &length);
+
+	return token != NULL && length == (int) strlen(expected) &&
+		memcmp(token, expected, length) == 0;

I am not much a fan of spreading pg_strtok() calls more than
necessary in this module.  How about extending
test_ext_node_next_token() with an "expected" argument to enforce some
validation?  If "expected" is NULL, fall back to the default of
failing if the end has been reached and a NULL token is found, for the
read callback.

+   return token != NULL && length == (int) strlen(expected) &&
+       memcmp(token, expected, length) == 0;

And perhaps also split these in multiple lines, just because it would
feel slightly cleaner..  I know I'm picky on these things.

I am not convinced that we need to check all the input function calls
in 0001 and 0002.  For 0001, I'd just pick up one for simplicity of
parsing the test.  I can see that you are mostly doing that in 0002,
with test_bms_num_members().  Let's just pick up one, reduce the
duplicates.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-08 16:16  Greg Burd <greg@burd.me>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 6+ messages in thread

From: Greg Burd @ 2026-09-08 16:16 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; aleksander@tigerdata.com

On Mon, Sep 8, 2026, at 3:14 AM, Michael Paquier wrote:
> I am not much a fan of spreading pg_strtok() calls more than
> necessary in this module. How about extending
> test_ext_node_next_token() with an "expected" argument to enforce some
> validation?
  
Agreed, one chokepoint for pg_strtok() is better. Worth being explicit
in the comment that the two modes fail differently, though: with
expected == NULL the function ereports on a short read, and with a
non-NULL expected it has to hand a mismatch back to the caller so
text_to_test_ext_node() can keep emitting its single "argument is not a
serialized %s" error rather than four separate ones. That's a bit of
behavior keyed off whether an argument is NULL, so it should be
spelled out rather than inferred.
  
> I am not convinced that we need to check all the input function calls
> in 0001 and 0002. [...] Let's just pick up one, reduce the duplicates.
  
+1, and for test_bitmapset there's a concrete reason it's safe: every
SQL wrapper decodes through the same PG_ARG_GETBITMAPSET ->
text_to_bitmapset() path, 48 call sites, and there's no second decode
route. So one function exercises the whole thing and the rest is
duplicate coverage. I'd fold test_bms_copy('{VAR}') into
test_bms_num_members() with the others.
  
I'd keep test_bms_copy('<>'), though. That one isn't testing rejection,
it's checking that an empty set survives the round trip and comes back
out as '<>', which num_members can't show you since it just returns 0.
Different coverage, cheap to keep.
  
I did confirm the bug is real before the fix: with 0002 reverted,
SELECT test_bms_num_members('{QUERY}') takes the backend down and the
cluster goes into recovery, no privileges needed. So this is worth
fixing even for a test module. Thanks for catching it, Zsolt.
  
One thing not yet mentioned: after the fix, bad input surfaces as
elog(ERROR) out of _readBitmapset(), so you get
  
  ERROR:  XX000: unrecognized token: "{"
  
XX000 normally means "we hit a bug," not "you passed garbage." Those
elogs are internal-error paths in readfuncs.c that were never meant to
be user-reachable, and the SQL functions here make them reachable. I
don't think this patch should try to fix readfuncs.c, and I'm fine
with it as-is for a test module, but if we're holding this up as a
template for extension authors then the honest version catches the bad
input in text_to_bitmapset() and reports it with
ERRCODE_INVALID_TEXT_REPRESENTATION, the way test_extensible already
does for its own node. Happy to leave that as a follow-up if you'd
rather keep these patches tight.
  
best,
  
-greg






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-08 23:35  Michael Paquier <michael@paquier.xyz>
  parent: Greg Burd <greg@burd.me>
  0 siblings, 1 reply; 6+ messages in thread

From: Michael Paquier @ 2026-09-08 23:35 UTC (permalink / raw)
  To: Greg Burd <greg@burd.me>; +Cc: Zsolt Parragi <zsolt.parragi@percona.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; aleksander@tigerdata.com

On Tue, Sep 08, 2026 at 12:16:36PM -0400, Greg Burd wrote:
> +1, and for test_bitmapset there's a concrete reason it's safe: every
> SQL wrapper decodes through the same PG_ARG_GETBITMAPSET ->
> text_to_bitmapset() path, 48 call sites, and there's no second decode
> route. So one function exercises the whole thing and the rest is
> duplicate coverage. I'd fold test_bms_copy('{VAR}') into
> test_bms_num_members() with the others.

FWIW, I'm also finding the addition of readBitmapset() in
test_bitmapset() kind of ugly to rely on.  I'd rather keep to zero
the number of calls outside readfuncs.c, as this is historically a
function kept for compatibility with external code, more efficient
than nodeRead().  I was tempted to eliminate it from src/include/ a
couple of weeks ago, until Matthias' arguments convinced me otherwise
so they have been moved to readfuncs.h instead, as there is still some
external code using it.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-09 09:36  Zsolt Parragi <zsolt.parragi@percona.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 6+ messages in thread

From: Zsolt Parragi @ 2026-09-09 09:36 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Greg Burd <greg@burd.me>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; aleksander@tigerdata.com

I addressed all comments in v2

0002 is now a bit longer with not relying on readBitmapset, but I
think it's still okay/readable.

Attachments: download all (2 files, .tar.gz)

v2-0001-test_extensible-Reject-other-node-types-before-ca.patch (application/octet-stream, 7.3K) download From 61b07bb2f72723d3306e3afa817df567770dcf2a Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <zsolt.parragi@percona.com> Date: Sat, 5 Sep 2026 21:34:56 +0000 Subject: [PATCH v2 1/2] test_extensible: Reject other node types before calling stringToNode() The SQL wrappers of this module decode their text argument with stringToNode(), and only then check that the result is a TestExtNode. stringToNode() assumes its input to be valid, though: the read routines of readfuncs.c fetch tokens with pg_strtok() and feed them to atoi() without checking for NULL, so any other node type name, like '{QUERY}' or '{VAR}', crashes the backend on the first missing field. The functions are executable by any user, so this was reachable without any privileges. To fix, look at the leading tokens of the string before handing it to stringToNode(), and reject anything but an EXTENSIBLENODE named TestExtNode, so that only the nodeRead callback of this module, which does check for missing tokens, ever sees the rest of the string. Oversight in commit 0e944fe3579. --- .../expected/test_extensible.out | 9 +++ .../test_extensible/sql/test_extensible.sql | 6 ++ .../modules/test_extensible/test_extensible.c | 64 +++++++++++++++---- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/test/modules/test_extensible/expected/test_extensible.out b/src/test/modules/test_extensible/expected/test_extensible.out index 1fa3b6d2c55..694efb7142f 100644 --- a/src/test/modules/test_extensible/expected/test_extensible.out +++ b/src/test/modules/test_extensible/expected/test_extensible.out @@ -91,6 +91,15 @@ SELECT test_ext_node_equal(test_ext_node_make('1234'::oid, 2), -- correct node type, missing field SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename TestExtNode}'); ERROR: unexpected end of "TestExtNode" +-- other node types, rejected before reaching their core read routines +SELECT test_ext_node_get_relid('{QUERY}'); +ERROR: argument is not a serialized "TestExtNode" +-- extensible node of another kind +SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename NoSuchExtNode}'); +ERROR: argument is not a serialized "TestExtNode" +-- not a node string at all +SELECT test_ext_node_get_relid('42'); +ERROR: argument is not a serialized "TestExtNode" -- CustomScan tests CREATE TABLE test_extensible_tbl (id integer, val text); INSERT INTO test_extensible_tbl VALUES (1, 'one'), (2, 'two'), (3, 'three'); diff --git a/src/test/modules/test_extensible/sql/test_extensible.sql b/src/test/modules/test_extensible/sql/test_extensible.sql index b23bd1cd087..259f7c26fe6 100644 --- a/src/test/modules/test_extensible/sql/test_extensible.sql +++ b/src/test/modules/test_extensible/sql/test_extensible.sql @@ -33,6 +33,12 @@ SELECT test_ext_node_equal(test_ext_node_make('1234'::oid, 2), -- correct node type, missing field SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename TestExtNode}'); +-- other node types, rejected before reaching their core read routines +SELECT test_ext_node_get_relid('{QUERY}'); +-- extensible node of another kind +SELECT test_ext_node_get_relid('{EXTENSIBLENODE :extnodename NoSuchExtNode}'); +-- not a node string at all +SELECT test_ext_node_get_relid('42'); -- CustomScan tests CREATE TABLE test_extensible_tbl (id integer, val text); diff --git a/src/test/modules/test_extensible/test_extensible.c b/src/test/modules/test_extensible/test_extensible.c index 2a1344f28d5..944ba0cb161 100644 --- a/src/test/modules/test_extensible/test_extensible.c +++ b/src/test/modules/test_extensible/test_extensible.c @@ -99,21 +99,43 @@ test_ext_node_out_cb(StringInfo str, const ExtensibleNode *node) } /* - * Fetch the next token, erroring out instead of returning NULL. + * Fetch the next token of the string being read. * - * Unlike anything in readfuncs.c, this callback is reachable with arbitrary - * strings through SQL function calls, so we need this check. + * Unlike anything in readfuncs.c, this is reachable with arbitrary strings + * through SQL function calls, so the tokens need to be checked. The two + * modes of this function fail differently: + * + * - With "expected" set to NULL, any token is accepted, but running out of + * them is an error, reported here. This is what the nodeRead callback + * needs once the string is known to describe one of our nodes. + * + * - With a non-NULL "expected", a missing or different token is reported + * to the caller as NULL, so that text_to_test_ext_node() can check a + * sequence of tokens and issue a single error for the whole string. */ static const char * -test_ext_node_next_token(ReadNodeContext *ctx) +test_ext_node_next_token(ReadNodeContext *ctx, const char *expected) { int length; const char *token = pg_strtok(ctx, &length); if (token == NULL) + { + if (expected != NULL) + return NULL; + ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("unexpected end of \"%s\"", TEST_EXT_NODE_NAME))); + } + + if (expected != NULL) + { + if (length != (int) strlen(expected)) + return NULL; + if (memcmp(token, expected, length) != 0) + return NULL; + } return token; } @@ -123,11 +145,11 @@ test_ext_node_read_cb(ReadNodeContext *ctx, ExtensibleNode *node) { TestExtNode *tnode = (TestExtNode *) node; - (void) test_ext_node_next_token(ctx); /* skip :relid */ - tnode->relid = atooid(test_ext_node_next_token(ctx)); + (void) test_ext_node_next_token(ctx, NULL); /* skip :relid */ + tnode->relid = atooid(test_ext_node_next_token(ctx, NULL)); - (void) test_ext_node_next_token(ctx); /* skip :repeat_count */ - tnode->repeat_count = atoi(test_ext_node_next_token(ctx)); + (void) test_ext_node_next_token(ctx, NULL); /* skip :repeat_count */ + tnode->repeat_count = atoi(test_ext_node_next_token(ctx, NULL)); } static const ExtensibleNodeMethods test_ext_node_methods = @@ -463,22 +485,36 @@ test_get_custom_scan_methods(PG_FUNCTION_ARGS) } /* - * Decode a TestExtNode via stringToNode(), rejecting a string describing - * some other kind of node instead of misinterpreting it as one of ours. + * Decode a TestExtNode via stringToNode(). + * + * stringToNode() assumes its input to be valid, and the read routines of + * readfuncs.c it dispatches to do not check for missing tokens, so handing + * it a string naming any other node type could crash the backend. Hence, + * look at the leading tokens first and refuse anything that would not end + * up in our own nodeRead callback, the only one hardened against arbitrary + * input. */ static TestExtNode * text_to_test_ext_node(text *txt) { - Node *node = stringToNode(text_to_cstring(txt)); + char *str = text_to_cstring(txt); + ReadNodeContext ctx = {.str = str}; + TestExtNode *tnode; - if (node == NULL || !IsA(node, ExtensibleNode) || - strcmp(((ExtensibleNode *) node)->extnodename, TEST_EXT_NODE_NAME) != 0) + if (test_ext_node_next_token(&ctx, "{") == NULL || + test_ext_node_next_token(&ctx, "EXTENSIBLENODE") == NULL || + test_ext_node_next_token(&ctx, ":extnodename") == NULL || + test_ext_node_next_token(&ctx, TEST_EXT_NODE_NAME) == NULL) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument is not a serialized \"%s\"", TEST_EXT_NODE_NAME))); - return (TestExtNode *) node; + tnode = (TestExtNode *) stringToNode(str); + Assert(IsA(tnode, ExtensibleNode) && + strcmp(tnode->base.extnodename, TEST_EXT_NODE_NAME) == 0); + + return tnode; } /* -- 2.43.0
v2-0002-test_bitmapset-Check-argument-type-before-calling.patch (application/octet-stream, 6.2K) download From 8fe4e659ab62a3b4fa82cc88e18b61db08b9bd35 Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <zsolt.parragi@percona.com> Date: Sat, 5 Sep 2026 21:42:26 +0000 Subject: [PATCH v2 2/2] test_bitmapset: Check argument type before calling stringToNode() The SQL wrappers of this module decoded their text arguments with the generic stringToNode(), casting whatever came out to a Bitmapset. stringToNode() assumes its input to be valid: the read routines of readfuncs.c fetch tokens with pg_strtok() and feed them to atoi() without checking for NULL, so any other node type name, like '{QUERY}', crashes the backend on the first missing field. Strings for other node types that do read fine, like '42' or '(i 1 2)', were then misinterpreted as a Bitmapset. The functions are executable by any user, so this was reachable without any privileges. To fix, look at the leading tokens of the string before handing it to stringToNode(), and reject anything but the two forms nodeToString() writes for a Bitmapset: "<>" for an empty set, and "(b member ...)" otherwise. The code reading the latter in nodeRead() checks every token it consumes, so no other read routine is ever reached. Oversight in commit 00c3d87a5ca. --- .../expected/test_bitmapset.out | 27 +++++++++++ .../test_bitmapset/sql/test_bitmapset.sql | 12 +++++ .../modules/test_bitmapset/test_bitmapset.c | 48 +++++++++++++++++-- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/src/test/modules/test_bitmapset/expected/test_bitmapset.out b/src/test/modules/test_bitmapset/expected/test_bitmapset.out index f8f5cf5e7f0..7406ed929f9 100644 --- a/src/test/modules/test_bitmapset/expected/test_bitmapset.out +++ b/src/test/modules/test_bitmapset/expected/test_bitmapset.out @@ -1656,4 +1656,31 @@ SELECT test_random_offset_operations(NULL, 1000, 0, 1024) AS result; 1000 (1 row) +-- malformed inputs, rejected before reaching the read routines of other +-- node types +SELECT test_bms_num_members('{QUERY}'); -- error +ERROR: argument is not a serialized Bitmapset +SELECT test_bms_num_members('42'); -- error +ERROR: argument is not a serialized Bitmapset +SELECT test_bms_num_members('(i 1 2)'); -- error +ERROR: argument is not a serialized Bitmapset +SELECT test_bms_num_members('(b 1'); -- error +ERROR: unterminated Bitmapset structure +SELECT test_bms_num_members('(b x)'); -- error +ERROR: unrecognized integer: "x" +SELECT test_bms_num_members(''); -- error +ERROR: argument is not a serialized Bitmapset +-- empty set, as written by nodeToString() +SELECT test_bms_num_members('<>') AS result; + result +-------- + 0 +(1 row) + +SELECT test_bms_copy('<>') AS result; + result +-------- + <> +(1 row) + DROP EXTENSION test_bitmapset; diff --git a/src/test/modules/test_bitmapset/sql/test_bitmapset.sql b/src/test/modules/test_bitmapset/sql/test_bitmapset.sql index d44cda114a4..7096fa60fbe 100644 --- a/src/test/modules/test_bitmapset/sql/test_bitmapset.sql +++ b/src/test/modules/test_bitmapset/sql/test_bitmapset.sql @@ -426,4 +426,16 @@ SELECT test_random_operations(NULL, 10000, 0, 81920) > 0 AS result; -- perform some random tests on bms_offset_members() SELECT test_random_offset_operations(NULL, 1000, 0, 1024) AS result; +-- malformed inputs, rejected before reaching the read routines of other +-- node types +SELECT test_bms_num_members('{QUERY}'); -- error +SELECT test_bms_num_members('42'); -- error +SELECT test_bms_num_members('(i 1 2)'); -- error +SELECT test_bms_num_members('(b 1'); -- error +SELECT test_bms_num_members('(b x)'); -- error +SELECT test_bms_num_members(''); -- error +-- empty set, as written by nodeToString() +SELECT test_bms_num_members('<>') AS result; +SELECT test_bms_copy('<>') AS result; + DROP EXTENSION test_bitmapset; diff --git a/src/test/modules/test_bitmapset/test_bitmapset.c b/src/test/modules/test_bitmapset/test_bitmapset.c index af8c664949e..310dcf1a122 100644 --- a/src/test/modules/test_bitmapset/test_bitmapset.c +++ b/src/test/modules/test_bitmapset/test_bitmapset.c @@ -24,6 +24,7 @@ #include "nodes/bitmapset.h" #include "nodes/nodes.h" #include "nodes/pg_list.h" +#include "nodes/readfuncs.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/timestamp.h" @@ -86,16 +87,57 @@ PG_FUNCTION_INFO_V1(test_random_offset_operations); #expr, __FILE__, __LINE__); \ } while (0) -/* Encode/Decode to/from TEXT and Bitmapset */ +/* Encode a Bitmapset into its serialized representation */ #define BITMAPSET_TO_TEXT(bms) cstring_to_text(nodeToString(bms)) -#define TEXT_TO_BITMAPSET(str) ((Bitmapset *) stringToNode(text_to_cstring(str))) + +/* + * Decode a Bitmapset from its serialized representation via stringToNode(). + * + * stringToNode() assumes its input to be valid, and the read routines of + * readfuncs.c it dispatches to for other node types do not check for missing + * tokens, so handing it a string naming any of those could crash the backend. + * Hence, look at the leading tokens first and accept only the two forms + * written by nodeToString() for a Bitmapset: "<>" for an empty set, which + * pg_strtok() returns as a token of length zero, or "(b member ...)". The + * code reading the latter in nodeRead() checks every token it consumes. + */ +static Bitmapset * +text_to_bitmapset(text *txt) +{ + char *str = text_to_cstring(txt); + ReadNodeContext ctx = {.str = str}; + const char *token; + int length; + bool is_bitmapset = false; + Node *node; + + token = pg_strtok(&ctx, &length); + if (token != NULL && length == 0) + return NULL; + + if (token != NULL && length == 1 && token[0] == '(') + { + token = pg_strtok(&ctx, &length); + is_bitmapset = token != NULL && length == 1 && token[0] == 'b'; + } + + if (!is_bitmapset) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("argument is not a serialized Bitmapset"))); + + node = stringToNode(str); + Assert(node == NULL || IsA(node, Bitmapset)); + + return (Bitmapset *) node; +} /* * Helper macro to fetch text parameters as Bitmapsets. SQL-NULL means empty * set. */ #define PG_ARG_GETBITMAPSET(n) \ - (PG_ARGISNULL(n) ? NULL : TEXT_TO_BITMAPSET(PG_GETARG_TEXT_PP(n))) + (PG_ARGISNULL(n) ? NULL : text_to_bitmapset(PG_GETARG_TEXT_PP(n))) /* * Helper macro to handle converting sets back to text, returning the -- 2.43.0
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Improve error handling in test modules: test_extensible, test_bitmapset
@ 2026-09-10 02:31  Michael Paquier <michael@paquier.xyz>
  parent: Zsolt Parragi <zsolt.parragi@percona.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Michael Paquier @ 2026-09-10 02:31 UTC (permalink / raw)
  To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: Greg Burd <greg@burd.me>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; aleksander@tigerdata.com

On Wed, Sep 09, 2026 at 10:36:12AM +0100, Zsolt Parragi wrote:
> 0002 is now a bit longer with not relying on readBitmapset, but I
> think it's still okay/readable.

Cleaned up a lot of the overly-noisy comments, documented a bit more
the sanity checks done in the text->Bitmapset function, cleaned up a
couple of tests, and applied on HEAD for both (don't see much a point
in touching v19).
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-09-10 02:31 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-06 10:22 Improve error handling in test modules: test_extensible, test_bitmapset Zsolt Parragi <zsolt.parragi@percona.com>
2026-09-08 07:14 ` Michael Paquier <michael@paquier.xyz>
2026-09-08 16:16   ` Greg Burd <greg@burd.me>
2026-09-08 23:35     ` Michael Paquier <michael@paquier.xyz>
2026-09-09 09:36       ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-09-10 02:31         ` Michael Paquier <michael@paquier.xyz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox