Skip to content

Instantly share code, notes, and snippets.

@vathpela
Last active August 11, 2021 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vathpela/f1341a75e1aaa69a171674077a562c0e to your computer and use it in GitHub Desktop.
Save vathpela/f1341a75e1aaa69a171674077a562c0e to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char *p;
errno = 0;
p = malloc(SIZE_MAX);
printf("malloc(SIZE_MAX):%p %m\n", p);
return 0;
}
random:~/devel/local/test$ echo '#include <stdint.h>' | gcc -dM -E - | grep -E 'SIZE_MAX|PTRDIFF_MAX'
#define __SIZE_MAX__ 0xffffffffffffffffUL
#define __PTRDIFF_MAX__ 0x7fffffffffffffffL
#define SIZE_MAX (18446744073709551615UL)
#define PTRDIFF_MAX (9223372036854775807L)
random:~/devel/local/test$ make sizemax-malloc
gcc -Og -g3 -flto -fPIC -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -std=gnu2x -Wall -Wextra -Wformat=2 -Wno-unused-variable -Wno-missing-field-initializers -Werror -o sizemax-malloc sizemax-malloc.c
sizemax-malloc.c: In function ‘main’:
sizemax-malloc.c:12:13: error: argument 1 value ‘18446744073709551615’ exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
12 | p = malloc(SIZE_MAX);
| ^
/usr/include/stdlib.h:539:14: note: in a call to allocation function ‘malloc’ declared here
539 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
| ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: /usr/bin/gcc returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make: *** [Makefile:24: sizemax-malloc] Error 1
random:~/devel/local/test$
random:~/devel/local/test$ make zero-malloc
gcc -Og -g3 -flto -fPIC -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -std=gnu2x -Wall -Wextra -Wformat=2 -Wno-unused-variable -Wno-missing-field-initializers -Werror -o zero-malloc zero-malloc.c
random:~/devel/local/test$ ./zero-malloc
malloc(0):0x23682a0
random:~/devel/local/test$
#include <stdlib.h>
#include <stdio.h>
int
main(void)
{
char *p = malloc(0);
printf("malloc(0):%p\n", p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment