Skip to content

Instantly share code, notes, and snippets.

@kevinushey
Last active Feb 9, 2020
Embed
What would you like to do?
Fails to compile with gcc 10 on Fedora Rawhide.
void* callback(const char* name);
extern "C" {
inline void f1()
{
static void (*f)();
f = (void(*)()) callback("f1");
f();
}
inline void f2()
{
static void (*f)();
f = (void(*)()) callback("f2");
f();
}
} // extern "C"
int main()
{
f1();
f2();
}
@kevinushey
Copy link
Author

kevinushey commented Feb 9, 2020

[root@decce70a480c scratch]# g++ test.cpp
/tmp/ccYIN1q3.s: Assembler messages:
/tmp/ccYIN1q3.s:59: Error: symbol `f' is already defined

@QuLogic
Copy link

QuLogic commented Feb 9, 2020

Line 11 calls callback, which returns 0. Then line 12 attempts to call that pointer, which is a NULL dereference. The same for f2. This is likely undefined behaviour, so you can't say much on the result being right or wrong.

This may be a bit too simplified from the original.

@Enchufa2
Copy link

Enchufa2 commented Feb 9, 2020

That causes a runtime crash, but that's not the point. The point here is that gcc 10 generates two symbols f for lines 10 and 17, and they collide. Previous versions of gcc generate _ZZ2f1E1f and _ZZ2f2E1f respectively, i.e., f1::f and f2::f demangled.

@kevinushey
Copy link
Author

kevinushey commented Feb 9, 2020

I've removed the implementation of callback() so at least this should no longer be UB. The underlying issue though is as @Enchufa2 says, with duplicate (and non-mangled) names for f generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment