Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill an unreachable statement (thanks sonar)
[simgrid.git] / src / mc / compare.cpp
index 5d60d76..a477e83 100644 (file)
@@ -1,15 +1,15 @@
-/* Copyright (c) 2008-2016. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-/** \file mc_compare.cpp Memory snapshooting and comparison                 */
+/** \file compare.cpp Memory snapshooting and comparison                    */
 
 #include <cinttypes>
 
 #include <array>
 #include <memory>
+#include <set>
 #include <utility>
 #include <unordered_set>
 
@@ -23,7 +23,6 @@
 #include "src/internal_config.h"
 
 #include "src/xbt/mmalloc/mmprivate.h"
-#include "src/xbt/ex_interface.h"
 
 #if HAVE_SMPI
 #include "src/smpi/private.h"
@@ -48,6 +47,10 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
 namespace simgrid {
 namespace mc {
 
+struct HeapLocation;
+typedef std::array<HeapLocation, 2> HeapLocationPair;
+typedef std::set<HeapLocationPair> HeapLocationPairs;
+struct HeapArea;
 struct ProcessComparisonState;
 struct StateComparator;
 
@@ -55,7 +58,7 @@ static int compare_heap_area(
   StateComparator& state,
   int process_index, const void *area1, const void* area2,
   Snapshot* snapshot1, Snapshot* snapshot2,
-  xbt_dynar_t previous, Type* type, int pointer_level);
+  HeapLocationPairs* previous, Type* type, int pointer_level);
 
 }
 }
