GNU extensions - different behaviour of g++ and clang for taking address of temporary array

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

GNU extensions - different behaviour of g++ and clang for taking address of temporary array

Hans Wennborg via cfe-dev

Hi all,

Consider a test case:

#include <stdio.h>

typedef int a[];

void print(int *c) {
   printf("%d\n", c[0]);
}

int main() {
   print(a{2});
   return 0;
}

`g++ -std=gnu++11 -fsyntax-only` fails to compile this code: "error: taking address of temporary array". However, clang accepts it without any warnings and there is no any ASan output for it. Could someone explain me if it a GCC or Clang issue?

-- 
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: GNU extensions - different behaviour of g++ and clang for taking address of temporary array

Hans Wennborg via cfe-dev
On 19 Feb 2018 10:31, "Aleksei Sidorin via cfe-dev" <[hidden email]> wrote:

Hi all,

Consider a test case:

#include <stdio.h>

typedef int a[];

void print(int *c) {
   printf("%d\n", c[0]);
}

int main() {
   print(a{2});
   return 0;
}

`g++ -std=gnu++11 -fsyntax-only` fails to compile this code: "error: taking address of temporary array". However, clang accepts it without any warnings and there is no any ASan output for it. Could someone explain me if it a GCC or Clang issue?

It's a GCC bug -- this code is valid. The array to pointer conversion is used to convert the int[1] temporary to a pointer; a reference is not bound to it.

(GCC's behaviour is not unreasonable, though, and perhaps we should warn on such constructs. I would imagine the GCC diagnostic would be suppressed by making c a pointer to const int?)
-- 
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: GNU extensions - different behaviour of g++ and clang for taking address of temporary array

Hans Wennborg via cfe-dev
Thank you for explanation Richard!
As I see, turning 'c' into 'const int *' doesn't suppress GCC diagnostic. However, this is all information I need.

19.02.2018 21:44, Richard Smith пишет:
On 19 Feb 2018 10:31, "Aleksei Sidorin via cfe-dev" <[hidden email]> wrote:

Hi all,

Consider a test case:

#include <stdio.h>

typedef int a[];

void print(int *c) {
   printf("%d\n", c[0]);
}

int main() {
   print(a{2});
   return 0;
}

`g++ -std=gnu++11 -fsyntax-only` fails to compile this code: "error: taking address of temporary array". However, clang accepts it without any warnings and there is no any ASan output for it. Could someone explain me if it a GCC or Clang issue?

It's a GCC bug -- this code is valid. The array to pointer conversion is used to convert the int[1] temporary to a pointer; a reference is not bound to it.

(GCC's behaviour is not unreasonable, though, and perhaps we should warn on such constructs. I would imagine the GCC diagnostic would be suppressed by making c a pointer to const int?)
-- 
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



-- 
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev