Bladeren bron

Fix two index-out-of-range errors when accessing the log.

Jing Yang 4 jaren geleden
bovenliggende
commit
990edc7eba
2 gewijzigde bestanden met toevoegingen van 6 en 2 verwijderingen
  1. 4 1
      src/install_snapshot.rs
  2. 2 1
      src/lib.rs

+ 4 - 1
src/install_snapshot.rs

@@ -53,7 +53,10 @@ impl<C: Clone + Default + serde::Serialize> Raft<C> {
             && args.last_included_index >= rf.log.start()
             && args.last_included_term == rf.log[args.last_included_index].term
         {
-            rf.log.shift(args.last_included_index, args.data);
+            // Do nothing if the index and term match the current snapshot.
+            if args.last_included_index != rf.log.start() {
+                rf.log.shift(args.last_included_index, args.data);
+            }
         } else {
             rf.log.reset(
                 args.last_included_index,

+ 2 - 1
src/lib.rs

@@ -290,7 +290,8 @@ where
 
         self.election.reset_election_timer();
 
-        if rf.log.end() <= args.prev_log_index
+        if rf.log.start() > args.prev_log_index
+            || rf.log.end() <= args.prev_log_index
             || rf.log[args.prev_log_index].term != args.prev_log_term
         {
             return AppendEntriesReply {