]> granicus.if.org Git - strace/commitdiff
unwind: use xgrowarray
authorEugene Syromiatnikov <esyr@redhat.com>
Tue, 8 Aug 2017 14:37:01 +0000 (16:37 +0200)
committerDmitry V. Levin <ldv@altlinux.org>
Thu, 14 Dec 2017 00:05:04 +0000 (00:05 +0000)
* unwind.c (build_mmap_cache): Use xgrowarray instead of manual dynamic
array management.

unwind.c

index 19853d1625c5622b2f1a00b8a628aab309fc76de..18f06254654cf41f62f1863796da31949ddd1e9b 100644 (file)
--- a/unwind.c
+++ b/unwind.c
@@ -139,9 +139,8 @@ static void
 build_mmap_cache(struct tcb *tcp)
 {
        FILE *fp;
-       struct mmap_cache_t *cache_head;
-       /* start with a small dynamically-allocated array and then expand it */
-       size_t cur_array_size = 10;
+       struct mmap_cache_t *cache_head = NULL;
+       size_t cur_array_size = 0;
        char filename[sizeof("/proc/4294967296/maps")];
        char buffer[PATH_MAX + 80];
 
@@ -154,8 +153,6 @@ build_mmap_cache(struct tcb *tcp)
                return;
        }
 
-       cache_head = xcalloc(cur_array_size, sizeof(*cache_head));
-
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
                struct mmap_cache_t *entry;
                unsigned long start_addr, end_addr, mmap_offset;
@@ -199,11 +196,9 @@ build_mmap_cache(struct tcb *tcp)
                        }
                }
 
-               if (tcp->mmap_cache_size >= cur_array_size) {
-                       cur_array_size *= 2;
-                       cache_head = xreallocarray(cache_head, cur_array_size,
-                                                  sizeof(*cache_head));
-               }
+               if (tcp->mmap_cache_size >= cur_array_size)
+                       cache_head = xgrowarray(cache_head, &cur_array_size,
+                                               sizeof(*cache_head));
 
                entry = &cache_head[tcp->mmap_cache_size];
                entry->start_addr = start_addr;