@@ -71,11 +74,29 @@ namespace mc {
 struct HeapLocation {
   int block = 0;
   int fragment = 0;
+
   HeapLocation() {}
   HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
+
+  bool operator==(HeapLocation const& that) const
+  {
+    return block == that.block && fragment == that.fragment;
+  }
+  bool operator<(HeapLocation const& that) const
+  {
+    return std::make_pair(block, fragment)
+      < std::make_pair(that.block, that.fragment);
+  }
 };
 
-typedef std::array<HeapLocation, 2> HeapLocationPair;
+static inline
+HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
+{
+  return simgrid::mc::HeapLocationPair{{
+    simgrid::mc::HeapLocation(block1, fragment1),
+    simgrid::mc::HeapLocation(block2, fragment2)
+  }};
+}
 
 struct HeapArea : public HeapLocation {
   bool valid = false;
@@ -172,7 +193,6 @@ struct StateComparator {
 
   /** Check whether two blocks are known to be matching
    *
-   *  @param state  State used
    *  @param b1     Block of state 1
    *  @param b2     Block of state 2
    *  @return       if the blocks are known to be matching
@@ -185,7 +205,6 @@ struct StateComparator {
 
   /** Check whether two fragments are known to be matching
    *
-   *  @param state  State used
    *  @param b1     Block of state 1
    *  @param f1     Fragment of state 1
    *  @param b2     Block of state 2
@@ -200,7 +219,7 @@ struct StateComparator {
         && this->equals_to2_(b2, f2).fragment == f1;
   }
 
-  void match_equals(xbt_dynar_t list);
+  void match_equals(HeapLocationPairs* list);
 };
 
 }
@@ -208,35 +227,6 @@ struct StateComparator {
 
 /************************************************************************************/
 
-static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
-                                 int block2, int fragment2)
-{
-
-  unsigned int cursor = 0;
-  simgrid::mc::HeapLocationPair* current_pair;
-  xbt_dynar_foreach(list, cursor, current_pair)
-    if ((*current_pair)[0].block == block1
-        && (*current_pair)[1].block == block2
-        && (*current_pair)[0].fragment == fragment1
-        && (*current_pair)[1].fragment == fragment2)
-      return 0;
-  return 1;
-}
-
-static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
-                              int block2, int fragment2)
-{
-  if (!is_new_heap_area_pair(list, block1, fragment1, block2, fragment2))
-    return 0;
-  simgrid::mc::HeapLocationPair* pair = xbt_new0(simgrid::mc::HeapLocationPair, 1);
-  (*pair)[0].block = block1;
-  (*pair)[0].fragment = fragment1;
-  (*pair)[1].block = block2;
-  (*pair)[1].fragment = fragment2;
-  xbt_dynar_push(list, &pair);
-  return 1;
-}
-
 static ssize_t heap_comparison_ignore_size(
   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
   const void *address)
@@ -278,22 +268,19 @@ static bool is_block_stack(int block)
 namespace simgrid {
 namespace mc {
 
-void StateComparator::match_equals(xbt_dynar_t list)
+void StateComparator::match_equals(HeapLocationPairs* list)
 {
-  unsigned int cursor = 0;
-  simgrid::mc::HeapLocationPair* current_pair;
-
-  xbt_dynar_foreach(list, cursor, current_pair) {
-    if ((*current_pair)[0].fragment != -1) {
-      this->equals_to1_((*current_pair)[0].block, (*current_pair)[0].fragment) =
-          simgrid::mc::HeapArea((*current_pair)[1].block, (*current_pair)[1].fragment);
-      this->equals_to2_((*current_pair)[1].block, (*current_pair)[1].fragment) =
-          simgrid::mc::HeapArea((*current_pair)[0].block, (*current_pair)[0].fragment);
+  for (auto const& pair : *list) {
+    if (pair[0].fragment != -1) {
+      this->equals_to1_(pair[0].block, pair[0].fragment) =
+          simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
+      this->equals_to2_(pair[1].block, pair[1].fragment) =
+          simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
     } else {
-      this->equals_to1_((*current_pair)[0].block, 0) =
-          simgrid::mc::HeapArea((*current_pair)[1].block, (*current_pair)[1].fragment);
-      this->equals_to2_((*current_pair)[1].block, 0) =
-          simgrid::mc::HeapArea((*current_pair)[0].block, (*current_pair)[0].fragment);
+      this->equals_to1_(pair[0].block, 0) =
+          simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
+      this->equals_to2_(pair[1].block, 0) =
+          simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
     }
   }
 }
@@ -301,8 +288,8 @@ void StateComparator::match_equals(xbt_dynar_t list)
 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
 {
-  auto heaplimit = ((struct mdesc *) heap)->heaplimit;
-  this->heapsize = ((struct mdesc *) heap)->heapsize;
+  auto heaplimit  = heap->heaplimit;
+  this->heapsize  = heap->heapsize;
   this->to_ignore = i;
   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
@@ -312,13 +299,9 @@ int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
 {
-  if ((((struct mdesc *) heap1)->heaplimit !=
-       ((struct mdesc *) heap2)->heaplimit)
-      ||
-      ((((struct mdesc *) heap1)->heapsize !=
-        ((struct mdesc *) heap2)->heapsize)))
+  if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
     return -1;
-  this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
+  this->heaplimit     = heap1->heaplimit;
   this->std_heap_copy = *mc_model_checker->process().get_heap();
   this->processStates[0].initHeapInformation(heap1, i1);
   this->processStates[1].initHeapInformation(heap2, i2);
@@ -349,7 +332,6 @@ int mmalloc_compare_heap(
   int equal, res_compare = 0;
 
   /* Check busy blocks */
-
   i1 = 1;
 
   malloc_info heapinfo_temp1, heapinfo_temp2;
@@ -460,7 +442,6 @@ int mmalloc_compare_heap(
         }
 
         i2++;
-
       }
 
       if (!equal) {
@@ -522,7 +503,7 @@ int mmalloc_compare_heap(
           }
 
           if (heapinfo2b->type < 0) {
-            fprintf(stderr, "Unkown mmalloc block type.\n");
+            fprintf(stderr, "Unknown mmalloc block type.\n");
             abort();
           }
 
@@ -549,11 +530,9 @@ int mmalloc_compare_heap(
               equal = 1;
               break;
             }
-
           }
 
           i2++;
-
         }
 
         if (!equal) {
@@ -566,13 +545,10 @@ int mmalloc_compare_heap(
           nb_diff1++;
           break;
         }
-
       }
 
       i1++;
-
     }
-
   }
 
   /* All blocks/fragments are equal to another block/fragment ? */
@@ -604,7 +580,7 @@ int mmalloc_compare_heap(
   }
 
   if (i1 == state.heaplimit)
-    XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
+    XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
 
   for (i=1; i < state.heaplimit; i++) {
     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
@@ -633,7 +609,7 @@ int mmalloc_compare_heap(
   }
 
   if (i1 == state.heaplimit)
-    XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
+    XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
 
   return nb_diff1 > 0 || nb_diff2 > 0;
 }
@@ -654,7 +630,7 @@ static int compare_heap_area_without_type(
   const void *real_area1, const void *real_area2,
   simgrid::mc::Snapshot* snapshot1,
   simgrid::mc::Snapshot* snapshot2,
-  xbt_dynar_t previous, int size,
+  HeapLocationPairs* previous, int size,
   int check_ignore)
 {
   simgrid::mc::Process* process = &mc_model_checker->process();
@@ -727,7 +703,7 @@ static int compare_heap_area_without_type(
  * @param snapshot1      Snapshot of state 1
  * @param snapshot2      Snapshot of state 2
  * @param previous
- * @param type_id
+ * @param type
  * @param area_size      either a byte_size or an elements_count (?)
  * @param check_ignore
  * @param pointer_level
@@ -738,7 +714,7 @@ static int compare_heap_area_with_type(
   const void *real_area1, const void *real_area2,
   simgrid::mc::Snapshot* snapshot1,
   simgrid::mc::Snapshot* snapshot2,
-  xbt_dynar_t previous, simgrid::mc::Type* type,
+  HeapLocationPairs* previous, simgrid::mc::Type* type,
   int area_size, int check_ignore,
   int pointer_level)
 {
@@ -931,7 +907,6 @@ top:
     return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
                                           snapshot1, snapshot2, previous,
                                           type->byte_size, check_ignore);
-    return 0;
 
   default:
     return 0;
@@ -946,7 +921,7 @@ top:
  *
  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
  *
- * @param  type_id            DWARF type ID of the root address
+ * @param  type               DWARF type ID of the root address
  * @param  area_size
  * @return                    DWARF type ID for given offset
  */
@@ -987,7 +962,7 @@ static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::
     return nullptr;
 
   default:
-    /* FIXME : other cases ? */
+    /* FIXME: other cases ? */
     return nullptr;
 
   }
@@ -1009,7 +984,7 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
                       const void *area1, const void *area2,
                       simgrid::mc::Snapshot* snapshot1,
                       simgrid::mc::Snapshot* snapshot2,
-                      xbt_dynar_t previous,
+                      HeapLocationPairs* previous,
                       simgrid::mc::Type* type, int pointer_level)
 {
   simgrid::mc::Process* process = &mc_model_checker->process();
@@ -1025,7 +1000,7 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
   int new_size1 = -1, new_size2 = -1;
   simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
 
-  int match_pairs = 0;
+  bool match_pairs = false;
 
   // This is the address of std_heap->heapinfo in the application process:
   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
@@ -1037,10 +1012,10 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
 
   malloc_info heapinfo_temp1, heapinfo_temp2;
 
+  simgrid::mc::HeapLocationPairs current;
   if (previous == nullptr) {
-    previous = xbt_dynar_new(sizeof(simgrid::mc::HeapLocationPair*), [](void *d) {
-      xbt_free((simgrid::mc::HeapLocationPair*) * (void **) d); });
-    match_pairs = 1;
+    previous = &current;
+    match_pairs = true;
   }
 
   // Get block number:
@@ -1053,11 +1028,10 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
 
   // If either block is a stack block:
   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
-    add_heap_area_pair(previous, block1, -1, block2, -1);
-    if (match_pairs) {
+    previous->insert(simgrid::mc::makeHeapLocationPair(
+      block1, -1, block2, -1));
+    if (match_pairs)
       state.match_equals(previous);
-      xbt_dynar_free(&previous);
-    }
     return 0;
   }
 
@@ -1066,8 +1040,6 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
       || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
       || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
       || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
-    if (match_pairs)
-      xbt_dynar_free(&previous);
     return 1;
   }
 
@@ -1107,10 +1079,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
     /* Free block */
-    if (match_pairs) {
+    if (match_pairs)
       state.match_equals(previous);
-      xbt_dynar_free(&previous);
-    }
     return 0;
   }
 
@@ -1126,10 +1096,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
     if (state.equals_to1_(block1, 0).valid
         && state.equals_to2_(block2, 0).valid
         && state.blocksEqual(block1, block2)) {
-      if (match_pairs) {
+      if (match_pairs)
         state.match_equals(previous);
-        xbt_dynar_free(&previous);
-      }
       return 0;
     }
 
@@ -1137,31 +1105,21 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
       if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
           && type_size != (ssize_t)   heapinfo2->busy_block.busy_size
           && (type->name.empty() || type->name == "struct s_smx_context")) {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
       }
     }
 
