IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 12, 2004, 9:05:02 AM (22 years ago)
Author:
desonia
Message:

Added buffer overflow detection and doxygen comments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psMemory.c

    r639 r648  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-05-11 20:11:17 $
     10 *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-05-12 19:05:02 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2020#include <stdio.h>
    2121#include <stdbool.h>
     22#include <stdint.h>
    2223
    2324#include "psMemory.h"
     
    3435 * Unique ID for allocated blocks
    3536 */
    36 static unsigned long memid = 0;
     37static psMemoryId memid = 0;
    3738
    3839/**
     
    6263                                      const char *file, int lineno)
    6364{
    64     if (ptr->refCounter <= 0) {
     65    if (ptr->refCounter < 1) {
    6566        psError(__func__,
    6667                "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
     
    9596 * Call the callbacks when these IDs are allocated/freed
    9697 */
    97 long p_psMemAllocateID = 0;  // notify user this block is allocated
    98 long p_psMemFreeID = 0;   // notify user this block is freed
    99 
    100 long psMemAllocateCallbackSetID(long id) // set p_psMemAllocateID to id
    101 {
    102     long old = p_psMemAllocateID;
     98psMemoryId p_psMemAllocateID = 0;  // notify user this block is allocated
     99psMemoryId p_psMemFreeID = 0;   // notify user this block is freed
     100
     101psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id
     102{
     103    psMemoryId old = p_psMemAllocateID;
    103104    p_psMemAllocateID = id;
    104105
     
    106107}
    107108
    108 long psMemFreeCallbackSetID(long id)  // set p_psMemFreeID to id
    109 {
    110     long old = p_psMemFreeID;
     109psMemoryId psMemFreeCallbackSetID(psMemoryId id)  // set p_psMemFreeID to id
     110{
     111    psMemoryId old = p_psMemFreeID;
    111112    p_psMemFreeID = id;
    112113
     
    120121 * isn't resignalled)
    121122 */
    122 static long memAllocateCallbackDefault(const psMemBlock *ptr)
    123 {
    124     static int incr = 0;  // "p_psMemAllocateID += incr"
     123static psMemoryId memAllocateCallbackDefault(const psMemBlock *ptr)
     124{
     125    static psMemoryId incr = 0;  // "p_psMemAllocateID += incr"
    125126
    126127    return incr;
    127128}
    128129
    129 static long memFreeCallbackDefault(const psMemBlock *ptr)
    130 {
    131     static int incr = 0;  // "p_psMemFreeID += incr"
     130static psMemoryId memFreeCallbackDefault(const psMemBlock *ptr)
     131{
     132    static psMemoryId incr = 0;  // "p_psMemFreeID += incr"
    132133
    133134    return incr;
     
    169170 * Return memory ID counter for next block to be allocated
    170171 */
    171 int psMemGetId(void)
     172psMemoryId psMemGetId(void)
    172173{
    173174    return memid + 1;
     
    199200
    200201    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
    201         psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (%p %p)",
     202        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer underflow %p %p)",
    202203                m->id,m->startblock,m->endblock);
     204        return(1);
     205    }
     206    if (*(void**)((int8_t*)(m+1)+m->userMemorySize) != P_PS_MEMMAGIC) {
     207        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer overflow, %p)",
     208                m->id,*(void**)((int8_t*)(m+1)+m->userMemorySize));
    203209        return(1);
    204210    }
     
    236242void *p_psAlloc(size_t size, const char *file, int lineno)
    237243{
    238     psMemBlock *ptr = malloc(sizeof(psMemBlock) + size);
     244    psMemBlock *ptr = malloc(sizeof(psMemBlock) + size + sizeof(void*));
    239245
    240246    if (ptr == NULL) {
     
    246252    }
    247253
    248     *(unsigned long*)&ptr->id = ++memid;
     254    *(psMemoryId*)&ptr->id = ++memid;
    249255    ptr->file = file;
    250256    *(unsigned int*)&ptr->lineno = lineno;
     
    252258    ptr->endblock = P_PS_MEMMAGIC;
    253259    ptr->refCounter = 1;                // one user so far
     260    ptr->userMemorySize = size;
     261    *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
    254262
    255263    // need exclusive access of the memory block list now...
     
    287295        isBlockLast = (ptr == lastMemBlockAllocated);
    288296
    289         ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
     297        ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size + sizeof(void*));
    290298
    291299        if (ptr == NULL) {
     
    293301                    size, file, lineno);
    294302        }
     303
     304        ptr->userMemorySize = size;
     305        *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
    295306
    296307        if (isBlockLast) {
     
    363374 */
    364375int psMemCheckLeaks(
    365     int id0,                            // don't list blocks with id < id0
     376    psMemoryId id0,                     // don't list blocks with id < id0
    366377    psMemBlock ***arr,                  // pointer to array of pointers to leaked blocks, or NULL
    367378    FILE *fd)                           // print list of leaks to fd (or NULL)
     
    416427 * Reference counting APIs
    417428 */
    418 int psMemGetRefCounter(void *vptr) // return refCounter
     429psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
    419430{
    420431    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
Note: See TracChangeset for help on using the changeset viewer.