ref: 7aef7a1b25479f97f3003cfca573053e636043b9
parent: 955bf7ef1e2d5c115a6e1752f976d10a7481e260
author: qwx <qwx@sciops.net>
date: Sat Mar 26 05:36:14 EDT 2022
vec: fix memory corruption and legibility improvements
--- a/asif.h
+++ b/asif.h
@@ -22,8 +22,8 @@
typedef struct VShard VShard;
typedef struct Vector Vector;
struct VShard{
- void *p;
- void *head;
+ uchar *p;
+ uchar *head;
int len;
VShard *prev;
VShard *next;
--- a/vec.c
+++ b/vec.c
@@ -26,7 +26,7 @@
VShard *s;
s = emalloc(sizeof *s);
- s->p = emalloc(v->elsz * Shardsz);
+ s->p = emalloc(Shardsz * v->elsz);
s->head = s->p;
shardlink(v, s);
return s;
@@ -53,16 +53,18 @@
assert(s != &v->vl);
assert(i >= 0 && i < s->len);
- p = (uchar *)s->head + i * v->elsz;
+ p = s->head + i * v->elsz;
+ assert(p < s->p + Shardsz * v->elsz);
memcpy(v->tmp, p, v->elsz);
s->len--;
v->len--;
if(i == 0)
- s->head = (uchar *)s->head + v->elsz;
+ s->head += v->elsz;
if(s->len == 0){
shardunlink(v, s);
shardlink(v, s);
}
+ assert(s->head >= s->p && s->head < s->p + Shardsz * v->elsz);
return v->tmp;
}
@@ -69,7 +71,6 @@
void *
vechpop(Vector *v)
{
- uchar *p;
VShard *s;
if(v->len <= 0)
@@ -98,11 +99,13 @@
uchar *p;
VShard *s;
- for(s=v->vl.prev; s != &v->vl && s->len >= Shardsz; s=s->prev)
- ;
+ for(s=v->vl.prev; s != &v->vl; s=s->prev)
+ if(s->len + (s->head - s->p) / v->elsz < Shardsz)
+ break;
if(s == &v->vl)
s = shard(v);
- p = (uchar *)s->head + s->len * v->elsz;
+ p = s->head + s->len * v->elsz;
+ assert(p >= s->p && p < s->p + Shardsz * v->elsz);
memcpy(p, e, v->elsz);
s->len++;
v->len++;
@@ -111,7 +114,6 @@
void *
vecget(Vector *v, int i)
{
- uchar *p;
VShard *s;
assert(i >= 0 && i < v->len);
@@ -120,7 +122,7 @@
assert(s != &v->vl);
assert(s->len > 0);
assert(i >= 0 && i < s->len);
- return (uchar *)s->head + i * v->elsz;
+ return s->head + i * v->elsz;
}
Vector *
@@ -127,7 +129,6 @@
vec(int elsz)
{
Vector *v;
- VShard *s;
assert(elsz > 0);
v = emalloc(sizeof *v);