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

  1. /*
  2.  * BCError.cpp
  3.  * $Header: /BoundsChecker/Examples/BUGBNCHX/MAINERR/BCERROR.CPP 20    5/08/97 10:19a Bob $
  4.  *
  5.  * Description:
  6.  *  Holds the data structures to fill the Tree Control with.  Also has
  7.  * the random error function.
  8.  *
  9.  * Notes:
  10.  *  <implementation notes go here>
  11.  *
  12.  ***********************************************************************
  13.  *
  14.  * Nu-Mega Technologies, Inc.
  15.  * P.O. Box 7780
  16.  * Nashua, NH 03060
  17.  *
  18.  * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
  19.  * ALL RIGHTS RESERVED.
  20.  *
  21.  ***********************************************************************
  22.  *
  23.  **********************************************************************/
  24. #include "stdafx.h"
  25. #include <time.h>
  26. #include "BCError.h"
  27.  
  28. #include "BCTree.h"
  29.  
  30. // The function prototypes for all the functions that actually cause
  31. //  errors.  How cool, I get to actually write bugs -- purposely, for
  32. //  a change!
  33. #include "OLEErr.h"
  34. #include "APIErr.h"
  35. #include "CPPErr.h"
  36. #include "LeakErr.h"
  37. #include "PtrErr.h"
  38. #include "ReadErr.h"
  39. #include "WriteErr.h"
  40.  
  41. ////////////////////////////////////////////////////////////////////////
  42. // Constants that give the sizes of the data structure arrays.
  43. ////////////////////////////////////////////////////////////////////////
  44. // The number of root objects. (0 level objects)
  45. const constNumRootObjects  = 9 ;
  46. // The maximum number of different errors under a particular root
  47. //  object.  (1st level objects)
  48. const constMaxErrsPerCatagory = 24 ;
  49. // The number of leaf nodes, and consequently, the number of error
  50. //  functions.
  51. const constMaxNumErrorFuncs   = 82 ;
  52. // The maximum possible error functions under a first level object.
  53. const constMaxErrsPerType  = 4 ;
  54. // The number of times we will randomly execute a function.
  55. const constNumRandomFuncs  = 5 ;
  56.  
  57. #ifdef BCBVER
  58. const TCHAR szModName [] = _T ( "bcberr.bug" ) ;
  59. #else
  60. const TCHAR szModName [] = _T ( "mainerr.bug" ) ;
  61. #endif
  62.  
  63. const constMaxLParam = 1024 ;
  64.  
  65. ////////////////////////////////////////////////////////////////////////
  66. int g_nLParam ;
  67. LParamInfo* g_lParamArray[ constMaxLParam ] ;
  68. ////////////////////////////////////////////////////////////////////////
  69. // The array of root objects, these are the error categories.
  70. ////////////////////////////////////////////////////////////////////////
  71.  
  72. ErrorCatagory g_RootObjects[ constNumRootObjects ] =
  73. {
  74.    {
  75.       IDS_APIANDOLECHECK         ,
  76.       IDS_APIANDOLECHECKDESC
  77.    } ,
  78.    {
  79.       IDS_MEMORYCHECK         ,
  80.       IDS_MEMORYCHECKDESC
  81.    } ,
  82.    {
  83.       IDS_POINTERANDLEAKCHECK         ,
  84.       IDS_POINTERANDLEAKCHECKDESC
  85.    } ,
  86.    // Always leave the NULL to indicate the end.
  87.    {
  88.       NULL                 ,
  89.       NULL
  90.    }
  91. } ;
  92.  
  93. ////////////////////////////////////////////////////////////////////////
  94. // The array of first level objects.  These are the individual errors
  95. //  or error types. This is a 2 dimentional array that has a string id
  96. //  in the first position and a TRUE/FALSE flag in the second position.
  97. //  The string id is the id of the string that will be displayed in the
  98. //  tree control. The boolean flag is used to indicate wether or not 
  99. //  the error can be caught without compile time instrumentation, CTI.
  100. ////////////////////////////////////////////////////////////////////////
  101.  
  102. ErrorType g_ArrayOfTypes[constNumRootObjects][constMaxErrsPerCatagory] =
  103. {
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // API AND OLE CHECK TYPES
  106.    {    
  107.       {// Questionable use of thread
  108.          IDS_QUESTIONALBEUSEOTHREAD    ,
  109.          TRUE                          ,
  110.       } ,
  111.       {// API failure: Windows function failed
  112.          IDS_APIFAILWINFUNCFAIL  ,
  113.          TRUE                    ,
  114.       } ,
  115.       {// API failure: function not implemented
  116.          IDS_APIFAILWINFUNCNOTIMP   ,
  117.          TRUE                       ,
  118.       } ,
  119.       {// Interface method failure
  120.          IDS_OLEFAILINTFUNCFAIL   ,
  121.          TRUE                       ,
  122.       } ,
  123.       {// Invalid argument
  124.          IDS_INVALIDARGGENERAL   ,
  125.          TRUE                    ,
  126.       } ,
  127.       {// Invalid argument - At least one format specifier is illegal
  128.          IDS_INVALIDARGBADFMTSPECIFIER ,
  129.          TRUE                          ,
  130.       } ,
  131.       {// Invalid argument  - bad destination pointer
  132.          IDS_INVALIDARGBADDESTPTR   ,
  133.          TRUE                       ,
  134.       } ,
  135.       {// Invalid argument - bad handle
  136.          IDS_INVALIDARGBADHANDLE ,
  137.          TRUE                    ,
  138.       } ,
  139.       {// Invalid argument  - bad source pointer
  140.          IDS_INVALIDARGBADSRCPTR ,
  141.          TRUE                    ,
  142.       } ,
  143.       {// Invalid argument - conflicting combination of flags
  144.          IDS_INVALIDARGCONFLICT  ,
  145.          TRUE                    ,
  146.       } ,
  147.       {// Invalid argument - format string not followed by valid arguments
  148.          IDS_INVALIDARGFMTINVALIDARG   ,
  149.          TRUE                          ,
  150.       } ,
  151.       {// Invalid argument: Invalid pointer to format string
  152.          IDS_INVALIDARGBADFMTSTR ,
  153.          TRUE                    ,
  154.       } ,
  155.       {// Invalid argument: Not enough arguments for this format string
  156.          IDS_INVALUDARGINSUFFPARAMS ,
  157.          TRUE                       ,
  158.       } ,
  159.       {// Invalid argument: Out of range
  160.          IDS_INVALIDARGOUTORANGE ,
  161.          TRUE                    ,
  162.       } ,
  163.       {// Invalid argument: Structure size field is not initialized
  164.          IDS_INVALIDARGSTRUCTURE ,
  165.          TRUE                    ,
  166.       } ,
  167.       {// Invalid argument - Too many arguments for this format string
  168.          IDS_INVALIDARGTOOMANYPARAMS   ,
  169.          TRUE                          ,
  170.       } ,
  171.       {// Invalid argument - Undefined or illegal flags
  172.          IDS_INVALIDARGUNDEFINED ,
  173.          TRUE                    ,
  174.       } ,
  175.       {// Invalid Interface method argument
  176.          IDS_OLEINVALIDARGGENERAL   ,
  177.          TRUE                       ,
  178.       } ,
  179.       {// Invalid Interface method argument: conflicting combination of flags
  180.          IDS_OLEINVALIDARGCONFFLAGS ,
  181.          TRUE                       ,
  182.       } ,
  183.       {// Invalid Interface method argument: Out of range
  184.          IDS_OLEINVALIDARGBADRANGE  ,
  185.          TRUE                       ,
  186.       } ,
  187.       {// Invalid Interface method argument: Structure size field is not initialized
  188.          IDS_OLEINVALIDARGSTRCTUNINT ,
  189.          TRUE                        ,
  190.       } ,
  191.       {// Invalid Interface method argument:  Undefined or illegal flags
  192.          IDS_OLEINVALIDARGUNDEFFLAGS   ,
  193.          TRUE                       ,
  194.       } ,
  195.       { NULL                           }
  196.    } ,
  197.  
  198. ///////////////////////////////////////////////////////////////////////////////
  199. // MEMORY CHECK TYPES
  200.    {    
  201.       {// Dynamic memory overrun
  202.          IDS_DYNMEMOVERRUN ,
  203.          TRUE              ,
  204.       } ,
  205.       {// Freed handle is still locked
  206.          IDS_INVALIDARGFREEDHANDLELOCK ,
  207.          TRUE                          ,
  208.       } ,
  209.       {// Handle is already unlocked
  210.          IDS_INVALIDARGHANDLEUNLOCKED  ,
  211.          TRUE                          ,
  212.       } ,
  213.       {// Memory allocation conflict
  214.          IDS_MEMALLOCCONFLICT ,
  215.          TRUE                 ,
  216.       } ,
  217.       {// Pointer references unlocked memory block
  218.          IDS_PTRREFSUNLOCKEDBLOCK   ,
  219.          TRUE                       ,
  220.       } ,
  221. #ifndef BCBVER
  222.       {// Reading overflows memory
  223.          IDS_READOVERFLOWSMEM ,
  224.          FALSE                ,
  225.       } ,
  226.       {// Reading uninitialized memory
  227.          IDS_READUNINITMEM ,
  228.          FALSE             ,
  229.       } ,
  230. #endif
  231.       {// Stack memory overrun
  232.          IDS_STACKMEMOVERRUN  ,
  233.          TRUE                 ,
  234.       } ,
  235.       {// Static memory overrun
  236.          IDS_STATICMEMOVERRUN ,
  237.          TRUE                 ,
  238.       } ,
  239. #ifndef BCBVER
  240.       {// Writing overflows memory
  241.          IDS_WRITINGOVERFLOWSMEM ,
  242.          FALSE                   ,
  243.       } ,
  244. #endif
  245.       { NULL                           }
  246.    } ,
  247.  
  248. ///////////////////////////////////////////////////////////////////////////////
  249. // POINTER AND LEAK CHECK TYPES
  250.    {
  251. #ifndef BCBVER
  252.       {// Array index out of range
  253.          IDS_ARRAYPARAMEXCEEDSRANGE ,
  254.          FALSE                      ,
  255.       } ,
  256.       {// Assigning pointer out of range
  257.          IDS_ASSIGNINGPTROUTOFRANGE ,
  258.          FALSE                      ,
  259.       } ,
  260.       {// Expression uses unrelated pointers
  261.          IDS_EXPRUSESUNRELPTR   ,
  262.          FALSE                   ,
  263.       } ,
  264.       {// Expression uses dangling pointer
  265.          IDS_EXPRTRUSESDANGLEPTR ,
  266.          FALSE                   ,
  267.       } ,
  268.       {// Function pointer is not a function
  269.          IDS_FNPTRNOTAFN   ,
  270.          FALSE             ,
  271.       } ,
  272. #endif
  273.       {// Interface leak
  274.          IDS_OLEINTLEAK ,
  275.          TRUE                   ,
  276.       } ,
  277.       {// Memory leak
  278.          IDS_MEMLEAKGENERAL   ,
  279.          TRUE                 ,
  280.       } ,
  281. #ifndef BCBVER
  282.       {// Memory leaked due to free
  283.          IDS_MEMLEAKTOFREE ,
  284.          FALSE             ,
  285.       } ,
  286.       {// Memory leaked due to reassignment
  287.          IDS_MEMLEAKREASSIGN  ,
  288.          FALSE                ,
  289.       } ,
  290.       {// Memory leaked leaving scope
  291.          IDS_MEMLEAKLEAVESCOPE   ,
  292.          FALSE                   ,
  293.       } ,
  294. #endif
  295.       {// Resource leak
  296.          IDS_RESOURCELEAK  ,
  297.          TRUE              ,
  298.       } ,
  299. #ifndef BCBVER
  300.       {// Returning pointer to local variable
  301.          IDS_RETURNINGPTRTOLOCALVAR,
  302.          FALSE                 ,
  303.       } ,
  304. #endif
  305.       {// Unallocated pointer
  306.          IDS_UNALLOCATEDPTR   ,
  307.          TRUE                 ,
  308.       } ,
  309.       { NULL                           }
  310.    }
  311. } ;
  312.  
  313.  
  314. ////////////////////////////////////////////////////////////////////////
  315. // The array of leaf nodes, or occurrences.  This is where the pointers
  316. //  to the error functions are stored.
  317. ////////////////////////////////////////////////////////////////////////
  318.  
  319. // The array that holds all the error occurrences.
  320. ErrorOccurance g_aOccurArray[constMaxNumErrorFuncs][constMaxErrsPerType]=
  321. {
  322. ///////////////////////////////////////////////////////////////////////////////
  323. // API AND OLE ERRORS
  324.  
  325.    {// Questionable use of thread
  326.       {
  327.          IDS_QUESTIONTHREADINST        ,
  328.          IDS_QUESTIONTHREADDESC        ,
  329.          API_QuestionableUseOfThread
  330.       } ,
  331.       { NULL } ,
  332.       { NULL } ,
  333.       { NULL }
  334.    } ,
  335.    {// API failure: Windows function failed
  336.       {
  337.          IDS_APIFAILLOADBITMAPINST     ,
  338.          IDS_APIFAILLOADBITMAPDESC     ,
  339.          API_Failure_LoadBitmap        ,
  340.       } ,
  341. #ifndef BCBVER
  342.       {
  343.          IDS_APIFAILMALLOCINST   ,
  344.          IDS_APIFAILMALLOCDESC   ,
  345.          API_Failure_Malloc      ,
  346.       } ,
  347. #else
  348.       { NULL },
  349. #endif
  350.       { NULL } ,
  351.       { NULL } ,
  352.    } ,
  353.    {// API failure: function not implemented
  354.       {
  355.          IDS_APIFAILNOTIMPINSTNT    ,
  356.          IDS_APIFAILNOTIMPDESCNT    ,
  357.          API_Failure_PaintDesktop   ,
  358.       } ,
  359.       {
  360.          IDS_APIFAILNOTIMPINST95    ,
  361.          IDS_APIFAILNOTIMPDESC95    ,
  362.          API_Failure_HeapValidate   ,
  363.       } ,
  364.       { NULL } ,
  365.       { NULL } ,
  366.    } ,
  367.    {// Interface method failure
  368.       {
  369.          IDS_OLEINTFMETHODFAIL            ,
  370.          IDS_OLEINTFMETHODFAILDESC        ,
  371.          OLE_IntfFail_QueryInterface      ,
  372.       } ,
  373.       { NULL } ,
  374.       { NULL } ,
  375.       { NULL } ,
  376.    } ,
  377.    {// Invalid argument
  378.       {
  379.          IDS_INVARGGENHDCINST       ,
  380.          IDS_INVARGGENHDCDESC       ,
  381.          API_General_HDC
  382.       } ,
  383.       {
  384.          IDS_INVARGGENHMENUINST     ,
  385.          IDS_INVARGGENHMENUDESC     ,
  386.          API_General_HMENU
  387.       } ,
  388.       { NULL } ,
  389.       { NULL } 
  390.    } ,
  391.    {// Invalid argument - At least one format specifier is illegal
  392.       {
  393.          IDS_ILLEGALFORMATSPECINST  ,
  394.          IDS_ILLEGALFORMATSPECDESC  ,
  395.          API_IllegalFormatSpec      ,
  396.       } ,
  397.       { NULL } ,
  398.       { NULL } ,
  399.       { NULL } ,
  400.    } ,
  401.    {// Invalid argument  - bad destination pointer
  402.       {
  403.          IDS_BADDESTPTRINST   ,
  404.          IDS_BADDESTPTRDESC   ,
  405.          API_BadDestPtr       ,
  406.       } ,
  407.       { NULL } ,
  408.       { NULL } ,
  409.       { NULL } ,
  410.    } ,
  411.    {// Invalid argument - bad handle
  412.       {
  413.          IDS_BADHANDLEFREEINST   ,
  414.          IDS_BADHANDLEFREEDESC   ,
  415.          API_BadHandleFree       ,
  416.       } ,
  417.       {
  418.          IDS_BADHANDLELFREEINST  ,
  419.          IDS_BADHANDLELFREEDESC  ,
  420.          API_BadHandleLocalFree  ,
  421.       } ,
  422. #ifndef BCBVER
  423.       {
  424.          IDS_BADHANDLEREMALLOCINST  ,
  425.          IDS_BADHANDLEREMALLOCDESC  ,
  426.          API_BadHandleRealloc       ,
  427.       } ,
  428. #else
  429.       { NULL } ,
  430. #endif     
  431.       { NULL } ,
  432.    } ,
  433.    {// Invalid argument  - bad source pointer
  434.       {
  435.          IDS_BADSOURCEPTRINST    ,
  436.          IDS_BADSOURCEPTRDESC    ,
  437.          API_BadSourcePtr        ,
  438.       } ,
  439.       { NULL } ,
  440.       { NULL } ,
  441.       { NULL } ,
  442.    } ,
  443.    {// Invalid argument - conflicting combination of flags
  444.       {
  445.          IDS_INVALIDARGGETSTRINGTYPEINST  ,
  446.          IDS_INVALIDARGGETSTRINGTYPEDESC  ,
  447.          API_ConflictFlags_GetStringTypeEx
  448.       } ,
  449.       {
  450.          IDS_INVALIDARGVIRTUALFREEINST ,
  451.          IDS_INVALIDARGVIRTUALFREEDESC ,
  452.          API_ConflictFlags_VirtualFree
  453.       } ,
  454.       { NULL } ,
  455.       { NULL }
  456.    } ,
  457.    {// Invalid argument - format string not followed by valid arguments
  458.       {
  459.          IDS_FMTSTRNOTFOLLOWEDINST  ,
  460.          IDS_FMTSTRNOTFOLLOWEDDESC  ,
  461.          API_FmtStrNotFollowed      ,
  462.       } ,
  463.       { NULL } ,
  464.       { NULL } ,
  465.       { NULL } ,
  466.    } ,
  467.    {// Invalid argument: Invalid pointer to format string
  468.       {
  469.          IDS_INVPTRTOFMTSTRINST  ,
  470.          IDS_INVPTRTOFMTSTRDESC  ,
  471.          API_InvPtrToFmtString   ,
  472.       } ,
  473.       { NULL } ,
  474.       { NULL } ,
  475.       { NULL } ,
  476.    } ,
  477.    {// Invalid argument: Not enough arguments for this format string
  478.       {
  479.          IDS_NOTENUFARGSFORFMTINST  ,
  480.          IDS_NOTENUFARGSFORFMTDESC  ,
  481.          API_NotEnufArgsForFmtStr   ,
  482.       } ,
  483.       { NULL } ,
  484.       { NULL } ,
  485.       { NULL } ,
  486.    } ,
  487.    {// Invalid argument: Out of range
  488.       {
  489.          IDS_INVALIDARGRANGEINST     ,
  490.          IDS_INVALIDARGRANGEDESC     ,
  491.          API_ArgOutOfRange_GetKeyState
  492.       } ,
  493.       {
  494.          IDS_INVALIDARGCREATEFONTINST   ,
  495.          IDS_INVALIDARGCREATEFONTDESC   ,
  496.          API_ArgOutOfRange_CreateFont
  497.       } ,
  498.       {
  499.          IDS_INVARGARGCREATEPIPEINST  ,
  500.          IDS_INVARGARGCREATEPIPEDESC  ,
  501.          API_ArgOutOfRange_CreateFile
  502.       } ,
  503.       { NULL }
  504.    } ,
  505.    {// Invalid argument: Structure size field is not initialized
  506.       {
  507.          IDS_STRCTSIZEFIELDUNINITINST  ,
  508.          IDS_STRCTSIZEFIELDUNINITDESC  ,
  509.          API_StructSizeZero            ,
  510.       } ,
  511.       { NULL } ,
  512.       { NULL } ,
  513.       { NULL } ,
  514.    } ,
  515.    {// Invalid argument - Too many arguments for this format string
  516.       {
  517.          IDS_TOOMANYARGSFORFMTSTRINST  ,
  518.          IDS_TOOMANYARGSFORFMTSTRDESC  ,
  519.          API_TooManyArgsForFmtStr      ,
  520.       } ,
  521.       { NULL } ,
  522.       { NULL } ,
  523.       { NULL } ,
  524.    } ,
  525.    {// Invalid argument - Undefined or illegal flags
  526.       {
  527.          IDS_INVALIDARGAPPENDMENUINST  ,
  528.          IDS_INVALIDARGAPPENDMENUDESC  ,
  529.          API_IllegalFlags_AppendMenu
  530.       } ,
  531.       {
  532.          IDS_INVALIDARGFOLDSTRINGINST  ,
  533.          IDS_INVALIDARGFOLDSTRINGDESC  ,
  534.          API_IllegalFlags_FoldString
  535.       } ,
  536.       { NULL },
  537.       { NULL },
  538.    } ,
  539.    {// Invalid Interface method argument
  540.       {
  541.          IDS_OLEINVARGGEN                          ,
  542.          IDS_OLEINVARGGENDESC                      ,
  543.          OLE_IntfArg_General_QueryGetData          ,
  544.       } ,
  545.       { NULL } ,
  546.       { NULL } ,
  547.       { NULL } ,
  548.    } ,
  549.    {// Invalid Interface method argument: conflicting combination of flags
  550.       {
  551.          IDS_OLEINVALIDFLAGCOMBO                       ,
  552.          IDS_OLEINVALIDFLAGCOMBODESC                   ,
  553.          OLE_IntfArg_BadComboFlag_SetAdvise            ,
  554.       } ,
  555.       { NULL } ,
  556.       { NULL } ,
  557.       { NULL } ,
  558.    } ,
  559.    {// Invalid Interface method argument: Out of range
  560.       {
  561.          IDS_OLEINVARGOUTRANGE                  ,
  562.          IDS_OLEINVARGOUTRANGEDESC              ,
  563.          OLE_IntfArg_BadRange_Draw              ,
  564.       } ,
  565.       { NULL } ,
  566.       { NULL } ,
  567.       { NULL } ,
  568.    } ,
  569.    {// Invalid Interface method argument: Structure size field is not initialized
  570.       {
  571.          IDS_OLESTRCTSIZFLD                 ,
  572.          IDS_OLESTRCTSIZFLDDESC             ,
  573.          OLE_IntfArg_StructSize_Draw        ,
  574.       } ,
  575.       { NULL } ,
  576.       { NULL } ,
  577.       { NULL } ,
  578.    } ,
  579.    {// Invalid Interface method argument:  Undefined or illegal flags
  580.       {
  581.          IDS_OLEINVALIDFLAG                     ,
  582.          IDS_OLEINVALIDFLAGDESC                 ,
  583.          OLE_IntfArg_InvalidFlag_SetAdvise      ,
  584.       } ,
  585.       { NULL } ,
  586.       { NULL } ,
  587.       { NULL } ,
  588.    } ,
  589.  
  590. ///////////////////////////////////////////////////////////////////////////////
  591. // MEMORY ERRORS
  592.  
  593.    {// Dynamic memory overrun
  594.       {
  595.          IDS_DYNMEMOVERRUNINST   ,
  596.          IDS_DYNMEMOVERRUNDESC   ,
  597.          Write_DynMemOverrun     ,
  598.       } ,
  599.       { NULL } ,
  600.       { NULL } ,
  601.       { NULL } ,
  602.    } ,
  603.    {// Freed handle is still locked
  604.       {
  605.          IDS_FREEDHANDLELOCKEDINST  ,
  606.          IDS_FREEDHANDLELOCKEDDESC  ,
  607.          API_FreedHandleStillLocked ,
  608.       } ,
  609.       { NULL } ,
  610.       { NULL } ,
  611.       { NULL } ,
  612.    } ,
  613.    {// Handle is already unlocked
  614.       {
  615.          IDS_HANDLEALREADYUNLOCKEDINST ,
  616.          IDS_HANDLEALREADYUNLOCKEDDESC ,
  617.          API_HandleAlreadyUnlocked     ,
  618.       } ,
  619.       { NULL } ,
  620.       { NULL } ,
  621.       { NULL } ,
  622.    } ,
  623.    {// Memory allocation conflict
  624.       {
  625.          IDS_MEMCONFLICTMALLOCDELINST  ,
  626.          IDS_MEMCONFLICTMALLOCDELDESC  ,
  627.          CPP_MemConflictMallocDelete
  628.       } ,
  629.       {
  630.          IDS_MEMCONFLICTNEWFREEINST ,
  631.          IDS_MEMCONFLICTNEWFREEDESC ,
  632.          CPP_MemConflictNewFree
  633.       } ,
  634.       { NULL } ,
  635.       { NULL }
  636.    } ,
  637.    {// Pointer references unlocked memory block
  638.       {
  639.          IDS_PTRREFSUNLOCKEDBLOCKINST  ,
  640.          IDS_PTRREFSUNLOCKEDBLOCKDESC  ,
  641.          Pointer_PtrRefsUnlockedBlock  ,
  642.       } ,
  643.       { NULL } ,
  644.       { NULL } ,
  645.       { NULL } ,
  646.    } ,
  647. #ifndef BCBVER
  648.    {// Reading overflows memory
  649.       {
  650.          IDS_READOVERFLOWSSTRUCTINST   ,
  651.          IDS_READOVERFLOWSSTRUCTDESC   ,
  652.          Read_ReadOverflowsStructure
  653.       } ,
  654.       { NULL } ,
  655.       { NULL } ,
  656.       { NULL } ,
  657.    } ,
  658.    {// Reading uninitialized memory
  659.       {
  660.          IDS_READUNINITMEMINST   ,
  661.          IDS_READUNINITMEMDESC   ,
  662.          Read_ReadUninitMem      ,
  663.       } ,
  664.       { NULL } ,
  665.       { NULL } ,
  666.       { NULL } ,
  667.    } ,
  668. #endif
  669.    {// Stack memory overrun
  670.       {
  671.          IDS_STACKMEMOVERRUNINST ,
  672.          IDS_STACKMEMOVERRUNDESC ,
  673.          Write_StackMemOverrun   ,
  674.       } ,
  675.       { NULL } ,
  676.       { NULL } ,
  677.       { NULL } ,
  678.    } ,
  679.    {// Static memory overrun
  680.       {
  681.          IDS_STATICMEMOVERRUNINST   ,
  682.          IDS_STATICMEMOVERRUNDESC   ,
  683.          Write_StaticMemOverrun     ,
  684.       } ,
  685.       { NULL } ,
  686.       { NULL } ,
  687.       { NULL } ,
  688.    } ,
  689. #ifndef BCBVER
  690.    {// Writing overflows memory
  691.       {
  692.          IDS_WRITEOVERFLOWSMEMINST  ,
  693.          IDS_WRITEOVERFLOWSMEMDESC  ,
  694.          Write_WriteOverflowsMem    ,
  695.       } ,
  696.       { NULL } ,
  697.       { NULL } ,
  698.       { NULL } ,
  699.    } ,
  700.  
  701.  
  702. ///////////////////////////////////////////////////////////////////////////////
  703. // POINTER AND LEAK ERRORS
  704.    {// Array index out of range
  705.       {
  706.          IDS_ARRAYPARAMEXRANGEINST  ,
  707.          IDS_ARRAYPARAMEXRANGEDESC  ,
  708.          Pointer_ArrayParamExRange  ,
  709.       } ,
  710.       { NULL } ,
  711.       { NULL } ,
  712.       { NULL } ,
  713.    } ,
  714.    {// Assigning pointer out of range
  715.       {
  716.          IDS_ASSIGNPTROUTOFRANGEINST,
  717.          IDS_ASSIGNPTROUTOFRANGEDESC,
  718.          Pointer_AssignOutOfRange   ,
  719.       } ,
  720.       { NULL } ,
  721.       { NULL } ,
  722.       { NULL } ,
  723.    } ,
  724.    {// Expression uses unrelated pointers
  725.       {
  726.          IDS_EXPRUSESUNRELPTRINST     ,
  727.          IDS_EXPRUSESUNRELPTRDESC     ,
  728.          Pointer_ExprUsesUnrelPtrs ,
  729.       } ,
  730.       {
  731.          IDS_EXPRSUBSUNRELPTRSINST  ,
  732.          IDS_EXPRSUBSUNRELPTRSDESC  ,
  733.          Pointer_ExprSubsUnrelPtrs  ,
  734.       } ,
  735.       { NULL } ,
  736.       { NULL } ,
  737.    } ,
  738.    {// Expression uses dangling pointer
  739.       {
  740.          IDS_EXPRUSESDANGLPTRINST   ,
  741.          IDS_EXPRUSESDANGLPTRDESC   ,
  742.          Pointer_ExprUsesDanglPtr   ,
  743.       } ,
  744.       { NULL } ,
  745.       { NULL } ,
  746.       { NULL } ,
  747.    } ,
  748.    {// Function pointer is not a function
  749.       {
  750.          IDS_FNPTRNOTAFNINST     ,
  751.          IDS_FNPTRNOTAFNDESC     ,
  752.          Pointer_FuncPtrIsNotAFn ,
  753.       } ,
  754.       { NULL } ,
  755.       { NULL } ,
  756.       { NULL } ,
  757.    } ,
  758. #endif
  759.    {// Interface leak
  760.       {
  761.          IDS_OLEINTFLEAK            ,
  762.          IDS_OLEINTFLEAKDESC        ,
  763.          OLE_Interface_Leak         ,
  764.       } ,
  765.       { NULL } ,
  766.       { NULL } ,
  767.       { NULL } ,
  768.    } ,
  769.    {// Memory leak
  770.       {
  771.          IDS_MEMORYLEAKINST   ,
  772.          IDS_MEMORYLEAKDESC   ,
  773.          Leak_MemoryLeak      ,
  774.       } ,
  775.       { NULL } ,
  776.       { NULL } ,
  777.       { NULL } ,
  778.    } ,
  779. #ifndef BCBVER
  780.    {// Memory leaked due to free
  781.       {
  782.          IDS_MEMLEAKFROMFREEINST ,
  783.          IDS_MEMLEAKFROMFREEDESC ,
  784.          Leak_LeakFromFree       ,
  785.       } ,
  786.       { NULL } ,
  787.       { NULL } ,
  788.       { NULL } ,
  789.    } ,
  790.    {// Memory leaked due to reassignment
  791.       {
  792.          IDS_MEMLEAKFROMREASSIGNINST   ,
  793.          IDS_MEMLEAKFROMREASSIGNDESC   ,
  794.          Leak_LeakFromReassign         ,
  795.       } ,
  796.       { NULL } ,
  797.       { NULL } ,
  798.       { NULL } ,
  799.    } ,
  800.    {// Memory leaked leaving scope
  801.       {
  802.          IDS_MEMLEAKFROMIGNOREINST  ,
  803.          IDS_MEMLEAKFROMIGNOREDESC  ,
  804.          Leak_LeakFromIgnore        ,
  805.       } ,
  806.       {
  807.          IDS_MEMLEAKFROMSCOPEINST   ,
  808.          IDS_MEMLEAKFROMSCOPEDESC   ,
  809.          Leak_LeakFromScope         ,
  810.       } ,
  811.       { NULL } ,
  812.       { NULL } ,
  813.    } ,
  814. #endif
  815.    {// Resource leak
  816.       {
  817.          IDS_RESLEAKBITMAPINST   ,
  818.          IDS_RESLEAKBITMAPDESC   ,
  819.          Leak_ResLeakBitmap      ,
  820.       } ,
  821.       {
  822.          IDS_RESLEAKMETAINST     ,
  823.          IDS_RESLEAKMETADESC     ,
  824.          Leak_ResLeakMetafile    ,
  825.       } ,
  826.       {
  827.          IDS_RESLEAKMENUINST     ,
  828.          IDS_RESLEAKMENUDESC     ,
  829.          Leak_ResLeakMenu        ,
  830.       } ,
  831.       { NULL } ,
  832.    } ,
  833. #ifndef BCBVER
  834.    {// Returning pointer to local variable
  835.       {
  836.          IDS_RETURNINGPTRTOLOCALVARINST,
  837.          IDS_RETURNINGPTRTOLOCALVARDESC,
  838.          Pointer_ReturnPtrToLocalVar,
  839.       } ,
  840.       { NULL } ,
  841.       { NULL } ,
  842.       { NULL } ,
  843.    } ,
  844. #endif
  845.    {// Unallocated pointer
  846.       {
  847.          IDS_UNALLOCDPTRINST  ,
  848.          IDS_UNALLOCDPTRDESC  ,
  849.          Pointer_UnallocdPtr  ,
  850.       } ,
  851.       { NULL } ,
  852.       { NULL } ,
  853.       { NULL } ,
  854.    } ,
  855.  
  856. } ;
  857.  
  858. ////////////////////////////////////////////////////////////////////////
  859. // BCERROR normal functions start here.
  860. ////////////////////////////////////////////////////////////////////////
  861.  
  862. void DeleteLParam ( )
  863. {
  864.     for ( int i = 0 ; i < g_nLParam ; i++ ) 
  865.     {
  866.         delete g_lParamArray[ i ] ;
  867.     }
  868. }
  869.  
  870. HTREEITEM GetChildWithName ( HWND hWnd , HTREEITEM hParent , LPCTSTR szName ) 
  871. {
  872.     ASSERT ( IsWindow ( hWnd ) ) ;
  873.     HTREEITEM hChild = TreeCtrl_GetChildItem ( hWnd , hParent ) ;
  874.  
  875.     while ( NULL != hChild ) 
  876.     {
  877.         TV_ITEM tviChild ;
  878.         TCHAR szChildText [ 1024 ] ;
  879.  
  880.         tviChild.mask = TVIF_TEXT | TVIF_HANDLE ;
  881.         tviChild.hItem = hChild ;
  882.         tviChild.pszText = & ( szChildText[0] ) ;
  883.         tviChild.cchTextMax = 1024 ;
  884.  
  885.         if ( TreeView_GetItem ( hWnd , & tviChild ) )
  886.         {
  887.             if ( 0 == lstrcmp ( szName , szChildText ) )
  888.             {
  889.                 return hChild ;
  890.             }
  891.         }
  892.  
  893.         hChild = TreeView_GetNextItem ( hWnd , hChild , TVGN_NEXT ) ;
  894.     }
  895.  
  896.     return NULL ;
  897. }
  898.         
  899.  
  900.  
  901. // The function to fill the tree control.
  902. void FillTree ( HWND hwndTree )
  903. {
  904.    // The insert structure used throughout the function.
  905.    TV_INSERTSTRUCT stTVIS ;
  906.    HTREEITEM hItemLevel1 ;
  907.  
  908.    g_nLParam = 0 ;
  909.  
  910.    // Set up the insert structure.
  911.    memset ( &stTVIS , NULL , sizeof ( TV_INSERTSTRUCT ) ) ;
  912.    stTVIS.hParent = NULL ;
  913.    stTVIS.hInsertAfter = TVI_SORT ;
  914.    stTVIS.item.mask = TVIF_TEXT | TVIF_PARAM ;
  915.  
  916.    // Start with the root objects array.
  917.    ErrorCatagory * pstEC = g_RootObjects ;
  918.    // The buffer we use to load the strings into.
  919.    TCHAR szBuff[ 1024 ] ;
  920.    // The current point we are at in the type array.
  921.    int iCurrTypes = 0 ;
  922.    // The current point we are at in the occupancy array.
  923.    int iCurrErrorOccur = 0 ;
  924.  
  925.    // While we are not done with the root object array.
  926.    while ( 0 != pstEC->uiCatagory )
  927.    {
  928.       // When filling root objects, always force the
  929.       //  parent to NULL.
  930.       stTVIS.hParent = NULL ;
  931.  
  932.       // The root object category string is used as the text so load it.
  933.       VERIFY ( ::LoadString ( GetModuleHandle ( szModName ) ,
  934.                               pstEC->uiCatagory        ,
  935.                               szBuff                   ,
  936.                               sizeof ( szBuff )         ) ) ;
  937.       // Set the text for the item.
  938.       stTVIS.item.pszText = szBuff ;
  939.       // Allocate the structure that we store in the lParam field.
  940.       LParamInfo * pLPI = new LParamInfo ;
  941.       g_lParamArray[ g_nLParam++ ] = pLPI ;
  942.  
  943.       // This is a root object.
  944.       pLPI->iType = 0 ;
  945.       pLPI->hModule = GetModuleHandle ( szModName ) ;
  946.  
  947.       // Set the union to the current value.
  948.       pLPI->stEC = *pstEC ;
  949.       // Set the lParam and insert it saving off the HTREEITEM.
  950.       stTVIS.item.lParam  = (LPARAM)pLPI ;
  951.  
  952.       hItemLevel1 = GetChildWithName ( hwndTree , stTVIS.hParent , szBuff ) ;
  953.  
  954.       if ( NULL == hItemLevel1 )
  955.         hItemLevel1 = TreeCtrl_InsertItem ( hwndTree , &stTVIS ) ;
  956.  
  957.       // Now fill in the error types for this root object.
  958.       // Get the current part of the two dimensional types array and
  959.       //  loop until we hit the NULL value.
  960.       ErrorType * pstET = g_ArrayOfTypes[ iCurrTypes ] ;
  961.       while ( 0 != pstET->uiError )
  962.       {
  963.          // The parent for the current type.
  964.          stTVIS.hParent = hItemLevel1 ;
  965.          // The error string serves as the text for the tree so load it
  966.          //  and set it.
  967.          VERIFY ( ::LoadString ( GetModuleHandle ( szModName ) ,
  968.                                  pstET->uiError           ,
  969.                                  szBuff                   ,
  970.                                  sizeof ( szBuff )         ) ) ;
  971.          stTVIS.item.pszText = szBuff ;
  972.          // Allocate the structure we set in lParam.
  973.          pLPI = new LParamInfo ;
  974.          g_lParamArray[ g_nLParam++ ] = pLPI ;
  975.          // This is a level one item.
  976.          pLPI->iType = 1 ;
  977.          pLPI->stET = *pstET ;
  978.          pLPI->hModule = GetModuleHandle ( szModName ) ;
  979.          // Set the lParam and insert it into the tree.
  980.          stTVIS.item.lParam = (LPARAM)pLPI ;
  981.  
  982.          HTREEITEM hItem = GetChildWithName ( hwndTree , stTVIS.hParent , szBuff ) ;
  983.          if ( NULL == hItem ) 
  984.              hItem = TreeCtrl_InsertItem ( hwndTree , &stTVIS ) ;
  985.  
  986.          // Fill in the error occurrences (leaves) for this type.
  987.          // Get the current portion of the two dimensional occurrence
  988.          //  array.
  989.          ErrorOccurance * pstEO = g_aOccurArray[ iCurrErrorOccur ] ;
  990.          stTVIS.hParent = hItem ;
  991.  
  992.          while ( 0 != pstEO->uiInstance )
  993.          {
  994.             // The instance serves as the text for the tree.  Load it
  995.             //  and set it.
  996.             VERIFY ( ::LoadString ( GetModuleHandle ( szModName ) ,
  997.                                     pstEO->uiInstance        ,
  998.                                     szBuff                   ,
  999.                                     sizeof ( szBuff )         ) ) ;
  1000.             stTVIS.item.pszText = szBuff ;
  1001.             // Allocate the structure we put in lParam.
  1002.             pLPI = new LParamInfo ;
  1003.             g_lParamArray[ g_nLParam++ ] = pLPI ;
  1004.             // This is a leaf node.
  1005.             pLPI->iType = 2 ;
  1006.             pLPI->stEO = *pstEO ;
  1007.             pLPI->hModule = GetModuleHandle ( szModName ) ;
  1008.             // Set the lParam and insert it.
  1009.             stTVIS.item.lParam = (LPARAM)pLPI ;
  1010.  
  1011.             if ( NULL == GetChildWithName ( hwndTree , stTVIS.hParent , szBuff ) )
  1012.                 TreeCtrl_InsertItem ( hwndTree , &stTVIS ) ;
  1013.             // Get the next item in the occurrence array.
  1014.             pstEO++ ;
  1015.          }
  1016.          // Get the next error type.
  1017.          pstET++ ;
  1018.          // Bump up the occurrence item index.
  1019.          iCurrErrorOccur++ ;
  1020.       }
  1021.       // Bump up the types item index.
  1022.       iCurrTypes++ ;
  1023.       // Get the next category.
  1024.       pstEC++ ;
  1025.    }
  1026.  
  1027.    TreeView_SelectItem ( hwndTree , hItemLevel1 ) ;
  1028.  
  1029. }
  1030.  
  1031. extern "C" __declspec(dllexport)
  1032. void DoRandomErrors ( void )
  1033. {
  1034.    // Reseed each time.
  1035.    srand( (unsigned) time( NULL ) ) ;
  1036.  
  1037.    for ( int iLoop = 0 ; iLoop < constNumRandomFuncs ; iLoop++ )
  1038.    {
  1039.       // Pick one of the error types to do.
  1040.       int iType = rand ( ) % constMaxNumErrorFuncs ;
  1041.  
  1042.       // Now pick one of the occurrences for this type.
  1043.       // Since many have just one item, we will check for that occurrence
  1044.       //  as a special case.  If it does have a single item, then we
  1045.       //  will just call it.
  1046.       if ( NULL == g_aOccurArray[ iType ][ 1 ].pFunc )
  1047.       {
  1048.          // Double check that the function is not NULL.  If it is, then
  1049.          //  we will not call it.
  1050.          if ( NULL != g_aOccurArray[ iType ][ 0 ].pFunc )
  1051.          {
  1052.             g_aOccurArray[ iType ][ 0 ].pFunc ( ) ;
  1053.          }
  1054.       }
  1055.       else
  1056.       {
  1057.          // We have one of the rare ones that have multiple functions.
  1058.          //  We will loop looking for a random valid one, then call it.
  1059.          int iOccur ;
  1060.          do
  1061.          {
  1062.             iOccur = rand ( ) % constMaxErrsPerType ;
  1063.          }
  1064.          while ( NULL == g_aOccurArray[ iType ][ iOccur ].pFunc ) ;
  1065.          g_aOccurArray[ iType ][ iOccur ].pFunc ( ) ;
  1066.       }
  1067.    }
  1068. }
  1069.  
  1070. static BOOL g_DoingAllErrors = FALSE ;
  1071. extern "C" __declspec(dllexport)
  1072. void DoAllErrors ( void )
  1073. {
  1074.    // Unfortunately, there are several functions which will bring down
  1075.    //  BugBench when they are called.  Generally, anything that writes
  1076.    //  to an uninitialized stack pointer will cause this.  Since we
  1077.    //  don't want to wipe out at the first one (freeing local memory)
  1078.    //  we have to set the special flag so those functions can skip
  1079.    //  themselves if need be!
  1080.    g_DoingAllErrors = TRUE ;
  1081.  
  1082.    // Loop through all items in g_aOccurArray and if there is a function
  1083.    //  to call, call it.
  1084.    for ( int iType = 0 ; iType < constMaxNumErrorFuncs ; iType++ )
  1085.    {
  1086.       for ( int iOccur = 0 ; iOccur < constMaxErrsPerType ; iOccur++ )
  1087.       {
  1088.          if ( NULL == g_aOccurArray[ iType ][ iOccur ].pFunc )
  1089.             break ;
  1090.          g_aOccurArray[ iType ][ iOccur ].pFunc ( ) ;
  1091.       }
  1092.    }
  1093.  
  1094.    // Reset the flag.
  1095.    g_DoingAllErrors = FALSE ;
  1096. }
  1097.  
  1098. BOOL DoingAllErrors ( void )
  1099. {
  1100.    return ( g_DoingAllErrors ) ;
  1101. }
  1102.