]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Silence uninitialized variable warning with some compiler versions
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 17 Jun 2026 06:00:49 +0000 (09:00 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 17 Jun 2026 06:00:49 +0000 (09:00 +0300)
The first "if (difffile)" block initializes the startpos variable, and
the second "if (difffile)" block reads it. The second if-condition can
only be true when the first one was true, so the startpos variable is
always initialized when it's used. However, the compiler might not be
able to deduce that, and warn about startpos being used uninitialized.

To silence the warning, rearrange the if-checks. Also, bail out if the
diff file cannot be opened, instead of ignoring it silently.

Author: Mikhail Litsarev <m.litsarev@postgrespro.ru>
Reviewed-by; Ewan Young <kdbase.hack@gmail.com>
Discussion: https://www.postgresql.org/message-id/ee06f058c626cd37babd8c81579ffb1e@postgrespro.ru

src/test/regress/pg_regress.c

index 1c052cc0fbfaa99ab41d33d07703121d845254e4..b53821dd10cb6e91a01bda0d4c97a3923bb11853 100644 (file)
@@ -1534,31 +1534,33 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
         */
 
        difffile = fopen(difffilename, "a");
-       if (difffile)
-       {
-               startpos = ftell(difffile);
-
-               /* Write diff header */
-               fprintf(difffile,
-                               "diff %s %s %s\n",
-                               pretty_diff_opts, best_expect_file, resultsfile);
-               fclose(difffile);
+       if (!difffile)
+               bail("could not open file \"%s\" for writing: %m", difffilename);
+       startpos = ftell(difffile);
 
-               /* Run diff */
-               snprintf(cmd, sizeof(cmd),
-                                "diff %s \"%s\" \"%s\" >> \"%s\"",
-                                pretty_diff_opts, best_expect_file, resultsfile, difffilename);
-               run_diff(cmd, difffilename);
+       /* Write diff header */
+       fprintf(difffile,
+                       "diff %s %s %s\n",
+                       pretty_diff_opts, best_expect_file, resultsfile);
+       fclose(difffile);
 
-               /*
-                * Reopen the file for reading to emit the diff as TAP diagnostics. We
-                * can't keep the file open while diff appends to it, because on
-                * Windows the file lock prevents diff from writing.
-                */
-               difffile = fopen(difffilename, "r");
-       }
+       /* Run diff */
+       snprintf(cmd, sizeof(cmd),
+                        "diff %s \"%s\" \"%s\" >> \"%s\"",
+                        pretty_diff_opts, best_expect_file, resultsfile, difffilename);
+       run_diff(cmd, difffilename);
 
-       if (difffile)
+       /*
+        * Emit the diff output as TAP diagnostics
+        *
+        * Reopen the file for reading. We can't keep the file open while diff
+        * appends to it, because on Windows the file lock prevents diff from
+        * writing.
+        */
+       difffile = fopen(difffilename, "r");
+       if (!difffile)
+               bail("could not open file \"%s\" for reading: %m", difffilename);
+       else
        {
                /*
                 * In case of a crash the diff can be huge and all of the subsequent