-    if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
-      if (match_pairs)
-        xbt_dynar_free(&previous);
+    if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
       return 1;
-    }
-
-    if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
-      if (match_pairs)
-        xbt_dynar_free(&previous);
+    if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
       return 1;
-    }
 
-    if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
-      if (match_pairs) {
+    if (!previous->insert(simgrid::mc::makeHeapLocationPair(
+        block1, -1, block2, -1)).second) {
+      if (match_pairs)
         state.match_equals(previous);
-        xbt_dynar_free(&previous);
-      }
       return 0;
     }
 
@@ -1175,10 +1133,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
       state.types2_(block2, 0) = type;
 
     if (size <= 0) {
-      if (match_pairs) {
+      if (match_pairs)
         state.match_equals(previous);
-        xbt_dynar_free(&previous);
-      }
       return 0;
     }
 
@@ -1209,19 +1165,15 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
     if (type_size != -1) {
       if (heapinfo1->busy_frag.frag_size[frag1] == -1
           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
       }
       // ?
       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
       }
     }
@@ -1230,10 +1182,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
     if (state.equals_to1_(block1, frag1).valid
         && state.equals_to2_(block2, frag2).valid) {
       if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return 0;
       }
     }
@@ -1241,16 +1191,11 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
     if (heapinfo1->busy_frag.frag_size[frag1] !=
         heapinfo2->busy_frag.frag_size[frag2]) {
       if (type_size == -1) {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
-      } else {
-        if (match_pairs)
-          xbt_dynar_free(&previous);
+      } else
         return 1;
-      }
     }
 
     // Size of the fragment:
@@ -1298,10 +1243,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
             get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
                             offset2, size, snapshot2, process_index);
       } else {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
       }
 
@@ -1318,10 +1261,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
         new_size2 = type->byte_size;
 
       } else {
-        if (match_pairs) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return -1;
       }
     }
@@ -1332,19 +1273,16 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
     }
 
     if (offset1 == 0 && offset2 == 0
-      && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
-        if (match_pairs) {
+      && !previous->insert(simgrid::mc::makeHeapLocationPair(
+        block1, frag1, block2, frag2)).second) {
+        if (match_pairs)
           state.match_equals(previous);
-          xbt_dynar_free(&previous);
-        }
         return 0;
       }
 
     if (size <= 0) {
-      if (match_pairs) {
+      if (match_pairs)
         state.match_equals(previous);
-        xbt_dynar_free(&previous);
-      }
       return 0;
     }
 
@@ -1353,11 +1291,8 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
             heapinfo1->busy_frag.ignore[frag1]))
       check_ignore = heapinfo1->busy_frag.ignore[frag1];
 
-  } else {
-    if (match_pairs)
-      xbt_dynar_free(&previous);
+  } else
     return 1;
-  }
 
 
   /* Start comparison */
@@ -1371,17 +1306,11 @@ int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
                                        previous, size, check_ignore);
 
-  if (res_compare == 1) {
-    if (match_pairs)
-      xbt_dynar_free(&previous);
+  if (res_compare == 1)
     return res_compare;
-  }
 
-  if (match_pairs) {
+  if (match_pairs)
     state.match_equals(previous);
-    xbt_dynar_free(&previous);
-  }
-
   return 0;
 }
 
@@ -1541,7 +1470,7 @@ static int compare_areas_with_type(simgrid::mc::StateComparator& state,
     return -1;
     break;
   default:
-    XBT_VERB("Unknown case : %d", type->type);
+    XBT_VERB("Unknown case: %d", type->type);
     break;
   }
 
