[lib] owl_close_fd(): handle negative fd

This commit is contained in:
Matteo Cypriani 2014-10-31 12:59:39 -04:00
parent fe84a5f097
commit c567592167
1 changed files with 7 additions and 4 deletions

View File

@ -413,6 +413,8 @@ float owl_swap_float(const float f)
/** /**
* Opens a UDP transmission socket and returns its descriptor. * Opens a UDP transmission socket and returns its descriptor.
* Uppon error, a message is displayed and a negative error code is
* returned.
* *
* @param[in] server_address The server's IP address. * @param[in] server_address The server's IP address.
* @param[in] server_port The listening port on the server. * @param[in] server_port The listening port on the server.
@ -585,15 +587,16 @@ void owl_sigterm_handler(const int num)
/** /**
* Closes the file descriptor `fd`. * Closes the file descriptor `fd`.
* `fd` must be passed as an int pointer (`int*`). If `fd` is `NULL`, * `fd` must be passed as an int pointer (`int*`). If `fd` is `NULL`,
* nothing will be done. Uppon error, a message is displayed on the * or if the pointed value is negative, nothing will be done.
* standard error. * Uppon error, a message is displayed on the standard error.
*/ */
void owl_close_fd(void *const fd) void owl_close_fd(void *const fd)
{ {
if (fd == NULL) const int *const file_desc = fd;
if (file_desc == NULL || *file_desc < 0)
return ; return ;
const int *const file_desc = fd ;
if (close(*file_desc) != 0) if (close(*file_desc) != 0)
perror("Error closing file descriptor") ; perror("Error closing file descriptor") ;
#ifndef NDEBUG #ifndef NDEBUG