Commit 9c175f04d07bcd65a24f5319140844c47cfcbf98

Thomas de Grivel 2024-07-28T23:36:38

make test_http

diff --git a/Makefile b/Makefile
index a57ffd5..5b5ccb4 100644
--- a/Makefile
+++ b/Makefile
@@ -182,6 +182,15 @@ gdb_test_http:
 	${MAKE} -C http debug
 	${MAKE} -C test gdb_test_http
 
+gdb_test_http_asan:
+	${MAKE} -C libtommath asan
+	${MAKE} -C ucd2c
+	${MAKE} -C libkc3 asan
+	${MAKE} -C ikc3 asan
+	${MAKE} -C kc3s asan
+	${MAKE} -C http asan
+	${MAKE} -C test gdb_test_http_asan
+
 gdb_test_ikc3:
 	${MAKE} -C libtommath debug
 	${MAKE} -C ucd2c
diff --git a/http/socket.c b/http/socket.c
index 2cc11a6..2e47430 100644
--- a/http/socket.c
+++ b/http/socket.c
@@ -99,11 +99,13 @@ p_socket socket_init_listen (p_socket s, const s_str *host,
       error_reason = "socket_init_listen: listen: ";
       goto next;
     }
+    freeaddrinfo(res0);
     *s = sockfd;
     return s;
   next:
     res = res->ai_next;
   }
+  freeaddrinfo(res0);
   err_write_1(error_reason);
   err_puts(strerror(e));
   assert(! "socket_init_listen");
diff --git a/http/socket_buf.c b/http/socket_buf.c
index a661e8a..da32e5e 100644
--- a/http/socket_buf.c
+++ b/http/socket_buf.c
@@ -22,6 +22,15 @@
 void socket_buf_clean (s_socket_buf *sb)
 {
   assert(sb);
+  buf_rw_clean(&sb->buf_rw);
+  if (sb->addr)
+    socket_addr_delete(sb->addr);
+}
+
+void socket_buf_close (s_socket_buf *sb)
+{
+  assert(sb);
+  buf_rw_fd_close(&sb->buf_rw);
   close(sb->sockfd);
   buf_rw_clean(&sb->buf_rw);
   if (sb->addr)
diff --git a/http/socket_buf.h b/http/socket_buf.h
index e5989d4..91f4027 100644
--- a/http/socket_buf.h
+++ b/http/socket_buf.h
@@ -18,6 +18,7 @@
 /* Stack-allocation compatible functions, call socket_buf_clean after
    use. */
 void           socket_buf_clean (s_socket_buf *sb);
+void           socket_buf_close (s_socket_buf *sb);
 s_socket_buf * socket_buf_init (s_socket_buf *sb, t_socket sockfd,
                                 struct sockaddr *addr, u32 addr_len);
 s_socket_buf * socket_buf_init_connect (s_socket_buf *sb,
diff --git a/libkc3/cfn.c b/libkc3/cfn.c
index 468f95d..a6fde5a 100644
--- a/libkc3/cfn.c
+++ b/libkc3/cfn.c
@@ -135,6 +135,7 @@ s_tag * cfn_apply (s_cfn *cfn, s_list *args, s_tag *dest)
         assert(! "cfn_apply: invalid result pointer");
         goto ko;
       }
+      tag_clean(&tmp);
       *dest = tmp2;
     }
     else
diff --git a/libkc3/struct.c b/libkc3/struct.c
index e82ec13..5965c2b 100644
--- a/libkc3/struct.c
+++ b/libkc3/struct.c
@@ -41,8 +41,7 @@ s_tag * struct_access (const s_struct *s, const s_sym *key, s_tag *dest)
     return NULL;
   if (st) {
     tmp.data.struct_.type = st;
-    if (! tmp.data.struct_.data &&
-        ! struct_allocate(&tmp.data.struct_))
+    if (! struct_allocate(&tmp.data.struct_))
       return NULL;
   }
   if (! tag_to_pointer(&tmp, type, &tmp_data))
diff --git a/libkc3/tag.c b/libkc3/tag.c
index ce9025f..2082e52 100644
--- a/libkc3/tag.c
+++ b/libkc3/tag.c
@@ -1242,7 +1242,16 @@ bool tag_to_pointer (s_tag *tag, const s_sym *type, void **dest)
   case TAG_PTR_FREE:    *dest = &tag->data.ptr_free.p;  return true;
   case TAG_QUOTE:       *dest = &tag->data.quote;       return true;
   case TAG_STR:         *dest = &tag->data.str;         return true;
-  case TAG_STRUCT:      *dest = &tag->data.struct_;     return true;
+  case TAG_STRUCT:
+    if (type == &g_sym_Struct) {
+      *dest = &tag->data.struct_;
+      return true;
+    }
+    if (type == tag->data.struct_.type->module) {
+      *dest = tag->data.struct_.data;
+      return true;
+    }
+    goto invalid_cast;
   case TAG_STRUCT_TYPE: *dest = &tag->data.struct_type; return true;
   case TAG_SYM:         *dest = &tag->data.sym;         return true;
   case TAG_TUPLE:       *dest = &tag->data.tuple;       return true;
@@ -1253,6 +1262,17 @@ bool tag_to_pointer (s_tag *tag, const s_sym *type, void **dest)
   err_puts("tag_to_pointer: invalid tag type");
   assert(! "tag_to_pointer: invalid tag type");
   return false;
+ invalid_cast:
+  err_write_1("tag_to_pointer: invalid cast from ");
+  if (tag->type == TAG_STRUCT)
+    err_inspect_sym(&tag->data.struct_.type->module);
+  else
+    err_write_1(tag_type_to_string(tag->type));
+  err_write_1(" to ");
+  err_inspect_sym(&type);
+  err_write_1("\n");
+  assert(! "tag_to_pointer: invalid cast");
+  return false;
 }
 
 const s_sym ** tag_type (const s_tag *tag, const s_sym **dest)
diff --git a/libtommath b/libtommath
index 3bd6700..d481898 160000
--- a/libtommath
+++ b/libtommath
@@ -1 +1 @@
-Subproject commit 3bd6700b68f2f8ebbc56d8990e5aef0d69b2ad3b
+Subproject commit d4818983e34f7faf8da74643027dc70e5a1beec7
diff --git a/test/Makefile b/test/Makefile
index 8de70e0..02926eb 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -80,6 +80,9 @@ gdb_test_ekc3:
 gdb_test_http:
 	cd http && ${GDB} ../../ikc3/.libs/ikc3_debug
 
+gdb_test_http_asan:
+	cd http && ${GDB} ../../ikc3/.libs/ikc3_asan
+
 gdb_test_ikc3:
 	cd ikc3 && ${GDB} ../../ikc3/.libs/ikc3_debug