@@ -1569,12 +1498,12 @@ static int compare_global_variables(
 
     // Compare the global variables separately for each simulates process:
     for (size_t process_index = 0; process_index < process_count; process_index++) {
-      int is_diff = compare_global_variables(state,
-        object_info, process_index,
-        &r1->privatized_data()[process_index],
-        &r2->privatized_data()[process_index],
-        snapshot1, snapshot2);
-      if (is_diff) return 1;
+      if (compare_global_variables(state,
+          object_info, process_index,
+          &r1->privatized_data()[process_index],
+          &r2->privatized_data()[process_index],
+          snapshot1, snapshot2))
+        return 1;
     }
     return 0;
   }
@@ -1686,51 +1615,45 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
   simgrid::mc::Process* process = &mc_model_checker->process();
 
   int errors = 0;
-  int res_init;
 
   int hash_result = 0;
   if (_sg_mc_hash) {
     hash_result = (s1->hash != s2->hash);
     if (hash_result) {
-      XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
-               num2, s1->hash, s2->hash);
+      XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
 #ifndef MC_DEBUG
       return 1;
 #endif
     } else
-      XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
+      XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
   }
 
   /* Compare enabled processes */
   if (s1->enabled_processes != s2->enabled_processes) {
-      XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
-      // return 1; ??
+    XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
+    return 1;
   }
 
-  unsigned long i = 0;
-  size_t size_used1, size_used2;
-  int is_diff = 0;
-
   /* Compare size of stacks */
-  while (i < s1->stacks.size()) {
-    size_used1 = s1->stack_sizes[i];
-    size_used2 = s2->stack_sizes[i];
+  int is_diff = 0;
+  for (unsigned long i = 0; i < s1->stacks.size(); i++) {
+    size_t size_used1 = s1->stack_sizes[i];
+    size_t size_used2 = s2->stack_sizes[i];
     if (size_used1 != size_used2) {
 #ifdef MC_DEBUG
-      XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
-                num2, size_used1, size_used2);
+      XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
       errors++;
       is_diff = 1;
 #else
 #ifdef MC_VERBOSE
-      XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
-               num2, size_used1, size_used2);
+      XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
 #endif
       return 1;
 #endif
     }
-    i++;
   }
+  if (is_diff) // do not proceed if there is any stacks that don't match
+    return 1;
 
   /* Init heap information used in heap comparison algorithm */
   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
@@ -1741,8 +1664,7 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
     remote(process->heap_address),
     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
-  res_init = state_comparator->initHeapInformation(
-    heap1, heap2, &s1->to_ignore, &s2->to_ignore);
+  int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
 
   if (res_init == -1) {
 #ifdef MC_DEBUG
@@ -1758,15 +1680,10 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
   }
 
   /* Stacks comparison */
-  unsigned cursor = 0;
   int diff_local = 0;
-#ifdef MC_DEBUG
-  is_diff = 0;
-#endif
-  mc_snapshot_stack_t stack1, stack2;
-  while (cursor < s1->stacks.size()) {
-    stack1 = &s1->stacks[cursor];
-    stack2 = &s2->stacks[cursor];
+  for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
+    mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
+    mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
 
     if (stack1->process_index != stack2->process_index) {
       diff_local = 1;
@@ -1791,7 +1708,6 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
       return 1;
 #endif
     }
-    cursor++;
   }
 
   size_t regions_count = s1->snapshot_regions.size();
@@ -1813,12 +1729,9 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
     std::string const& name = region1->object_info()->file_name;
 
     /* Compare global variables */
-    is_diff =
-      compare_global_variables(*state_comparator,
-        region1->object_info(), simgrid::mc::ProcessIndexDisabled,
-        region1, region2, s1, s2);
+    if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
+                                 region2, s1, s2)) {
 
-    if (is_diff != 0) {
 #ifdef MC_DEBUG
       XBT_DEBUG("(%d - %d) Different global variables in %s",
         num1, num2, name.c_str());
@@ -1845,7 +1758,6 @@ int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc:
 #ifdef MC_VERBOSE
     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
 #endif
-
     return 1;
 #endif
   }