diff seobeo/s_network.c @ 251:117c4d53c9a4

[ui] Add HTML-first Web Component system Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Tue, 04 Aug 2026 09:14:57 -0700
parents b8aa08503378
children 609d3c6aff4e
line wrap: on
line diff
--- a/seobeo/s_network.c	Tue Aug 04 06:23:37 2026 -0700
+++ b/seobeo/s_network.c	Tue Aug 04 09:14:57 2026 -0700
@@ -41,7 +41,8 @@
   pthread_once(&g_sigpipe_once, Seobeo_Process_Ignore_Sigpipe);
   Seobeo_Handle *p_handle;
   struct addrinfo hints, *server_infos, *free_server_info;
-  int32 socket_fd, yes = 1;  // Need this for setsockopt 
+  int32 socket_fd = -1;
+  int32 yes = 1;
 
   memset(&hints, 0, sizeof hints);
   hints.ai_family   = AF_UNSPEC;
@@ -70,20 +71,39 @@
     Seobeo_Socket_Disable_Sigpipe(socket_fd);
 
      if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
-     { perror("setsockopt SO_REUSEADDR"); continue; }
+     {
+       perror("setsockopt SO_REUSEADDR");
+       close(socket_fd);
+       socket_fd = -1;
+       continue;
+     }
 
 #ifdef SO_REUSEPORT
      // SO_REUSEPORT allows multiple threads/processes to bind to the same port
      // The kernel will distribute incoming connections among them
      if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) == -1)
-     { perror("setsockopt SO_REUSEPORT"); continue; }
+     {
+       perror("setsockopt SO_REUSEPORT");
+       close(socket_fd);
+       socket_fd = -1;
+       continue;
+     }
 #endif
 
      if (bind(socket_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1)
-     { perror("v_network: Couldn't make socket non-blocking\n"); continue; }
+     {
+       perror("bind");
+       close(socket_fd);
+       socket_fd = -1;
+       continue;
+     }
 
      break;
   }
+  freeaddrinfo(server_infos);
+
+  if (socket_fd < 0)
+    return NULL;
 
   if (listen(socket_fd, 16) != 0)
   { 
@@ -92,15 +112,25 @@
   }
 
   int flags = fcntl(socket_fd, F_GETFL, 0);
-	if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; }
-  freeaddrinfo(server_infos);
+  if (flags < 0 ||
+      fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0)
+  {
+    perror("fcntl");
+    close(socket_fd);
+    return NULL;
+  }
 
-  p_handle = malloc(sizeof(*p_handle));
+  p_handle = calloc(1, sizeof(*p_handle));
+  if (!p_handle)
+  {
+    close(socket_fd);
+    return NULL;
+  }
   p_handle->socket = socket_fd;
   p_handle->type = SEOBEO_STREAM_TYPE_SERVER;
   p_handle->connected = FALSE;
 
-  p_handle->host = host != NULL ? strdup(host) : "localhost";
+  p_handle->host = strdup(host != NULL ? host : "localhost");
   p_handle->port = strdup(port);
 
   p_handle->ssl_ctx              = NULL;
@@ -117,6 +147,15 @@
 
   p_handle->destroyed = FALSE;
 
+  if (!p_handle->host ||
+      !p_handle->port ||
+      !p_handle->read_buffer ||
+      !p_handle->write_buffer)
+  {
+    Seobeo_Handle_Destroy(p_handle);
+    return NULL;
+  }
+
   return p_handle;
 }
 
@@ -258,7 +297,7 @@
 
   Seobeo_SSL_Cleanup(p_handle);
 
-  if (p_handle->socket) {
+  if (p_handle->socket >= 0) {
     Seobeo_Log(SEOBEO_DEBUG, "Closing handle socket: %d\n", p_handle->socket);
     close(p_handle->socket);
   }