Hi I’m working with C++ on Linux platform, i have to get the value of a environment variable(user defined) and use it further.
Following is the code I’m using,
const char *show_line = getenv ("MY_SHOW_LINES");
bool myFlag = (strcmp(show_line, "1") == 0) ? false : true;
Above code executes properly when i set the value of environment variable(MY_SHOW_LINES) equal to 1 but when i unset the value of it(i.e. unset MY_SHOW_LINES).
Above code gives memory fault.
Any suggestion on above UN-expected behavior??
Thanks in advance
If you pass a NULL pointer to
strcmp, you get undefined behavior, in this case a very likely crash.Are you aware that pointers can be
NULL, i.e. purposely invalid?You should guard against this possibility: