pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Suspicious strcmp() in src/backend/parser/parse_expr.c
13+ messages / 6 participants
[nested] [flat]

Commits from this thread:
  2019-04-12 d527fda621  Fix more strcmp() calls using boolean-like comparisons for result checks
  2019-04-10 4cae471d1b 930930c476 99bbff5c15 62d4953956 b9b7fe8ca4  Fix backwards test in operator_precedence_warning logic.
* Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-10 21:40  Rikard Falkeborn <rikard.falkeborn@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Rikard Falkeborn @ 2019-04-10 21:40 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

In src/backend/parser/parse_expr.c the following snippet of code is found
(lines 3238-3242, rev 765525c8c2c6e55abe):

    if (strcmp(*nodename, "+") == 0 ||
            strcmp(*nodename, "-")) // <-- notice the lack of comparisson
here
            group = 0;
    else
            group = PREC_GROUP_PREFIX_OP;

Should the second part of the || be strcmp(*nodename, "-") == 0?

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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-10 22:33  Andrew Gierth <andrew@tao11.riddles.org.uk>
  parent: Rikard Falkeborn <rikard.falkeborn@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Andrew Gierth @ 2019-04-10 22:33 UTC (permalink / raw)
  To: Rikard Falkeborn <rikard.falkeborn@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

>>>>> "Rikard" == Rikard Falkeborn <rikard.falkeborn@gmail.com> writes:

 Rikard> In src/backend/parser/parse_expr.c the following snippet of code is found
 Rikard> (lines 3238-3242, rev 765525c8c2c6e55abe):

 Rikard>     if (strcmp(*nodename, "+") == 0 ||
 Rikard>             strcmp(*nodename, "-")) // <-- notice the lack of comparisson
 Rikard> here
 Rikard>             group = 0;
 Rikard>     else
 Rikard>             group = PREC_GROUP_PREFIX_OP;

 Rikard> Should the second part of the || be strcmp(*nodename, "-") ==
 Rikard> 0?

Yes it should.

The effect of this bug is to produce a false operator precedence warning
when those are enabled, like so:

postgres=# set operator_precedence_warning = on;
SET
postgres=# select -random() is null;
WARNING:  operator precedence change: IS is now lower precedence than -

when in fact "-random() is null" always did parse as "(-random()) is null"
making the warning spurious.

-- 
Andrew (irc:RhodiumToad)





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-10 22:43  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
  0 siblings, 1 reply; 13+ messages in thread

From: Tom Lane @ 2019-04-10 22:43 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Rikard Falkeborn <rikard.falkeborn@gmail.com>; pgsql-bugs@lists.postgresql.org

Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> "Rikard" == Rikard Falkeborn <rikard.falkeborn@gmail.com> writes:
>  Rikard>     if (strcmp(*nodename, "+") == 0 ||
>  Rikard>             strcmp(*nodename, "-")) // <-- notice the lack of comparisson here
>  Rikard> Should the second part of the || be strcmp(*nodename, "-") == 0?

> Yes it should.

Indeed.  Considering how much I hate using strcmp's result as a boolean,
you'd think I'd have got that right.  Thanks for noticing!

			regards, tom lane





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 02:19  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 13+ messages in thread

From: Michael Paquier @ 2019-04-11 02:19 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; pgsql-bugs@lists.postgresql.org

On Wed, Apr 10, 2019 at 06:43:32PM -0400, Tom Lane wrote:
> Indeed.  Considering how much I hate using strcmp's result as a boolean,
> you'd think I'd have got that right.  Thanks for noticing!

Just a note about those strcmp() calls using a boolean as return
result in the tree:
src/backend/commands/lockcmds.c: (!strcmp(rte->eref->aliasname, "old")
|| !strcmp(rte->eref->aliasname, "new")))
src/test/modules/test_rls_hooks/test_rls_hooks.c:   if
(strcmp(RelationGetRelationName(relation), "rls_test_permissive")
src/test/modules/test_rls_hooks/test_rls_hooks.c:       &&
strcmp(RelationGetRelationName(relation), "rls_test_both"))
src/test/modules/test_rls_hooks/test_rls_hooks.c:   if
(strcmp(RelationGetRelationName(relation), "rls_test_restrictive")
src/test/modules/test_rls_hooks/test_rls_hooks.c:       &&
strcmp(RelationGetRelationName(relation), "rls_test_both"))

Would it be worth changing these as well?
--
Michael

Attachments:

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

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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 03:26  Thomas Munro <thomas.munro@gmail.com>
  parent: Michael Paquier <michael@paquier.xyz>
  1 sibling, 2 replies; 13+ messages in thread

