简体   繁体   中英

In C, what exactly happens when you pass a NULL pointer to strcmp()?

I have read that the following results in undefined behavior.

strcmp(foo, NULL); 

But what exactly happens "underneath the hood," so to speak? Is foo compared to garbage data? Is NULL dereferenced? What are the details that cause "undefined behavior"?

It depends on the implementation, which is free to assume your parameters are valid (ie not null in this case). The behaviour may or may not be reproducible from execution to execution, or from one implementation/platform to another.

C11 makes this very clear in 7.1.4, "Use of library functions":

Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as [...] a null pointer [...]) [...], the behavior is undefined.

The description of strcmp in 7.24.4 does not state otherwise, so the behaviour is indeed undefined.

This is the current implementation of strcmp in glibc:

/* Compare S1 and S2, returning less than, equal to or
   greater than zero if S1 is lexicographically less than,
   equal to or greater than S2.  */
int
strcmp (p1, p2)
     const char *p1;
     const char *p2;
{
  const unsigned char *s1 = (const unsigned char *) p1;
  const unsigned char *s2 = (const unsigned char *) p2;
  unsigned char c1, c2;

  do
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0')
    return c1 - c2;
    }
  while (c1 == c2);

  return c1 - c2;
}

You pass two pointers, and strcmp dereferences their contents and compares until it meets the difference or null character. Fail happens at different abstraction level, strcmp is fail-free on it's own. Many systems generate SIGSEGV signsl on dereferencing NULL pointer, but this is not the requirement.

Please note that ISO standards do not define many things, leaving implementation details up to implementations. At ISO C level there is nothing wrong with your example, but the results are not guaranteed to be predictable. (And no practical test is guaranteed to be precise and reproducible, unless you consult the rules of underlying system and they say otherwise).

When we are talking about abstraction levels, we cannot ask "what if", because the rules are clear and say "do not do that, behavior is not defined here".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM