Thread: NULL Pointer Access?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    Question NULL Pointer Access?

    I am having some problems with the following code...

    I believe it should pass through without any major issues. Am I doing enough checking for NULL pointers in here.

    Within the Netware environment where this is executed, I had this chunk of code error on 2 different servers whilst processing some data.

    Code:
    if ( pItem -> pItemLevel2 == NULL )
    {
    sprintf ( MiscBuff, "Internal error code 9007 line %i" , __LINE__ ) ;
    Exception ( EX_CRITICAL, 7, FBS_INTDBASE, MiscBuff ) ;
    break ;
    }
    
    for ( pLoop = pItem->pItemLevel2->pItemLevel3 ; 
           pLoop != NULL ;
           pLoop = pLoop -> pNext )
                if ( pLoop -> cStatus != CHECK )
    	break ;
    Does anyone have any ideas for why this might fail - am I just unlucky?

    Cheers in advance.

    DS
    Smell my cheese you Mother

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Am I doing enough checking for NULL pointers in here.
    >Does anyone have any ideas for why this might fail

    Do you check to see whether pItem is NULL before dereferencing it using the -> operator?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    Yes, pItem is checked before that section.
    Smell my cheese you Mother

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here are the potential unsafe parts looking only at what you have given:

    >if ( pItem -> pItemLevel2 == NULL )
    pItem may be NULL, thus dereferencing it would result in some form of access violation.

    >sprintf ( MiscBuff, "Internal error code 9007 line %i" , __LINE__ ) ;
    MiscBuff may not have memory allocated to it if it is a pointer and may not be large enough to hold the string and a decimal value if it is an array.

    >Exception ( EX_CRITICAL, 7, FBS_INTDBASE, MiscBuff ) ;
    Theoretically this could be unsafe, but I can't be more specific without the code for the Exception routine. I'll assume that it works correctly and all of the arguments are valid.

    >for ( pLoop = pItem->pItemLevel2->pItemLevel3 ;
    pItem may be NULL, pItemLevel2 may be NULL. Either of these cases would result in some form of access violation.

    >pLoop = pLoop -> pNext )
    pLoop may not be a valid pointer. See the next comment.

    >if ( pLoop -> cStatus != CHECK )
    If pLoop is an invalid pointer but not NULL then dereferencing it would be a Bad Thing. This can occur if your list structure is corrupted somewhere along the line.
    My best code is written with the delete key.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The for could loop terminate (if nothing caused it to break) with pLoop equal to NULL. Any subsequent code that dereferences pLoop without first checking to see that it is not NULL would also be a problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM