pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14220: pg_get_expr() with an incorrect relation id crashes the server
4+ messages / 4 participants
[nested] [flat]

* BUG #14220: pg_get_expr() with an incorrect relation id crashes the server
@ 2016-06-29 22:43 christopher.m.hanks@gmail.com
  2016-06-30 20:42 ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Thomas Munro <thomas.munro@enterprisedb.com>
  0 siblings, 1 reply; 4+ messages in thread

From: christopher.m.hanks@gmail.com @ 2016-06-29 22:43 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14220
Logged by:          Chris Hanks
Email address:      christopher.m.hanks@gmail.com
PostgreSQL version: 9.5.3
Operating system:   Linux Mint 17.2 64-bit
Description:        

SELECT version():
"PostgreSQL 9.5.3 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu
4.8.2-19ubuntu1) 4.8.2, 64-bit"

Reproduction:

CREATE TABLE "test_table" ("id" serial PRIMARY KEY, "column_1" int4 NOT
NULL, "column_2" int4);

CREATE INDEX "idx" ON "test_table" ("column_1") WHERE ("column_2" = 4);

SELECT pg_get_expr(i.indpred, i.indexrelid) FROM pg_index i;

I realize that the query is incorrect, in order to retrieve the filter
expression as SQL I need to call pg_get_expr(i.indpred, i.indrelid). But it
seems like it should raise an error, rather than causing the server to
crash.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


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

* Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server
  2016-06-29 22:43 BUG #14220: pg_get_expr() with an incorrect relation id crashes the server christopher.m.hanks@gmail.com
@ 2016-06-30 20:42 ` Thomas Munro <thomas.munro@enterprisedb.com>
  2016-07-01 01:12   ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Munro @ 2016-06-30 20:42 UTC (permalink / raw)
  To: christopher.m.hanks@gmail.com; +Cc: pgsql-bugs

On Thu, Jun 30, 2016 at 10:43 AM,  <christopher.m.hanks@gmail.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference:      14220
> Logged by:          Chris Hanks
> Email address:      christopher.m.hanks@gmail.com
> PostgreSQL version: 9.5.3
> Operating system:   Linux Mint 17.2 64-bit
> Description:
>
> SELECT version():
> "PostgreSQL 9.5.3 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu
> 4.8.2-19ubuntu1) 4.8.2, 64-bit"
>
> Reproduction:
>
> CREATE TABLE "test_table" ("id" serial PRIMARY KEY, "column_1" int4 NOT
> NULL, "column_2" int4);
>
> CREATE INDEX "idx" ON "test_table" ("column_1") WHERE ("column_2" = 4);
>
> SELECT pg_get_expr(i.indpred, i.indexrelid) FROM pg_index i;
>
> I realize that the query is incorrect, in order to retrieve the filter
> expression as SQL I need to call pg_get_expr(i.indpred, i.indrelid). But it
> seems like it should raise an error, rather than causing the server to
> crash.

Maybe the attnum bounds check should be an error rather than an
assertion, like in the attached.  Thought perhaps with a better
message...

-- 
Thomas Munro
http://www.enterprisedb.com

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

reject-bogus-attnum.patch (application/octet-stream, 591B) download diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index afc26d4..b686651 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -6028,7 +6028,8 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context) else if (attnum > 0) { /* Get column name to use from the colinfo struct */ - Assert(attnum <= colinfo->num_cols); + if (attnum > colinfo->num_cols) + elog(ERROR, "cannot decompile bogus attnum: %d", attnum); attname = colinfo->colnames[attnum - 1]; Assert(attname != NULL); }
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server
  2016-06-29 22:43 BUG #14220: pg_get_expr() with an incorrect relation id crashes the server christopher.m.hanks@gmail.com
  2016-06-30 20:42 ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Thomas Munro <thomas.munro@enterprisedb.com>
@ 2016-07-01 01:12   ` Michael Paquier <michael.paquier@gmail.com>
  2016-07-01 14:32     ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Michael Paquier @ 2016-07-01 01:12 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@enterprisedb.com>; +Cc: pgsql-bugs

On Fri, Jul 1, 2016 at 5:42 AM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
> Maybe the attnum bounds check should be an error rather than an
> assertion, like in the attached.  Thought perhaps with a better
> message...

Quite interesting report. It looks right to me to do so with an elog(ERROR).

+        if (attnum > colinfo->num_cols)
+            elog(ERROR, "cannot decompile bogus attnum: %d", attnum);
Why not "incorrect number of arguments: specified %d but found %d" instead?

I have poked at this bug report yesterday and today, and found that it
is as well possible to hit the second assertion two lines below with a
dropped column. So I would like to propose the patch attached to
replace both assertions with an elog(ERROR). Test cases are included,
though I am not sure if they bring much value.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

