diff --git a/elf/dl-debug.c b/elf/dl-debug.c
index 2cd5f09753..85b087455e 100644
--- a/elf/dl-debug.c
+++ b/elf/dl-debug.c
@@ -16,6 +16,8 @@
License along with the GNU C Library; if not, see
. */
+#include
+#include
#include
@@ -49,7 +51,11 @@ _dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns)
if (ns == LM_ID_BASE)
r = &_r_debug;
else
- r = &GL(dl_ns)[ns]._ns_debug;
+ {
+ assert (ns >= 0);
+ assert (ns < array_length (GL (dl_ns)));
+ r = &GL(dl_ns)[ns]._ns_debug;
+ }
if (r->r_map == NULL || ldbase != 0)
{