home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / testerr.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  5KB  |  189 lines

  1. /*
  2.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  3.  *
  4.  * This software may be distributed freely as long as the following conditions
  5.  * are met:
  6.  *         * the distribution, or any derivative thereof, may not be
  7.  *          included as part of a commercial product
  8.  *        * full source code is provided including this copyright
  9.  *        * there is no charge for the software itself (there may be
  10.  *          a minimal charge for the copying or distribution effort)
  11.  *        * this copyright notice is not modified or removed from any
  12.  *          source file
  13.  */
  14. #include "sysdefs.h"
  15. #include <stdio.h>
  16. #if ANSI_HEADERS
  17. #include <stdlib.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include "malloc.h"
  21.  
  22. #define ALLOCSIZE    20
  23. #define BIG_ALIGN    (4*1024)
  24. #define SMALL_ALIGN    (1*1024)
  25.  
  26. /*ARGSUSED*/
  27. int
  28. main(argc,argv)
  29.     int              argc;
  30.     char            **argv[];
  31. {
  32.  
  33.     union dbmalloptarg      m;
  34.     unsigned long          oldsize;
  35.     char            * s;
  36.     unsigned long          size;
  37.     char            * t;
  38.     char            * u;
  39.  
  40.  
  41.     /*
  42.      * make sure we have both chain checking and fill area enabled
  43.      */
  44.     m.i = 1;
  45.     dbmallopt(MALLOC_CKCHAIN,&m);
  46.     m.i = 3;
  47.     dbmallopt(MALLOC_FILLAREA,&m);
  48.  
  49.     /*
  50.      * test leak detection software
  51.      */
  52.     fprintf(stderr,"-------------------------------------\n");
  53.     fprintf(stderr,"Testing malloc_inuse()...");
  54.     oldsize = malloc_inuse( (unsigned long *)0);
  55.     s = malloc(ALLOCSIZE);
  56.     size = malloc_inuse( (unsigned long *)0);
  57.     if( size != (oldsize + ALLOCSIZE))
  58.     {
  59.         fprintf(stderr,"ERROR\n");
  60.         fprintf(stderr,"\toldsize = %lu, size = %lu - should be %lu\n",
  61.             oldsize, size, oldsize+ALLOCSIZE);
  62.     }
  63.     else
  64.     {
  65.         fprintf(stderr,"OK\n");
  66.     }
  67.     
  68.     fprintf(stderr,"-------------------------------------\n");
  69.     fprintf(stderr,"Testing malloc_mark()...");
  70.     malloc_mark(s);
  71.     size = malloc_inuse( (unsigned long *) 0);
  72.     if( size != oldsize )
  73.     {
  74.         fprintf(stderr,"ERROR\n");
  75.         fprintf(stderr,"\tsize = %lu, should be %lu\n",size,oldsize);
  76.     }
  77.     else
  78.     {
  79.         fprintf(stderr,"OK\n");
  80.     }
  81.  
  82.     /*
  83.      * test new malloc_size function
  84.      */
  85.     fprintf(stderr,"-------------------------------------\n");
  86.     fprintf(stderr,"Testing malloc_size()...");
  87.     size = malloc_size(s);
  88.     if( size != ALLOCSIZE )
  89.     {
  90.         fprintf(stderr,"ERROR\n");
  91.         fprintf(stderr,"\tsize = %lu, should be %d\n",size,ALLOCSIZE);
  92.     }
  93.     else
  94.     {
  95.         fprintf(stderr,"OK\n");
  96.     }
  97.             
  98.         
  99.     /*
  100.      * test memalign
  101.      */
  102.     fprintf(stderr,"-------------------------------------\n");
  103.     fprintf(stderr,"Testing memalign()...");
  104.     s = memalign((SIZETYPE)BIG_ALIGN,(SIZETYPE)10);
  105.     t = memalign((SIZETYPE)BIG_ALIGN,(SIZETYPE)20);
  106.     if( (s == NULL) || ((((SIZETYPE)s)&(BIG_ALIGN-1)) != 0)
  107.             || ((((SIZETYPE)t)&(SMALL_ALIGN-1)) != 0) )
  108.     {
  109.         fprintf(stderr,"ERROR\n");
  110.         fprintf(stderr,"\ts = 0x%lx(align=%d), t = 0x%lx(align=%d)\n",
  111.             s, BIG_ALIGN, t, SMALL_ALIGN);
  112.     }
  113.     else
  114.     {
  115.         fprintf(stderr,"OK\n");
  116.     }
  117.     
  118.     s = memalign((SIZETYPE)4096,(SIZETYPE)10);
  119.     
  120.  
  121.     t = malloc(20);
  122.  
  123.     s = malloc(10);
  124.  
  125.     fprintf(stderr,"-------------------------------------\n");
  126.     fprintf(stderr,"Error from strcpy() - out of bounds\n");
  127.     fflush(stderr);
  128.     strncpy(s," ",11);
  129.  
  130.     fprintf(stderr,"-------------------------------------\n");
  131.     fprintf(stderr,"Error from memset() - out of bounds (beyond)\n");
  132.     fflush(stderr);
  133.     memset(t,' ',21);
  134.  
  135.     fprintf(stderr,"-------------------------------------\n");
  136.     fprintf(stderr,"Error from free() - overrun\n");
  137.     fflush(stderr);
  138.     free(t);
  139.  
  140.     fprintf(stderr,"-------------------------------------\n");
  141.     fprintf(stderr,"Error from free() - double free\n");
  142.     fflush(stderr);
  143.     free(t);
  144.  
  145.     fprintf(stderr,"-------------------------------------\n");
  146.     fprintf(stderr,"NO error from bzero\n");
  147.     fflush(stderr);
  148.     bzero(s,10);
  149.  
  150.     fprintf(stderr,"-------------------------------------\n");
  151.     fprintf(stderr,"Error from bzero() - out of bounds\n");
  152.     fflush(stderr);
  153.     bzero(s,11);
  154.  
  155.     fprintf(stderr,"-------------------------------------\n");
  156.     fprintf(stderr,"Error from free() - overrun\n");
  157.     fflush(stderr);
  158.     free(s);
  159.  
  160.     u = malloc(1);
  161.  
  162.     fprintf(stderr,"-------------------------------------\n");
  163.     fprintf(stderr,"Error from memset() - out of bounds (before)\n");
  164.     fflush(stderr);
  165.     memset(u-2,' ',3);
  166.  
  167.     fprintf(stderr,"-------------------------------------\n");
  168.     fprintf(stderr,"Error from free() - underrun\n");
  169.     fflush(stderr);
  170.     free(u);
  171.  
  172.     s = malloc(10);
  173.     t = malloc(500);     /* do this to make sure memset doesnt core */
  174.  
  175.     fprintf(stderr,"-------------------------------------\n");
  176.     fprintf(stderr,"Error from memset() - out of bounds\n");
  177.     fflush(stderr);
  178.     memset(s,'1',100);
  179.  
  180.     
  181.     fprintf(stderr,"-------------------------------------\n");
  182.     fprintf(stderr,"Error from malloc() - chain broken\n");
  183.     fflush(stderr);
  184.     t = malloc(10);
  185.     
  186.     return(0);
  187. }
  188.  
  189.