#vulnerable code 
                                @Override
    public Raft.AppendEntriesResponse appendEntries(Raft.AppendEntriesRequest request) {
        LOG.info("Receive AppendEntries request from server {} " +
                        "in term {} (my term was {})",
                request.getServerId(), request.getTerm(),
                raftNode.getCurrentTerm());
        raftNode.getLock().lock();
        try {
            Raft.AppendEntriesResponse.Builder responseBuilder = Raft.AppendEntriesResponse.newBuilder();
            responseBuilder.setTerm(raftNode.getCurrentTerm());
            responseBuilder.setSuccess(false);
            responseBuilder.setLastLogIndex(raftNode.getRaftLog().getLastLogIndex());
            if (request.getTerm() < raftNode.getCurrentTerm()) {
                return responseBuilder.build();
            }
            raftNode.stepDown(request.getTerm());
            if (raftNode.getLeaderId() == 0) {
                raftNode.setLeaderId(request.getServerId());
            }

            if (request.getPrevLogIndex() > raftNode.getRaftLog().getLastLogIndex()) {
                LOG.debug("Rejecting AppendEntries RPC: would leave gap");
                return responseBuilder.build();
            }
            if (request.getPrevLogIndex() >= raftNode.getRaftLog().getFirstLogIndex()
                    && raftNode.getRaftLog().getEntryTerm(request.getPrevLogIndex())
                    != request.getPrevLogTerm()) {
                LOG.debug("Rejecting AppendEntries RPC: terms don't agree");
                return responseBuilder.build();
            }

            responseBuilder.setSuccess(true);
            List<Raft.LogEntry> entries = new ArrayList<>();
            long index = request.getPrevLogIndex();
            for (Raft.LogEntry entry : request.getEntriesList()) {
                index++;
                if (index < raftNode.getRaftLog().getFirstLogIndex()) {
                    continue;
                }
                if (raftNode.getRaftLog().getLastLogIndex() >= index) {
                    if (raftNode.getRaftLog().getEntryTerm(index) == entry.getTerm()) {
                        continue;
                    }
                    // truncate segment log from index
                    long lastIndexKept = index - 1;
                    raftNode.getRaftLog().truncateSuffix(lastIndexKept);
                }
                entries.add(entry);
            }
            raftNode.getRaftLog().append(entries);
            responseBuilder.setLastLogIndex(raftNode.getRaftLog().getLastLogIndex());

            if (raftNode.getCommitIndex() < request.getCommitIndex()) {
                raftNode.setCommitIndex(request.getCommitIndex());
                // apply state machine
                for (index = raftNode.getLastAppliedIndex() + 1; index <= raftNode.getCommitIndex(); index++) {
                    raftNode.getStateMachine().apply(
                            raftNode.getRaftLog().getEntry(index).getData().toByteArray());
                }
            }
            return responseBuilder.build();
        } finally {
            raftNode.getLock().unlock();
        }
    } 
                            #location 6 
                            #vulnerability type THREAD_SAFETY_VIOLATION
#fixed code
                                @Override
    public Raft.AppendEntriesResponse appendEntries(Raft.AppendEntriesRequest request) {
        raftNode.getLock().lock();
        try {
            Raft.AppendEntriesResponse.Builder responseBuilder = Raft.AppendEntriesResponse.newBuilder();
            responseBuilder.setTerm(raftNode.getCurrentTerm());
            responseBuilder.setSuccess(false);
            responseBuilder.setLastLogIndex(raftNode.getRaftLog().getLastLogIndex());
            if (request.getTerm() < raftNode.getCurrentTerm()) {
                return responseBuilder.build();
            }
            raftNode.stepDown(request.getTerm());
            if (raftNode.getLeaderId() == 0) {
                raftNode.setLeaderId(request.getServerId());
            }

            if (request.getPrevLogIndex() > raftNode.getRaftLog().getLastLogIndex()) {
                LOG.debug("Rejecting AppendEntries RPC: would leave gap");
                return responseBuilder.build();
            }
            if (request.getPrevLogIndex() >= raftNode.getRaftLog().getFirstLogIndex()
                    && raftNode.getRaftLog().getEntryTerm(request.getPrevLogIndex())
                    != request.getPrevLogTerm()) {
                LOG.debug("Rejecting AppendEntries RPC: terms don't agree");
                return responseBuilder.build();
            }

            responseBuilder.setSuccess(true);
            List<Raft.LogEntry> entries = new ArrayList<>();
            long index = request.getPrevLogIndex();
            for (Raft.LogEntry entry : request.getEntriesList()) {
                index++;
                if (index < raftNode.getRaftLog().getFirstLogIndex()) {
                    continue;
                }
                if (raftNode.getRaftLog().getLastLogIndex() >= index) {
                    if (raftNode.getRaftLog().getEntryTerm(index) == entry.getTerm()) {
                        continue;
                    }
                    // truncate segment log from index
                    long lastIndexKept = index - 1;
                    raftNode.getRaftLog().truncateSuffix(lastIndexKept);
                }
                entries.add(entry);
            }
            raftNode.getRaftLog().append(entries);
            responseBuilder.setLastLogIndex(raftNode.getRaftLog().getLastLogIndex());

            if (raftNode.getCommitIndex() < request.getCommitIndex()) {
                raftNode.setCommitIndex(request.getCommitIndex());
                // apply state machine
                for (index = raftNode.getLastAppliedIndex() + 1; index <= raftNode.getCommitIndex(); index++) {
                    raftNode.getStateMachine().apply(
                            raftNode.getRaftLog().getEntry(index).getData().toByteArray());
                }
            }
            LOG.info("Receive AppendEntries request from server {} " +
                            "in term {} (my term is {}), success={}",
                    request.getServerId(), request.getTerm(),
                    raftNode.getCurrentTerm(), responseBuilder.getSuccess());
            return responseBuilder.build();
        } finally {
            raftNode.getLock().unlock();
        }
    }