Thread: An interesting bug on CLANG (at least in version 11)

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078

    An interesting bug on CLANG (at least in version 11)

    Code:
    #include <stdio.h>
    
    static void (*ptr)(void) = NULL;
    
    void f(void) { puts( "hello" ); }
    
    void g(void) { ptr = f; }
    
    int main( void ) { ptr(); }
    In clang, strange as it seems, this code will print "hello" (but g() is never called, how is this possible?).

    Ok... derreferencing a NULL pointer is a UB... but every other compiler gives us an 'segmentation fault' or 'access violation'. Why clang initializes ptr with f, instead of the explicit (void *)0?

    Take 'static' off and the code behaves as expected.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can't really call it a bug when the compiler still works perfectly fine and remains standard conforming. An odd way of dealing (or just not dealing) with UB, perhaps?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working in MSVC but not in CLANG
    By graviton in forum C Programming
    Replies: 10
    Last Post: 09-26-2020, 07:45 PM
  2. Clang 6.0.0 released
    By ordak in forum C Programming
    Replies: 8
    Last Post: 03-14-2018, 10:23 PM
  3. Compiling with Clang on Visual Studio
    By Osman Zakir in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2016, 02:16 PM
  4. Stream operator inconsistency between GCC and Clang
    By milli-961227 in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2015, 08:30 AM
  5. gcc/clang support for avx
    By Aslaville in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2014, 02:01 AM

Tags for this Thread