[Libguestfs] [PATCH 2/5] Avoid gcc warnings about infinite loop.

Richard W.M. Jones rjones at redhat.com
Mon Nov 9 23:02:12 UTC 2015


This seemingly redundant change avoids a gcc warning/error:

  error: cannot optimize possibly infinite loops

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34114#c3 and
following for an explanation.

Of course gcc is completely wrong and stupid here, because there's no
possibility on current or future hardware that an array of size
SSIZE_MAX could be allocated since it would by definition occupy at
least all addressible memory (if it was an array of bytes, which this
isn't), leaving no room for the code that is being compiled.
---
 daemon/debug.c | 2 +-
 daemon/lvm.c   | 4 ++--
 daemon/md.c    | 2 +-
 daemon/xattr.c | 2 +-
 src/drives.c   | 2 +-
 5 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/daemon/debug.c b/daemon/debug.c
index a210db1..abc7699 100644
--- a/daemon/debug.c
+++ b/daemon/debug.c
@@ -547,7 +547,7 @@ debug_progress (const char *subcmd, size_t argc, char *const *const argv)
     uint64_t tsecs = secs * 10; /* 1/10ths of seconds */
     uint64_t i;
 
-    for (i = 1; i <= tsecs; ++i) {
+    for (i = 1; i < tsecs+1; ++i) {
       usleep (100000);
       notify_progress (i, tsecs);
     }
diff --git a/daemon/lvm.c b/daemon/lvm.c
index 421c1c5..d989986 100644
--- a/daemon/lvm.c
+++ b/daemon/lvm.c
@@ -215,7 +215,7 @@ do_vgcreate (const char *volgroup, char *const *physvols)
   argv[0] = str_lvm;
   argv[1] = "vgcreate";
   argv[2] = volgroup;
-  for (i = 3; i <= argc; ++i)
+  for (i = 3; i < argc+1; ++i)
     argv[i] = physvols[i-3];
 
   r = commandv (NULL, &err, (const char * const*) argv);
@@ -522,7 +522,7 @@ do_vg_activate (int activate, char *const *volgroups)
   argv[1] = "vgchange";
   argv[2] = "-a";
   argv[3] = activate ? "y" : "n";
-  for (i = 4; i <= argc; ++i)
+  for (i = 4; i < argc+1; ++i)
     argv[i] = volgroups[i-4];
 
   r = commandv (NULL, &err, (const char * const*) argv);
diff --git a/daemon/md.c b/daemon/md.c
index cf826c5..8bb4b54 100644
--- a/daemon/md.c
+++ b/daemon/md.c
@@ -467,7 +467,7 @@ parse_md_stat_line (char *line)
   return ret;
 
  error:
-  for (i = 0; i <= spaces; ++i) {
+  for (i = 0; i < spaces+1; ++i) {
     free (ret->guestfs_int_mdstat_list_val[i].mdstat_device);
     free (ret->guestfs_int_mdstat_list_val[i].mdstat_flags);
   }
diff --git a/daemon/xattr.c b/daemon/xattr.c
index c05124d..b599408 100644
--- a/daemon/xattr.c
+++ b/daemon/xattr.c
@@ -376,7 +376,7 @@ do_internal_lxattrlist (const char *path, char *const *names)
      * entry[1..nr_attrs] are the attributes.
      */
     entry = &ret->guestfs_int_xattr_list_val[ret->guestfs_int_xattr_list_len-nr_attrs-1];
-    for (m = 1; m <= nr_attrs; ++m) {
+    for (m = 1; m < nr_attrs+1; ++m) {
       entry[m].attrname = NULL;
       entry[m].attrval.attrval_len = 0;
       entry[m].attrval.attrval_val = NULL;
diff --git a/src/drives.c b/src/drives.c
index b9cc813..dc88495 100644
--- a/src/drives.c
+++ b/src/drives.c
@@ -524,7 +524,7 @@ add_drive_to_handle_at (guestfs_h *g, struct drive *d, size_t drv_index)
   if (drv_index >= g->nr_drives) {
     g->drives = safe_realloc (g, g->drives,
                               sizeof (struct drive *) * (drv_index + 1));
-    while (g->nr_drives <= drv_index) {
+    while (g->nr_drives < drv_index+1) {
       g->drives[g->nr_drives] = NULL;
       g->nr_drives++;
     }
-- 
2.5.0




More information about the Libguestfs mailing list