]> git.tuebingen.mpg.de Git - tfortune.git/commitdiff
com_stats(): Work around bogus gcc warning.
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 18 Jun 2019 17:07:24 +0000 (19:07 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 19 Jun 2019 09:32:51 +0000 (11:32 +0200)
For some versions of gcc (7.4.0, 8.3.0, and 8.3.0-7 as shipped with
Debian-11, but not gcc-7.4.0-1ubuntu1~18.04.1), the previous patch
introduced the following warning:

tfortune.c:912:3: warning: 'lh_stats' may be used uninitialized in this function [-Wmaybe-uninitialized]
   free(lh_stats);
   ^~~~~~~~~~~~~~

We only assign to lh_stats when --verbose is given, and only free the
memory if --verbose is given.  Apparently gcc has started to believe
that the value of the "verbose" boolean variable might change, which is
impossible. Therefore, the warning is believed to be a false positive.

However, gcc has a point: There is no need to check for --verbose
twice, as a single branch is sufficient. This not only gets rid of
the warning but also makes the boolean variable pointless and removes
more lines than it adds.

tfortune.c

index ec4c323f7c29040b0bbb7d1e883f303c6559951c..724b8badcec20ae1e4c4a5399b2c80ec388f7c37 100644 (file)
@@ -863,9 +863,8 @@ static int com_stats(void)
        struct linhash_item *itemp;
        unsigned num_epi_files, num_epis, num_unique_tags, num_x = 0;
        long unsigned num_tags = 0;
-       char *xdir, *lh_stats;
+       char *xdir;
        struct regfile_iter *riter;
-       bool verbose = OPT_GIVEN(STATS, VERBOSE);
 
        tagtab = hash_tags(&num_epi_files, &num_epis);
        for (
@@ -876,10 +875,6 @@ static int com_stats(void)
                num_tags += (long unsigned)itemp->object;
        num_unique_tags = linhash_num_items(tagtab);
        linhash_iterator_free(liter);
-       if (verbose)
-               lh_stats = linhash_statistics(tagtab);
-       linhash_free(tagtab);
-
        xdir = get_xdir();
        for (
                regfile_iter_new(xdir, &riter);
@@ -900,10 +895,12 @@ static int com_stats(void)
                (float)num_tags / num_epis : 0);
        printf("average number of tag recurrence... %8.02f\n", num_unique_tags > 0?
                (float)num_tags / num_unique_tags : 0);
-       if (verbose) {
+       if (OPT_GIVEN(STATS, VERBOSE)) {
+               char *lh_stats = linhash_statistics(tagtab);
                printf("\nlinear hashing statistics:\n%s\n", lh_stats);
                free(lh_stats);
        }
+       linhash_free(tagtab);
        return 1;
 }
 EXPORT_CMD_HANDLER(stats);