Fix compilation warnings when CFLAGS -Og is specified

Started by Hayato Kuroda (Fujitsu)over 2 years ago3 messages
#1Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
1 attachment(s)

Dear hackers,

# Background

Based on [1]https://wiki.postgresql.org/wiki/Developer_FAQ#:~:text=or%20MSVC%20tracepoints.-,What%20debugging%20features%20are%20available%3F,-Compile%2Dtime, I did configure and build with options:
(I used Meson build system, but it could be reproduced by Autoconf/Make)

```
$ meson setup -Dcassert=true -Ddebug=true -Dc_args=-Og ../builder
$ cd ../builder
$ ninja
```

My gcc version is 4.8.5, and ninja is 1.10.2.

# Problem

I got warnings while compiling. I found that these were reported when -Og was specified.
All of raised said that the variable might be used without initialization.

```
[122/1910] Compiling C object src/backend/postgres_lib.a.p/libpq_be-fsstubs.c.o
../postgres/src/backend/libpq/be-fsstubs.c: In function ‘be_lo_export’:
../postgres/src/backend/libpq/be-fsstubs.c:537:24: warning: ‘fd’ may be used uninitialized in this function [-Wmaybe-uninitialized]
if (CloseTransientFile(fd) != 0)
^
[390/1910] Compiling C object src/backend/postgres_lib.a.p/commands_trigger.c.o
In file included from ../postgres/src/backend/commands/trigger.c:14:0:
../postgres/src/backend/commands/trigger.c: In function ‘ExecCallTriggerFunc’:
../postgres/src/include/postgres.h:314:2: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
return (Pointer) X;
^
../postgres/src/backend/commands/trigger.c:2316:9: note: ‘result’ was declared here
Datum result;
^
[1023/1910] Compiling C object src/backend/postgres_lib.a.p/utils_adt_xml.c.o
../postgres/src/backend/utils/adt/xml.c: In function ‘xml_pstrdup_and_free’:
../postgres/src/backend/utils/adt/xml.c:1359:2: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
return result;
^
[1600/1910] Compiling C object contrib/pg_stat_statements/pg_stat_statements.so.p/pg_stat_statements.c.o
../postgres/contrib/pg_stat_statements/pg_stat_statements.c: In function ‘pgss_planner’:
../postgres/contrib/pg_stat_statements/pg_stat_statements.c:960:2: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
return result;
^
[1651/1910] Compiling C object contrib/postgres_fdw/postgres_fdw.so.p/postgres_fdw.c.o
../postgres/contrib/postgres_fdw/postgres_fdw.c: In function ‘postgresAcquireSampleRowsFunc’:
../postgres/contrib/postgres_fdw/postgres_fdw.c:5346:14: warning: ‘reltuples’ may be used uninitialized in this function [-Wmaybe-uninitialized]
*totalrows = reltuples;
^
[1910/1910] Linking target src/interfaces/ecpg/test/thread/alloc
```

# Solution

PSA the patch to keep the compiler quiet. IIUC my compiler considered that
substitutions in PG_TRY() might be skipped. I'm not sure it is real problem,
but the workarounds are harmless.

Or, did I miss something for ignoring above?

[1]: https://wiki.postgresql.org/wiki/Developer_FAQ#:~:text=or%20MSVC%20tracepoints.-,What%20debugging%20features%20are%20available%3F,-Compile%2Dtime

Best Regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

