Changeset 1009 for trunk/dll/fortify.c


Ignore:
Timestamp:
May 10, 2008, 9:51:58 AM (17 years ago)
Author:
Steven Levine
Message:

Add xfree xstrdup Fortify support
Add MT capable Fortify scope logic

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dll/fortify.c

    r1005 r1009  
    2727 */
    2828
    29 /* 
     29/*
    3030 *     If  you use this software at all, I'd love to hear from
    3131 * you.   All  questions,  criticisms, suggestions, praise and
    3232 * postcards are most welcome.
    33  * 
     33 *
    3434 *            email:    sbullen@cybergraphic.com.au
    35  * 
     35 *
    3636 *            snail:    Simon P. Bullen
    3737 *                      PO BOX 12138
     
    3939 *                      Melbourne 3000
    4040 *                      Australia
     41 */
     42
     43 /* 06 May 08 SHL Rework scope logic to be MT capable
    4144 */
    4245
     
    5659
    5760/* Prototypes and such */
    58 #define __FORTIFY_C__
     61#define __FORTIFY_C__                   // Suppress malloc replacement etc.
    5962#include "fortify.h"
    6063
    6164
     65#if defined(__WATCOMC__) && defined(_MT)
     66#define MT_SCOPES 1
     67unsigned long Get_TID_Ordinal(void);
     68#pragma aux Get_TID_Ordinal = \
     69 "mov eax, far ptr fs:[0x14]" \
     70 modify exact [eax] \
     71 value [eax]
     72#endif
     73
    6274/*
    6375 * Round x up to the nearest multiple of n.
     
    6678
    6779/*
    68  * struct Header - this structure is used 
    69  * internally by Fortify to manage it's 
     80 * struct Header - this structure is used
     81 * internally by Fortify to manage it's
    7082 * own private lists of memory.
    7183 */
     
    8395    struct Header *Prev;        /* Previous link                     */
    8496    struct Header *Next;        /* Next link                         */
    85     char          *Label;               /* User's Label (may be null)        */
     97    char          *Label;       /* User's Label (may be null)        */
    8698    unsigned char  Scope;       /* Scope level of the caller         */
    8799    unsigned char  Allocator;   /* malloc/realloc/new/etc            */
     100#   ifdef MT_SCOPES
     101    unsigned short Ordinal;     /* TID ordinal of caller             */
     102#   endif
    88103};
    89104
     
    99114 */
    100115#define FORTIFY_ALIGNED_BEFORE_SIZE ( \
    101                    ROUND_UP(FORTIFY_HEADER_SIZE + FORTIFY_BEFORE_SIZE, FORTIFY_ALIGNMENT) \
    102                    - FORTIFY_HEADER_SIZE)
     116                   ROUND_UP(FORTIFY_HEADER_SIZE + FORTIFY_BEFORE_SIZE, FORTIFY_ALIGNMENT) \
     117                   - FORTIFY_HEADER_SIZE)
    103118
    104119/*
     
    107122 */
    108123#define FORTIFY_OVERHEAD ( FORTIFY_HEADER_SIZE \
    109                         + FORTIFY_ALIGNED_BEFORE_SIZE \
    110                          + FORTIFY_AFTER_SIZE)
     124                        + FORTIFY_ALIGNED_BEFORE_SIZE \
     125                         + FORTIFY_AFTER_SIZE)
    111126
    112127
     
    151166 *
    152167 */
    153 static struct Header *st_AllocatedHead   = 0;
    154 static int  st_AllocateFailRate = 0;
     168static struct Header *st_AllocatedHead;
     169static int  st_AllocateFailRate;
    155170static char st_Buffer[256];
    156171static Fortify_OutputFuncPtr st_Output    = st_DefaultOutput;
    157172static const char    *st_LastVerifiedFile = "unknown";
    158 static unsigned long  st_LastVerifiedLine = 0;
     173static unsigned long  st_LastVerifiedLine;
     174#ifdef MT_SCOPES
     175static unsigned volatile st_cScopes;
     176static unsigned volatile char*  st_pScopes;
     177#else
    159178static unsigned char  st_Scope            = 0;
     179#endif
    160180static unsigned char  st_Disabled = 0;
    161181
     
    183203#endif
    184204
    185  
     205
    186206/* allocators */
    187207static const char *st_AllocatorName[] =
     
    231251    int another_try;
    232252
    233     /*
     253#ifdef MT_SCOPES
     254    unsigned short ordinal;
     255#endif
     256
     257    /*
    234258     * If Fortify has been disabled, then it's easy
    235259     */
     
    237261    {
    238262#ifdef FORTIFY_FAIL_ON_ZERO_MALLOC
    239         if(size == 0 && (allocator == Fortify_Allocator_new
    240                       || allocator == Fortify_Allocator_array_new))
     263        if(size == 0 && (allocator == Fortify_Allocator_new
     264                      || allocator == Fortify_Allocator_array_new))
    241265                {
    242266                        /*
    243                          * A new of zero bytes must succeed, but a malloc of 
    244              * zero bytes probably won't
    245              */
     267                         * A new of zero bytes must succeed, but a malloc of
     268             * zero bytes probably won't
     269             */
    246270                        return malloc(1);
    247271                }
    248272#endif
    249273
    250         return malloc(size);
     274        return malloc(size);
    251275    }
    252276
    253277#ifdef FORTIFY_CHECK_ALL_MEMORY_ON_ALLOCATE
    254278    Fortify_CheckAllMemory(file, line);
    255 #endif 
     279#endif
    256280
    257281    if(st_AllocateFailRate > 0)
    258282    {
    259         if(rand() % 100 < st_AllocateFailRate)
    260         {
     283        if(rand() % 100 < st_AllocateFailRate)
     284        {
    261285#ifdef FORTIFY_WARN_ON_FALSE_FAIL
    262             sprintf(st_Buffer,
    263                     "\nFortify: A \"%s\" of %lu bytes \"false failed\" at %s.%lu\n",
    264                     st_AllocatorName[allocator], (unsigned long)size, file, line);
    265             st_Output(st_Buffer);
    266 #endif
    267             return(0);
    268         }
    269     }
    270 
    271     /* Check to see if this allocation will 
     286            sprintf(st_Buffer,
     287                    "\nFortify: A \"%s\" of %lu bytes \"false failed\" at %s.%lu\n",
     288                    st_AllocatorName[allocator], (unsigned long)size, file, line);
     289            st_Output(st_Buffer);
     290#endif
     291            return(0);
     292        }
     293    }
     294
     295    /* Check to see if this allocation will
    272296     * push us over the artificial limit
    273297     */
     
    275299    {
    276300#ifdef FORTIFY_WARN_ON_FALSE_FAIL
    277         sprintf(st_Buffer,
    278                 "\nFortify: A \"%s\" of %lu bytes \"false failed\" at %s.%lu\n",
    279                 st_AllocatorName[allocator], (unsigned long)size, file, line);
    280         st_Output(st_Buffer);
    281 #endif
    282         return(0);
     301        sprintf(st_Buffer,
     302                "\nFortify: A \"%s\" of %lu bytes \"false failed\" at %s.%lu\n",
     303                st_AllocatorName[allocator], (unsigned long)size, file, line);
     304        st_Output(st_Buffer);
     305#endif
     306        return(0);
    283307    }
    284308
    285309#ifdef FORTIFY_WARN_ON_ZERO_MALLOC
    286310        if(size == 0 && (allocator == Fortify_Allocator_malloc ||
    287                      allocator == Fortify_Allocator_calloc ||
    288                      allocator == Fortify_Allocator_realloc ))
    289         {                     
    290                 sprintf(st_Buffer, 
    291                         "\nFortify: A \"%s\" of 0 bytes attempted at %s.%lu\n",
    292                 st_AllocatorName[allocator], file, line);
    293         st_Output(st_Buffer);
     311                     allocator == Fortify_Allocator_calloc ||
     312                     allocator == Fortify_Allocator_realloc ))
     313        {
     314                sprintf(st_Buffer,
     315                "\nFortify: A \"%s\" of 0 bytes attempted at %s.%lu\n",
     316                st_AllocatorName[allocator], file, line);
     317        st_Output(st_Buffer);
    294318        }
    295319#endif /* FORTIFY_WARN_ON_ZERO_MALLOC */
     
    297321#ifdef FORTIFY_FAIL_ON_ZERO_MALLOC
    298322        if(size == 0 && (allocator == Fortify_Allocator_malloc ||
    299                      allocator == Fortify_Allocator_calloc ||
    300                      allocator == Fortify_Allocator_realloc ))
    301         {                     
     323                     allocator == Fortify_Allocator_calloc ||
     324                     allocator == Fortify_Allocator_realloc ))
     325        {
    302326#ifdef FORTIFY_WARN_ON_ALLOCATE_FAIL
    303         sprintf(st_Buffer, "\nFortify: A \"%s\" of %lu bytes failed at %s.%lu\n",
    304                 st_AllocatorName[allocator], (unsigned long)size, file, line);
    305         st_Output(st_Buffer);
     327        sprintf(st_Buffer, "\nFortify: A \"%s\" of %lu bytes failed at %s.%lu\n",
     328                st_AllocatorName[allocator], (unsigned long)size, file, line);
     329        st_Output(st_Buffer);
    306330#endif /* FORTIFY_WARN_ON_ALLOCATE_FAIL */
    307331                return 0;
    308332        }
    309 #endif /* FORTIFY_FAIL_ON_ZERO_MALLOC */       
     333#endif /* FORTIFY_FAIL_ON_ZERO_MALLOC */
    310334
    311335#ifdef FORTIFY_WARN_ON_SIZE_T_OVERFLOW
     
    316340     */
    317341    {
    318         size_t private_size = FORTIFY_HEADER_SIZE
    319                             + FORTIFY_ALIGNED_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE;
    320 
    321         if(private_size < size)
    322         {
    323             sprintf(st_Buffer,
    324                     "\nFortify: A \"%s\" of %lu bytes has overflowed size_t at %s.%lu\n",
    325                     st_AllocatorName[allocator], (unsigned long)size, file, line);
    326             st_Output(st_Buffer);
    327             return(0);
    328         }                             
    329     }
    330 #endif                             
     342        size_t private_size = FORTIFY_HEADER_SIZE
     343                            + FORTIFY_ALIGNED_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE;
     344
     345        if(private_size < size)
     346        {
     347            sprintf(st_Buffer,
     348                    "\nFortify: A \"%s\" of %lu bytes has overflowed size_t at %s.%lu\n",
     349                    st_AllocatorName[allocator], (unsigned long)size, file, line);
     350            st_Output(st_Buffer);
     351            return(0);
     352        }
     353    }
     354#endif
    331355
    332356    another_try = 1;
    333357    do
    334358    {
    335         /*
    336          * malloc the memory, including the space
    337         * for the header and fortification buffers
    338          */ 
    339         ptr = (unsigned char *)malloc(  FORTIFY_HEADER_SIZE
    340                                       + FORTIFY_ALIGNED_BEFORE_SIZE
    341                                       + size
    342                                       + FORTIFY_AFTER_SIZE );
     359        /*
     360         * malloc the memory, including the space
     361        * for the header and fortification buffers
     362         */
     363        ptr = (unsigned char *)malloc(  FORTIFY_HEADER_SIZE
     364                                      + FORTIFY_ALIGNED_BEFORE_SIZE
     365                                      + size
     366                                      + FORTIFY_AFTER_SIZE );
    343367
    344368#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    345         /*
    346         * If we're tracking deallocated memory, then
    347         * we can free some of it, rather than let
    348         * this malloc fail
    349         */
    350         if(!ptr)
    351         {
    352             another_try = st_PurgeDeallocatedBlocks(size, file, line);
    353         }
     369        /*
     370        * If we're tracking deallocated memory, then
     371        * we can free some of it, rather than let
     372        * this malloc fail
     373        */
     374        if(!ptr)
     375        {
     376            another_try = st_PurgeDeallocatedBlocks(size, file, line);
     377        }
    354378#endif /* FORTIFY_TRACK_DEALLOCATED_MEMORY */
    355379
     
    360384    {
    361385#ifdef FORTIFY_WARN_ON_ALLOCATE_FAIL
    362         sprintf(st_Buffer, "\nFortify: A \"%s\" of %lu bytes failed at %s.%lu\n",
    363                 st_AllocatorName[allocator], (unsigned long)size, file, line);
    364         st_Output(st_Buffer);
    365 #endif
    366         return(0);       
     386        sprintf(st_Buffer, "\nFortify: A \"%s\" of %lu bytes failed at %s.%lu\n",
     387                st_AllocatorName[allocator], (unsigned long)size, file, line);
     388        st_Output(st_Buffer);
     389#endif
     390        return(0);
    367391    }
    368392
     
    373397
    374398
    375     /* 
     399    /*
    376400     * Make the head's prev pointer point to us
    377401     * ('cos we're about to become the head)
     
    379403    if(st_AllocatedHead)
    380404    {
    381         st_CheckBlock(st_AllocatedHead, file, line);
    382         /* what should we do if this fails? (apart from panic) */
    383 
    384         st_AllocatedHead->Prev = (struct Header *)ptr;
    385         st_MakeHeaderValid(st_AllocatedHead); 
    386     }
     405        st_CheckBlock(st_AllocatedHead, file, line);
     406        /* what should we do if this fails? (apart from panic) */
     407
     408        st_AllocatedHead->Prev = (struct Header *)ptr;
     409        st_MakeHeaderValid(st_AllocatedHead);
     410    }
     411
     412#   ifdef MT_SCOPES
     413    ordinal = Get_TID_Ordinal();
     414#   endif
    387415
    388416    /*
     
    392420    h->Size      = size;
    393421    h->File      = file;
    394     h->Line      = line; 
     422    h->Line      = line;
    395423    h->Next      = st_AllocatedHead;
    396424    h->Prev      = 0;
     425#   ifdef MT_SCOPES
     426    h->Scope     = ordinal < st_cScopes ? st_pScopes[ordinal] : 0;
     427    h->Ordinal = ordinal;
     428#   else
    397429    h->Scope     = st_Scope;
     430#   endif
    398431    h->Allocator = allocator;
    399432    h->Label     = 0;
     
    405438    st_MakeHeaderValid(h);
    406439    st_AllocatedHead = h;
    407  
     440
    408441    /*
    409442     * Initialize the fortifications
    410      */   
     443     */
    411444    st_SetFortification(ptr + FORTIFY_HEADER_SIZE,
    412                      FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
     445                     FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
    413446    st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + size,
    414                      FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
     447                     FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
    415448
    416449#ifdef FORTIFY_FILL_ON_ALLOCATE
     
    419452     */
    420453    st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    421                         FORTIFY_FILL_ON_ALLOCATE_VALUE, size);
     454                        FORTIFY_FILL_ON_ALLOCATE_VALUE, size);
    422455#endif
    423456
     
    436469    st_CurAllocation += size;
    437470    if(st_CurBlocks > st_MaxBlocks)
    438         st_MaxBlocks = st_CurBlocks;
     471        st_MaxBlocks = st_CurBlocks;
    439472    if(st_CurAllocation > st_MaxAllocation)
    440         st_MaxAllocation = st_CurAllocation;
     473        st_MaxAllocation = st_CurAllocation;
    441474
    442475    /*
     
    456489{
    457490    unsigned char *ptr = (unsigned char *)uptr
    458                         - FORTIFY_HEADER_SIZE
    459                         - FORTIFY_ALIGNED_BEFORE_SIZE;
     491                        - FORTIFY_HEADER_SIZE
     492                        - FORTIFY_ALIGNED_BEFORE_SIZE;
    460493    struct Header *h   = (struct Header *)ptr;
     494
     495#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
     496#ifdef MT_SCOPES
     497    unsigned ordinal = Get_TID_Ordinal();
     498#endif
     499#endif
    461500
    462501#ifdef FORTIFY_CHECK_ALL_MEMORY_ON_DEALLOCATE
     
    470509    if(st_Disabled)
    471510    {
    472         /* there is a possibility that this memory
    473         * block was allocated when Fortify was
    474         * enabled, so we must check the Allocated
    475         * list before we free it.
    476         */
    477         if(!st_IsOnAllocatedList(h))
    478         {
    479             free(uptr);   
    480             return;
    481         }   
    482         else
    483         {
    484             /* the block was allocated by Fortify, so we
    485              * gotta free it differently.
    486              */
    487             /*
    488              * Begin critical region
    489              */
    490             FORTIFY_LOCK();
    491        
    492             /*
    493              * Remove the block from the list
    494              */
    495             if(h->Prev)
    496                 h->Prev->Next = h->Next;
    497             else
    498                 st_AllocatedHead = h->Next;
    499        
    500             if(h->Next)
    501                 h->Next->Prev = h->Prev;
    502        
    503             /*
    504              * End Critical Region
    505              */
    506             FORTIFY_UNLOCK();
    507 
    508             /*
    509              * actually free the memory
    510              */
    511             free(ptr);
    512             return;
    513         }
     511        /* there is a possibility that this memory
     512        * block was allocated when Fortify was
     513        * enabled, so we must check the Allocated
     514        * list before we free it.
     515        */
     516        if(!st_IsOnAllocatedList(h))
     517        {
     518            free(uptr);
     519            return;
     520        }
     521        else
     522        {
     523            /* the block was allocated by Fortify, so we
     524             * gotta free it differently.
     525             */
     526            /*
     527             * Begin critical region
     528             */
     529            FORTIFY_LOCK();
     530
     531            /*
     532             * Remove the block from the list
     533             */
     534            if(h->Prev)
     535                h->Prev->Next = h->Next;
     536            else
     537                st_AllocatedHead = h->Next;
     538
     539            if(h->Next)
     540                h->Next->Prev = h->Prev;
     541
     542            /*
     543             * End Critical Region
     544             */
     545            FORTIFY_UNLOCK();
     546
     547            /*
     548             * actually free the memory
     549             */
     550            free(ptr);
     551            return;
     552        }
    514553    }
    515554
     
    519558    {
    520559#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    521         if(st_IsOnDeallocatedList(h))
    522         {
    523             sprintf(st_Buffer, "\nFortify: \"%s\" twice of %s detected at %s.%lu\n",
    524                                 st_DeallocatorName[deallocator],
    525                                 st_MemoryBlockString(h), file, line);
    526             st_Output(st_Buffer);
    527 
    528             sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    529                                 st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    530             st_Output(st_Buffer);
    531             st_OutputDeleteTrace();
    532             return;
    533         }
     560        if(st_IsOnDeallocatedList(h))
     561        {
     562            sprintf(st_Buffer, "\nFortify: \"%s\" twice of %s detected at %s.%lu\n",
     563                                st_DeallocatorName[deallocator],
     564                                st_MemoryBlockString(h), file, line);
     565            st_Output(st_Buffer);
     566
     567            sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     568                                st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     569            st_Output(st_Buffer);
     570            st_OutputDeleteTrace();
     571            return;
     572        }
    534573#endif /* FORTIFY_TRACK_DEALLOCATED_MEMORY */
    535574
    536575#ifdef FORTIFY_NO_PERCENT_P
    537         sprintf(st_Buffer, "\nFortify: Possible \"%s\" twice of (0x%08lx) was detected at %s.%lu\n",
     576        sprintf(st_Buffer, "\nFortify: Possible \"%s\" twice of (0x%08lx) was detected at %s.%lu\n",
    538577#else
    539         sprintf(st_Buffer, "\nFortify: Possible \"%s\" twice of (%p) was detected at %s.%lu\n",
    540 #endif
    541                             st_DeallocatorName[deallocator],
    542                             uptr, file, line);
    543         st_Output(st_Buffer);
    544         st_OutputDeleteTrace();
    545         return;
    546     }
    547 #endif /* FORTIFY_PARANOID_DELETE */
     578        sprintf(st_Buffer, "\nFortify: Possible \"%s\" twice of (%p) was detected at %s.%lu\n",
     579#endif
     580                            st_DeallocatorName[deallocator],
     581                            uptr, file, line);
     582        st_Output(st_Buffer);
     583        st_OutputDeleteTrace();
     584        return;
     585    }
     586#endif /* FORTIFY_PARANOID_DEALLOCATE */
    548587
    549588    /*
     
    555594    if(!st_CheckBlock(h, file, line))
    556595    {
    557         st_OutputDeleteTrace();
    558         return;
     596        st_OutputDeleteTrace();
     597        return;
    559598    }
    560599
     
    562601    /*
    563602     * Make sure the block hasn't been freed already
    564      * (we can get to here if FORTIFY_PARANOID_DELETE
     603     * (we can get to here if FORTIFY_PARANOID_DEALLOCATE
    565604     * is off, but FORTIFY_TRACK_DEALLOCATED_MEMORY
    566605     * is on).
     
    568607    if(h->Deallocator != Fortify_Deallocator_nobody)
    569608    {
    570         sprintf(st_Buffer, "\nFortify: \"%s\" twice of %s detected at %s.%lu\n",
    571                               st_DeallocatorName[deallocator],
    572                             st_MemoryBlockString(h), file, line);
    573         st_Output(st_Buffer);
    574 
    575         sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    576                             st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    577         st_Output(st_Buffer);
    578         st_OutputDeleteTrace();
    579         return;
     609        sprintf(st_Buffer, "\nFortify: \"%s\" twice of %s detected at %s.%lu\n",
     610                              st_DeallocatorName[deallocator],
     611                            st_MemoryBlockString(h), file, line);
     612        st_Output(st_Buffer);
     613
     614        sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     615                            st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     616        st_Output(st_Buffer);
     617        st_OutputDeleteTrace();
     618        return;
    580619    }
    581620#endif /* FORTIFY_TRACK_DEALLOCATED_MEMORY */
     
    587626    if((st_ValidDeallocator[h->Allocator] & (1<<deallocator)) == 0)
    588627    {
    589         sprintf(st_Buffer, "\nFortify: Incorrect deallocator \"%s\" detected at %s.%lu\n",
    590                               st_DeallocatorName[deallocator], file, line);
    591         st_Output(st_Buffer);
    592         sprintf(st_Buffer,   "         %s was allocated with \"%s\"\n",
    593                               st_MemoryBlockString(h), st_AllocatorName[h->Allocator]);
    594         st_Output(st_Buffer);
    595         st_OutputDeleteTrace();
     628        sprintf(st_Buffer, "\nFortify: Incorrect deallocator \"%s\" detected at %s.%lu\n",
     629                              st_DeallocatorName[deallocator], file, line);
     630        st_Output(st_Buffer);
     631        sprintf(st_Buffer,   "         %s was allocated with \"%s\"\n",
     632                              st_MemoryBlockString(h), st_AllocatorName[h->Allocator]);
     633        st_Output(st_Buffer);
     634        st_OutputDeleteTrace();
    596635    }
    597636
     
    606645    if(h->Prev)
    607646    {
    608         if(!st_CheckBlock(h->Prev, file, line))
    609         {
    610             FORTIFY_UNLOCK();
    611             st_OutputDeleteTrace();
    612             return;
    613         }
    614 
    615         h->Prev->Next = h->Next;
    616         st_MakeHeaderValid(h->Prev);
     647        if(!st_CheckBlock(h->Prev, file, line))
     648        {
     649            FORTIFY_UNLOCK();
     650            st_OutputDeleteTrace();
     651            return;
     652        }
     653
     654        h->Prev->Next = h->Next;
     655        st_MakeHeaderValid(h->Prev);
    617656    }
    618657    else
    619         st_AllocatedHead = h->Next;
     658        st_AllocatedHead = h->Next;
    620659
    621660    if(h->Next)
    622661    {
    623         if(!st_CheckBlock(h->Next, file, line))
    624         {
    625             FORTIFY_UNLOCK();
    626             st_OutputDeleteTrace();
    627             return;
    628         }
    629 
    630         h->Next->Prev = h->Prev;
    631         st_MakeHeaderValid(h->Next);
     662        if(!st_CheckBlock(h->Next, file, line))
     663        {
     664            FORTIFY_UNLOCK();
     665            st_OutputDeleteTrace();
     666            return;
     667        }
     668
     669        h->Next->Prev = h->Prev;
     670        st_MakeHeaderValid(h->Next);
    632671    }
    633672
     
    644683    st_CurAllocation -= h->Size;
    645684
    646 
    647685#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
     686#ifdef MT_SCOPES
     687    ordinal = Get_TID_Ordinal();
     688    if(ordinal < st_cScopes && st_pScopes[ordinal] > 0)
     689#else
    648690    if(st_Scope > 0)
    649     {
    650         /*
    651          * Don't _actually_ free the memory block, just yet.
    652          * Place it onto the deallocated list, instead, so
    653          * we can check later to see if it's been written to.
    654          */
     691#endif
     692    {
     693        /*
     694         * Don't _actually_ free the memory block, just yet.
     695         * Place it onto the deallocated list, instead, so
     696         * we can check later to see if it's been written to.
     697         */
    655698    #ifdef FORTIFY_FILL_ON_DEALLOCATE
    656         /*
    657         * Nuke out all user memory that is about to be freed
    658         */
    659         st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    660                                     FORTIFY_FILL_ON_DEALLOCATE_VALUE,
    661                                   h->Size);
     699        /*
     700        * Nuke out all user memory that is about to be freed
     701        */
     702        st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     703                                    FORTIFY_FILL_ON_DEALLOCATE_VALUE,
     704                                  h->Size);
    662705    #endif /* FORTIFY_FILL_ON_DEALLOCATE */
    663706
    664         /*
    665          * Begin critical region
    666          */
    667         FORTIFY_LOCK();
    668 
    669         /*
    670          * Place the block on the deallocated list
    671          */
    672         if(st_DeallocatedHead)
    673         {
    674             st_DeallocatedHead->Prev = (struct Header *)ptr;
    675             st_MakeHeaderValid(st_DeallocatedHead);
    676         }
    677 
    678         h = (struct Header *)ptr;
    679         h->FreedFile   = file;
    680         h->FreedLine   = line;
    681         h->Deallocator = deallocator;
    682         h->Next        = st_DeallocatedHead;
    683         h->Prev        = 0;
    684         st_MakeHeaderValid(h);
    685         st_DeallocatedHead = h;
    686 
    687         if(!st_DeallocatedTail)
    688         {
    689             st_DeallocatedTail = h;
    690         }
    691 
    692         st_TotalDeallocated += h->Size;
     707        /*
     708         * Begin critical region
     709         */
     710        FORTIFY_LOCK();
     711
     712        /*
     713         * Place the block on the deallocated list
     714         */
     715        if(st_DeallocatedHead)
     716        {
     717            st_DeallocatedHead->Prev = (struct Header *)ptr;
     718            st_MakeHeaderValid(st_DeallocatedHead);
     719        }
     720
     721        h = (struct Header *)ptr;
     722        h->FreedFile   = file;
     723        h->FreedLine   = line;
     724        h->Deallocator = deallocator;
     725        h->Next        = st_DeallocatedHead;
     726        h->Prev        = 0;
     727        st_MakeHeaderValid(h);
     728        st_DeallocatedHead = h;
     729
     730        if(!st_DeallocatedTail)
     731            st_DeallocatedTail = h;
     732
     733        st_TotalDeallocated += h->Size;
    693734
    694735    #ifdef FORTIFY_DEALLOCATED_MEMORY_LIMIT
    695         /*
    696         * If we've got too much on the deallocated list; free some
    697         */
    698         if(st_TotalDeallocated > FORTIFY_DEALLOCATED_MEMORY_LIMIT)
    699         {
    700              st_PurgeDeallocatedBlocks(st_TotalDeallocated - FORTIFY_DEALLOCATED_MEMORY_LIMIT, file, line);
    701         }
     736        /*
     737        * If we've got too much on the deallocated list; free some
     738        */
     739        if(st_TotalDeallocated > FORTIFY_DEALLOCATED_MEMORY_LIMIT)
     740        {
     741             st_PurgeDeallocatedBlocks(st_TotalDeallocated - FORTIFY_DEALLOCATED_MEMORY_LIMIT, file, line);
     742        }
    702743    #endif
    703744
    704         /*
    705         * End critical region
    706         */
    707         FORTIFY_UNLOCK();
     745        /*
     746        * End critical region
     747        */
     748        FORTIFY_UNLOCK();
    708749    }
    709750    else
    710751#endif /* FORTIFY_TRACK_DEALLOCATED_MEMORY */
    711752    {
    712                 /*
     753                /*
    713754                 * Free the User Label
    714755                 */
     
    716757                {
    717758                        free(h->Label);
    718                 }                       
     759                }
    719760
    720761#ifdef FORTIFY_FILL_ON_DEALLOCATE
    721         /*
    722         * Nuke out all memory that is about to be freed, including the header
    723         */
    724         st_SetFortification(ptr, FORTIFY_FILL_ON_DEALLOCATE_VALUE,
    725                               FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
     762        /*
     763        * Nuke out all memory that is about to be freed, including the header
     764        */
     765        st_SetFortification(ptr, FORTIFY_FILL_ON_DEALLOCATE_VALUE,
     766                              FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
    726767#endif /* FORTIFY_FILL_ON_DEALLOCATE */
    727768
    728         /*
    729         * And do the actual free
    730         */
    731         free(ptr);
     769        /*
     770        * And do the actual free
     771        */
     772        free(ptr);
    732773    }
    733774}
     
    747788        {
    748789                unsigned char *ptr = (unsigned char *)uptr
    749                               - FORTIFY_HEADER_SIZE - FORTIFY_ALIGNED_BEFORE_SIZE;
     790                              - FORTIFY_HEADER_SIZE - FORTIFY_ALIGNED_BEFORE_SIZE;
    750791                struct Header *h = (struct Header *)ptr;
    751792
    752793                /* make sure the pointer is okay */
    753794                Fortify_CheckPointer(uptr, file, line);
    754        
     795
    755796                /* free the previous label */
    756797                if(h->Label)
    757798                {
    758799                        free(h->Label);
    759                 }       
    760        
     800                }
     801
    761802                /* make sure the label is sensible */
    762803                assert(label);
     
    765806                h->Label = (char*)malloc(strlen(label)+1);
    766807                strcpy(h->Label, label);
    767                
     808
    768809                /* update the checksum */
    769810                st_MakeHeaderValid(h);
     
    782823{
    783824    unsigned char *ptr = (unsigned char *)uptr
    784                               - FORTIFY_HEADER_SIZE - FORTIFY_ALIGNED_BEFORE_SIZE;
     825                              - FORTIFY_HEADER_SIZE - FORTIFY_ALIGNED_BEFORE_SIZE;
    785826    struct Header *h = (struct Header *)ptr;
    786827    int r;
    787828
    788829    if(st_Disabled)
    789         return 1;
     830        return 1;
    790831
    791832    FORTIFY_LOCK();
     
    794835    {
    795836#ifdef FORTIFY_NO_PERCENT_P
    796         sprintf(st_Buffer, "\nFortify: Invalid pointer (0x%08lx) detected at %s.%lu\n",
     837        sprintf(st_Buffer, "\nFortify: Invalid pointer (0x%08lx) detected at %s.%lu\n",
    797838#else
    798         sprintf(st_Buffer, "\nFortify: Invalid pointer (%p) detected at %s.%lu\n",
    799 #endif
    800                                 uptr, file, line);
    801         st_Output(st_Buffer);
    802         FORTIFY_UNLOCK();
    803         return(0);
     839        sprintf(st_Buffer, "\nFortify: Invalid pointer (%p) detected at %s.%lu\n",
     840#endif
     841                                uptr, file, line);
     842        st_Output(st_Buffer);
     843        FORTIFY_UNLOCK();
     844        return(0);
    804845    }
    805846
     
    808849    {
    809850#ifdef FORTIFY_NO_PERCENT_P
    810         sprintf(st_Buffer, "\nFortify: Deallocated pointer (0x%08lx) detected at %s.%lu\n",
     851        sprintf(st_Buffer, "\nFortify: Deallocated pointer (0x%08lx) detected at %s.%lu\n",
    811852#else
    812         sprintf(st_Buffer, "\nFortify: Deallocated pointer (%p) detected at %s.%lu\n",
    813 #endif
    814                            uptr, file, line);
    815         st_Output(st_Buffer);
    816         sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    817                            st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    818         st_Output(st_Buffer);
    819         FORTIFY_UNLOCK();
    820         return(0);
     853        sprintf(st_Buffer, "\nFortify: Deallocated pointer (%p) detected at %s.%lu\n",
     854#endif
     855                           uptr, file, line);
     856        st_Output(st_Buffer);
     857        sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     858                           st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     859        st_Output(st_Buffer);
     860        FORTIFY_UNLOCK();
     861        return(0);
    821862    }
    822863#endif
     
    830871 * Fortify_SetOutputFunc(Fortify_OutputFuncPtr Output) -
    831872 * Sets the function used to output all error and
    832  * diagnostic messages. The output function  takes 
    833  * a single const unsigned char * argument, and must be 
    834  * able to handle newlines. This function returns the 
     873 * diagnostic messages. The output function  takes
     874 * a single const unsigned char * argument, and must be
     875 * able to handle newlines. This function returns the
    835876 * old output function.
    836877 */
     
    841882
    842883    st_Output = Output;
    843  
     884
    844885    return(Old);
    845886}
    846887
    847888/*
    848  * Fortify_SetAllocateFailRate(int Percent) - 
    849  * Fortify_Allocate() will "fail" this Percent of 
    850  * the time, even if the memory is available. 
    851  * Useful to "stress-test" an application. 
     889 * Fortify_SetAllocateFailRate(int Percent) -
     890 * Fortify_Allocate() will "fail" this Percent of
     891 * the time, even if the memory is available.
     892 * Useful to "stress-test" an application.
    852893 * Returns the old value.
    853894 * The fail rate defaults to 0 (a good default I think).
     
    857898{
    858899    int Old = st_AllocateFailRate;
    859  
     900
    860901    st_AllocateFailRate = Percent;
    861  
     902
    862903    return(Old);
    863904}
    864905
    865  
     906
    866907/*
    867908 * Fortify_CheckAllMemory() - Checks the fortifications
    868  * of all memory on the allocated list. And, if 
     909 * of all memory on the allocated list. And, if
    869910 * FORTIFY_DEALLOCATED_MEMORY is enabled, all the
    870911 * known deallocated memory as well.
    871  * Returns the number of blocks that failed. 
     912 * Returns the number of blocks that failed.
    872913 * Always returns 0 if Fortify is disabled.
    873914 */
     
    879920
    880921    if(st_Disabled)
    881         return 0;
     922        return 0;
    882923
    883924    FORTIFY_LOCK();
     
    885926    /*
    886927     * Check the allocated memory
    887      */ 
     928     */
    888929    while(curr)
    889930    {
    890         if(!st_CheckBlock(curr, file, line))
    891             count++;
    892 
    893         curr = curr->Next;     
     931        if(!st_CheckBlock(curr, file, line))
     932            count++;
     933
     934        curr = curr->Next;
    894935    }
    895936
     
    901942    while(curr)
    902943    {
    903         if(!st_CheckDeallocatedBlock(curr, file, line))
    904             count++;
    905 
    906         curr = curr->Next;     
    907     }
    908 #endif   
    909 
    910     /* 
     944        if(!st_CheckDeallocatedBlock(curr, file, line))
     945            count++;
     946
     947        curr = curr->Next;
     948    }
     949#endif
     950
     951    /*
    911952     * If we know where we are, and everything is cool,
    912953     * remember that. It might be important.
     
    914955    if(file && count == 0)
    915956    {
    916         st_LastVerifiedFile = file;
    917         st_LastVerifiedLine = line;
     957        st_LastVerifiedFile = file;
     958        st_LastVerifiedLine = line;
    918959    }
    919960
     
    924965
    925966/*
    926  * Fortify_EnterScope() - enters a new Fortify scope 
     967 * Fortify_EnterScope() - enters a new Fortify scope
    927968 * level. Returns the new scope level.
    928969 */
     
    930971Fortify_EnterScope(const char *file, unsigned long line)
    931972{
     973#ifdef MT_SCOPES
     974    unsigned ordinal = Get_TID_Ordinal();
     975
     976    if (ordinal >= st_cScopes) {
     977        st_pScopes = realloc((void*)st_pScopes, sizeof(*st_pScopes) * (st_cScopes = (ordinal + 1)));
     978        st_pScopes[ordinal] = 0;
     979    }
     980    return(++st_pScopes[ordinal]);
     981#else
    932982    return(++st_Scope);
     983#endif
    933984}
    934985
    935986/* Fortify_LeaveScope - leaves a Fortify scope level,
    936  * also prints a memory dump of all non-freed memory 
     987 * also prints a memory dump of all non-freed memory
    937988 * that was allocated during the scope being exited.
    938989 * Does nothing and returns 0 if Fortify is disabled.
     
    943994    struct Header *curr = st_AllocatedHead;
    944995    unsigned long size = 0, count = 0;
     996#ifdef MT_SCOPES
     997    unsigned ordinal;
     998#endif
    945999
    9461000    if(st_Disabled)
    947         return 0;
     1001        return 0;
    9481002
    9491003    FORTIFY_LOCK();
    9501004
    951     st_Scope--;
     1005#ifdef MT_SCOPES
     1006    // 06 May 08 SHL fixme to complain to leave without enter
     1007    ordinal = Get_TID_Ordinal();
     1008    if (ordinal < st_cScopes && st_pScopes[ordinal] > 0)
     1009        st_pScopes[ordinal]--;
     1010    else {
     1011        sprintf(st_Buffer, "\nFortify: attempting to leave scope before enter at %s.%lu\n", file, line);
     1012        st_Output(st_Buffer);
     1013    }
     1014#else
     1015    if (st_Scope > 0)
     1016        st_Scope--;
     1017    else {
     1018        sprintf(st_Buffer, "\nFortify: attempting to leave scope before enter at %s.%lu\n", file, line);
     1019        st_Output(st_Buffer);
     1020    }
     1021#endif
    9521022    while(curr)
    9531023    {
    954         if(curr->Scope > st_Scope)
    955         {
    956             if(count == 0)
    957             {
    958                 sprintf(st_Buffer, "\nFortify: Memory leak detected leaving scope at %s.%lu\n", file, line);
    959                 st_Output(st_Buffer);
    960                 sprintf(st_Buffer, "%10s %8s %s\n", "Address", "Size", "Allocator");
    961                 st_Output(st_Buffer);
    962             }
    963            
    964             st_OutputHeader(curr);
    965             count++;
    966             size += curr->Size;
    967         }
    968 
    969         curr = curr->Next;     
     1024#ifdef MT_SCOPES
     1025        if(curr->Ordinal == ordinal && ordinal < st_cScopes && curr->Scope > st_pScopes[ordinal])
     1026#else
     1027        if(curr->Scope > st_Scope)
     1028#endif
     1029        {
     1030            if(count == 0)
     1031            {
     1032                // Report just first occurrance
     1033                sprintf(st_Buffer, "\nFortify: Memory leak detected leaving scope at %s.%lu\n", file, line);
     1034                st_Output(st_Buffer);
     1035                sprintf(st_Buffer, "%10s %8s %s\n", "Address", "Size", "Allocator");
     1036                st_Output(st_Buffer);
     1037            }
     1038
     1039            st_OutputHeader(curr);
     1040            count++;
     1041            size += curr->Size;
     1042        }
     1043
     1044        curr = curr->Next;
    9701045    }
    9711046
    9721047    if(count)
    9731048    {
    974         sprintf(st_Buffer,"%10s %8lu bytes in %lu blocks with %lu bytes overhead\n",
    975                 "total", size, count, count * FORTIFY_OVERHEAD);
    976         st_Output(st_Buffer);
     1049        sprintf(st_Buffer,"%10s %8lu bytes in %lu blocks with %lu bytes overhead\n",
     1050                "total", size, count, count * FORTIFY_OVERHEAD);
     1051        st_Output(st_Buffer);
    9771052    }
    9781053
    9791054#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    980     /* 
    981      * Quietly free all the deallocated memory 
    982      * that was allocated in this scope that 
     1055    /*
     1056     * Quietly free all the deallocated memory
     1057     * that was allocated in this scope that
    9831058     * we are still tracking
    9841059     */
     1060#ifdef MT_SCOPES
     1061    st_PurgeDeallocatedScope( ordinal < st_cScopes ? st_pScopes[ordinal] : 0,
     1062                              file, line );
     1063#else
    9851064    st_PurgeDeallocatedScope( st_Scope, file, line );
     1065#endif
    9861066#endif /* FORTIFY_TRACK_DEALLOCATED_MEMORY */
    9871067
    9881068    FORTIFY_UNLOCK();
     1069#ifdef MT_SCOPES
     1070    return(ordinal < st_cScopes ? st_pScopes[ordinal] : 0);
     1071#else
    9891072    return(st_Scope);
    990 }
    991 
    992 /*
    993  * Fortify_ListAllMemory() - Outputs the entire
    994  * list of currently allocated memory. For each block
    995  * is output it's Address, Size, and the SourceFile and
     1073#endif
     1074}
     1075
     1076/*
     1077 * Fortify_ListAllMemory() - Outputs the entire
     1078 * list of currently allocated memory. For each block
     1079 * is output it's Address, Size, and the SourceFile and
    9961080 * Line that allocated it.
    9971081 *
    998  * If there is no memory on the list, this function 
     1082 * If there is no memory on the list, this function
    9991083 * outputs nothing.
    10001084 *
    1001  * It returns the number of blocks on the list, unless 
    1002  * Fortify has been disabled, in which case it always 
     1085 * It returns the number of blocks on the list, unless
     1086 * Fortify has been disabled, in which case it always
    10031087 * returns 0.
    10041088 */
     
    10101094
    10111095    if(st_Disabled)
    1012         return 0;
     1096        return 0;
    10131097
    10141098    Fortify_CheckAllMemory(file, line);
     
    10181102    if(curr)
    10191103    {
    1020         sprintf(st_Buffer, "\nFortify: Memory List at %s.%lu\n", file, line);
    1021         st_Output(st_Buffer);
    1022         sprintf(st_Buffer, "%10s %8s %s\n", "Address", "Size", "Allocator");
    1023         st_Output(st_Buffer);
    1024                                
    1025         while(curr)
    1026         {
    1027             st_OutputHeader(curr);
    1028             count++;
    1029             size += curr->Size;
    1030             curr = curr->Next;     
    1031         }
    1032                      
    1033         sprintf(st_Buffer, "%10s %8lu bytes in %lu blocks and %lu bytes overhead\n",
    1034                            "total", size, count, count * FORTIFY_OVERHEAD);
    1035         st_Output(st_Buffer);
    1036     }
    1037  
     1104        sprintf(st_Buffer, "\nFortify: Memory List at %s.%lu\n", file, line);
     1105        st_Output(st_Buffer);
     1106        sprintf(st_Buffer, "%10s %8s %s\n", "Address", "Size", "Allocator");
     1107        st_Output(st_Buffer);
     1108
     1109        while(curr)
     1110        {
     1111            st_OutputHeader(curr);
     1112            count++;
     1113            size += curr->Size;
     1114            curr = curr->Next;
     1115        }
     1116
     1117        sprintf(st_Buffer, "%10s %8lu bytes in %lu blocks and %lu bytes overhead\n",
     1118                           "total", size, count, count * FORTIFY_OVERHEAD);
     1119        st_Output(st_Buffer);
     1120    }
     1121
    10381122    FORTIFY_UNLOCK();
    10391123    return(count);
     
    10411125
    10421126/*
    1043  * Fortify_DumpAllMemory() - Outputs the entire list of 
    1044  * currently allocated memory. For each allocated block 
     1127 * Fortify_DumpAllMemory() - Outputs the entire list of
     1128 * currently allocated memory. For each allocated block
    10451129 * is output it's Address, Size, the SourceFile and Line
    1046  * that allocated it, a hex dump of the contents of the 
     1130 * that allocated it, a hex dump of the contents of the
    10471131 * memory and an ascii dump of printable characters.
    10481132 *
     
    10561140
    10571141    if(st_Disabled)
    1058         return 0;
     1142        return 0;
    10591143
    10601144    Fortify_CheckAllMemory(file, line);
     
    10641148    while(curr)
    10651149    {
    1066         sprintf(st_Buffer, "\nFortify: Hex Dump of %s at %s.%lu\n",
    1067                 st_MemoryBlockString(curr), file, line);
    1068         st_Output(st_Buffer);
    1069         st_OutputMemory(curr);
    1070         st_Output("\n");
    1071         count++;
    1072 
    1073         curr = curr->Next;
     1150        sprintf(st_Buffer, "\nFortify: Hex Dump of %s at %s.%lu\n",
     1151                st_MemoryBlockString(curr), file, line);
     1152        st_Output(st_Buffer);
     1153        st_OutputMemory(curr);
     1154        st_Output("\n");
     1155        count++;
     1156
     1157        curr = curr->Next;
    10741158    }
    10751159
     
    10781162}
    10791163
    1080 /* Fortify_OutputStatistics() - displays statistics 
    1081  * about the maximum amount of memory that was 
     1164/* Fortify_OutputStatistics() - displays statistics
     1165 * about the maximum amount of memory that was
    10821166 * allocated at any one time.
    10831167 */
     
    10861170{
    10871171    if(st_Disabled)
    1088         return;
     1172        return;
    10891173
    10901174    sprintf(st_Buffer, "\nFortify: Statistics at %s.%lu\n", file, line);
     
    10921176
    10931177    sprintf(st_Buffer, "         Memory currently allocated: %lu bytes in %lu blocks\n",
    1094                                 st_CurAllocation, st_CurBlocks);
     1178                                st_CurAllocation, st_CurBlocks);
    10951179    st_Output(st_Buffer);
    1096     sprintf(st_Buffer, "         Maximum memory allocated at one time: %lu bytes in %lu blocks\n", 
    1097                                 st_MaxAllocation, st_MaxBlocks);
     1180    sprintf(st_Buffer, "         Maximum memory allocated at one time: %lu bytes in %lu blocks\n",
     1181                                st_MaxAllocation, st_MaxBlocks);
    10981182    st_Output(st_Buffer);
    10991183    sprintf(st_Buffer, "         There have been %lu allocations and %lu deallocations\n",
    1100                                 st_Allocations, st_Frees);
     1184                                st_Allocations, st_Frees);
    11011185    st_Output(st_Buffer);
    11021186    sprintf(st_Buffer, "         There was a total of %lu bytes allocated\n",
    1103                                 st_TotalAllocation);
     1187                                st_TotalAllocation);
    11041188    st_Output(st_Buffer);
    1105    
     1189
    11061190    if(st_Allocations > 0)
    11071191    {
    1108         sprintf(st_Buffer, "         The average allocation was %lu bytes\n",
    1109                                      st_TotalAllocation / st_Allocations);
    1110         st_Output(st_Buffer);                   
    1111     }   
     1192        sprintf(st_Buffer, "         The average allocation was %lu bytes\n",
     1193                                     st_TotalAllocation / st_Allocations);
     1194        st_Output(st_Buffer);
     1195    }
    11121196}
    11131197
     
    11191203{
    11201204    if(st_Disabled)
    1121         return 0;
     1205        return 0;
    11221206
    11231207    return st_CurAllocation;
     
    11391223 * The less memory allocated by Fortify when it is disabled
    11401224 * the better.
    1141  * (Previous versions of Fortify did not allow it to be 
     1225 * (Previous versions of Fortify did not allow it to be
    11421226 * disabled if there was any memory allocated at the time,
    11431227 * but since in C++ memory is often allocated before main
     
    11701254    if(!st_IsHeaderValid(h))
    11711255    {
    1172         sprintf(st_Buffer,
     1256        sprintf(st_Buffer,
    11731257#ifdef FORTIFY_NO_PERCENT_P
    1174                 "\nFortify: Invalid pointer (0x%08lx) or corrupted header detected at %s.%lu\n",
     1258                "\nFortify: Invalid pointer (0x%08lx) or corrupted header detected at %s.%lu\n",
    11751259#else
    1176                 "\nFortify: Invalid pointer (%p) or corrupted header detected at %s.%lu\n",
    1177 #endif
    1178                 ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE, file, line);
    1179         st_Output(st_Buffer);
    1180         st_OutputLastVerifiedPoint();
    1181         return(0);
     1260                "\nFortify: Invalid pointer (%p) or corrupted header detected at %s.%lu\n",
     1261#endif
     1262                ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE, file, line);
     1263        st_Output(st_Buffer);
     1264        st_OutputLastVerifiedPoint();
     1265        return(0);
    11821266    }
    11831267
    11841268    if(!st_CheckFortification(ptr + FORTIFY_HEADER_SIZE,
    1185                            FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE))
    1186     {
    1187         sprintf(st_Buffer, "\nFortify: Underwrite detected before block %s at %s.%lu\n",
    1188                            st_MemoryBlockString(h), file, line);
    1189         st_Output(st_Buffer);
    1190 
    1191         st_OutputLastVerifiedPoint();
    1192         st_OutputFortification(ptr + FORTIFY_HEADER_SIZE,
    1193                             FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
    1194         result = 0;                       
     1269                           FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE))
     1270    {
     1271        sprintf(st_Buffer, "\nFortify: Underwrite detected before block %s at %s.%lu\n",
     1272                           st_MemoryBlockString(h), file, line);
     1273        st_Output(st_Buffer);
     1274
     1275        st_OutputLastVerifiedPoint();
     1276        st_OutputFortification(ptr + FORTIFY_HEADER_SIZE,
     1277                            FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
     1278        result = 0;
    11951279
    11961280#ifdef FORTIFY_FILL_ON_CORRUPTION
    1197         st_SetFortification(ptr + FORTIFY_HEADER_SIZE, FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
    1198 #endif
    1199     }
    1200                            
     1281        st_SetFortification(ptr + FORTIFY_HEADER_SIZE, FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
     1282#endif
     1283    }
     1284
    12011285    if(!st_CheckFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1202                            FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))
    1203     {
    1204         sprintf(st_Buffer, "\nFortify: Overwrite detected after block %s at %s.%lu\n",
    1205                            st_MemoryBlockString(h), file, line);
    1206         st_Output(st_Buffer);
    1207 
    1208         st_OutputLastVerifiedPoint();
    1209         st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1210                             FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
    1211         result = 0;
     1286                           FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))
     1287    {
     1288        sprintf(st_Buffer, "\nFortify: Overwrite detected after block %s at %s.%lu\n",
     1289                           st_MemoryBlockString(h), file, line);
     1290        st_Output(st_Buffer);
     1291
     1292        st_OutputLastVerifiedPoint();
     1293        st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
     1294                            FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
     1295        result = 0;
    12121296
    12131297#ifdef FORTIFY_FILL_ON_CORRUPTION
    1214         st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1215                         FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
    1216 #endif
    1217     }
    1218  
    1219     return(result);   
     1298        st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
     1299                        FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
     1300#endif
     1301    }
     1302
     1303    return(result);
    12201304}
    12211305
    12221306#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    12231307
    1224 /* 
     1308/*
    12251309 * st_CheckDeallocatedBlock - Check a deallocated block's header and fortifications.
    12261310 * Returns true if the block is happy.
    12271311 */
    1228 static int 
     1312static int
    12291313st_CheckDeallocatedBlock(struct Header *h, const char *file, unsigned long line)
    12301314{
     
    12341318    if(!st_IsHeaderValid(h))
    12351319    {
    1236         sprintf(st_Buffer,
     1320        sprintf(st_Buffer,
    12371321#ifdef FORTIFY_NO_PERCENT_P
    1238                 "\nFortify: Invalid deallocated pointer (0x%08lx) or corrupted header detected at %s.%lu\n",
     1322                "\nFortify: Invalid deallocated pointer (0x%08lx) or corrupted header detected at %s.%lu\n",
    12391323#else
    1240                 "\nFortify: Invalid deallocated pointer (%p) or corrupted header detected at %s.%lu\n",
    1241 #endif
    1242                 ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE, file, line);
    1243         st_Output(st_Buffer);
    1244         st_OutputLastVerifiedPoint();
    1245         return(0);
     1324                "\nFortify: Invalid deallocated pointer (%p) or corrupted header detected at %s.%lu\n",
     1325#endif
     1326                ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE, file, line);
     1327        st_Output(st_Buffer);
     1328        st_OutputLastVerifiedPoint();
     1329        return(0);
    12461330    }
    12471331
    12481332    if(!st_CheckFortification(ptr + FORTIFY_HEADER_SIZE,
    1249                            FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE))
    1250     {
    1251         sprintf(st_Buffer, "\nFortify: Underwrite detected before deallocated block %s at %s.%lu\n",
    1252                            st_MemoryBlockString(h), file, line);
    1253         st_Output(st_Buffer);
    1254         sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    1255                            st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    1256         st_Output(st_Buffer);
    1257 
    1258         st_OutputLastVerifiedPoint();
    1259         st_OutputFortification(ptr + FORTIFY_HEADER_SIZE,
    1260                             FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
     1333                           FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE))
     1334    {
     1335        sprintf(st_Buffer, "\nFortify: Underwrite detected before deallocated block %s at %s.%lu\n",
     1336                           st_MemoryBlockString(h), file, line);
     1337        st_Output(st_Buffer);
     1338        sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     1339                           st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     1340        st_Output(st_Buffer);
     1341
     1342        st_OutputLastVerifiedPoint();
     1343        st_OutputFortification(ptr + FORTIFY_HEADER_SIZE,
     1344                            FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
    12611345
    12621346#ifdef FORTIFY_FILL_ON_CORRUPTION
    1263         st_SetFortification(ptr + FORTIFY_HEADER_SIZE, FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
    1264 #endif
    1265         result = 0;
    1266     }
    1267                            
     1347        st_SetFortification(ptr + FORTIFY_HEADER_SIZE, FORTIFY_BEFORE_VALUE, FORTIFY_ALIGNED_BEFORE_SIZE);
     1348#endif
     1349        result = 0;
     1350    }
     1351
    12681352    if(!st_CheckFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1269                            FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))
    1270     {
    1271         sprintf(st_Buffer, "\nFortify: Overwrite detected after deallocated block %s at %s.%lu\n",
    1272                            st_MemoryBlockString(h), file, line);
    1273         st_Output(st_Buffer);
    1274         sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    1275                            st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    1276         st_Output(st_Buffer);
    1277 
    1278         st_OutputLastVerifiedPoint();
    1279         st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1280                             FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
     1353                           FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))
     1354    {
     1355        sprintf(st_Buffer, "\nFortify: Overwrite detected after deallocated block %s at %s.%lu\n",
     1356                           st_MemoryBlockString(h), file, line);
     1357        st_Output(st_Buffer);
     1358        sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     1359                           st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     1360        st_Output(st_Buffer);
     1361
     1362        st_OutputLastVerifiedPoint();
     1363        st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
     1364                            FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
    12811365
    12821366#ifdef FORTIFY_FILL_ON_CORRUPTION
    1283         st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
    1284                         FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
    1285 #endif
    1286         result = 0;
     1367        st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size,
     1368                        FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
     1369#endif
     1370        result = 0;
    12871371    }
    12881372
    12891373#ifdef FORTIFY_FILL_ON_DEALLOCATE
    12901374    if(!st_CheckFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1291                            FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size))
    1292     {
    1293         sprintf(st_Buffer, "\nFortify: Write to deallocated block %s detected at %s.%lu\n",
    1294                            st_MemoryBlockString(h), file, line);
    1295         st_Output(st_Buffer);
    1296 
    1297         sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
    1298                            st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    1299         st_Output(st_Buffer);
    1300         st_OutputLastVerifiedPoint();
    1301    
    1302         st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1303                             FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size);
     1375                           FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size))
     1376    {
     1377        sprintf(st_Buffer, "\nFortify: Write to deallocated block %s detected at %s.%lu\n",
     1378                           st_MemoryBlockString(h), file, line);
     1379        st_Output(st_Buffer);
     1380
     1381        sprintf(st_Buffer, "         Memory block was deallocated by \"%s\" at %s.%lu\n",
     1382                           st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     1383        st_Output(st_Buffer);
     1384        st_OutputLastVerifiedPoint();
     1385
     1386        st_OutputFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1387                            FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size);
    13041388
    13051389#ifdef FORTIFY_FILL_ON_CORRUPTION
    1306         st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1307                             FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size);
     1390        st_SetFortification(ptr + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1391                            FORTIFY_FILL_ON_DEALLOCATE_VALUE, h->Size);
    13081392#endif /* FORTIFY_FILL_ON_CORRUPTION */
    1309         result = 0;
     1393        result = 0;
    13101394    }
    13111395#endif /* FORTIFY_FILL_ON_DEALLOCATE */
     
    13171401
    13181402/*
    1319  * st_CheckFortification - Checks if the _size_ 
     1403 * st_CheckFortification - Checks if the _size_
    13201404 * bytes from _ptr_ are all set to _value_
    13211405 * Returns true if all is happy.
    13221406 */
    1323 static int 
     1407static int
    13241408st_CheckFortification(unsigned char *ptr, unsigned char value, size_t size)
    13251409{
    13261410    while(size--)
    1327         if(*ptr++ != value)
    1328             return(0);
    1329      
    1330     return(1);     
     1411        if(*ptr++ != value)
     1412            return(0);
     1413
     1414    return(1);
    13311415}
    13321416
     
    13341418 * st_SetFortification - Set the _size_ bytes from _ptr_ to _value_.
    13351419 */
    1336 static void 
     1420static void
    13371421st_SetFortification(unsigned char *ptr, unsigned char value, size_t size)
    13381422{
     
    13411425
    13421426/*
    1343  * st_OutputFortification - Output the corrupted section of the fortification 
     1427 * st_OutputFortification - Output the corrupted section of the fortification
    13441428 */
    13451429static void
     
    13541438    while(offset < size)
    13551439    {
    1356         /*
    1357         * Skip 3 or more 'correct' lines
    1358         */
    1359         if((size - offset) < 3 * 16)             
    1360             advance = size - offset;
    1361         else
    1362             advance = 3 * 16;
    1363         if(advance > 0 && st_CheckFortification(ptr+offset, value, advance))
    1364         {
    1365             offset += advance;
    1366             skipped = advance;
    1367                  
    1368             if(size - offset < 16)             
    1369                 advance = size - offset;
    1370             else
    1371                 advance = 16;
    1372              
    1373             while(advance > 0 && st_CheckFortification(ptr+offset, value, advance))
    1374             {
    1375                 offset  += advance;
    1376                 skipped += advance;
    1377                 if(size - offset < 16)             
    1378                     advance = size - offset;
    1379                 else
    1380                     advance = 16;
    1381             }   
    1382             sprintf(st_Buffer, "\n                        ...%lu bytes skipped...", (unsigned long)skipped);
    1383             st_Output(st_Buffer);
    1384             continue;
    1385         }
    1386         else
    1387         {
    1388             if(size - offset < 16)
    1389                 st_HexDump(ptr, offset, size-offset, 0);
    1390             else
    1391                 st_HexDump(ptr, offset, 16, 0);
    1392                
    1393             offset += 16;
    1394         }
     1440        /*
     1441        * Skip 3 or more 'correct' lines
     1442        */
     1443        if((size - offset) < 3 * 16)
     1444            advance = size - offset;
     1445        else
     1446            advance = 3 * 16;
     1447        if(advance > 0 && st_CheckFortification(ptr+offset, value, advance))
     1448        {
     1449            offset += advance;
     1450            skipped = advance;
     1451
     1452            if(size - offset < 16)
     1453                advance = size - offset;
     1454            else
     1455                advance = 16;
     1456
     1457            while(advance > 0 && st_CheckFortification(ptr+offset, value, advance))
     1458            {
     1459                offset  += advance;
     1460                skipped += advance;
     1461                if(size - offset < 16)
     1462                    advance = size - offset;
     1463                else
     1464                    advance = 16;
     1465            }
     1466            sprintf(st_Buffer, "\n                        ...%lu bytes skipped...", (unsigned long)skipped);
     1467            st_Output(st_Buffer);
     1468            continue;
     1469        }
     1470        else
     1471        {
     1472            if(size - offset < 16)
     1473                st_HexDump(ptr, offset, size-offset, 0);
     1474            else
     1475                st_HexDump(ptr, offset, 16, 0);
     1476
     1477            offset += 16;
     1478        }
    13951479    }
    13961480
     
    14091493
    14101494    if(title)
    1411         st_Output("   Address   Offset Data");
     1495        st_Output("   Address   Offset Data");
    14121496
    14131497    column = 0;
     
    14171501    while(output < size)
    14181502    {
    1419         if(column == 0)
    1420         {
     1503        if(column == 0)
     1504        {
    14211505#ifdef FORTIFY_NO_PERCENT_P
    1422             sprintf(st_Buffer, "\n0x%08lx %8lu ", ptr, (unsigned long)offset);
     1506            sprintf(st_Buffer, "\n0x%08lx %8lu ", ptr, (unsigned long)offset);
    14231507#else
    1424             sprintf(st_Buffer, "\n%10p %8lu ", ptr, (unsigned long)offset);
    1425 #endif
    1426             st_Output(st_Buffer);
    1427         }
    1428 
    1429         sprintf(st_Buffer, "%02x%s", *ptr, ((column % 4) == 3) ? " " : "");
    1430         st_Output(st_Buffer);
    1431 
    1432         ascii[ column ] = isprint( *ptr ) ? (char)(*ptr) : (char)('.');
    1433         ascii[ column + 1 ] = '\0';
    1434 
    1435         ptr++;
    1436         offset++;
    1437         output++;
    1438         column++;
    1439 
    1440         if(column == 16)
    1441         {
    1442             st_Output( "   \"" );
    1443             st_Output( ascii );
    1444             st_Output( "\"" );
    1445             column = 0;
    1446         }
     1508            sprintf(st_Buffer, "\n%10p %8lu ", ptr, (unsigned long)offset);
     1509#endif
     1510            st_Output(st_Buffer);
     1511        }
     1512
     1513        sprintf(st_Buffer, "%02x%s", *ptr, ((column % 4) == 3) ? " " : "");
     1514        st_Output(st_Buffer);
     1515
     1516        ascii[ column ] = isprint( *ptr ) ? (char)(*ptr) : (char)('.');
     1517        ascii[ column + 1 ] = '\0';
     1518
     1519        ptr++;
     1520        offset++;
     1521        output++;
     1522        column++;
     1523
     1524        if(column == 16)
     1525        {
     1526            st_Output( "   \"" );
     1527            st_Output( ascii );
     1528            st_Output( "\"" );
     1529            column = 0;
     1530        }
    14471531    }
    14481532
    14491533    if ( column != 0 )
    14501534    {
    1451         while ( column < 16 )
    1452         {
    1453             if( column % 4 == 3 )
    1454                 st_Output( "   " );
    1455             else
    1456                 st_Output( "  " );
    1457 
    1458             column++;
    1459         }
    1460         st_Output( "   \"" );
    1461         st_Output( ascii );
    1462         st_Output( "\"" );
    1463     }
    1464 }
    1465 
    1466 /*
    1467  * st_IsHeaderValid - Returns true if the 
    1468  * supplied pointer does indeed point to a 
     1535        while ( column < 16 )
     1536        {
     1537            if( column % 4 == 3 )
     1538                st_Output( "   " );
     1539            else
     1540                st_Output( "  " );
     1541
     1542            column++;
     1543        }
     1544        st_Output( "   \"" );
     1545        st_Output( ascii );
     1546        st_Output( "\"" );
     1547    }
     1548}
     1549
     1550/*
     1551 * st_IsHeaderValid - Returns true if the
     1552 * supplied pointer does indeed point to a
    14691553 * real Header
    14701554 */
    1471 static int 
    1472 st_IsHeaderValid(struct Header *h)                               
     1555static int
     1556st_IsHeaderValid(struct Header *h)
    14731557{
    14741558    return(st_ChecksumHeader(h) == FORTIFY_CHECKSUM_VALUE);
     
    14761560
    14771561/*
    1478  * st_MakeHeaderValid - Updates the checksum 
     1562 * st_MakeHeaderValid - Updates the checksum
    14791563 * to make the header valid
    14801564 */
    1481 static void 
     1565static void
    14821566st_MakeHeaderValid(struct Header *h)
    14831567{
     
    14871571
    14881572/*
    1489  * st_ChecksumHeader - Calculate (and return) 
    1490  * the checksum of the header. (Including the 
    1491  * Checksum field itself. If all is well, the 
     1573 * st_ChecksumHeader - Calculate (and return)
     1574 * the checksum of the header. (Including the
     1575 * Checksum field itself. If all is well, the
    14921576 * checksum returned by this function should
    14931577 * be FORTIFY_CHECKSUM_VALUE
     
    14971581{
    14981582    unsigned short c, checksum, *p;
    1499  
    1500     for(c = 0, checksum = 0, p = (unsigned short *)h; 
    1501         c < FORTIFY_HEADER_SIZE/sizeof(unsigned short); c++)
    1502     {   
    1503         checksum += *p++; 
    1504     }   
    1505    
     1583
     1584    for(c = 0, checksum = 0, p = (unsigned short *)h;
     1585        c < FORTIFY_HEADER_SIZE/sizeof(unsigned short); c++)
     1586    {
     1587        checksum += *p++;
     1588    }
     1589
    15061590    return(checksum);
    1507 }                 
    1508 
    1509 /* 
    1510  * st_IsOnAllocatedList - Examines the allocated 
     1591}
     1592
     1593/*
     1594 * st_IsOnAllocatedList - Examines the allocated
    15111595 * list to see if the given header is on it.
    1512  */ 
    1513 static int 
     1596 */
     1597static int
    15141598st_IsOnAllocatedList(struct Header *h)
    15151599{
    15161600    struct Header *curr;
    1517  
     1601
    15181602    curr = st_AllocatedHead;
    15191603    while(curr)
    15201604    {
    1521         if(curr == h)
    1522             return(1);
    1523      
    1524         curr = curr->Next;
    1525     }
    1526  
     1605        if(curr == h)
     1606            return(1);
     1607
     1608        curr = curr->Next;
     1609    }
     1610
    15271611    return(0);
    15281612}
    15291613
    15301614#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    1531 /* 
    1532  * st_IsOnDeallocatedList - Examines the deallocated 
     1615/*
     1616 * st_IsOnDeallocatedList - Examines the deallocated
    15331617 * list to see if the given header is on it.
    1534  */ 
    1535 static int 
     1618 */
     1619static int
    15361620st_IsOnDeallocatedList(struct Header *h)
    15371621{
    15381622    struct Header *curr;
    1539  
     1623
    15401624    curr = st_DeallocatedHead;
    15411625    while(curr)
    15421626    {
    1543         if(curr == h)
    1544             return(1);
    1545      
    1546         curr = curr->Next;
    1547     }
    1548  
     1627        if(curr == h)
     1628            return(1);
     1629
     1630        curr = curr->Next;
     1631    }
     1632
    15491633    return(0);
    15501634}
     
    15521636/*
    15531637 * st_PurgeDeallocatedBlocks - free at least "Bytes"
    1554  * worth of deallocated memory, starting at the 
     1638 * worth of deallocated memory, starting at the
    15551639 * oldest deallocated block.
    15561640 * Returns true if any blocks were freed.
     
    15631647
    15641648#ifdef FORTIFY_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY
    1565     sprintf(st_Buffer, "\nFortify: Warning - Discarding deallocated memory at %s.%lu\n", 
    1566                        file, line);
     1649    sprintf(st_Buffer, "\nFortify: Warning - Discarding deallocated memory at %s.%lu\n",
     1650                       file, line);
    15671651    st_Output(st_Buffer);
    15681652#endif /* FORTIFY_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY */
     
    15701654    while(st_DeallocatedTail && FreedBytes < Bytes)
    15711655    {
    1572         st_CheckDeallocatedBlock(st_DeallocatedTail, file, line);
    1573         FreedBytes += st_DeallocatedTail->Size;
    1574         FreedBlocks++;
     1656        st_CheckDeallocatedBlock(st_DeallocatedTail, file, line);
     1657        FreedBytes += st_DeallocatedTail->Size;
     1658        FreedBlocks++;
    15751659#ifdef FORTIFY_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY
    15761660#ifdef FORTIFY_VERBOSE_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY
    1577         sprintf(st_Buffer, "                %s\n",
    1578                            st_DeallocatedMemoryBlockString(st_DeallocatedTail));
    1579         st_Output(st_Buffer);
     1661        sprintf(st_Buffer, "                %s\n",
     1662                           st_DeallocatedMemoryBlockString(st_DeallocatedTail));
     1663        st_Output(st_Buffer);
    15801664#endif /* FORTIFY_VERBOSE_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY */
    15811665#endif /* FORTIFY_WARN_WHEN_DISCARDING_DEALLOCATED_MEMORY */
    1582         st_FreeDeallocatedBlock(st_DeallocatedTail, file, line);
     1666        st_FreeDeallocatedBlock(st_DeallocatedTail, file, line);
    15831667    }
    15841668
     
    15951679    struct Header *curr, *next;
    15961680    unsigned long FreedBlocks = 0;
     1681#ifdef MT_SCOPES
     1682    unsigned short ordinal = Get_TID_Ordinal();
     1683#endif
    15971684
    15981685    curr = st_DeallocatedHead;
    15991686    while(curr)
    16001687    {
    1601         next = curr->Next;
    1602         if(curr->Scope >= Scope)
    1603         {
    1604             st_FreeDeallocatedBlock(curr, file, line);
    1605             FreedBlocks++;
    1606         }
    1607 
    1608         curr = next;
    1609     }
    1610    
     1688        next = curr->Next;
     1689#ifdef MT_SCOPES
     1690        if(curr->Ordinal == ordinal && curr->Scope >= Scope)
     1691#else
     1692        if(curr->Scope >= Scope)
     1693#endif
     1694        {
     1695            st_FreeDeallocatedBlock(curr, file, line);
     1696            FreedBlocks++;
     1697        }
     1698
     1699        curr = next;
     1700    }
     1701
    16111702    return FreedBlocks != 0;
    16121703}
     
    16141705/*
    16151706 * st_FreeDeallocatedBlock - actually remove
    1616  * a deallocated block from the deallocated 
     1707 * a deallocated block from the deallocated
    16171708 * list, and actually free it's memory.
    16181709 */
     
    16221713    st_CheckDeallocatedBlock( h, file, line );
    16231714
    1624    /* 
    1625     * Begin Critical region 
     1715   /*
     1716    * Begin Critical region
    16261717    */
    16271718    FORTIFY_LOCK();
    16281719
    16291720    st_TotalDeallocated -= h->Size;
    1630    
     1721
    16311722    if(st_DeallocatedHead == h)
    16321723    {
    1633         st_DeallocatedHead = h->Next;
    1634     }
    1635    
     1724        st_DeallocatedHead = h->Next;
     1725    }
     1726
    16361727    if(st_DeallocatedTail == h)
    16371728    {
    1638         st_DeallocatedTail = h->Prev;
    1639     }
    1640    
     1729        st_DeallocatedTail = h->Prev;
     1730    }
     1731
    16411732    if(h->Prev)
    16421733    {
    1643         st_CheckDeallocatedBlock(h->Prev, file, line);
    1644         h->Prev->Next = h->Next;
    1645         st_MakeHeaderValid(h->Prev);   
    1646     }
    1647    
     1734        st_CheckDeallocatedBlock(h->Prev, file, line);
     1735        h->Prev->Next = h->Next;
     1736        st_MakeHeaderValid(h->Prev);
     1737    }
     1738
    16481739    if(h->Next)
    16491740    {
    1650         st_CheckDeallocatedBlock(h->Next, file, line);
    1651         h->Next->Prev = h->Prev;
    1652         st_MakeHeaderValid(h->Next);   
     1741        st_CheckDeallocatedBlock(h->Next, file, line);
     1742        h->Next->Prev = h->Prev;
     1743        st_MakeHeaderValid(h->Next);
    16531744    }
    16541745
     
    16561747         * Free the label
    16571748         */
    1658         if(h->Label) 
     1749        if(h->Label)
    16591750        {
    16601751                free(h->Label);
     
    16651756     */
    16661757    st_SetFortification((unsigned char*)h, FORTIFY_FILL_ON_DEALLOCATE_VALUE,
    1667                         FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
     1758                        FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
    16681759
    16691760    /*
     
    16711762     */
    16721763    free(h);
    1673    
     1764
    16741765    /*
    16751766     * End critical region
    16761767     */
    1677     FORTIFY_UNLOCK();   
     1768    FORTIFY_UNLOCK();
    16781769}
    16791770
     
    16811772
    16821773/*
    1683  * st_OutputMemory - Hex and ascii dump the 
     1774 * st_OutputMemory - Hex and ascii dump the
    16841775 * user memory of a block.
    16851776 */
     
    16881779{
    16891780    st_HexDump((unsigned char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1690                0, h->Size, 1);
     1781               0, h->Size, 1);
    16911782}
    16921783
     
    16951786 * st_OutputHeader - Output the header
    16961787 */
    1697 static void 
     1788static void
    16981789st_OutputHeader(struct Header *h)
    16991790{
     
    17011792        {
    17021793#ifdef FORTIFY_NO_PERCENT_P
    1703             sprintf(st_Buffer, "0x%08lx %8lu %s.%lu\n", 
     1794            sprintf(st_Buffer, "0x%08lx %8lu %s.%lu\n",
    17041795#else
    1705             sprintf(st_Buffer, "%10p %8lu %s.%lu\n", 
    1706 #endif
    1707                                (unsigned char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1708                            (unsigned long)h->Size,
    1709                                h->File, h->Line);
    1710     }                   
     1796            sprintf(st_Buffer, "%10p %8lu %s.%lu\n",
     1797#endif
     1798                               (unsigned char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1799                           (unsigned long)h->Size,
     1800                       h->File, h->Line);
     1801    }
    17111802    else
    17121803    {
    17131804#ifdef FORTIFY_NO_PERCENT_P
    1714             sprintf(st_Buffer, "0x%08lx %8lu %s.%lu %s\n", 
     1805            sprintf(st_Buffer, "0x%08lx %8lu %s.%lu %s\n",
    17151806#else
    1716             sprintf(st_Buffer, "%10p %8lu %s.%lu %s\n", 
    1717 #endif
    1718                            (unsigned char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1719                                (unsigned long)h->Size,
    1720                            h->File, h->Line, h->Label);
     1807            sprintf(st_Buffer, "%10p %8lu %s.%lu %s\n",
     1808#endif
     1809                           (unsigned char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1810                       (unsigned long)h->Size,
     1811                   h->File, h->Line, h->Label);
    17211812    }
    17221813    st_Output(st_Buffer);
     
    17271818 * known point where everything was hoopy.
    17281819 */
    1729 static void 
     1820static void
    17301821st_OutputLastVerifiedPoint()
    17311822{
    1732     sprintf(st_Buffer, "         Memory integrity was last verified at %s.%lu\n", 
    1733                        st_LastVerifiedFile,
    1734                        st_LastVerifiedLine);
     1823    sprintf(st_Buffer, "         Memory integrity was last verified at %s.%lu\n",
     1824                       st_LastVerifiedFile,
     1825                       st_LastVerifiedLine);
    17351826    st_Output(st_Buffer);
    17361827}
    17371828
    17381829/*
    1739  * st_MemoryBlockString - constructs a string that 
     1830 * st_MemoryBlockString - constructs a string that
    17401831 * desribes a memory block. (pointer,size,allocator,label)
    17411832 */
     
    17441835{
    17451836    static char st_BlockString[512];
    1746    
     1837
    17471838    if(h->Label == 0)
    17481839    {
     
    17521843            sprintf(st_BlockString,"(%p,%lu,%s.%lu)",
    17531844#endif
    1754                 (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1755                     (unsigned long)h->Size, h->File, h->Line);
    1756         }           
     1845                (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1846                    (unsigned long)h->Size, h->File, h->Line);
     1847        }
    17571848        else
    17581849        {
     
    17621853            sprintf(st_BlockString,"(%p,%lu,%s.%lu,%s)",
    17631854#endif
    1764                 (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1765                     (unsigned long)h->Size, h->File, h->Line, h->Label);
     1855                (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1856                    (unsigned long)h->Size, h->File, h->Line, h->Label);
    17661857        }
    17671858
     
    17901881            sprintf(st_BlockString,"(%p,%lu,%s.%lu,%s.%lu)",
    17911882#endif
    1792                 (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1793                     (unsigned long)h->Size, h->File, h->Line, h->FreedFile, h->FreedLine);
    1794         }           
     1883                (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1884                    (unsigned long)h->Size, h->File, h->Line, h->FreedFile, h->FreedLine);
     1885        }
    17951886        else
    17961887        {
     
    18001891            sprintf(st_BlockString,"(%p,%lu,%s.%lu,%s.%lu,%s)",
    18011892#endif
    1802                 (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
    1803                     (unsigned long)h->Size, h->File, h->Line, h->FreedFile, h->FreedLine, h->Label);
     1893                (char*)h + FORTIFY_HEADER_SIZE + FORTIFY_ALIGNED_BEFORE_SIZE,
     1894                    (unsigned long)h->Size, h->File, h->Line, h->FreedFile, h->FreedLine, h->Label);
    18041895        }
    18051896
     
    18411932
    18421933    /*
    1843      * If Fortify is disabled, we gotta do this a little 
     1934     * If Fortify is disabled, we gotta do this a little
    18441935     * differently.
    18451936     */
    1846     if(!st_Disabled) 
    1847     {
    1848         if(!uptr)
    1849             return(Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line));
    1850        
    1851         if(!st_IsOnAllocatedList(h))
    1852         {
     1937    if(!st_Disabled)
     1938    {
     1939        if(!uptr)
     1940            return(Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line));
     1941
     1942        if(!st_IsOnAllocatedList(h))
     1943        {
    18531944#ifdef FORTIFY_TRACK_DEALLOCATED_MEMORY
    1854             if(st_IsOnDeallocatedList(h))
    1855             {
    1856                 sprintf(st_Buffer, "\nFortify: Deallocated memory block passed to \"%s\" at %s.%lu\n",
    1857                                     st_AllocatorName[Fortify_Allocator_realloc], file, line);
    1858                 st_Output(st_Buffer);
    1859                 sprintf(st_Buffer,   "         Memory block %s was deallocated by \"%s\" at %s.%lu\n",
    1860                                    st_MemoryBlockString(h),
    1861                                    st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
    1862                 st_Output(st_Buffer);
    1863                 return 0;
    1864             }
    1865 #endif
    1866 
    1867             sprintf(st_Buffer,
     1945            if(st_IsOnDeallocatedList(h))
     1946            {
     1947                sprintf(st_Buffer, "\nFortify: Deallocated memory block passed to \"%s\" at %s.%lu\n",
     1948                                    st_AllocatorName[Fortify_Allocator_realloc], file, line);
     1949                st_Output(st_Buffer);
     1950                sprintf(st_Buffer,   "         Memory block %s was deallocated by \"%s\" at %s.%lu\n",
     1951                                   st_MemoryBlockString(h),
     1952                                   st_DeallocatorName[h->Deallocator], h->FreedFile, h->FreedLine);
     1953                st_Output(st_Buffer);
     1954                return 0;
     1955            }
     1956#endif
     1957
     1958            sprintf(st_Buffer,
    18681959#ifdef FORTIFY_NO_PERCENT_P
    1869                     "\nFortify: Invalid pointer (0x%08lx) passed to realloc at %s.%lu\n",
     1960                    "\nFortify: Invalid pointer (0x%08lx) passed to realloc at %s.%lu\n",
    18701961#else
    1871                     "\nFortify: Invalid pointer (%p) passed to realloc at %s.%lu\n",
    1872 #endif           
    1873                     ptr, file, line);
    1874             st_Output(st_Buffer);
    1875             return 0;
    1876         }
    1877 
    1878         if(!st_CheckBlock(h, file, line))
    1879             return 0;
    1880      
    1881         new_ptr = Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line);
    1882         if(!new_ptr)
    1883         {
    1884             return(0);
    1885         }
    1886      
    1887         if(h->Size < new_size)
    1888             memcpy(new_ptr, uptr, h->Size);
    1889         else
    1890             memcpy(new_ptr, uptr, new_size);
    1891    
    1892         Fortify_Deallocate(uptr, Fortify_Deallocator_realloc, file, line);
    1893         return(new_ptr);
     1962                    "\nFortify: Invalid pointer (%p) passed to realloc at %s.%lu\n",
     1963#endif
     1964                    ptr, file, line);
     1965            st_Output(st_Buffer);
     1966            return 0;
     1967        }
     1968
     1969        if(!st_CheckBlock(h, file, line))
     1970            return 0;
     1971
     1972        new_ptr = Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line);
     1973        if(!new_ptr)
     1974        {
     1975            return(0);
     1976        }
     1977
     1978        if(h->Size < new_size)
     1979            memcpy(new_ptr, uptr, h->Size);
     1980        else
     1981            memcpy(new_ptr, uptr, new_size);
     1982
     1983        Fortify_Deallocate(uptr, Fortify_Deallocator_realloc, file, line);
     1984        return(new_ptr);
    18941985    }
    18951986    else
    18961987    {
    1897         /*
    1898         * If the old block was fortified, we can't use normal realloc.
    1899         */
    1900         if(st_IsOnAllocatedList(h))
    1901         {
    1902             new_ptr = Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line);
    1903             if(!new_ptr)
    1904                 return(0);
    1905          
    1906             if(h->Size < new_size)
    1907                 memcpy(new_ptr, uptr, h->Size);
    1908             else
    1909                 memcpy(new_ptr, uptr, new_size);
    1910        
    1911             Fortify_Deallocate(uptr, Fortify_Deallocator_realloc, file, line);
    1912             return(new_ptr);
    1913         }
    1914         else /* easy */
    1915         {
    1916             return realloc(uptr, new_size);
    1917         }
     1988        /*
     1989        * If the old block was fortified, we can't use normal realloc.
     1990        */
     1991        if(st_IsOnAllocatedList(h))
     1992        {
     1993            new_ptr = Fortify_Allocate(new_size, Fortify_Allocator_realloc, file, line);
     1994            if(!new_ptr)
     1995                return(0);
     1996
     1997            if(h->Size < new_size)
     1998                memcpy(new_ptr, uptr, h->Size);
     1999            else
     2000                memcpy(new_ptr, uptr, new_size);
     2001
     2002            Fortify_Deallocate(uptr, Fortify_Deallocator_realloc, file, line);
     2003            return(new_ptr);
     2004        }
     2005        else /* easy */
     2006        {
     2007            return realloc(uptr, new_size);
     2008        }
    19182009    }
    19192010}
     
    19272018    if(!st_Disabled)
    19282019    {
    1929         void *ptr = Fortify_Allocate(size * num, Fortify_Allocator_calloc, file, line);
    1930         if(ptr)
    1931         {
    1932             memset(ptr, 0, size*num);
    1933         }
    1934         return ptr;
    1935     }   
     2020        void *ptr = Fortify_Allocate(size * num, Fortify_Allocator_calloc, file, line);
     2021        if(ptr)
     2022        {
     2023            memset(ptr, 0, size*num);
     2024        }
     2025        return ptr;
     2026    }
    19362027    else
    19372028    {
    1938         return calloc(num, size);
     2029        return calloc(num, size);
    19392030    }
    19402031}
     
    19482039        /* it is defined to be safe to free(0) */
    19492040        if(uptr == 0)
    1950         return;
     2041        return;
    19512042
    19522043    Fortify_Deallocate(uptr, Fortify_Deallocator_free, file, line);
     
    19632054    if(!st_Disabled)
    19642055    {
    1965         char *newStr = (char *)Fortify_Allocate(strlen(oldStr)+1, Fortify_Allocator_strdup, file, line);
    1966         if(newStr)
    1967         {
    1968             strcpy(newStr, oldStr);
    1969         }
    1970    
    1971         return newStr;
    1972     }   
     2056        char *newStr = (char *)Fortify_Allocate(strlen(oldStr)+1, Fortify_Allocator_strdup, file, line);
     2057        if(newStr)
     2058        {
     2059            strcpy(newStr, oldStr);
     2060        }
     2061
     2062        return newStr;
     2063    }
    19732064    else
    19742065    {
    1975         return strdup(oldStr);
     2066        return strdup(oldStr);
    19762067    }
    19772068}
     
    19842075    if(st_DeleteStackTop > 1)
    19852076    {
    1986         sprintf(st_Buffer, "Delete Trace: %s.%lu\n", st_DeleteFile[st_DeleteStackTop-1],
    1987                                                      st_DeleteLine[st_DeleteStackTop-1]);
    1988         st_Output(st_Buffer);
    1989         for(int c = st_DeleteStackTop-2; c >= 0; c--)
    1990         {
    1991             sprintf(st_Buffer, "              %s.%lu\n", st_DeleteFile[c],
    1992                                                         st_DeleteLine[c]);
    1993             st_Output(st_Buffer);
    1994         }
     2077        sprintf(st_Buffer, "Delete Trace: %s.%lu\n", st_DeleteFile[st_DeleteStackTop-1],
     2078                                                     st_DeleteLine[st_DeleteStackTop-1]);
     2079        st_Output(st_Buffer);
     2080        for(int c = st_DeleteStackTop-2; c >= 0; c--)
     2081        {
     2082            sprintf(st_Buffer, "              %s.%lu\n", st_DeleteFile[c],
     2083                                                        st_DeleteLine[c]);
     2084            st_Output(st_Buffer);
     2085        }
    19952086    }
    19962087#endif
     
    20122103    Fortify_NewHandlerFunc handler = set_new_handler(0);
    20132104
    2014     /* and set it back (since we cant 
     2105    /* and set it back (since we cant
    20152106     * get it without changing it)
    20162107     */
    20172108    set_new_handler(handler);
    2018    
     2109
    20192110    return handler;
    20202111}
    20212112
    20222113/*
    2023  * operator new - Fortify's replacement new, 
     2114 * operator new - Fortify's replacement new,
    20242115 * without source-code information.
    20252116 */
     
    20282119{
    20292120    void *p;
    2030    
    2031     while((p = Fortify_Allocate(size, Fortify_Allocator_new, 
    2032                                 st_AllocatorName[Fortify_Allocator_new], 0)) == 0)
    2033     {                           
    2034         if(st_NewHandler())
    2035             (*st_NewHandler())();
    2036         else
    2037             return 0;
     2121
     2122    while((p = Fortify_Allocate(size, Fortify_Allocator_new,
     2123                                st_AllocatorName[Fortify_Allocator_new], 0)) == 0)
     2124    {
     2125        if(st_NewHandler())
     2126            (*st_NewHandler())();
     2127        else
     2128            return 0;
    20382129    }
    20392130
     
    20522143    while((p = Fortify_Allocate(size, Fortify_Allocator_new, file, line)) == 0)
    20532144    {
    2054         if(st_NewHandler())
    2055             (*st_NewHandler())();
    2056         else
    2057             return 0;
    2058     }
    2059    
     2145        if(st_NewHandler())
     2146            (*st_NewHandler())();
     2147        else
     2148            return 0;
     2149    }
     2150
    20602151    return p;
    20612152}
     
    20672158 */
    20682159void *FORTIFY_STORAGE
    2069 operator new[](size_t size) 
     2160operator new[](size_t size)
    20702161{
    20712162    void *p;
    20722163
    20732164    while((p = Fortify_Allocate(size, Fortify_Allocator_array_new,
    2074                                 st_AllocatorName[Fortify_Allocator_array_new], 0)) == 0)
    2075     {                               
    2076         if(st_NewHandler())
    2077             (*st_NewHandler())();   
    2078         else   
    2079             return 0;
    2080     }
    2081    
     2165                                st_AllocatorName[Fortify_Allocator_array_new], 0)) == 0)
     2166    {
     2167        if(st_NewHandler())
     2168            (*st_NewHandler())();
     2169        else
     2170            return 0;
     2171    }
     2172
    20822173    return p;
    20832174}
     
    20932184    while((p = Fortify_Allocate(size, Fortify_Allocator_array_new, file, line)) == 0)
    20942185    {
    2095         if(st_NewHandler())
    2096             (*st_NewHandler())();
    2097         else
    2098             return 0;
     2186        if(st_NewHandler())
     2187            (*st_NewHandler())();
     2188        else
     2189            return 0;
    20992190    }
    21002191
     
    21202211    if(st_DeleteStackTop < FORTIFY_DELETE_STACK_SIZE)
    21212212    {
    2122         st_DeleteFile[st_DeleteStackTop] = file;
    2123         st_DeleteLine[st_DeleteStackTop] = line;
     2213        st_DeleteFile[st_DeleteStackTop] = file;
     2214        st_DeleteLine[st_DeleteStackTop] = line;
    21242215    }
    21252216
     
    21522243      */
    21532244    if(uptr == 0)
    2154         return;
     2245        return;
    21552246
    21562247    /*
     
    21592250    if(st_DeleteStackTop)
    21602251    {
    2161         if(st_DeleteStackTop < FORTIFY_DELETE_STACK_SIZE)
    2162         {
    2163             file = st_DeleteFile[st_DeleteStackTop-1];
    2164             line = st_DeleteLine[st_DeleteStackTop-1];
    2165         }
    2166         else
    2167         {
    2168             file = st_DeleteFile[FORTIFY_DELETE_STACK_SIZE-1];
    2169             line = st_DeleteLine[FORTIFY_DELETE_STACK_SIZE-1];
    2170         }
     2252        if(st_DeleteStackTop < FORTIFY_DELETE_STACK_SIZE)
     2253        {
     2254            file = st_DeleteFile[st_DeleteStackTop-1];
     2255            line = st_DeleteLine[st_DeleteStackTop-1];
     2256        }
     2257        else
     2258        {
     2259            file = st_DeleteFile[FORTIFY_DELETE_STACK_SIZE-1];
     2260            line = st_DeleteLine[FORTIFY_DELETE_STACK_SIZE-1];
     2261        }
    21712262    }
    21722263    else
    21732264    {
    2174         file = st_DeallocatorName[Fortify_Deallocator_delete];
    2175         line = 0;
     2265        file = st_DeallocatorName[Fortify_Deallocator_delete];
     2266        line = 0;
    21762267    }
    21772268
     
    21942285      */
    21952286    if(uptr == 0)
    2196         return;
     2287        return;
    21972288
    21982289    /*
     
    22012292    if(st_DeleteStackTop)
    22022293    {
    2203         if(st_DeleteStackTop < FORTIFY_DELETE_STACK_SIZE)
    2204         {
    2205             file = st_DeleteFile[st_DeleteStackTop-1];
    2206             line = st_DeleteLine[st_DeleteStackTop-1];
    2207         }
    2208         else
    2209         {
    2210             file = st_DeleteFile[FORTIFY_DELETE_STACK_SIZE-1];
    2211             line = st_DeleteLine[FORTIFY_DELETE_STACK_SIZE-1];
    2212         }
     2294        if(st_DeleteStackTop < FORTIFY_DELETE_STACK_SIZE)
     2295        {
     2296            file = st_DeleteFile[st_DeleteStackTop-1];
     2297            line = st_DeleteLine[st_DeleteStackTop-1];
     2298        }
     2299        else
     2300        {
     2301            file = st_DeleteFile[FORTIFY_DELETE_STACK_SIZE-1];
     2302            line = st_DeleteLine[FORTIFY_DELETE_STACK_SIZE-1];
     2303        }
    22132304    }
    22142305    else
    22152306    {
    2216         file = st_DeallocatorName[Fortify_Deallocator_array_delete];
    2217         line = 0;
     2307        file = st_DeallocatorName[Fortify_Deallocator_array_delete];
     2308        line = 0;
    22182309    }
    22192310
     
    22432334    Fortify_AutoLogFile()
    22442335    {
    2245         written_something = 0;
    2246         Fortify_SetOutputFunc(Fortify_AutoLogFile::Output);
    2247         Fortify_EnterScope(init_string, 0);
     2336        written_something = 0;
     2337        Fortify_SetOutputFunc(Fortify_AutoLogFile::Output);
     2338        Fortify_EnterScope(init_string, 0);
    22482339    }
    22492340
    22502341    static void Output(const char *s)
    22512342    {
    2252         if(written_something == 0)
    2253         {
    2254             FORTIFY_FIRST_ERROR_FUNCTION;
    2255             fp = fopen(FORTIFY_LOG_FILENAME, "w");
    2256             if(fp)
    2257             {
    2258                 time_t t;
    2259                 time(&t);
    2260                 fprintf(fp, "Fortify log started at %s\n", ctime(&t));
    2261                 written_something = 1;
    2262             }   
    2263         }
    2264 
    2265         if(fp)
    2266         {
    2267             fputs(s, fp);
    2268             fflush(fp);
    2269         }   
    2270     }
    2271    
     2343        if(written_something == 0)
     2344        {
     2345            FORTIFY_FIRST_ERROR_FUNCTION;
     2346            fp = fopen(FORTIFY_LOG_FILENAME, "w");
     2347            if(fp)
     2348            {
     2349                time_t t;
     2350                time(&t);
     2351                fprintf(fp, "Fortify log started at %s\n", ctime(&t));
     2352                written_something = 1;
     2353            }
     2354        }
     2355
     2356        if(fp)
     2357        {
     2358            fputs(s, fp);
     2359            fflush(fp);
     2360        }
     2361    }
     2362
    22722363    ~Fortify_AutoLogFile()
    22732364    {
    2274         Fortify_LeaveScope(term_string, 0);
    2275         Fortify_CheckAllMemory(term_string, 0);
    2276         if(fp)
    2277         {
    2278             time_t t;
    2279             time(&t);
    2280             fprintf(fp, "\nFortify log closed at %s\n", ctime(&t));
    2281             fclose(fp);
    2282             fp = 0;
    2283         }   
     2365        Fortify_LeaveScope(term_string, 0);
     2366        Fortify_CheckAllMemory(term_string, 0);
     2367        if(fp)
     2368        {
     2369            time_t t;
     2370            time(&t);
     2371            fprintf(fp, "\nFortify log closed at %s\n", ctime(&t));
     2372            fclose(fp);
     2373            fp = 0;
     2374        }
    22842375    }
    22852376};
Note: See TracChangeset for help on using the changeset viewer.