Browse Source

fix(proclist::find()): check iter null

greatbridf 2 years ago
parent
commit
9483e98eca
3 changed files with 11 additions and 7 deletions
  1. 7 4
      include/kernel/process.hpp
  2. 2 0
      src/kernel/process.cpp
  3. 2 3
      src/kernel/syscall.cpp

+ 7 - 4
include/kernel/process.hpp

@@ -459,8 +459,8 @@ public:
     constexpr process* find(pid_t pid)
     {
         auto iter = m_procs.find(pid);
-        // TODO: change this
-        assert(!!iter);
+        if (!iter)
+            return nullptr;
         return &iter->value;
     }
 
@@ -476,8 +476,11 @@ public:
         if (children) {
             auto init_children = m_child_idx.find(1);
             for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
-                init_children->value.push_back(*iter);
-                this->find(*iter)->ppid = 1;
+                auto* proc = this->find(*iter);
+                if (proc) {
+                    init_children->value.push_back(*iter);
+                    proc->ppid = 1;
+                }
             }
             m_child_idx.remove(children);
         }

+ 2 - 0
src/kernel/process.cpp

@@ -152,6 +152,7 @@ process::process(pid_t _ppid,
 void proclist::kill(pid_t pid, int exit_code)
 {
     process* proc = this->find(pid);
+    assert(proc);
 
     // remove threads from ready list
     for (auto& thd : proc->thds.underlying_list()) {
@@ -179,6 +180,7 @@ void proclist::kill(pid_t pid, int exit_code)
     // notify parent process and init
     auto* parent = this->find(proc->ppid);
     auto* init = this->find(1);
+    assert(parent && init);
 
     bool flag = false;
     {

+ 2 - 3
src/kernel/syscall.cpp

@@ -380,9 +380,8 @@ int _syscall_setpgid(interrupt_stack* data)
         pgid = pid;
 
     auto* proc = procs->find(pid);
-    // TODO: check whether the process exists
-    // if (!proc)
-    //     return -ESRCH;
+    if (!proc)
+        return -ESRCH;
 
     // TODO: check whether pgid and the original
     //       pgid is in the same session