While running log4cpp-1.1.2rc3 through a static code analyzer (required for our company) I got a defect that is most likely a bug.
In RemoteSyslogAppender.cpp line 118:
if ((_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
results in checking if the unsigned int _socket is less than zero - which it will never be.
A very sneaky tiny little bug :)
Thanks again to all developers for their outstanding work!
p.s. The static code analyer software is Coverity if that matters.
Thanks for the report, but POSIX socket() function may return negative:
See http://man7.org/linux/man-pages/man2/socket.2.html, for example
Hi Alexander - first thanks for looking into this issue!
To the question - in the case of error it doesn't return "any negative value" - it returns a specific value (negetive one to be exact).
I think the solution would be to compare the result with -1 as in this example:
http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
Even though it is not optimal to compare an unsigned int with a negative value the compiler will do the right thing at least.
Proof of concept:
Just for the sake of this experiment I moved the assignment on a separate line and looked at the compiled code:
*Note: I hadd to add a dummy statement after the if statement as the compiler wanted to optimize the code and removed it all - in the current form whether or not the result is an error is irrelevant because in both cases the function just returns and nothing else happens.
*
Thanks,
Vladimir
Vladimir,
I realized that you were talking about WIN32 platform, where socket is of type SOCKET which is in fact unsigned (in contrast to POSIX)
typedef UINT_PTR SOCKET;
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740506(v=vs.85).aspx
I'll make that check platform-dependent.
Thank you for noticing that.
Alexander
Yeah, Microsoft did get us a little surprise with that :)
Thanks Alexander!