Is `std :: string (strerror (errno))` dangerous?

In some places in my code, I print debugging messages as follows:

int ret = getLinkSpeed(device.getSysName(), linkSpeed);
if (ret < 0)
{
    logDebug("Failed to obtain port speed for this device. Error: " + std::string(strerror(errno)));
}

It’s not clear from the documentation whether strerror will return 0 under certain conditions (which may cause my code to crash). Does anyone know if this is safe?

+3
source share
3 answers

Why not write a function for this:

string ErrStr() {
   char * e = strerror(errno);
   return e ? e : "";
}

It is easy to use, self-documenting, can be adapted to reformat the output and covers the possibility that strerror () can return NULL (I don't know if it can).

+11
source

(+1), . , , , POSIX.

, - GNU libc. , 0.

p00ya ( , -) , , , , , , .

+3

If you may have problems, this is if you are using a multi-threaded application. In this case you need to use strerror_r

+3
source

Source: https://habr.com/ru/post/1748974/


All Articles