6ca6e8
commit 2ff6775ad341b10a08e3b27d6e1df1da637747c7
6ca6e8
Author: Javier Pello <devel@otheo.eu>
6ca6e8
Date:   Mon Sep 5 20:09:01 2022 +0200
6ca6e8
6ca6e8
    elf: Fix hwcaps string size overestimation
6ca6e8
    
6ca6e8
    Commit dad90d528259b669342757c37dedefa8577e2636 added glibc-hwcaps
6ca6e8
    support for LD_LIBRARY_PATH and, for this, it adjusted the total
6ca6e8
    string size required in _dl_important_hwcaps. However, in doing so
6ca6e8
    it inadvertently altered the calculation of the size required for
6ca6e8
    the power set strings, as the computation of the power set string
6ca6e8
    size depended on the first value assigned to the total variable,
6ca6e8
    which is later shifted, resulting in overallocation of string
6ca6e8
    space. Fix this now by using a different variable to hold the
6ca6e8
    string size required for glibc-hwcaps.
6ca6e8
    
6ca6e8
    Signed-off-by: Javier Pello <devel@otheo.eu>
6ca6e8
    (cherry picked from commit a23820f6052a740246fdc7dcd9c43ce8eed0c45a)
6ca6e8
6ca6e8
diff --git a/elf/dl-hwcaps.c b/elf/dl-hwcaps.c
6ca6e8
index e3c611e005ffbc0d..045911eb6d5d315a 100644
6ca6e8
--- a/elf/dl-hwcaps.c
6ca6e8
+++ b/elf/dl-hwcaps.c
6ca6e8
@@ -193,7 +193,7 @@ _dl_important_hwcaps (const char *glibc_hwcaps_prepend,
6ca6e8
   /* Each hwcaps subdirectory has a GLIBC_HWCAPS_PREFIX string prefix
6ca6e8
      and a "/" suffix once stored in the result.  */
6ca6e8
   hwcaps_counts.maximum_length += strlen (GLIBC_HWCAPS_PREFIX) + 1;
6ca6e8
-  size_t total = (hwcaps_counts.count * (strlen (GLIBC_HWCAPS_PREFIX) + 1)
6ca6e8
+  size_t hwcaps_sz = (hwcaps_counts.count * (strlen (GLIBC_HWCAPS_PREFIX) + 1)
6ca6e8
 		  + hwcaps_counts.total_length);
6ca6e8
 
6ca6e8
   /* Count the number of bits set in the masked value.  */
6ca6e8
@@ -229,11 +229,12 @@ _dl_important_hwcaps (const char *glibc_hwcaps_prepend,
6ca6e8
   assert (m == cnt);
6ca6e8
 
6ca6e8
   /* Determine the total size of all strings together.  */
6ca6e8
+  size_t total;
6ca6e8
   if (cnt == 1)
6ca6e8
-    total += temp[0].len + 1;
6ca6e8
+    total = temp[0].len + 1;
6ca6e8
   else
6ca6e8
     {
6ca6e8
-      total += temp[0].len + temp[cnt - 1].len + 2;
6ca6e8
+      total = temp[0].len + temp[cnt - 1].len + 2;
6ca6e8
       if (cnt > 2)
6ca6e8
 	{
6ca6e8
 	  total <<= 1;
6ca6e8
@@ -255,6 +256,7 @@ _dl_important_hwcaps (const char *glibc_hwcaps_prepend,
6ca6e8
   /* This is the overall result, including both glibc-hwcaps
6ca6e8
      subdirectories and the legacy hwcaps subdirectories using the
6ca6e8
      power set construction.  */
6ca6e8
+  total += hwcaps_sz;
6ca6e8
   struct r_strlenpair *overall_result
6ca6e8
     = malloc (*sz * sizeof (*result) + total);
6ca6e8
   if (overall_result == NULL)