From: Thomas Munro @ 2019-04-11 03:26 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, Apr 11, 2019 at 2:20 PM Michael Paquier <michael@paquier.xyz> wrote:
> On Wed, Apr 10, 2019 at 06:43:32PM -0400, Tom Lane wrote:
> > Indeed.  Considering how much I hate using strcmp's result as a boolean,
> > you'd think I'd have got that right.  Thanks for noticing!
>
> Just a note about those strcmp() calls using a boolean as return
> result in the tree:
> src/backend/commands/lockcmds.c: (!strcmp(rte->eref->aliasname, "old")

Don't look at contrib/spi/refint.c if you value your sanity.

-- 
Thomas Munro
https://enterprisedb.com





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 03:43  David Rowley <david.rowley@2ndquadrant.com>
  parent: Thomas Munro <thomas.munro@gmail.com>
  1 sibling, 0 replies; 13+ messages in thread

From: David Rowley @ 2019-04-11 03:43 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, 11 Apr 2019 at 15:27, Thomas Munro <thomas.munro@gmail.com> wrote:
>
> On Thu, Apr 11, 2019 at 2:20 PM Michael Paquier <michael@paquier.xyz> wrote:
> > On Wed, Apr 10, 2019 at 06:43:32PM -0400, Tom Lane wrote:
> > > Indeed.  Considering how much I hate using strcmp's result as a boolean,
> > > you'd think I'd have got that right.  Thanks for noticing!
> >
> > Just a note about those strcmp() calls using a boolean as return
> > result in the tree:
> > src/backend/commands/lockcmds.c: (!strcmp(rte->eref->aliasname, "old")
>
> Don't look at contrib/spi/refint.c if you value your sanity.

hmm, yeah. Take a non-bool expression, make it into a bool expression,
then compare that to 0 to make it look like a non-bool expression.
Weird.

If we're fixing some, we may as well do them all.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 03:46  Michael Paquier <michael@paquier.xyz>
  parent: Thomas Munro <thomas.munro@gmail.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Michael Paquier @ 2019-04-11 03:46 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, Apr 11, 2019 at 03:26:31PM +1200, Thomas Munro wrote:
> Don't look at contrib/spi/refint.c if you value your sanity.

Perhaps I don't value it much then.  At least it compares to 0 after
compiling a set of bool results.
--
Michael

Attachments:

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

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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 04:55  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael@paquier.xyz>
  1 sibling, 1 reply; 13+ messages in thread

From: Tom Lane @ 2019-04-11 04:55 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; pgsql-bugs@lists.postgresql.org

Michael Paquier <michael@paquier.xyz> writes:
> On Wed, Apr 10, 2019 at 06:43:32PM -0400, Tom Lane wrote:
>> Indeed.  Considering how much I hate using strcmp's result as a boolean,
>> you'd think I'd have got that right.  Thanks for noticing!

> Would it be worth changing these as well?

I'd be +1 for that, just on the grounds of having consistent coding
style.  But I'm not sufficiently excited about it to do the work
myself ...

			regards, tom lane





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 06:50  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 13+ messages in thread

From: Michael Paquier @ 2019-04-11 06:50 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; pgsql-bugs@lists.postgresql.org

On Thu, Apr 11, 2019 at 12:55:49AM -0400, Tom Lane wrote:
> I'd be +1 for that, just on the grounds of having consistent coding
> style.  But I'm not sufficiently excited about it to do the work
> myself ...

Well, here you go as per the attached as we are on it.  I am not
noticing any other spots.
--
Michael

Attachments:

strcmp-fixes.patch (text/x-diff, 2.6K) download diff --git a/contrib/spi/refint.c b/contrib/spi/refint.c index f90f2bce0e..adf0490f85 100644 --- a/contrib/spi/refint.c +++ b/contrib/spi/refint.c @@ -473,9 +473,12 @@ check_foreign_key(PG_FUNCTION_ARGS) nv = SPI_getvalue(newtuple, tupdesc, fn); type = SPI_gettype(tupdesc, fn); - if ((strcmp(type, "text") && strcmp(type, "varchar") && - strcmp(type, "char") && strcmp(type, "bpchar") && - strcmp(type, "date") && strcmp(type, "timestamp")) == 0) + if (strcmp(type, "text") == 0 || + strcmp(type, "varchar") == 0 || + strcmp(type, "char") == 0 || + strcmp(type, "bpchar") == 0 || + strcmp(type, "date") == 0 || + strcmp(type, "timestamp") == 0) is_char_type = 1; #ifdef DEBUG_QUERY elog(DEBUG4, "check_foreign_key Debug value %s type %s %d", diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c index df681e3234..97cf0bc4ad 100644 --- a/src/backend/commands/lockcmds.c +++ b/src/backend/commands/lockcmds.c @@ -219,7 +219,8 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context) * skipped. */ if (relid == context->viewoid && - (!strcmp(rte->eref->aliasname, "old") || !strcmp(rte->eref->aliasname, "new"))) + (strcmp(rte->eref->aliasname, "old") == 0 || + strcmp(rte->eref->aliasname, "new") == 0)) continue; /* Currently, we only allow plain tables or views to be locked. */ diff --git a/src/test/modules/test_rls_hooks/test_rls_hooks.c b/src/test/modules/test_rls_hooks/test_rls_hooks.c index 8bf8f764ac..10379bc59c 100644 --- a/src/test/modules/test_rls_hooks/test_rls_hooks.c +++ b/src/test/modules/test_rls_hooks/test_rls_hooks.c @@ -75,8 +75,8 @@ test_rls_hooks_permissive(CmdType cmdtype, Relation relation) ParseState *qual_pstate; RangeTblEntry *rte; - if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") - && strcmp(RelationGetRelationName(relation), "rls_test_both")) + if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 && + strcmp(RelationGetRelationName(relation), "rls_test_both") != 0) return NIL; qual_pstate = make_parsestate(NULL); @@ -140,8 +140,8 @@ test_rls_hooks_restrictive(CmdType cmdtype, Relation relation) RangeTblEntry *rte; - if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") - && strcmp(RelationGetRelationName(relation), "rls_test_both")) + if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 && + strcmp(RelationGetRelationName(relation), "rls_test_both") != 0) return NIL; qual_pstate = make_parsestate(NULL);
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 13+ messages in thread

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 14:06  David Rowley <david.rowley@2ndquadrant.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 2 replies; 13+ messages in thread

From: David Rowley @ 2019-04-11 14:06 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, 11 Apr 2019 at 18:51, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Thu, Apr 11, 2019 at 12:55:49AM -0400, Tom Lane wrote:
> > I'd be +1 for that, just on the grounds of having consistent coding
> > style.  But I'm not sufficiently excited about it to do the work
> > myself ...
>
> Well, here you go as per the attached as we are on it.  I am not
> noticing any other spots.

Patch looks fine to me.  I also made a quick pass and noticed a few
more things out of place.

formatting.c in NUM_prepare_locale()

else if (strcmp(Np->decimal, ",") !=0)


spell.c in NISortDictionary()

if (i == 0
|| strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag))

if (i == 0
|| strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]))

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-11 14:12  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David Rowley <david.rowley@2ndquadrant.com>
  1 sibling, 1 reply; 13+ messages in thread

