From 64bd1f9d3188cf93b4bac7f071981748ff394986 Mon Sep 17 00:00:00 2001 From: pfg Date: Thu, 4 May 2017 15:00:09 +0000 Subject: [PATCH] MFC r317200, r317201, r317216: libthread_db: unsign map_len and use reallocarray(3). Lengths are not negative, so map_len should be unsigned. Unsign the corresponding indexes too and bring a small use of reallocarray(3). Reorder the memset to be consistent with the reallocarray. --- lib/libthread_db/libpthread_db.c | 17 +++++++++-------- lib/libthread_db/libpthread_db.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/libthread_db/libpthread_db.c b/lib/libthread_db/libpthread_db.c index e190e561168..49dd98f64aa 100644 --- a/lib/libthread_db/libpthread_db.c +++ b/lib/libthread_db/libpthread_db.c @@ -74,7 +74,8 @@ pt_map_thread(const td_thragent_t *const_ta, psaddr_t pt, enum pt_type type) { td_thragent_t *ta = __DECONST(td_thragent_t *, const_ta); struct pt_map *new; - int i, first = -1; + int first = -1; + unsigned int i; /* leave zero out */ for (i = 1; i < ta->map_len; ++i) { @@ -94,12 +95,12 @@ pt_map_thread(const td_thragent_t *const_ta, psaddr_t pt, enum pt_type type) ta->map_len = 20; first = 1; } else { - new = realloc(ta->map, - sizeof(struct pt_map) * ta->map_len * 2); + new = reallocarray(ta->map, ta->map_len, + 2 * sizeof(struct pt_map)); if (new == NULL) return (-1); - memset(new + ta->map_len, '\0', sizeof(struct pt_map) * - ta->map_len); + memset(new + ta->map_len, '\0', ta->map_len * + sizeof(struct pt_map)); first = ta->map_len; ta->map = new; ta->map_len *= 2; @@ -226,7 +227,7 @@ pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th) TDBG_FUNC(); - if (id < 0 || id >= ta->map_len || ta->map[id].type == PT_NONE) + if (id < 0 || id >= (long)ta->map_len || ta->map[id].type == PT_NONE) return (TD_NOTHR); ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt); @@ -1047,7 +1048,7 @@ pt_thr_sstep(const td_thrhandle_t *th, int step) static void pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp) { - int i; + unsigned int i; for (i = 0; i < ta->map_len; ++i) { if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) { @@ -1061,7 +1062,7 @@ static int pt_validate(const td_thrhandle_t *th) { - if (th->th_tid < 0 || th->th_tid >= th->th_ta->map_len || + if (th->th_tid < 0 || th->th_tid >= (long)th->th_ta->map_len || th->th_ta->map[th->th_tid].type == PT_NONE) return (TD_NOTHR); return (TD_OK); diff --git a/lib/libthread_db/libpthread_db.h b/lib/libthread_db/libpthread_db.h index 44c5de6ce93..79f6aa5bcac 100644 --- a/lib/libthread_db/libpthread_db.h +++ b/lib/libthread_db/libpthread_db.h @@ -77,7 +77,7 @@ struct td_thragent { int thread_off_sigmask; int thread_off_sigpend; struct pt_map *map; - int map_len; + unsigned int map_len; }; void pt_md_init(void); -- 2.45.0