Thread: silence compiler warning in brin.c

silence compiler warning in brin.c

From
Nathan Bossart
Date:
Hi hackers,

I'm seeing a compiler warning in brin.c with an older version of gcc.
Specifically, it seems worried that a variable might not be initialized.
AFAICT there is no real risk, so I've attached a small patch to silence the
warning.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

Attachment

Re: silence compiler warning in brin.c

From
Zhihong Yu
Date:


On Wed, Jun 1, 2022 at 9:35 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
Hi hackers,

I'm seeing a compiler warning in brin.c with an older version of gcc.
Specifically, it seems worried that a variable might not be initialized.
AFAICT there is no real risk, so I've attached a small patch to silence the
warning.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com
Hi,
It seems the variable can be initialized to the value of GUCNestLevel since later in the func:

    /* Roll back any GUC changes executed by index functions */
    AtEOXact_GUC(false, save_nestlevel);

Cheers

Re: silence compiler warning in brin.c

From
Tom Lane
Date:
Zhihong Yu <zyu@yugabyte.com> writes:
> On Wed, Jun 1, 2022 at 9:35 AM Nathan Bossart <nathandbossart@gmail.com>
> wrote:
>> I'm seeing a compiler warning in brin.c with an older version of gcc.
>> Specifically, it seems worried that a variable might not be initialized.
>> AFAICT there is no real risk, so I've attached a small patch to silence the
>> warning.

Yeah, I noticed the other day that a couple of older buildfarm members
(curculio, gaur) are grousing about this too.  We don't really have a
hard-n-fast rule about how old a compiler needs to be before we stop
worrying about its notions about uninitialized variables, but these are
kind of old.  Still, since this is the only such warning from these
animals, I'm inclined to silence it.

> It seems the variable can be initialized to the value of GUCNestLevel since
> later in the func:
>     /* Roll back any GUC changes executed by index functions */
>     AtEOXact_GUC(false, save_nestlevel);

That seems pretty inappropriate.  If, thanks to some future thinko,
control were able to reach the AtEOXact_GUC call despite not having
called NewGUCNestLevel, we'd want that to fail.  It looks like
AtEOXact_GUC asserts nestLevel > 0, so that either 0 or -1 would
do as an "invalid" value; I'd lean a bit to using 0.

            regards, tom lane



Re: silence compiler warning in brin.c

From
Nathan Bossart
Date:
On Wed, Jun 01, 2022 at 01:06:21PM -0400, Tom Lane wrote:
> Zhihong Yu <zyu@yugabyte.com> writes:
>> It seems the variable can be initialized to the value of GUCNestLevel since
>> later in the func:
>>     /* Roll back any GUC changes executed by index functions */
>>     AtEOXact_GUC(false, save_nestlevel);
> 
> That seems pretty inappropriate.  If, thanks to some future thinko,
> control were able to reach the AtEOXact_GUC call despite not having
> called NewGUCNestLevel, we'd want that to fail.

+1

> It looks like
> AtEOXact_GUC asserts nestLevel > 0, so that either 0 or -1 would
> do as an "invalid" value; I'd lean a bit to using 0.

I only chose -1 to follow a117ceb's example in amcheck.  I have no
preference.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com



Re: silence compiler warning in brin.c

From
Tom Lane
Date:
Zhihong Yu <zyu@yugabyte.com> writes:
> Hi,

>     if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
>         ereport(ERROR,

> I wonder why the above check is not placed in the else block:

>     else
>         heapRel = NULL;

Because we don't want to throw that error until we've exhausted the
possibilities for throwing other errors.

            regards, tom lane



Re: silence compiler warning in brin.c

From
Zhihong Yu
Date:


On Wed, Jun 1, 2022 at 10:06 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Zhihong Yu <zyu@yugabyte.com> writes:
> On Wed, Jun 1, 2022 at 9:35 AM Nathan Bossart <nathandbossart@gmail.com>
> wrote:
>> I'm seeing a compiler warning in brin.c with an older version of gcc.
>> Specifically, it seems worried that a variable might not be initialized.
>> AFAICT there is no real risk, so I've attached a small patch to silence the
>> warning.

Yeah, I noticed the other day that a couple of older buildfarm members
(curculio, gaur) are grousing about this too.  We don't really have a
hard-n-fast rule about how old a compiler needs to be before we stop
worrying about its notions about uninitialized variables, but these are
kind of old.  Still, since this is the only such warning from these
animals, I'm inclined to silence it.

> It seems the variable can be initialized to the value of GUCNestLevel since
> later in the func:
>     /* Roll back any GUC changes executed by index functions */
>     AtEOXact_GUC(false, save_nestlevel);

That seems pretty inappropriate.  If, thanks to some future thinko,
control were able to reach the AtEOXact_GUC call despite not having
called NewGUCNestLevel, we'd want that to fail.  It looks like
AtEOXact_GUC asserts nestLevel > 0, so that either 0 or -1 would
do as an "invalid" value; I'd lean a bit to using 0.

                        regards, tom lane
Hi,

    if (heapRel == NULL || heapoid != IndexGetRelation(indexoid, false))
        ereport(ERROR,

I wonder why the above check is not placed in the else block:

    else
        heapRel = NULL;

because heapRel is not modified between the else and the above check.
If the check is placed in the else block, we can potentially save the call to index_open().

Cheers

Re: silence compiler warning in brin.c

From
Tom Lane
Date:
Nathan Bossart <nathandbossart@gmail.com> writes:
> On Wed, Jun 01, 2022 at 01:06:21PM -0400, Tom Lane wrote:
>> It looks like
>> AtEOXact_GUC asserts nestLevel > 0, so that either 0 or -1 would
>> do as an "invalid" value; I'd lean a bit to using 0.

> I only chose -1 to follow a117ceb's example in amcheck.  I have no
> preference.

Hmm, if we're following amcheck's example it should be more like this:

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index 52f171772d..0de1441dc6 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1051,7 +1051,13 @@ brin_summarize_range(PG_FUNCTION_ARGS)
         save_nestlevel = NewGUCNestLevel();
     }
     else
+    {
         heapRel = NULL;
+        /* Set these just to suppress "uninitialized variable" warnings */
+        save_userid = InvalidOid;
+        save_sec_context = -1;
+        save_nestlevel = -1;
+    }
 
     indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
 
I like this better anyway since the fact that the other two variables
aren't warned about seems like an implementation artifact.

            regards, tom lane



Re: silence compiler warning in brin.c

From
Nathan Bossart
Date:
On Wed, Jun 01, 2022 at 05:08:03PM -0400, Tom Lane wrote:
> Hmm, if we're following amcheck's example it should be more like this:
> 
> diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
> index 52f171772d..0de1441dc6 100644
> --- a/src/backend/access/brin/brin.c
> +++ b/src/backend/access/brin/brin.c
> @@ -1051,7 +1051,13 @@ brin_summarize_range(PG_FUNCTION_ARGS)
>          save_nestlevel = NewGUCNestLevel();
>      }
>      else
> +    {
>          heapRel = NULL;
> +        /* Set these just to suppress "uninitialized variable" warnings */
> +        save_userid = InvalidOid;
> +        save_sec_context = -1;
> +        save_nestlevel = -1;
> +    }
>  
>      indexRel = index_open(indexoid, ShareUpdateExclusiveLock);
>  
> I like this better anyway since the fact that the other two variables
> aren't warned about seems like an implementation artifact.

Yeah, that is better.  It's not clear why the other variables aren't
subject to the same warnings, so we might as well cover our bases.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com