commit fbb983f25f2d15d10fb878ee24116cb76ce1dee1
parent 9524898ffaacc3ffd1f6b61f5dace2edc6b86329
Author: glenda <glenda@9front.local>
Date: Wed, 25 Aug 2021 13:30:08 +0000
fix crash on big text inserts
Diffstat:
2 files changed, 8 insertions(+), 12 deletions(-)
diff --git a/array.c b/array.c
@@ -56,7 +56,7 @@ arraygrow(Array *ap, long n, void *v)
{
assert(ap->magic == MAGIC);
- void *ve;
+ char *ve;
if (n < 0) {
werrstr("arraygrow: negative growth size");
return nil;
@@ -68,21 +68,19 @@ arraygrow(Array *ap, long n, void *v)
qlock(ap->l);
ap->count += n;
- if (ap->count > ap->n) {
- ap->n += ap->n;
- if (ap->count > ap->n)
- ap->n = ap->count;
+ if (ap->count >= ap->n) {
+ ap->n = 2 * ap->count;
ap->p = realloc(ap->p, ap->size * ap->n);
}
- ve = (void *)(ap->p + ap->size * (ap->count - n));
+ ve = ap->p + ap->size * (ap->count - n);
memset(ve, 0, n * ap->size);
if (v != nil) {
memcpy(ve, v, n * ap->size);
}
qunlock(ap->l);
- return ve;
+ return (void *)ve;
}
int
diff --git a/richterm.c b/richterm.c
@@ -819,9 +819,6 @@ objsettext(Object *obj, char *data, long count)
long n, m, dn;
char *p, *pe;
- p = arrayget(rich.text, obj->offset, nil);
- pe = arrayend(rich.text);
-
n = objtextlen(obj);
m = count;
dn = m - n;
@@ -829,14 +826,15 @@ objsettext(Object *obj, char *data, long count)
if (dn > 0) arraygrow(rich.text, dn, nil);
else rich.text->count += dn;
+ p = arrayget(rich.text, obj->offset, nil);
+ pe = arrayget(rich.text, rich.text->count - dn, nil);
+
qlock(rich.text->l);
if (p != nil) memcpy(p + m, p + n, pe - p);
else p = pe;
memcpy(p, data, count);
qunlock(rich.text->l);
- assert(rich.objects->magic == 0x1234);
-
qlock(rich.objects->l);
for (obj = obj->next; obj != nil; obj = obj->next) {
obj->offset += dn;