What is behavior of NULL parameters to strstr?
Asked Answered
C

2

8

What is the behavior when a NULL is passed as the parameters in strstr?

Given:

char * p = NULL;
char * s = NULL;

Case 1: strstr(p, "Hello");

Case 2: strstr("With my dog", p);

Case 3: strstr(p, s);

My understanding is that the behavior is undefined and left up to the implementation for all 3 cases.

According to Microsoft Visual Studio documentation, they perform Parameter Validation and handle it there. See Remarks section.

We are using C99 on IAR Workbench.

Background: Some testing folks are writing unit tests and assigning NULL to the string variables.

Civic answered 25/10, 2013 at 1:24 Comment(0)
I
6

The ISO C standard says that the behavior is undefined.

Quoting N1570, which is a draft of the 2011 ISO C standard, section 7.1.4:

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 [...], or a null pointer, [...], the behavior is undefined.

The description of strstr in 7.24.5.7 says:

The strstr function locates the first occurrence in the string pointed to by s1 of the sequence of characters (excluding the terminating null character) in the string pointed to by s2.

which, in addition to the statement in 7.1.4, says that the arguments have to point to some string (which a null pointer does not).

These statements are similar, if not identical, in the C90 and C99 standards.

Note that "undefined behavior" does not imply that the program will necessarily crash. For example, this program:

#include <stdio.h>
#include <string.h>
int main(void) {
    char *p = strstr(NULL, "");
    if (p == NULL) {
        printf("p == NULL\n");
    }
    else {
        printf("p = %p\n", p);
    }
}

when compiled and run on my system (Linux, gcc 4.7.2, glibc 2.15) prints:

p == NULL

probably because strstr optimizes the case of an empty string for the second argument. Undefined behavior is an error that need not be detected or diagnosed; it's entirely your responsibility as a programmer to avoid undefined behavior in the first place.

Igneous answered 25/10, 2013 at 1:57 Comment(0)
C
1

You can pretty much expect a crash for undefined behavior.

The MSFT docs say that for strstr parameters: 'If str or strSearch is NULL, the invalid parameter handler is invoked' and, 'The default invalid parameter invokes Watson crash reporting, which causes the application to crash' (http://msdn.microsoft.com/en-us/library/vstudio/ksazx244.aspx).

You can change this, however by using the function _set_invalid_parameter_handler (at least in the MSFT world). When that function is called, you have to decide how to recover and if the user should be informed or if the program can continue.

Captive answered 25/10, 2013 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.