Thread: silence GCC4 warning

silence GCC4 warning

From
Neil Conway
Date:
This patch silences a GCC 4.0 warning about a potentially-uninitialized
variable in pl_comp.c, and makes some minor associated cleanups. Barring
any objections I'll apply this to HEAD tomorrow.

-Neil
Index: src/pl/plpgsql/src/pl_comp.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/pl/plpgsql/src/pl_comp.c,v
retrieving revision 1.91
diff -c -r1.91 pl_comp.c
*** src/pl/plpgsql/src/pl_comp.c    10 Jun 2005 16:23:11 -0000    1.91
--- src/pl/plpgsql/src/pl_comp.c    4 Jul 2005 07:48:09 -0000
***************
*** 1369,1375 ****
      HeapTuple    attrtup = NULL;
      Form_pg_attribute attrStruct;
      HeapTuple    typetup = NULL;
!     char       *cp[2];
      char       *colname[1];
      int            qualified_att_len;
      int            numdots = 0;
--- 1369,1375 ----
      HeapTuple    attrtup = NULL;
      Form_pg_attribute attrStruct;
      HeapTuple    typetup = NULL;
!     char       *cp[2] = {NULL, NULL};
      char       *colname[1];
      int            qualified_att_len;
      int            numdots = 0;
***************
*** 1389,1410 ****
      {
          if (word[i] == '.' && ++numdots == 2)
          {
!             cp[0] = (char *) palloc((i + 1) * sizeof(char));
!             memset(cp[0], 0, (i + 1) * sizeof(char));
              memcpy(cp[0], word, i * sizeof(char));

              /*
               * qualified_att_len - one based position + 1 (null
               * terminator)
               */
!             cp[1] = (char *) palloc((qualified_att_len - i) * sizeof(char));
!             memset(cp[1], 0, (qualified_att_len - i) * sizeof(char));
              memcpy(cp[1], &word[i + 1], (qualified_att_len - i - 1) * sizeof(char));

              break;
          }
      }

      relvar = makeRangeVarFromNameList(stringToQualifiedNameList(cp[0], "plpgsql_parse_tripwordtype"));
      classOid = RangeVarGetRelid(relvar, true);
      if (!OidIsValid(classOid))
--- 1389,1411 ----
      {
          if (word[i] == '.' && ++numdots == 2)
          {
!             cp[0] = (char *) palloc0((i + 1) * sizeof(char));
              memcpy(cp[0], word, i * sizeof(char));

              /*
               * qualified_att_len - one based position + 1 (null
               * terminator)
               */
!             cp[1] = (char *) palloc0((qualified_att_len - i) * sizeof(char));
              memcpy(cp[1], &word[i + 1], (qualified_att_len - i - 1) * sizeof(char));

              break;
          }
      }

+     /* We must have found a match */
+     Assert(i < qualified_att_len);
+
      relvar = makeRangeVarFromNameList(stringToQualifiedNameList(cp[0], "plpgsql_parse_tripwordtype"));
      classOid = RangeVarGetRelid(relvar, true);
      if (!OidIsValid(classOid))

Re: silence GCC4 warning

From
Tom Lane
Date:
Neil Conway <neilc@samurai.com> writes:
> This patch silences a GCC 4.0 warning about a potentially-uninitialized
> variable in pl_comp.c,

AFAICT, gcc4's default behavior is to warn about

    int    foo;

    somefunc(&foo);

which unfortunately is a very common usage that I'm not real thrilled
about having to fix every occurrence of.  I've been meaning to look into
whether they provided a pragma or something to mark function parameters
as output-only, so that this message could be suppressed where
appropriate without adding a lot of useless initializations.

In the meantime, I don't see the point of patching just one place.

            regards, tom lane

Re: silence GCC4 warning

From
Peter Eisentraut
Date:
Tom Lane wrote:
> AFAICT, gcc4's default behavior is to warn about
>
>     int    foo;
>
>     somefunc(&foo);

A simple test shows that this is not the case, even with lots of -W
options.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

Re: silence GCC4 warning

From
Neil Conway
Date:
Peter Eisentraut wrote:
> A simple test shows that this is not the case, even with lots of -W
> options.

Right -- AFAICS we only get the following warnings of this class with
GCC 4.0 on current sources:

% gcc --version
gcc-4.0 (GCC) 4.0.1 20050701 (prerelease) (Debian 4.0.0-12)
[...]
% make -s clean all
[...]
In file included from /home/neilc/pgsql/src/backend/regex/regexec.c:1070:
/home/neilc/pgsql/src/backend/regex/rege_dfa.c: In function 'getvacant':
/home/neilc/pgsql/src/backend/regex/rege_dfa.c:581: warning: 'lastap.ss'
may be used uninitialized in this function
/home/neilc/pgsql/src/backend/regex/rege_dfa.c:581: warning: 'lastap.co'
may be used uninitialized in this function

/home/neilc/pgsql/src/backend/utils/adt/inet_net_ntop.c: In function
'inet_net_ntop_ipv6':
/home/neilc/pgsql/src/backend/utils/adt/inet_net_ntop.c:427: warning:
'cur.len' may be used uninitialized in this function
/home/neilc/pgsql/src/backend/utils/adt/inet_net_ntop.c:427: warning:
'best.len' may be used uninitialized in this function

/home/neilc/pgsql/src/interfaces/ecpg/pgtypeslib/datetime.c: In function
'PGTYPESdate_defmt_asc':
/home/neilc/pgsql/src/interfaces/ecpg/pgtypeslib/datetime.c:335:
warning: 'tm.tm_mday' may be used uninitialized in this function
/home/neilc/pgsql/src/interfaces/ecpg/pgtypeslib/datetime.c:335:
warning: 'tm.tm_mon' may be used uninitialized in this function
/home/neilc/pgsql/src/interfaces/ecpg/pgtypeslib/datetime.c:335:
warning: 'tm.tm_year' may be used uninitialized in this function

/home/neilc/pgsql/src/pl/plpgsql/src/pl_comp.c: In function
'plpgsql_parse_tripwordtype':
/home/neilc/pgsql/src/pl/plpgsql/src/pl_comp.c:1372: warning: 'cp[0]'
may be used uninitialized in this function
/home/neilc/pgsql/src/pl/plpgsql/src/pl_comp.c:1372: warning: 'cp[1]'
may be used uninitialized in this function

(Plus many errors regarding signedness, which is a separate matter.)

All these warnings like somewhat reasonable, AFAICS: while the code may
actually always initialize the variables before using them, it's
understandable that the compiler can't infer this automatically. And
this is surely a tiny fraction of the changes that would be required if
something as fundamental as initialization via out parameters was broken.

-Neil

Re: silence GCC4 warning

From
Tom Lane
Date:
Neil Conway <neilc@samurai.com> writes:
> Peter Eisentraut wrote:
>> A simple test shows that this is not the case, even with lots of -W
>> options.

> Right -- AFAICS we only get the following warnings of this class with
> GCC 4.0 on current sources:

Hmm ... my comments were based on experiments with a prerelease gcc4
that was circulating inside Red Hat; IIRC it showed some hundreds of
warnings of this type on the PG sources :-(.  It sounds like they've
improved it somewhat, but I'm still not clear on what it is warning
about that prior releases did not.

            regards, tom lane