From: Tom Lane @ 2019-04-11 14:12 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

David Rowley <david.rowley@2ndquadrant.com> writes:
> formatting.c in NUM_prepare_locale()

> else if (strcmp(Np->decimal, ",") !=0)

I'll bet a nickel that that's pgindent's fault.  It probably thinks
that "decimal" is a typedef (greps typedefs.list ... yes), and that
tends to result in funny space-omissions later in the line.

			regards, tom lane





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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-12 00:58  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 13+ messages in thread

From: Michael Paquier @ 2019-04-12 00:58 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David Rowley <david.rowley@2ndquadrant.com>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, Apr 11, 2019 at 10:12:06AM -0400, Tom Lane wrote:
> I'll bet a nickel that that's pgindent's fault.  It probably thinks
> that "decimal" is a typedef (greps typedefs.list ... yes), and that
> tends to result in funny space-omissions later in the line.

Yes, I can see as well that pgindent complains here.  As that would
result in more noise on follow-up pgindent runs, I am not changing
this one.
--
Michael

Attachments:

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

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

* Re: Suspicious strcmp() in src/backend/parser/parse_expr.c
@ 2019-04-12 01:29  Michael Paquier <michael@paquier.xyz>
  parent: David Rowley <david.rowley@2ndquadrant.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Michael Paquier @ 2019-04-12 01:29 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; Rikard Falkeborn <rikard.falkeborn@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Fri, Apr 12, 2019 at 02:06:11AM +1200, David Rowley wrote:
> spell.c in NISortDictionary()
> 
> if (i == 0
> || strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag))
> 
> if (i == 0
> || strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]))

Good catch on these two.  I have included these and fixed all the
spots found in d527fda.
--
Michael

Attachments:

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

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


end of thread, other threads:[~2019-04-12 01:29 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-04-10 21:40 Suspicious strcmp() in src/backend/parser/parse_expr.c Rikard Falkeborn <rikard.falkeborn@gmail.com>
2019-04-10 22:33 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-04-10 22:43   ` Tom Lane <tgl@sss.pgh.pa.us>
2019-04-11 02:19     ` Michael Paquier <michael@paquier.xyz>
2019-04-11 03:26       ` Thomas Munro <thomas.munro@gmail.com>
2019-04-11 03:43         ` David Rowley <david.rowley@2ndquadrant.com>
2019-04-11 03:46         ` Michael Paquier <michael@paquier.xyz>
2019-04-11 04:55       ` Tom Lane <tgl@sss.pgh.pa.us>
2019-04-11 06:50         ` Michael Paquier <michael@paquier.xyz>
2019-04-11 14:06           ` David Rowley <david.rowley@2ndquadrant.com>
2019-04-11 14:12             ` Tom Lane <tgl@sss.pgh.pa.us>
2019-04-12 00:58               ` Michael Paquier <michael@paquier.xyz>
2019-04-12 01:29             ` 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