home *** CD-ROM | disk | FTP | other *** search
- /*
- * PtrErr.cpp
- * $Header: /BoundsChecker/Examples/BUGBNCHX/MAINERR/PTRERR.CPP 11 2/13/97 6:17p Stevea $
- *
- * Description:
- * Pointer Check error functions.
- *
- * Notes:
- * <implementation notes go here>
- *
- ***********************************************************************
- *
- * Nu-Mega Technologies, Inc.
- * P.O. Box 7780
- * Nashua, NH 03060
- *
- * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
- * ALL RIGHTS RESERVED.
- *
- ***********************************************************************
- *
- **********************************************************************/
- #include "stdafx.h"
- #include "PtrErr.h"
- #include "BCError.h"
-
- // Pointer Check - Array parameter is dangling pointer.
- TCHAR TakeDanglPtrParam ( TCHAR a[ 10 ] )
- {
- return ( a[ 0 ] ) ;
- }
- // Pointer Check - Array parameter is null.
- TCHAR TakeNULLPtrParam ( TCHAR a[ 10 ] )
- {
- return ( a[ 0 ] ) ;
- }
- // Pointer Check - Array parameter is uninitialized pointer.
- TCHAR TakeUninitPtrParam ( TCHAR a[ 10 ] )
- {
- return ( a[ 0 ] ) ;
- }
- // Pointer Check - Expression uses unrelated pointers.
- void Pointer_ExprUsesUnrelPtrs ( )
- {
- _TRY
- {
- char a[ 10 ] ;
- char * b ;
-
- b = (char *)malloc( 10 ) ;
- // The following line is the error because a and b do not
- // point to the same memory block.
- if ( a > b )
- a[0] = 'x' ;
- else
- a[0] = 'y' ;
- free ( b ) ;
- }
- _CATCH
- {
- }
- }
- // Pointer Check - Expression subtracts unrelated pointers.
- void Pointer_ExprSubsUnrelPtrs ( )
- {
- _TRY
- {
- char a[ 10 ] ;
- char * b ;
- int d ;
-
- b = (char *)malloc( 10 ) ;
- d = b - a ;
- free ( b ) ;
- }
- _CATCH
- {
- }
- }
- // Pointer Check - Expression uses dangling pointer.
- void Pointer_ExprUsesDanglPtr ( )
- {
- _TRY
- {
- char *a = (char *)malloc( 10 ) ;
- char b[ 10 ] ;
-
- free ( a ) ;
- if ( a > b )
- a = b ;
- }
- _CATCH
- {
- }
- }
- // Pointer Check - Function pointer is not a function.
- union {
- int * iptr ;
- int (*fptr)( ) ;
- } uMakeTrouble ;
- void Pointer_FuncPtrIsNotAFn ( )
- {
- _TRY
- {
- // Setting i to 0xC3 provides a known behavior for a this function
- // pointer that isn't a function. The call to uMakeTrouble.fptr ( )
- // simply returns instead of having undefined results.
- const int RETURN_FROM_FUNCTION_ID = 0xC3;
- int i = RETURN_FROM_FUNCTION_ID;
- uMakeTrouble.iptr = &i ;
- uMakeTrouble.fptr ( ) ;
- }
- _CATCH
- {
- }
- }
- // Pointer Check - Pointer references unlocked memory block
- void Pointer_PtrRefsUnlockedBlock ( )
- {
- _TRY
- {
- HANDLE hMem ;
- LPVOID lpPtr ;
-
- // This memory is allocated as movable.
- hMem = GlobalAlloc ( GHND , 0x100 ) ;
- lpPtr = GlobalLock ( hMem ) ;
- GlobalUnlock ( hMem ) ;
- // Use the unlocked pointer.
- memset ( lpPtr , 0 , 0x100 ) ;
- GlobalFree ( hMem ) ;
- }
- _CATCH
- {
- }
- }
- // Pointer Check - Unallocated pointer.
- void Pointer_UnallocdPtr ( )
- {
- _TRY
- {
- HLOCAL hMem = LocalAlloc( LMEM_MOVEABLE, 4 );
- char * pmem = (char *)LocalLock( hMem );
- LocalUnlock( hMem );
- LocalFree( hMem );
-
- // Because we are overwriting a dangling pointer we need to save
- // and restore it to prevent major problems later.
- DWORD saveBuff;
- saveBuff = *((DWORD *)pmem);
- memset ( pmem , 0 , 4 ) ;
- *((DWORD *)pmem) = saveBuff;
- }
- _CATCH
- {
- }
- }
-
- // Pointer Check - Assigning pointer out of range
- void Pointer_AssignOutOfRange ( )
- {
- _TRY
- {
- char buff[10];
- char * pbuff;
- pbuff = (buff + 11);
- }
- _CATCH
- {
- }
- }
-
- // Pointer Check - Array index out of range
- void Pointer_ArrayParamExRange ( )
- {
- _TRY
- {
- int a[ 5 ] ;
- int b;
- b = a[6];
- }
-
- _CATCH
- {
- }
- }
-
- // Pointer Check - Returning pointer to local variable
- // private helper function
- int * retLocalInt( void )
- {
- int *i, j = 11;
- i = &j;
- return i;
- }
- // Pointer Check - Returning pointer to local variable
- void Pointer_ReturnPtrToLocalVar( )
- {
- int* pi = retLocalInt();
- }