Thread: Undefined behavior

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    918

    Undefined behavior

    This same function works in one program and crashes in another at the line : strcpy(buf, str);
    Is this an undefined behavior?

    Code:
    int function(char * buf)
    {
        char * szLocalIP = functionreturningchar*();
        char str[260];
        strcpy(str, szLocalIP);
    
        char * pch = strrchr(str, '.');
    
        str[pch-str+1] = '\0';
        strcat(str, "0");
    
        strcpy(buf, str); // crash
    }
    Windows 10 with Visual Studio 2019 and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    Well it is if you call it with say

    function("hello");

    or

    char tooSmall[3];
    function(tooSmall);


    You need to show the context for a decent answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    918
    Sorry.

    Code:
        char * ip;
        function(ip);
    Windows 10 with Visual Studio 2019 and MingW.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    In other words, you're trying to append to an uninitialised pointer.

    Pass a pointer to some memory you can write to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    918
    Thanks.
    Last edited by Ducky; 11-16-2013 at 04:21 AM.
    Windows 10 with Visual Studio 2019 and MingW.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ip is an uninitialised pointer. Obtaining its value gives undefined behaviour. Dereferencing it (which also requires obtaining its value) also gives undefined behaviour.

    Before calling function(), you need to ensure that ip points to the (first character of) a buffer that is long enough to hold whatever you will be copying to it. Otherwise your function will (probably) be writing data to some random area of memory. When modern operating systems catch programs doing that, they tend to forceably terminate the program (aka force it to crash).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    918
    It works with initializing with calloc().
    But what's wrong with initializing like this?
    It will crash though I'm writing less character to it than it was initialized with.

    Code:
    char * ip = "111222333444555";
    function(ip);
    Windows 10 with Visual Studio 2019 and MingW.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    "111222333444555" is a string literal. It's probably stored by the compiler in an area of memory that's read only. I.e. you'd be safer having const char *ip = "111222333444555";

    Edit:
    If you want something you can modify try char ip[] = "111222333444555";
    Last edited by Hodor; 11-16-2013 at 04:35 AM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    "111222333444555" is a string literal. It's probably stored by the compiler in an area of memory that's read only.
    What's really wrong with modifying a string literal is that the standard says that doing so gives undefined behaviour. That's really all a C programmer needs to care about.

    The string literal might not even be in memory as the programmer understands it. On some systems, for example, string literals are (literally) retrieved from the executable file on demand (and the executable file itself is locked so it can't be modified while being run). On those systems, changing the string literal has no effect that the program can observe, since the literal will revert to the same string whenever it is retrieved. Admittedly, such systems are not often encountered these days.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Ducky View Post
    But what's wrong with initializing like this?
    It will crash though I'm writing less character to it than it was initialized with.

    Code:
    char * ip = "111222333444555";
    function(ip);
    Try initializing like this instead:

    Code:
    char ip[] = "111222333444555";
    Then the memory ip points to will not be read-only. It is a shorthand for typing
    Code:
     char ip[] = {'1', '1', '1', '2', ..., '\0'};

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    918
    I see this often. Is this undefined behavior?

    Code:
    char * buff;
    buff = " Hello";
    printf("%s", buff);
    Windows 10 with Visual Studio 2019 and MingW.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by Ducky View Post
    I see this often. Is this undefined behavior?

    Code:
    char * buff;
    buff = " Hello";
    printf("%s", buff);
    No till you try to modify string using your buff pointer.
    if you declare pointer as const char* compiler will wanr about such attempts
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    918
    Thanks.

    I declared as const char and modified it and it still compiles and executes. How so?
    Code:
        const char * buf;
        buf = "hello";
        printf("%s\n", buf);
    
        buf = "hello world";
        printf("%s\n", buf);
    Windows 10 with Visual Studio 2019 and MingW.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Ducky View Post
    Thanks.

    I declared as const char and modified it and it still compiles and executes. How so?
    You didn't modify the data the const char * points to. Just reassigned it. That's allowed

    Kurt

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to understand the difference between "const char *buf" (where the individual chars are const) and "char * const buf" (where the pointer is const, but the char's are not).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this not undefined behavior?
    By Syscal in forum C Programming
    Replies: 6
    Last Post: 07-15-2013, 01:07 AM
  2. General question about undefined behavior
    By kjwilliams in forum C Programming
    Replies: 46
    Last Post: 06-18-2013, 01:51 PM
  3. Undefined behavior
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 02-18-2013, 11:14 PM
  4. Is x=x++; Undefined Behavior?
    By envec83 in forum C Programming
    Replies: 5
    Last Post: 10-04-2011, 01:27 AM
  5. Undefined behavior from VC6 to 2k5
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2011, 07:56 PM