From f53fc42716c042e560a824244fecab215ba036d1 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 22 Feb 2021 11:42:03 -0500 Subject: [PATCH] gdb: add asserts in thread code Unlike the previous patch, I don't propose that we take this patch into gdb-10-branch. This patch adds two asserts, prompted by investigating and fixing the bug fixed by the previous patch. The assert in find_thread_ptid would have caught the original issue before the segfault (I think it's slightly more use friendly). The assert in add_thread_silent would have made it clear that the solution proposed in [1] isn't the right one. The solution ended up passing nullptr as a target to add_thread. We don't want that, because add_thread_silent uses it to look up the inferior to which to add the thread. If the target is nullptr, we could find an inferior with the same pid, but belonging to an unrelated target. So we always want a non-nullptr target in add_thread_silent. gdb/ChangeLog: * thread.c (add_thread_silent): Add assert. (find_thread_ptid): Add assert. [1] https://sourceware.org/pipermail/gdb-patches/2021-February/176202.html Change-Id: Ie593ee45c5eb02235e8e9fbcda612d48ce883852 --- gdb/ChangeLog | 5 +++++ gdb/thread.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index c2c86705e0c..9de0e235dc0 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2021-02-22 Simon Marchi + + * thread.c (add_thread_silent): Add assert. + (find_thread_ptid): Add assert. + 2021-02-22 Simon Marchi PR gdb/27435 diff --git a/gdb/thread.c b/gdb/thread.c index 82107067217..3e7d6e14bf7 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -246,6 +246,8 @@ new_thread (struct inferior *inf, ptid_t ptid) struct thread_info * add_thread_silent (process_stratum_target *targ, ptid_t ptid) { + gdb_assert (targ != nullptr); + inferior *inf = find_inferior_ptid (targ, ptid); /* We may have an old thread with the same id in the thread list. @@ -535,6 +537,8 @@ find_thread_ptid (process_stratum_target *targ, ptid_t ptid) struct thread_info * find_thread_ptid (inferior *inf, ptid_t ptid) { + gdb_assert (inf != nullptr); + for (thread_info *tp : inf->non_exited_threads ()) if (tp->ptid == ptid) return tp; -- 2.30.2