reject-bogus-attnum-v2.patch (text/x-diff, 3.0K) download diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index afc26d4..136e2e6 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -6028,9 +6028,12 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context) else if (attnum > 0) { /* Get column name to use from the colinfo struct */ - Assert(attnum <= colinfo->num_cols); + if (attnum > colinfo->num_cols) + elog(ERROR, "incorrect number of arguments: specified %d but found %d", + attnum, colinfo->num_cols); attname = colinfo->colnames[attnum - 1]; - Assert(attname != NULL); + if (attname == NULL) + elog(ERROR, "invalid attribute name"); } else { diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 125c31b..d066572 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -3057,3 +3057,25 @@ SELECT * FROM hat_data WHERE hat_name IN ('h8', 'h9', 'h7') ORDER BY hat_name; DROP RULE hat_upsert ON hats; drop table hats; drop table hat_data; +-- tests for pg_get_expr +CREATE TABLE tab_short (a1 int, CHECK(a1 > 0)); +CREATE TABLE tab_long (a2 text, b2 text, CHECK(a2 IS NOT NULL AND b2 IS NOT NULL)); +CREATE TABLE tab_dropped_col (a3 int, b3 int); +ALTER TABLE tab_dropped_col DROP COLUMN b3; +-- contraint definition of tab_short uses column definitions from tab_long. +SELECT pg_get_expr(conbin, 'tab_long'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_short'::regclass; + pg_get_expr +------------- + (a2 > 0) +(1 row) + +-- error, column number mismatch +SELECT pg_get_expr(conbin, 'tab_short'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_long'::regclass; +ERROR: incorrect number of arguments: specified 2 but found 1 +-- error, invalid attribute name +SELECT pg_get_expr(conbin, 'tab_dropped_col'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_long'::regclass; +ERROR: invalid attribute name +DROP TABLE tab_short, tab_long, tab_dropped_col; diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index 4299a5b..360696c 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -1144,3 +1144,19 @@ DROP RULE hat_upsert ON hats; drop table hats; drop table hat_data; + +-- tests for pg_get_expr +CREATE TABLE tab_short (a1 int, CHECK(a1 > 0)); +CREATE TABLE tab_long (a2 text, b2 text, CHECK(a2 IS NOT NULL AND b2 IS NOT NULL)); +CREATE TABLE tab_dropped_col (a3 int, b3 int); +ALTER TABLE tab_dropped_col DROP COLUMN b3; +-- contraint definition of tab_short uses column definitions from tab_long. +SELECT pg_get_expr(conbin, 'tab_long'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_short'::regclass; +-- error, column number mismatch +SELECT pg_get_expr(conbin, 'tab_short'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_long'::regclass; +-- error, invalid attribute name +SELECT pg_get_expr(conbin, 'tab_dropped_col'::regclass) FROM pg_constraint + WHERE conrelid = 'tab_long'::regclass; +DROP TABLE tab_short, tab_long, tab_dropped_col;
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server
  2016-06-29 22:43 BUG #14220: pg_get_expr() with an incorrect relation id crashes the server christopher.m.hanks@gmail.com
  2016-06-30 20:42 ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Thomas Munro <thomas.munro@enterprisedb.com>
  2016-07-01 01:12   ` Re: BUG #14220: pg_get_expr() with an incorrect relation id crashes the server Michael Paquier <michael.paquier@gmail.com>
@ 2016-07-01 14:32     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-07-01 14:32 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Thomas Munro <thomas.munro@enterprisedb.com>; pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> I have poked at this bug report yesterday and today, and found that it
> is as well possible to hit the second assertion two lines below with a
> dropped column. So I would like to propose the patch attached to
> replace both assertions with an elog(ERROR).

There's a quite similar error check just a few lines above that prints

        elog(ERROR, "bogus varattno for subquery var: %d", var->varattno);

I'm not necessarily wedded to that exact phrasing, but I think these new
error messages should be consistent with that one.  I'm a bit inclined
to make all three of them read like

        elog(ERROR, "invalid attnum %d for table \"%s\"",
             attnum, rte->eref->aliasname);

> Test cases are included,
> though I am not sure if they bring much value.

Yeah, I can't get very excited about memorializing those.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2016-07-01 14:32 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-29 22:43 BUG #14220: pg_get_expr() with an incorrect relation id crashes the server christopher.m.hanks@gmail.com
2016-06-30 20:42 ` Thomas Munro <thomas.munro@enterprisedb.com>
2016-07-01 01:12   ` Michael Paquier <michael.paquier@gmail.com>
2016-07-01 14:32     ` Tom Lane <tgl@sss.pgh.pa.us>

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