#vulnerable code 
                                public void appendEntries(Peer peer) {
        long startLogIndex = raftLog.getStartLogIndex();
        if (peer.getNextIndex() < startLogIndex) {
            this.installSnapshot(peer);
            return;
        }

        long lastLogIndex = this.raftLog.getLastLogIndex();
        long prevLogIndex = peer.getNextIndex() - 1;
        long prevLogTerm = 0;
        if (prevLogIndex >= startLogIndex) {
            prevLogTerm = raftLog.getEntry(prevLogIndex).getTerm();
        } else if (prevLogIndex == 0) {
            prevLogTerm = 0;
        } else if (prevLogIndex == lastSnapshotIndex) {
            prevLogTerm = lastSnapshotTerm;
        } else {
            installSnapshot(peer);
            return;
        }

        Raft.AppendEntriesRequest.Builder requestBuilder = Raft.AppendEntriesRequest.newBuilder();
        requestBuilder.setServerId(localServer.getServerId());
        requestBuilder.setTerm(currentTerm);
        requestBuilder.setPrevLogTerm(prevLogTerm);
        requestBuilder.setPrevLogIndex(prevLogIndex);
        long numEntries = packEntries(peer.getNextIndex(), requestBuilder);
        requestBuilder.setCommitIndex(Math.min(commitIndex, prevLogIndex + numEntries));
        Raft.AppendEntriesRequest request = requestBuilder.build();

        Raft.AppendEntriesResponse response = peer.getRaftConsensusService().appendEntries(request);
        if (response == null) {
            LOG.warn("appendEntries with peer[{}:{}] failed",
                    peer.getServerAddress().getHost(),
                    peer.getServerAddress().getPort());
            return;
        }
        if (response.getTerm() > currentTerm) {
            LOG.info("Received AppendEntries response from server {} " +
                    "in term {} (this server's term was {})",
                    peer.getServerAddress().getServerId(),
                    response.getTerm(), currentTerm);
            stepDown(response.getTerm());
        } else {
            if (response.getSuccess()) {
                peer.setMatchIndex(prevLogIndex + numEntries);
                advanceCommitIndex();
                peer.setNextIndex(peer.getMatchIndex() + 1);
            } else {
                if (peer.getNextIndex() > 1) {
                    peer.setNextIndex(peer.getNextIndex() - 1);
                }
                if (response.getLastLogIndex() != 0
                        && peer.getNextIndex() > response.getLastLogIndex() + 1) {
                    peer.setNextIndex(response.getLastLogIndex() + 1);
                }
            }
        }
    } 
                            #location 16 
                            #vulnerability type THREAD_SAFETY_VIOLATION
#fixed code
                                public void appendEntries(Peer peer) {
        long firstLogIndex = raftLog.getFirstLogIndex();
        if (peer.getNextIndex() < firstLogIndex) {
            installSnapshot(peer);
            return;
        }

        long prevLogIndex = peer.getNextIndex() - 1;
        long prevLogTerm = 0;
        if (prevLogIndex >= firstLogIndex) {
            prevLogTerm = raftLog.getEntry(prevLogIndex).getTerm();
        } else if (prevLogIndex == 0) {
            prevLogTerm = 0;
        } else if (prevLogIndex == snapshot.getMetaData().getLastIncludedIndex()) {
            prevLogTerm = snapshot.getMetaData().getLastIncludedTerm();
        } else {
            installSnapshot(peer);
            return;
        }

        Raft.AppendEntriesRequest.Builder requestBuilder = Raft.AppendEntriesRequest.newBuilder();
        requestBuilder.setServerId(localServer.getServerId());
        requestBuilder.setTerm(currentTerm);
        requestBuilder.setPrevLogTerm(prevLogTerm);
        requestBuilder.setPrevLogIndex(prevLogIndex);
        long numEntries = packEntries(peer.getNextIndex(), requestBuilder);
        requestBuilder.setCommitIndex(Math.min(commitIndex, prevLogIndex + numEntries));
        Raft.AppendEntriesRequest request = requestBuilder.build();

        Raft.AppendEntriesResponse response = peer.getRaftConsensusService().appendEntries(request);
        if (response == null) {
            LOG.warn("appendEntries with peer[{}:{}] failed",
                    peer.getServerAddress().getHost(),
                    peer.getServerAddress().getPort());
            return;
        }
        if (response.getTerm() > currentTerm) {
            LOG.info("Received AppendEntries response from server {} " +
                    "in term {} (this server's term was {})",
                    peer.getServerAddress().getServerId(),
                    response.getTerm(), currentTerm);
            stepDown(response.getTerm());
        } else {
            if (response.getSuccess()) {
                peer.setMatchIndex(prevLogIndex + numEntries);
                advanceCommitIndex();
                peer.setNextIndex(peer.getMatchIndex() + 1);
            } else {
                if (peer.getNextIndex() > 1) {
                    peer.setNextIndex(peer.getNextIndex() - 1);
                }
                if (response.getLastLogIndex() != 0
                        && peer.getNextIndex() > response.getLastLogIndex() + 1) {
                    peer.setNextIndex(response.getLastLogIndex() + 1);
                }
            }
        }
    }