File: toobig-allocs.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
file content (27 lines) | stat: -rw-r--r-- 833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdlib.h>
#include <sys/mman.h>
#include <stdio.h>
 
int main(void)
{
   void *p;

   // This is the biggest word-sized signed number.  We use a signed number,
   // even though malloc takes an unsigned SizeT, because the "silly malloc
   // arg" checking done by memcheck treats the arg like a signed int in
   // order to detect the passing of a silly size arg like -1.
   unsigned long size = (~(0UL)) >> 1;
 
   fprintf(stderr, "Attempting too-big malloc()...\n");
   p = malloc(size);          // way too big!
   if (p)
      fprintf(stderr, "huge malloc() succeeded??\n");

   fprintf(stderr, "Attempting too-big mmap()...\n");
   p = mmap( 0, size, PROT_READ|PROT_WRITE|PROT_EXEC,
             MAP_PRIVATE|MAP_ANON, -1, 0 );
   if (-1 != (long)p)
      fprintf(stderr, "huge mmap() succeeded??\n");

   return 0;
}