C - GCC generates wrong instructions when returning local stack address

Benedikt Weiß

GCC generates the wrong instructions when I write a function which returns a reference to a local variable. I know perfectly that you shouldn't do that.

Here is the simple Code:

#include <stdio.h>
#include <stdlib.h>

int *func()
{
    int a = 100;
    return &a;
}

int main()
{
    printf("%p\n", func());
}

The output of the program is "(nil)".

I just compiled this with "gcc sample.c" and disassembled the executable with gdb:

Dump of assembler code for function func:
   0x00000000004004e6 <+0>: push   %rbp
   0x00000000004004e7 <+1>: mov    %rsp,%rbp
   0x00000000004004ea <+4>: movl   $0x64,-0x4(%rbp)
   0x00000000004004f1 <+11>:    mov    $0x0,%eax
   0x00000000004004f6 <+16>:    pop    %rbp
   0x00000000004004f7 <+17>:    retq   
End of assembler dump.
Dump of assembler code for function main:
   0x00000000004004f8 <+0>: push   %rbp
   0x00000000004004f9 <+1>: mov    %rsp,%rbp
   0x00000000004004fc <+4>: mov    $0x0,%eax
   0x0000000000400501 <+9>: callq  0x4004e6 <func>
   0x0000000000400506 <+14>:    mov    %rax,%rsi
   0x0000000000400509 <+17>:    mov    $0x4005a4,%edi
   0x000000000040050e <+22>:    mov    $0x0,%eax
   0x0000000000400513 <+27>:    callq  0x4003c0 <printf@plt>
   0x0000000000400518 <+32>:    mov    $0x0,%eax
   0x000000000040051d <+37>:    pop    %rbp
   0x000000000040051e <+38>:    retq   
End of assembler dump.

As you can see the return value is 0. It should be -0x4(%rbp). I've found nothing which this explains. My guess was that the GCC developers wanted that this code fails as fast as possible (null pointer dereferencing) but this couldn't be. A compiler has to generate the right instructions. I've tested this with GCC 5.3.0.

David Seiler

The compiler has generated the right instructions. What you are doing is not defined by the C standard, so the compiler is free to do whatever it pleases. In this case it seems that GCC pleases to return a null pointer, probably so that your program will fail as fast as possible.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GCC generates useless instructions?

From Dev

GCC generates SSE instructions instead of AVX

From Dev

C - Avoiding warning "address of stack memory associated with local variable returned"

From Dev

GCC generates wrong structure offset why?

From Dev

HTML <a> tag mailto generates wrong address

From Dev

Local site redirect to wrong address

From Dev

GCC - Linux - Set the stack to zero before returning?

From Dev

Meaningful stack traces for address sanitizer in GCC

From Dev

GCC inline - push address, not its value to stack

From Dev

GCC inline - push address, not its value to stack

From Dev

No warning when returning NULL with gcc

From Dev

Why is the Node.js AWS-SDK returning the wrong SQS queue URL when creating a local queue

From Dev

How does gcc push local variables on to the stack?

From Dev

When will gcc generate <UNDEFINED> instructions for function

From Dev

nslookup returning local router name and address

From Dev

Using C-string gives Warning: "Address of stack memory associated with local variable returned"

From Dev

Function is returning wrong type C

From Dev

Returning a local variable in C++

From Dev

C# Returning local variables

From Java

GitHub authentication failing over https, returning wrong email address

From Dev

GCC/Clang x86_64 C++ ABI mismatch when returning a tuple?

From Dev

If returning wrong result when comparing integer with string

From Dev

Wrong payload when using BlueZ stack as peripheral

From Dev

Wrong output when converting string to IP address

From Dev

Why is returning address of local variable or temporary only a warning and not an error?

From Dev

Rails generates path with wrong id when I use SimpleDelegator

From Dev

Work around error 'Address of stack memory associated with local variable returned'

From Dev

GCC C and ARM Assembly Stack Cleanup

From Dev

stack memory reuse gcc optimization in C?

Related Related

  1. 1

    GCC generates useless instructions?

  2. 2

    GCC generates SSE instructions instead of AVX

  3. 3

    C - Avoiding warning "address of stack memory associated with local variable returned"

  4. 4

    GCC generates wrong structure offset why?

  5. 5

    HTML <a> tag mailto generates wrong address

  6. 6

    Local site redirect to wrong address

  7. 7

    GCC - Linux - Set the stack to zero before returning?

  8. 8

    Meaningful stack traces for address sanitizer in GCC

  9. 9

    GCC inline - push address, not its value to stack

  10. 10

    GCC inline - push address, not its value to stack

  11. 11

    No warning when returning NULL with gcc

  12. 12

    Why is the Node.js AWS-SDK returning the wrong SQS queue URL when creating a local queue

  13. 13

    How does gcc push local variables on to the stack?

  14. 14

    When will gcc generate <UNDEFINED> instructions for function

  15. 15

    nslookup returning local router name and address

  16. 16

    Using C-string gives Warning: "Address of stack memory associated with local variable returned"

  17. 17

    Function is returning wrong type C

  18. 18

    Returning a local variable in C++

  19. 19

    C# Returning local variables

  20. 20

    GitHub authentication failing over https, returning wrong email address

  21. 21

    GCC/Clang x86_64 C++ ABI mismatch when returning a tuple?

  22. 22

    If returning wrong result when comparing integer with string

  23. 23

    Wrong payload when using BlueZ stack as peripheral

  24. 24

    Wrong output when converting string to IP address

  25. 25

    Why is returning address of local variable or temporary only a warning and not an error?

  26. 26

    Rails generates path with wrong id when I use SimpleDelegator

  27. 27

    Work around error 'Address of stack memory associated with local variable returned'

  28. 28

    GCC C and ARM Assembly Stack Cleanup

  29. 29

    stack memory reuse gcc optimization in C?

HotTag

Archive