keep_quiet.patchapplication/octet-stream; name=keep_quiet.patchDownload
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 55b957d251..e181a535eb 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -873,7 +873,7 @@ pgss_planner(Query *parse,
 			 int cursorOptions,
 			 ParamListInfo boundParams)
 {
-	PlannedStmt *result;
+	PlannedStmt *result = NULL;
 
 	/*
 	 * We can't process the query if no query_string is provided, as
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index c5cada55fb..660362dcc7 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -5064,7 +5064,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
 	int			server_version_num;
 	PgFdwSamplingMethod method = ANALYZE_SAMPLE_AUTO;	/* auto is default */
 	double		sample_frac = -1.0;
-	double		reltuples;
+	double		reltuples = 0.0;
 	unsigned int cursor_number;
 	StringInfoData sql;
 	PGresult   *volatile res = NULL;
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 52177759ab..be66b82d43 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -2313,7 +2313,7 @@ ExecCallTriggerFunc(TriggerData *trigdata,
 {
 	LOCAL_FCINFO(fcinfo, 0);
 	PgStat_FunctionCallUsage fcusage;
-	Datum		result;
+	Datum		result = PointerGetDatum(0);	/* keep compiler quiet */
 	MemoryContext oldContext;
 
 	/*
diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c
index d189044a4f..6090197c84 100644
--- a/src/backend/libpq/be-fsstubs.c
+++ b/src/backend/libpq/be-fsstubs.c
@@ -482,7 +482,7 @@ be_lo_export(PG_FUNCTION_ARGS)
 {
 	Oid			lobjId = PG_GETARG_OID(0);
 	text	   *filename = PG_GETARG_TEXT_PP(1);
-	int			fd;
+	int			fd = -1;
 	int			nbytes,
 				tmp;
 	char		buf[BUFSIZE];
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 866d0d649a..4ae78d3a88 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -1339,7 +1339,7 @@ pg_xmlCharStrndup(const char *str, size_t len)
 static char *
 xml_pstrdup_and_free(xmlChar *str)
 {
-	char	   *result;
+	char	   *result = NULL;
 
 	if (str)
 	{
@@ -1353,8 +1353,6 @@ xml_pstrdup_and_free(xmlChar *str)
 		}
 		PG_END_TRY();
 	}
-	else
-		result = NULL;
 
 	return result;
 }
#2Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#1)
Re: Fix compilation warnings when CFLAGS -Og is specified

At Tue, 1 Aug 2023 04:51:55 +0000, "Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com> wrote in

Dear hackers,

# Background

Based on [1], I did configure and build with options:
(I used Meson build system, but it could be reproduced by Autoconf/Make)

```
$ meson setup -Dcassert=true -Ddebug=true -Dc_args=-Og ../builder
$ cd ../builder
$ ninja
```

My gcc version is 4.8.5, and ninja is 1.10.2.

gcc 4.8 looks very old?

AFAIS all of those complaints are false positives and if I did this
correclty, gcc 11.3 seems to have been fixed in this regard.

# Solution

PSA the patch to keep the compiler quiet. IIUC my compiler considered that
substitutions in PG_TRY() might be skipped. I'm not sure it is real problem,
but the workarounds are harmless.

Or, did I miss something for ignoring above?

[1]: https://wiki.postgresql.org/wiki/Developer_FAQ#:~:text=or%20MSVC%20tracepoints.-,What%20debugging%20features%20are%20available%3F,-Compile%2Dtime

I think we don't want "fix" those as far as modern compilers don't
emit the false positives.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#3Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Kyotaro Horiguchi (#2)
RE: Fix compilation warnings when CFLAGS -Og is specified

Dear Horiguchi-san,

Thanks for replying!

My gcc version is 4.8.5, and ninja is 1.10.2.

gcc 4.8 looks very old?

AFAIS all of those complaints are false positives and if I did this
correclty, gcc 11.3 seems to have been fixed in this regard.

I switched to newer gcc (8.3, still old...) and tried. As you said it did not raise
such warnings.

# Solution

PSA the patch to keep the compiler quiet. IIUC my compiler considered that
substitutions in PG_TRY() might be skipped. I'm not sure it is real problem,
but the workarounds are harmless.

Or, did I miss something for ignoring above?

[1]:

https://wiki.postgresql.org/wiki/Developer_FAQ#:~:text=or%20MSVC%20trace
points.-,What%20debugging%20features%20are%20available%3F,-Compile%2Dti
me

I think we don't want "fix" those as far as modern compilers don't
emit the false positives.

OK, I should use newer one for modern codes. Sorry for noise and thank you for confirmation.

Best Regards,
Hayato Kuroda
FUJITSU LIMITED