[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 6/8] safe-alloc: simplify via reallocarray
From: |
Paul Eggert |
Subject: |
[PATCH 6/8] safe-alloc: simplify via reallocarray |
Date: |
Sun, 18 Apr 2021 21:01:57 -0700 |
* lib/safe-alloc.c: Do not include xalloc-oversized.h.
(safe_alloc_alloc_n, safe_alloc_realloc_n):
Use reallocarray to check for size or ptrdiff_t overflow.
* modules/reallocarray (License): Switch from LGPL to LGPLv2+, as
this is needed for safe-alloc and anyway is more appropriate for
this library function common with BSD.
* modules/safe-alloc (Depends-on): Depend on reallocarray
rather than xalloc-oversized.
---
ChangeLog | 10 ++++++++++
lib/safe-alloc.c | 17 ++---------------
modules/reallocarray | 2 +-
modules/safe-alloc | 2 +-
4 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 1d2607a0b..76d714127 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2021-04-18 Paul Eggert <eggert@cs.ucla.edu>
+ safe-alloc: simplify via reallocarray
+ * lib/safe-alloc.c: Do not include xalloc-oversized.h.
+ (safe_alloc_alloc_n, safe_alloc_realloc_n):
+ Use reallocarray to check for size or ptrdiff_t overflow.
+ * modules/reallocarray (License): Switch from LGPL to LGPLv2+, as
+ this is needed for safe-alloc and anyway is more appropriate for
+ this library function common with BSD.
+ * modules/safe-alloc (Depends-on): Depend on reallocarray
+ rather than xalloc-oversized.
+
xalloc-oversized: fix SIZE_MAX optimization bug
* lib/xalloc-oversized.h (xalloc_count_t): Remove; no longer
needed and was evidently error-prone anyway.
diff --git a/lib/safe-alloc.c b/lib/safe-alloc.c
index 45e770015..116ac4371 100644
--- a/lib/safe-alloc.c
+++ b/lib/safe-alloc.c
@@ -22,8 +22,6 @@
/* Specification. */
#include "safe-alloc.h"
-#include "xalloc-oversized.h"
-
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
@@ -51,16 +49,10 @@ safe_alloc_alloc_n (void *ptrptr, size_t size, size_t
count, int zeroed)
return 0;
}
- if (xalloc_oversized (count, size))
- {
- errno = ENOMEM;
- return -1;
- }
-
if (zeroed)
*(void **) ptrptr = calloc (count, size);
else
- *(void **) ptrptr = malloc (count * size);
+ *(void **) ptrptr = reallocarray (NULL, count, size);
if (*(void **) ptrptr == NULL)
return -1;
@@ -91,12 +83,7 @@ safe_alloc_realloc_n (void *ptrptr, size_t size, size_t
count)
*(void **) ptrptr = NULL;
return 0;
}
- if (xalloc_oversized (count, size))
- {
- errno = ENOMEM;
- return -1;
- }
- tmp = realloc (*(void **) ptrptr, size * count);
+ tmp = reallocarray (*(void **) ptrptr, size, count);
if (!tmp)
return -1;
*(void **) ptrptr = tmp;
diff --git a/modules/reallocarray b/modules/reallocarray
index 11d32bb5f..a64c6fd2d 100644
--- a/modules/reallocarray
+++ b/modules/reallocarray
@@ -26,7 +26,7 @@ Include:
<stdlib.h>
License:
-LGPL
+LGPLv2+
Maintainer:
all
diff --git a/modules/safe-alloc b/modules/safe-alloc
index 13e78dec7..9453b49ee 100644
--- a/modules/safe-alloc
+++ b/modules/safe-alloc
@@ -7,7 +7,7 @@ lib/safe-alloc.c
m4/safe-alloc.m4
Depends-on:
-xalloc-oversized
+reallocarray
configure.ac:
gl_SAFE_ALLOC
--
2.27.0
- [PATCH 1/8] safe-alloc: improve doc, Paul Eggert, 2021/04/19
- [PATCH 2/8] backupfile: simplify via realloc-gnu, Paul Eggert, 2021/04/19
- [PATCH 3/8] group-member: simplify via realloc-gnu, Paul Eggert, 2021/04/19
- [PATCH 4/8] xalloc: new function xreallocarray, Paul Eggert, 2021/04/19
- [PATCH 6/8] safe-alloc: simplify via reallocarray,
Paul Eggert <=
- [PATCH 7/8] calloc-gnu: now LGPLv2+, Paul Eggert, 2021/04/19
- [PATCH 8/8] safe-alloc: fix pointer implementation, Paul Eggert, 2021/04/19
- [PATCH 5/8] xalloc-oversized: fix SIZE_MAX optimization bug, Paul Eggert, 2021/04/19
- Re: [PATCH 1/8] safe-alloc: improve doc, Eric Blake, 2021/04/22