commit e96e50d8fbc20e52354d63c2c785e645918d082d
parent c84a54bb008e5e273d33af4a94b65d64c7a18ddb
Author: ssnf <ssnf@ssnf.xyz>
Date: Thu, 31 Jul 2025 07:42:19 +0000
fix libvec bugs
Diffstat:
4 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/man/man3/vec.3 b/man/man3/vec.3
@@ -141,8 +141,10 @@ Vecclose(&v);
Vector of strings with cleanup:
.IP
.EX
+void str_free(void *p) { free(*(char**)p); }
+
char **v;
-Vecinitf(&v, nil, free);
+Vecinitf(&v, nil, str_free);
*(char**)Vecadd(&v) = strdup("hello");
*(char**)Vecadd(&v) = strdup("world");
Vecclose(&v);
diff --git a/src/libvec/Vecadd.c b/src/libvec/Vecadd.c
@@ -9,7 +9,7 @@ Vecadd_(Type *p, ulong ms)
Vecinsure_(p, ms, Vecsiz(*p) + 1);
v = Vec(*p);
- if (!v->init)
+ if (v->init)
v->init((char*)*p + v->n * ms);
else
memset((char*)*p + v->n * ms, 0, ms);
diff --git a/src/libvec/Vecdel.c b/src/libvec/Vecdel.c
@@ -12,8 +12,8 @@ Vecdel_(Type *p, ulong ms, ulong n)
if (!v->n)
return;
q = (char*)*p + n * ms;
- if (!v->close)
+ if (v->close)
v->close(q);
memmove(q, q + ms, (--v->n - n) * ms);
- memset(q + (v->n - n) * ms, 0, v->n * ms);
+ memset(q + (v->n - n) * ms, 0, ms);
}
diff --git a/src/libvec/Vecinit.c b/src/libvec/Vecinit.c
@@ -29,7 +29,7 @@ Vecclose_(Type *p, ulong ms)
Vector *v;
v = Vec(*p);
- if (!v->close)
+ if (v->close)
for (;v->n;)
v->close((char*)*p + --v->n * ms);
free(v);