[v1,14/14] refactoring elf_find_and_remove_property

Message ID 20250227104811.178094-15-matthieu.longo@arm.com
State New
Headers
Series Several clean-ups and refactorings before a long patch series on AArch64 build attributes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Matthieu Longo Feb. 27, 2025, 10:47 a.m. UTC
  This refactoring focuses primarily on code readability and reuse.
- Use the already defined _bfd_elf_find_property instead of another
  raw for-loop.
- Extract _bfd_elf_remove_property out of the function body.
---
 bfd/elf-properties.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
  

Patch

diff --git a/bfd/elf-properties.c b/bfd/elf-properties.c
index 0dea9d53c45..07ab38098e6 100644
--- a/bfd/elf-properties.c
+++ b/bfd/elf-properties.c
@@ -70,6 +70,27 @@  _bfd_elf_insert_property (elf_property_list *l,
   return l;
 }
 
+/* Remove a property from the list after prev.  */
+static elf_property_list *
+_bfd_elf_remove_property (elf_property_list *l,
+			  elf_property_list *what,
+			  elf_property_list *prev)
+{
+  if (l == NULL)
+    return l;
+
+  if (prev == NULL) // Pop front
+    {
+      BFD_ASSERT (what == l);
+      l = what->next;
+    }
+  else
+    prev->next = what->next;
+
+  what->next = NULL;
+  return l;
+}
+
 /* Get a property, allocate a new one if needed.  */
 
 elf_property *
@@ -395,23 +416,16 @@  static elf_property *
 elf_find_and_remove_property (elf_property_list **listp,
 			      unsigned int type, bool rm)
 {
-  elf_property_list *list;
+  elf_property_list *prev;
+  elf_property_list *p = _bfd_elf_find_property (*listp, type, &prev);
+  if (p == NULL)
+    return NULL;
 
-  for (list = *listp; list; list = list->next)
-    {
-      if (type == list->property.pr_type)
-	{
-	  /* Remove this property.  */
-	  if (rm)
-	    *listp = list->next;
-	  return &list->property;
-	}
-      else if (type < list->property.pr_type)
-	break;
-      listp = &list->next;
-    }
+  if (rm)
+    *listp = _bfd_elf_remove_property (*listp, p, prev);
 
-  return NULL;
+  /* FIXME: we leak memory with this approach.  */
+  return &p->property;
 }
 
 /* Merge GNU property list *LISTP in ABFD with FIRST_PBFD.  */