Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is behavior of NULL parameters to strstr?

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.

like image 747
Thomas Matthews Avatar asked Oct 25 '13 01:10

Thomas Matthews


People also ask

Does Strstr return null?

Return Value The strstr() function returns a pointer to the beginning of the first occurrence of string2 in string1. If string2 does not appear in string1, the strstr() function returns NULL.

What is strstr () in C?

The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.

What is the purpose of Strstr s1 s2?

strstr() in C/C++ In C++, std::strstr() is a predefined function used for string handling. string. h is the header file required for string functions. This function takes two strings s1 and s2 as an argument and finds the first occurrence of the sub-string s2 in the string s1.


2 Answers

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.

like image 92
Keith Thompson Avatar answered Oct 02 '22 04:10

Keith Thompson


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.

like image 39
edtheprogrammerguy Avatar answered Oct 02 '22 04:10

edtheprogrammerguy