home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / PTRERR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  4.2 KB  |  200 lines

  1. /*
  2.  * PtrErr.cpp
  3.  * $Header: /BoundsChecker/Examples/BUGBNCHX/MAINERR/PTRERR.CPP 11    2/13/97 6:17p Stevea $
  4.  *
  5.  * Description:
  6.  *  Pointer Check error functions.
  7.  *
  8.  * Notes:
  9.  *  <implementation notes go here>
  10.  *
  11.  ***********************************************************************
  12.  *
  13.  * Nu-Mega Technologies, Inc.
  14.  * P.O. Box 7780
  15.  * Nashua, NH 03060
  16.  *
  17.  * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
  18.  * ALL RIGHTS RESERVED.
  19.  *
  20.  ***********************************************************************
  21.  *
  22.  **********************************************************************/
  23. #include "stdafx.h"
  24. #include "PtrErr.h"
  25. #include "BCError.h"
  26.  
  27. // Pointer Check - Array parameter is dangling pointer.
  28. TCHAR TakeDanglPtrParam ( TCHAR a[ 10 ] )
  29. {
  30.    return ( a[ 0 ] ) ;
  31. }
  32. // Pointer Check - Array parameter is null.
  33. TCHAR TakeNULLPtrParam ( TCHAR a[ 10 ] )
  34. {
  35.    return ( a[ 0 ] ) ;
  36. }
  37. // Pointer Check - Array parameter is uninitialized pointer.
  38. TCHAR TakeUninitPtrParam ( TCHAR a[ 10 ] )
  39. {
  40.    return ( a[ 0 ] ) ;
  41. }
  42. // Pointer Check - Expression uses unrelated pointers.
  43. void Pointer_ExprUsesUnrelPtrs ( )
  44. {
  45.    _TRY
  46.    {
  47.       char   a[ 10 ] ;
  48.       char * b       ;
  49.  
  50.       b = (char *)malloc( 10 ) ;
  51.       // The following line is the error because a and b do not
  52.       //  point to the same memory block.
  53.       if ( a > b )
  54.          a[0] = 'x' ;
  55.       else
  56.          a[0] = 'y' ;
  57.       free ( b ) ;
  58.    }
  59.    _CATCH
  60.    {
  61.    }
  62. }
  63. // Pointer Check - Expression subtracts unrelated pointers.
  64. void Pointer_ExprSubsUnrelPtrs ( )
  65. {
  66.    _TRY
  67.    {
  68.       char   a[ 10 ] ;
  69.       char * b       ;
  70.       int    d       ;
  71.  
  72.       b = (char *)malloc( 10 ) ;
  73.       d = b - a ;
  74.       free ( b ) ;
  75.    }
  76.    _CATCH
  77.    {
  78.    }
  79. }
  80. // Pointer Check - Expression uses dangling pointer.
  81. void Pointer_ExprUsesDanglPtr ( )
  82. {
  83.    _TRY
  84.    {
  85.       char *a = (char *)malloc( 10 ) ;
  86.       char b[ 10 ] ;
  87.  
  88.       free ( a ) ;
  89.       if ( a > b )
  90.          a = b ;
  91.    }
  92.    _CATCH
  93.    {
  94.    }
  95. }
  96. // Pointer Check - Function pointer is not a function.
  97. union {
  98.    int * iptr     ;
  99.    int (*fptr)( ) ;
  100. } uMakeTrouble ;
  101. void Pointer_FuncPtrIsNotAFn ( )
  102. {
  103.    _TRY
  104.    {
  105.       // Setting i to 0xC3 provides a known behavior for a this function 
  106.       // pointer that isn't a function. The call to uMakeTrouble.fptr ( )
  107.       // simply returns instead of having undefined results.
  108.       const int RETURN_FROM_FUNCTION_ID = 0xC3;
  109.       int i = RETURN_FROM_FUNCTION_ID;
  110.       uMakeTrouble.iptr = &i ;
  111.       uMakeTrouble.fptr ( ) ;
  112.    }
  113.    _CATCH
  114.    {
  115.    }
  116. }
  117. // Pointer Check - Pointer references unlocked memory block
  118. void Pointer_PtrRefsUnlockedBlock ( )
  119. {
  120.    _TRY
  121.    {
  122.       HANDLE hMem    ;
  123.       LPVOID lpPtr   ;
  124.  
  125.       // This memory is allocated as movable.
  126.       hMem = GlobalAlloc ( GHND , 0x100 ) ;
  127.       lpPtr = GlobalLock ( hMem ) ;
  128.       GlobalUnlock ( hMem ) ;
  129.       // Use the unlocked pointer.
  130.       memset ( lpPtr , 0 , 0x100 ) ;
  131.       GlobalFree ( hMem ) ;
  132.    }
  133.    _CATCH
  134.    {
  135.    }
  136. }
  137. // Pointer Check - Unallocated pointer.
  138. void Pointer_UnallocdPtr ( )
  139. {
  140.    _TRY
  141.    {
  142.       HLOCAL hMem = LocalAlloc( LMEM_MOVEABLE, 4 );
  143.       char * pmem = (char *)LocalLock( hMem );
  144.       LocalUnlock( hMem );
  145.       LocalFree( hMem );
  146.  
  147.       // Because we are overwriting a dangling pointer we need to save
  148.       // and restore it to prevent major problems later.
  149.       DWORD  saveBuff;
  150.       saveBuff = *((DWORD *)pmem);
  151.       memset ( pmem , 0 , 4 ) ;
  152.       *((DWORD *)pmem) = saveBuff;
  153.    }
  154.    _CATCH
  155.    {
  156.    }
  157. }
  158.  
  159. // Pointer Check - Assigning pointer out of range
  160. void Pointer_AssignOutOfRange ( )
  161. {
  162.    _TRY
  163.    {
  164.     char buff[10];
  165.     char * pbuff;
  166.     pbuff = (buff + 11);
  167.    }
  168.    _CATCH
  169.    {
  170.    }
  171. }
  172.  
  173. // Pointer Check - Array index out of range
  174. void Pointer_ArrayParamExRange ( )
  175. {
  176.    _TRY
  177.    {
  178.       int a[ 5 ] ;
  179.       int  b;
  180.       b = a[6];
  181.    }
  182.  
  183.    _CATCH
  184.    {
  185.    }
  186. }
  187.  
  188. // Pointer Check - Returning pointer to local variable
  189. //  private helper function
  190. int * retLocalInt( void )
  191. {
  192.    int   *i, j = 11;
  193.    i = &j;
  194.    return i;
  195. }
  196. // Pointer Check - Returning pointer to local variable
  197. void Pointer_ReturnPtrToLocalVar( )
  198. {
  199.    int* pi = retLocalInt();
  200. }