Understanding C Pointers Using GDB by Learning the Kernel and Call Stack

I did some professional coding in C for a while, but I'm still puzzled by some pointer related issues. I would really appreciate community help in understanding the issue below.

After splitting the code and the generated kernel file.

void func1()    // Frame 1 in GDB stack trace.  
{ 
    UTYPE  *ptr;  // pointer to user defined type  
    ...

    // data is of type UTYPE and has valid contents.
    // lets say its address is 0x100 
    ptr = &data;     --- (1)  
    ...

    func2(ptr);      --- (2) 
    ...
} 

void func2(UTYPE *inp)    // Frame 0 in GDB stack trace.  
{
    if(!inp)         --- (3) 
        return; 
    ...

    // another_ptr is of UTYPE * which is a NULL.  
    inp = another_ptr;   ---- (4)  

    /* Did not check for NULL and dereference inp and CRASH */    ---- (5) 
} 

Simplified backtrace from GDB:

Frame 0: 
    func2(inp = 0x0) 
    // crash at line (5) due to dereference 

Frame 1: 
    func1: func2(0x0)  
    // `ptr` at line (2) is 0x0. Why is this so? 

Why ptris it displayed as 0x0 (NULL)in frame 1?

When called func2(), its call stack looks like this:

  | //local vars  | 
  |               | 
  | another_ptr = |
  |      NULL     |
  +---------------+
  | return addr   |
  +---------------+
  | input args    |
  | copy of ptr   |
  |   contents    |
  |     0x100     |

For func1()its call stack should look like this:

  |               | 
  | ptr = 0x100   |
  |               |
  +---------------+
  | return addr   |
  +---------------+
  | input args    |
  |  none in this |
  |  func         |

When inpbecomes NULLin func2()in line (4), how is this reflected in func1 ()?

+4
2

C, . . , , .

32- - inp - , 12 , . . fooobar.com/questions/1524587/....

gdb , , , " func2 inp 4- *UTYPE, 12- % ebp".

- func2 inp, (4), inp, 0. , inp func2 , , , , " func2 inp 4- type *UTYPE, func2 , ptr, 4- % ebp." DWARF , .

, backtrace gdb ptr func1 0. inp NULL ptr gdb ptr .

+2

GDB , inp, func2, 0, GDB , 0, , func2 0.

:

another_ptr = 0  ( func2 local variable )
return address to func1                   
inp = 0          ( func2 parameter )
ptr              ( func1 local variable )
+2

Source: https://habr.com/ru/post/1524585/


All Articles