Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: return expected user-facing error for invalid unnest arguments #110956

Merged
merged 1 commit into from
Sep 22, 2023

Conversation

DrewKimball
Copy link
Collaborator

Previously, the unnest builtin function could trigger an internal error when passed multiple non array-type arguments. This is because it only checked whether the arguments were NULL when determining whether they were of a valid type. This is not a problem for some types, like INT, because they will prevent the function overloads from being resolved.

However, since TEXT arguments can be cast to ARRAY types, function overload resolution succeeds. Since unnest only checked for NULL, it would assume that the arguments were array types, and attempt to retrieve the (nil) array contents. For the single-argument case this wasn't a problem because nil is used to signal invalid arguments, anyway. However, the multiple-argument case wraps the array contents of each argument type into a tuple, resulting in a tuple-type of nil types. This caused a nil-pointer dereference later down the line.

This patch prevents the internal error by checking directly that the arguments are ARRAY types, to ensure that the array contents are non-nil. If the check fails, the (nil) tree.UnknownReturnType type is returned, which signals an invalid type. That results in an expected, user-facing error instead of an internal error.

The information_schema._pg_expandarray builtin function had a similar vulnerability. This patch fixes that as well.

Fixes #110952

Release note (bug fix): Fixed an edge case in the unnest and information_schema._pg_expandarray builtin functions that could cause an internal error when passed string arguments that could be cast to an array.

@DrewKimball DrewKimball added backport-22.2.x backport-23.1.x Flags PRs that need to be backported to 23.1 labels Sep 20, 2023
@DrewKimball DrewKimball requested review from michae2 and a team September 20, 2023 07:45
@DrewKimball DrewKimball requested a review from a team as a code owner September 20, 2023 07:45
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: thanks!

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice fix!

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @DrewKimball)


pkg/sql/logictest/testdata/logic_test/srfs line 1428 at r1 (raw file):


statement error pgcode 42804 pq: could not determine polymorphic type: unnest\(unknown, unknown, unknown\)
SELECT unnest(NULL, NULL, NULL);

Could you add the following testcases as well:

SELECT unnest('{1}'::int[], '{2}', '{3}');
SELECT unnest('{1}'::int[], '{}', '{}');
SELECT unnest('{1}', '{2}', '{3}'::int[]);
SELECT unnest('{}', '{}', '{3}'::int[]);

Previously, the `unnest` builtin function could trigger an internal
error when passed multiple non array-type arguments. This is because
it only checked whether the arguments were `NULL` when determining
whether they were of a valid type. This is not a problem for some types,
like `INT`, because they will prevent the function overloads from being
resolved.

However, since `TEXT` arguments can be cast to `ARRAY` types, function
overload resolution succeeds. Since `unnest` only checked for `NULL`,
it would assume that the arguments were array types, and attempt to
retrieve the (nil) array contents. For the single-argument case this
wasn't a problem because nil is used to signal invalid arguments, anyway.
However, the multiple-argument case wraps the array contents of each argument
type into a tuple, resulting in a tuple-type of nil types. This caused
a nil-pointer dereference later down the line.

This patch prevents the internal error by checking directly that the arguments
are `ARRAY` types, to ensure that the array contents are non-nil. If the
check fails, the (nil) `tree.UnknownReturnType` type is returned, which
signals an invalid type. That results in an expected, user-facing error instead
of an internal error.

The `information_schema._pg_expandarray` builtin function had a similar
vulnerability. This patch fixes that as well.

Fixes cockroachdb#110952

Release note (bug fix): Fixed an edge case in the `unnest` and
`information_schema._pg_expandarray` builtin functions that could cause
an internal error when passed string arguments that could be cast to
an array.
Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @michae2)


pkg/sql/logictest/testdata/logic_test/srfs line 1428 at r1 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Could you add the following testcases as well:

SELECT unnest('{1}'::int[], '{2}', '{3}');
SELECT unnest('{1}'::int[], '{}', '{}');
SELECT unnest('{1}', '{2}', '{3}'::int[]);
SELECT unnest('{}', '{}', '{3}'::int[]);

Done.

@DrewKimball
Copy link
Collaborator Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Sep 22, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Sep 22, 2023

Build succeeded:

@craig craig bot merged commit ed75b08 into cockroachdb:master Sep 22, 2023
@blathers-crl
Copy link

blathers-crl bot commented Sep 22, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 1f24340 to blathers/backport-release-22.2-110956: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


error creating merge commit from 1f24340 to blathers/backport-release-23.1-110956: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: unnest builtin function internal error for non-array arguments
4 participants