From ced8ab2f95695aee05424e9aa206cd59a3f90888 Mon Sep 17 00:00:00 2001
From: "Alexander V. Lukyanov" <lavv17f@gmail.com>
Date: Mon, 5 Jul 2021 19:59:33 +0300
Subject: [PATCH] fix a null deref on exit

Conflict: NA
Reference: https://github.com/lavv17/lftp/commit/ced8ab2f95695aee05424e9aa206cd59a3f90888

---
 src/Job.cc    | 7 +++++--
 src/SMTask.cc | 1 +
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/Job.cc b/src/Job.cc
index 63be3fb5..b783eae4 100644
--- a/src/Job.cc
+++ b/src/Job.cc
@@ -365,12 +365,15 @@ xstring& Job::FormatJobs(xstring& s,int verbose,int indent)
 
 void  Job::BuryDoneJobs()
 {
-   xlist_for_each_safe(Job,all_jobs,node,scan,next)
+   xarray<Job*> to_bury;
+   xlist_for_each(Job,all_jobs,node,scan)
    {
       if((scan->parent==this || scan->parent==0) && scan->jobno>=0
 		  && scan->Done())
-	 scan->DeleteLater();
+	 to_bury.append(scan);
    }
+   for(int i=0; i<to_bury.count(); i++)
+      to_bury[i]->DeleteLater();
    CollectGarbage();
 }
 
diff --git a/src/SMTask.cc b/src/SMTask.cc
index b8a84e2f..435ab8c3 100644
--- a/src/SMTask.cc
+++ b/src/SMTask.cc
@@ -129,6 +129,7 @@ void SMTask::DeleteLater()
 {
    if(deleting)
       return;
+   DEBUG(("DeleteLater(%p) from %p\n",this,current));
    deleting=true;
    deleted_tasks.add_tail(deleted_tasks_node);
    PrepareToDie();
