1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

malloc-gnu-tests, etc.: test ptrdiff_t overflow

* modules/calloc-gnu-tests (Depends-on):
* modules/malloc-gnu-tests (Depends-on):
* modules/realloc-gnu-tests (Depends-on): Add stdint.
* tests/test-calloc-gnu.c (main):
* tests/test-malloc-gnu.c (main):,
* tests/test-realloc-gnu.c (main): Test for ptrdiff_t overflow.
This commit is contained in:
Paul Eggert
2021-04-18 15:29:54 -07:00
parent b28520a806
commit abe94812b3
7 changed files with 44 additions and 2 deletions

View File

@@ -1,5 +1,13 @@
2021-04-18 Paul Eggert <eggert@cs.ucla.edu> 2021-04-18 Paul Eggert <eggert@cs.ucla.edu>
malloc-gnu-tests, etc.: test ptrdiff_t overflow
* modules/calloc-gnu-tests (Depends-on):
* modules/malloc-gnu-tests (Depends-on):
* modules/realloc-gnu-tests (Depends-on): Add stdint.
* tests/test-calloc-gnu.c (main):
* tests/test-malloc-gnu.c (main):,
* tests/test-realloc-gnu.c (main): Test for ptrdiff_t overflow.
malloc-gnu, etc.: prefer AS_CASE to woolly AS_IF malloc-gnu, etc.: prefer AS_CASE to woolly AS_IF
* m4/calloc.m4 (_AC_FUNC_CALLOC_IF): * m4/calloc.m4 (_AC_FUNC_CALLOC_IF):
* m4/malloc.m4 (_AC_FUNC_MALLOC_IF): * m4/malloc.m4 (_AC_FUNC_MALLOC_IF):

View File

@@ -2,6 +2,7 @@ Files:
tests/test-calloc-gnu.c tests/test-calloc-gnu.c
Depends-on: Depends-on:
stdint
configure.ac: configure.ac:

View File

@@ -2,6 +2,7 @@ Files:
tests/test-malloc-gnu.c tests/test-malloc-gnu.c
Depends-on: Depends-on:
stdint
configure.ac: configure.ac:

View File

@@ -2,6 +2,7 @@ Files:
tests/test-realloc-gnu.c tests/test-realloc-gnu.c
Depends-on: Depends-on:
stdint
configure.ac: configure.ac:

View File

@@ -17,6 +17,7 @@
#include <config.h> #include <config.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
/* Return 8. /* Return 8.
Usual compilers are not able to infer something about the return value. */ Usual compilers are not able to infer something about the return value. */
@@ -49,7 +50,7 @@ main ()
'volatile' is needed to defeat an incorrect optimization by clang 10, 'volatile' is needed to defeat an incorrect optimization by clang 10,
see <https://bugs.llvm.org/show_bug.cgi?id=46055>. */ see <https://bugs.llvm.org/show_bug.cgi?id=46055>. */
{ {
void * volatile p = calloc ((size_t) -1 / 8 + 1, eight ()); void * volatile p = calloc (SIZE_MAX / 8 + 1, eight ());
if (p != NULL) if (p != NULL)
{ {
free (p); free (p);
@@ -57,5 +58,16 @@ main ()
} }
} }
/* Likewise for PTRDIFF_MAX. */
if (PTRDIFF_MAX / 8 < SIZE_MAX)
{
void * volatile p = calloc (PTRDIFF_MAX / 8 + 1, eight ());
if (p != NULL)
{
free (p);
return 2;
}
}
return 0; return 0;
} }

View File

@@ -17,6 +17,7 @@
#include <config.h> #include <config.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
int int
main () main ()
@@ -25,7 +26,15 @@ main ()
char *p = malloc (0); char *p = malloc (0);
if (p == NULL) if (p == NULL)
return 1; return 1;
free (p); free (p);
/* Check that malloc (n) fails when n exceeds PTRDIFF_MAX. */
if (PTRDIFF_MAX < SIZE_MAX)
{
size_t n = PTRDIFF_MAX, n1 = n + 1;
if (malloc (n1) != NULL)
return 1;
}
return 0; return 0;
} }

View File

@@ -17,6 +17,7 @@
#include <config.h> #include <config.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
int int
main () main ()
@@ -26,6 +27,15 @@ main ()
if (p == NULL) if (p == NULL)
return 1; return 1;
/* Check that realloc (p, n) fails when p is non-null and n exceeds
PTRDIFF_MAX. */
if (PTRDIFF_MAX < SIZE_MAX)
{
size_t n = PTRDIFF_MAX, n1 = n + 1;
if (realloc (p, n1) != NULL)
return 1;
}
free (p); free (p);
return 0; return 0;
} }