misc/78256: strstr could be more robust

Dan Allen danallen46 at airwired.net
Tue Mar 1 22:20:13 GMT 2005

>Number:         78256
>Category:       misc
>Synopsis:       strstr could be more robust
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 01 22:20:12 GMT 2005
>Originator:     Dan Allen
>Release:        5.4 PRERELEASE
strstr does not check to see if its first argument is null or not.  The ISO standard is silent about error checking the first argument, but it seems to me that a single line of code could make strstr more robust.
char *p = getenv("TERM");
int i = strstr(p,"cons25") ? 1 : 0;

Runs fine from a console or from xterm under X-Windows, but if in a tool running under cron where TERM is not defined it causes a segmentation fault.  I know that I should test for a null string from getenv, but it seems that a bullet-proof strstr should have the fix shown below...
Add as the first line of code in strstr:

if (!s) return NULL;

to fix the problem.        

More information about the freebsd-bugs mailing list