plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

commit edbdfca9ae880879e0830f8eaf2a37abaeca4b54
parent e96e50d8fbc20e52354d63c2c785e645918d082d
Author: ssnf <ssnf@ssnf.xyz>
Date:   Thu, 31 Jul 2025 08:02:02 +0000

fix libstr bugs

Diffstat:
Msrc/libstr/Strdelete.c | 2++
Msrc/libstr/Strgets.c | 11+++++++----
Msrc/libstr/Strinsert.c | 2++
3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/libstr/Strdelete.c b/src/libstr/Strdelete.c @@ -10,6 +10,8 @@ Strdelc(String *p) void Strdelete(String *p, Posn p0, Posn p1) { + if (p0 > p1 || p1 > p->n) + sysfatal("Strdelete: bad range"); memmove(p->s + p0, p->s + p1, p->n - p1); p->n -= p1 - p0; p->s[p->n] = '\0'; diff --git a/src/libstr/Strgets.c b/src/libstr/Strgets.c @@ -4,12 +4,15 @@ int Strgets(String *p, Biobuf *b) { char *s; + ulong n; Strzero(p); s = Brdline(b, '\n'); - p->n = Blinelen(b); - if (p->n) - Stradds(p, Strn(s, p->n)); - return p->n; + n = Blinelen(b); + if (n && s[n - 1] == '\n') + --n; + if (n) + Stradds(p, Strn(s, n)); + return n; } diff --git a/src/libstr/Strinsert.c b/src/libstr/Strinsert.c @@ -3,6 +3,8 @@ void Strinsert(String *p, String q, Posn p0) { + if (p0 > p->n) + sysfatal("Strinsert: bad position"); Strinsure(p, p->n + q.n); memmove(p->s + p0 + q.n, p->s + p0, p->n - p0); memmove(p->s + p0, q.s, q.n);