From c22d38baefc5a7a1e1f5cdc9dbb556b1f0ec5c57 Mon Sep 17 00:00:00 2001
From: Nick Clifton <nickc@redhat.com>
Date: Thu, 30 Mar 2023 10:10:09 +0100
Subject: [PATCH] Fix an illegal memory access when an accessing a
 zer0-lengthverdef table.

  PR 30285
  * elf.c (_bfd_elf_slurp_version_tables): Fail if no version definitions are allocated.
---
 bfd/ChangeLog | 6 ++++++
 bfd/elf.c     | 5 +++++
 2 files changed, 11 insertions(+)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index cbe2a01148d..1bd87531bf1 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,9 @@
+2023-03-30  Nick Clifton  <nickc@redhat.com>
+
+	PR 30285
+	* elf.c (_bfd_elf_slurp_version_tables): Fail if no version
+	definitions are allocated.
+
 2022-08-05  Nick Clifton  <nickc@redhat.com>
 
 	2.39 Release.
diff --git a/bfd/elf.c b/bfd/elf.c
index 89484ceb233..e3c2e20599f 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -9033,6 +9033,9 @@ _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
 	  bfd_set_error (bfd_error_file_too_big);
 	  goto error_return_verdef;
 	}
+
+      if (amt == 0)
+	goto error_return_verdef;
       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
       if (elf_tdata (abfd)->verdef == NULL)
 	goto error_return_verdef;
@@ -9136,6 +9139,8 @@ _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
 	  bfd_set_error (bfd_error_file_too_big);
 	  goto error_return;
 	}
+      if (amt == 0)
+	goto error_return;
       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
       if (elf_tdata (abfd)->verdef == NULL)
 	goto error_return;
-- 
2.25.1