Hacker News new | past | comments | ask | show | jobs | submit login

> I couldn't find any function that returns 0 on failure. Or do you mean a null pointer?

server_socket_getport

An unknown address family will return a '0'.

> You want to see a zero tolerance policy for them?

No, but if you use that mechanism then it is preferable to have all the functions hitting that buffer to be visible from the scope of the declaration of the buffer. Passing it on to other libraries can cause problems when/if those libraries' maintainers mess up. So if you're defensive about this then you can't be hurt that way.

> I don't think so,

Agreed, looking still longer it looks like it will always end up with a NULL in it after the while, but that's very ugly to put it mildly.

> Also, `ptr != NULL` is unnecessary (just mentioning since OpenBSD/LibreSSL developers like to mention that too).

Yep. And there are plenty of other nitpicks like that but I don't even care that much about any of those, I mostly care about the way that the code is laid out making it an excellent place to hide some really nasty bugs.




>server_socket_getport

I found this interesting article: http://daniel.haxx.se/blog/2014/10/25/pretending-port-zero-i...

So since 0 can be okay and (in_port_t)-1 (=65535) is okay, it has to be changed to return int and take a in_port_t* to be acceptable for you. The return value may not even matter if only AF_INET/AF_INET6 as family is possible - a comment could be of help here.


> An unknown address family will return a '0'.

In this particular case, returning 0 doesn't necessarily indicate failure. Binding a socket to port 0 means you're asking the operating system to pick an available port for you, which one might argue is a reasonably safe default for unknown address families.


No, there is no 'default' at play here. If you don't know what address family is in use then you should simply abort rather than to let the end user of your product guess that the address family code is the culprit.

Let it crash, as close as possible to the point of origin of a problem is a very good principle.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: