3 ms·
Your errno checks aren't correct. errno is only set on an error. It's not cleared on success. If errno was previously set, the function will always abort().
by ksherlock 1y ago
Your errno checks aren't correct.
errno is only set on an error. It's not cleared on success. If errno was previously set, the function will always abort(). So you need to do something like:
int saved_errno = errno;
errno = 0;
....
errno = saved_errno;
return aInt;
- ben0x539 1y agoDoesn't it set errno to 0 first thing?
- kazinator 1y agoNo ISO C standard library function sets errno to zero, but functions not in the standard library are not so obliged. Functions that clobber errno to zero make it impossible to call several functions and use a nonzero value of errno to conclude that one or more of them went wrong. If you don't think that such code is a good idea (for instance because you believe that every function that can fail should be checked for its specific error), then you probably have nothing against functions which clobber errno to zero.
- rantouan 1y agohello, errno is set to 0 first thing. I added now a "/** * * */" separator to make it even more clear. I don't know, but I like separating things like that, I think it's handy for cases like now that setting the errno was kind of mixed with vars initialization. ``` errno = 0; char *strtolEndptr; ... ``` -> ``` errno = 0; /*** * * ***/ char *strtolEndptr; ... ```
- rantouan 1y agoFor the restoring part, could you make a pull request please ? Open source strength is that we work together on things.