]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
readprofile: fix static analyzer warning [coverity scan]
authorKarel Zak <kzak@redhat.com>
Thu, 18 Feb 2021 10:15:48 +0000 (11:15 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 18 Feb 2021 10:24:12 +0000 (11:24 +0100)
Make sure we do not use step=0 and zero "fn_add - add0" as divisor.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/readprofile.c

index 132386437620f2d101a08e6eacae28ee9d9731a2..a89fd180c9b615987ffdfa22df589572bb9db61c 100644 (file)
@@ -147,6 +147,7 @@ int main(int argc, char **argv)
        int maplineno = 1;
        int popenMap;           /* flag to tell if popen() has been used */
        int header_printed;
+       double rep = 0;
 
        static const struct option longopts[] = {
                {"mapfile", required_argument, NULL, 'm'},
@@ -346,7 +347,7 @@ int main(int argc, char **argv)
                        errx(EXIT_FAILURE,
                             _("profile address out of range. Wrong map file?"));
 
-               while (indx < (next_add - add0) / step) {
+               while (step > 0 && indx < (next_add - add0) / step) {
                        if (optBins && (buf[indx] || optAll)) {
                                if (!header_printed) {
                                        printf("%s:\n", fn_name);
@@ -370,7 +371,7 @@ int main(int argc, char **argv)
                        else
                                printf("%6u %-40s %8.4f\n",
                                       this, fn_name, this / (double)fn_len);
-                       if (optSub) {
+                       if (optSub && step > 0) {
                                unsigned long long scan;
 
                                for (scan = (fn_add - add0) / step + 1;
@@ -396,13 +397,16 @@ int main(int argc, char **argv)
        /* clock ticks, out of kernel text - probably modules */
        printf("%6u %s\n", buf[len / sizeof(*buf) - 1], "*unknown*");
 
+       if (fn_add > add0)
+               rep = total / (double)(fn_add - add0);
+
        /* trailer */
        if (optVerbose)
                printf("%016x %-40s %6u %8.4f\n",
-                      0, "total", total, total / (double)(fn_add - add0));
+                      0, "total", total, rep);
        else
                printf("%6u %-40s %8.4f\n",
-                      total, _("total"), total / (double)(fn_add - add0));
+                      total, _("total"), rep);
 
        popenMap ? pclose(map) : fclose(map);
        exit(EXIT_SUCCESS);