From 27a78044471e2bdfdba3219baf629033f83eb584 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 23 May 2022 23:30:52 -0500 Subject: [PATCH] Bugfix: Problems exposed by unit tests. Only call strncpy() if socket->name is defined. Only set socket->id to -1 for f_socket_close_fast_e. When received EINTR, do set socket->id to -1 for f_socket_close_fast_e. Be consistent and make length parameter optional for the socket read and write functions. --- level_0/f_socket/c/socket.c | 18 +++++++++++++----- level_0/f_socket/c/socket.h | 8 ++++---- level_0/f_socket/c/socket/common.h | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c index 47305ca..e4d9410 100644 --- a/level_0/f_socket/c/socket.c +++ b/level_0/f_socket/c/socket.c @@ -89,7 +89,12 @@ extern "C" { address->sun_family = f_socket_domain_file_d; - strncpy(address->sun_path, socket->name, sizeof(address->sun_path) - 1); + if (socket->name) { + strncpy(address->sun_path, socket->name, sizeof(address->sun_path) - 1); + } + else { + address->sun_path[0] = 0; + } } if (bind(socket->id, socket->address, sizeof(struct sockaddr_un)) == -1) { @@ -225,7 +230,7 @@ extern "C" { if (result == -1) { // According to man pages, retrying close() after another close on error is invalid on Linux because Linux releases the descriptor before stages that cause failures. - if (errno != EBADF && errno != EINTR) { + if (action == f_socket_close_fast_e && errno != EBADF) { socket->id = -1; } @@ -342,7 +347,9 @@ extern "C" { return F_status_set_error(F_failure); } - *length = (size_t) result; + if (length) { + *length = (size_t) result; + } return F_none; } @@ -376,7 +383,6 @@ extern "C" { if (errno == EPIPE) return F_status_set_error(F_pipe); if (errno == ETIMEDOUT) return F_status_set_error(F_time_out); - return F_status_set_error(F_failure); } @@ -422,7 +428,9 @@ extern "C" { return F_status_set_error(F_failure); } - *length = (size_t) result; + if (length) { + *length = (size_t) result; + } return F_none; } diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h index 7fdcf7c..357372d 100644 --- a/level_0/f_socket/c/socket.h +++ b/level_0/f_socket/c/socket.h @@ -187,7 +187,7 @@ extern "C" { /** * Create a new socket. * - * @param socket_structure + * @param socket * The socket structure. * The socket.address may point to any valid structure, like "struct sockaddr", "struct sockaddr_un", or "struct sockaddr_in". * The socket.domain must be assigned the desired domain. @@ -195,8 +195,6 @@ extern "C" { * The socket.protocol must be assigned the desired protocol. * The socket.id will be updated with a file descriptor representing the created socket. * - * This is named "socket_structure" rather than "socket" due to a conflict with the function "socket()". - * * @return * F_none on success. * @@ -215,7 +213,7 @@ extern "C" { * @see socket() */ #ifndef _di_f_socket_create_ - extern f_status_t f_socket_create(f_socket_t * const socket_structure); + extern f_status_t f_socket_create(f_socket_t * const socket); #endif // _di_f_socket_create_ /** @@ -453,6 +451,7 @@ extern "C" { * @param header * The message header. * @param length + * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length read. * Data may be lost if the amount of data read is larger than given buffer length. * @@ -544,6 +543,7 @@ extern "C" { * @param header * The message header. * @param length + * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length send. * Data may be lost if the amount of data send is larger than given buffer length. * diff --git a/level_0/f_socket/c/socket/common.h b/level_0/f_socket/c/socket/common.h index 978c8d5..41f2ccd 100644 --- a/level_0/f_socket/c/socket/common.h +++ b/level_0/f_socket/c/socket/common.h @@ -291,7 +291,7 @@ extern "C" { * domain: The socket domain (protocol family). * flag: Flags used for opening the file. * id: File descriptor, with a value of -1 represents a closed file. - * name: The name of the socket, if a name is given (for UNIX sockets this represents the path). + * name: The name of the socket, if a name is given (for UNIX sockets this represents the path) (Must be a NULL terminated string). * protocol: The socket protocol. * size_read: The default number of 1-byte characters to read at a time and is often used for the read buffer size. * size_write: The default number of 1-byte characters to read at a time and is often used for the write buffer size. -- 1.8.3.1