Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a dangling pointer by returning a pointer from a local C-style array

I am a bit confused by the following code:

#include <iostream>  const char* f() {     const char* arr[]={"test"};     return arr[0]; }  int main() {     auto x = f();     std::cout << x; } 

In my opinion, this code should be UB (undefined behaviour). We return a pointer to a C-style array element inside a local scope. Things should go wrong. However, none of the compilers I tested with complain (I used -Wall -Wextra -pedantic on both g++ and clang). valgrind does not complain either.

Is the code above valid or is it UB as one would think?

PS: running it seems to produce the "correct" result, i.e. displaying "test", but that's not an indication of correctness.

like image 697
vsoftco Avatar asked Apr 03 '18 16:04

vsoftco


1 Answers

No, it's not UB.

This:

const char* f() {     const char* arr[]={"test"};     return arr[0]; } 

Can be rewritten to the equivalent:

const char* f() {     const char* arr0 = "test";     return arr0; } 

So we're just returning a local pointer, to a string literal. String literals have static storage duration, nothing dangles. The function really is the same as:

const char* f() {     return "test"; } 

If you did something like this:

const char* f() {     const char arr[] = "test"; // local array of char, not array of char const*     return arr; } 

Now that is UB - we're returning a dangling pointer.

like image 197
Barry Avatar answered Sep 21 '22 10:09

Barry