]> Kevux Git Server - fll/commitdiff
Update: Add check in f_socket_disconnect() to explicitly check socket file descriptor.
authorKevin Day <thekevinday@gmail.com>
Wed, 6 Sep 2023 05:09:04 +0000 (00:09 -0500)
committerKevin Day <thekevinday@gmail.com>
Wed, 6 Sep 2023 05:09:04 +0000 (00:09 -0500)
Avoid depending on libc calls and instead explicitly check the socket file descriptor.
Return F_file_descriptor_not when -1.
Add unit test for this.

level_0/f_socket/c/socket.c
level_0/f_socket/c/socket.h
level_0/f_socket/tests/unit/c/test-socket-disconnect.c
level_0/f_socket/tests/unit/c/test-socket-disconnect.h
level_0/f_socket/tests/unit/c/test-socket.c

index 8a12998fde364bc1f423dd05605fe9eb943fd0af..fdff250bd7a1f4a90cf3131d9b8816669639dc35 100644 (file)
@@ -265,6 +265,8 @@ extern "C" {
       if (!socket) return F_status_set_error(F_parameter);
     #endif // _di_level_0_parameter_checking_
 
+    if (socket->id == -1) return F_file_descriptor_not;
+
     int result = 0;
 
     if (action == f_socket_close_fast_e) {
index f8bf5c37e7015ca8e7d4690da5d03949739c7fd3..4abc7b44953e20bd7600e67334569c80714dc910 100644 (file)
@@ -376,6 +376,7 @@ extern "C" {
  * @return
  *   F_okay on success.
  *   F_connect_not if the socket is not connected.
+ *   F_file_descriptor_not if socket.id is -1.
  *
  *   F_busy_address (with error bit) if address is already in use (therefore unavailable).
  *   F_file_descriptor (with error bit) if id is an invalid descriptor.
index da567a02d094e1c7573f27d601055903760be7a2..32833b4b19041d58fe4ea021f54cef29abb2b308 100644 (file)
@@ -157,6 +157,17 @@ void test__f_socket_disconnect__parameter_checking(void **state) {
   }
 }
 
+void test__f_socket_disconnect__returns_file_descriptor_not(void **state) {
+
+  {
+    f_socket_t socket = f_socket_t_initialize;
+
+    const f_status_t status = f_socket_disconnect(&socket, 0);
+
+    assert_int_equal(status, F_file_descriptor_not);
+  }
+}
+
 void test__f_socket_disconnect__works_for_close_fast(void **state) {
 
   f_socket_t socket = f_socket_t_initialize;
index a1093a73d1a7e50b06ef20e213333c919863e822..75e7447aaf6bde49a0b4cb4e21b563c460aedcfb 100644 (file)
@@ -46,6 +46,13 @@ extern void test__f_socket_disconnect__fails_for_close_write(void **state);
 extern void test__f_socket_disconnect__parameter_checking(void **state);
 
 /**
+ * Test that the function returns F_file_descriptor_not.
+ *
+ * @see f_socket_disconnect()
+ */
+extern void test__f_socket_disconnect__returns_file_descriptor_not(void **state);
+
+/**
  * Test that function works for f_socket_close_fast_e.
  *
  * @see f_socket_disconnect()
index a8f8e63bf365893269c721290ba2be5cf4fa4c67..4273decd32b39561ba4adeef2ef4113875b9f481 100644 (file)
@@ -47,6 +47,9 @@ int main(void) {
     cmocka_unit_test(test__f_socket_disconnect__fails_for_close_read),
     cmocka_unit_test(test__f_socket_disconnect__fails_for_close_unknown),
     cmocka_unit_test(test__f_socket_disconnect__fails_for_close_write),
+
+    cmocka_unit_test(test__f_socket_disconnect__returns_file_descriptor_not),
+
     cmocka_unit_test(test__f_socket_disconnect__works_for_close_fast),
     cmocka_unit_test(test__f_socket_disconnect__works_for_close_read),
     cmocka_unit_test(test__f_socket_disconnect__works_for_close_write),