* lib/fts.c (fts_sort): Use reallocarray instead of checking for
overflow before calling realloc.
* modules/fts (Depends-on): Add reallocarray.
---
 ChangeLog   | 7 +++++++
 lib/fts.c   | 5 ++---
 modules/fts | 1 +
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5ffd08aba2..b0adb2070f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2026-01-03  Collin Funk  <[email protected]>
+
+       fts: Prefer reallocarray to checking overflow by hand.
+       * lib/fts.c (fts_sort): Use reallocarray instead of checking for
+       overflow before calling realloc.
+       * modules/fts (Depends-on): Add reallocarray.
+
 2026-01-01  Collin Funk  <[email protected]>
 
        fts: Improve performance on the Lustre file system.
diff --git a/lib/fts.c b/lib/fts.c
index 876b65f7fc..290ec1847c 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -1885,9 +1885,8 @@ fts_sort (FTS *sp, FTSENT *head, register size_t nitems)
         if (nitems > sp->fts_nitems) {
                 sp->fts_nitems = nitems + 40;
                 FTSENT **a;
-                if (SIZE_MAX / sizeof *a < sp->fts_nitems
-                    || ! (a = realloc (sp->fts_array,
-                                       sp->fts_nitems * sizeof *a))) {
+                if (! (a = reallocarray (sp->fts_array,
+                                         sp->fts_nitems, sizeof *a))) {
                         free(sp->fts_array);
                         sp->fts_array = NULL;
                         sp->fts_nitems = 0;
diff --git a/modules/fts b/modules/fts
index 70e3c1bdc2..70ed1f18ef 100644
--- a/modules/fts
+++ b/modules/fts
@@ -36,6 +36,7 @@ openat
 openat-h
 opendirat
 readdir
+reallocarray
 realloc-posix
 bool
 stddef-h
-- 
2.52.0


Reply via email to