str: drop argument checks no caller can trip
sinit's nil and negative-length tests and stoutf's zero-size test only had test callers; every real caller passes a buffer and its size. The len > n check after fullrune succeeded could never fire.
This commit is contained in:
15
str.c
15
str.c
@@ -1,6 +1,7 @@
|
||||
#include "dat.h"
|
||||
#include "fn.h"
|
||||
|
||||
/* Fills s from n bytes of UTF-8; whole, valid, and at most Maxrunes. */
|
||||
int
|
||||
sinit(Str *s, char *src, int n)
|
||||
{
|
||||
@@ -8,19 +9,12 @@ sinit(Str *s, char *src, int n)
|
||||
Rune r;
|
||||
int len;
|
||||
|
||||
if(s == nil)
|
||||
return 0;
|
||||
s->n = 0;
|
||||
if(n < 0 || (n > 0 && src == nil))
|
||||
return 0;
|
||||
while(n > 0){
|
||||
if(tmp.n >= Maxrunes)
|
||||
return 0;
|
||||
if(!fullrune(src, n))
|
||||
if(tmp.n >= Maxrunes || !fullrune(src, n))
|
||||
return 0;
|
||||
len = chartorune(&r, src);
|
||||
if(len > n || (r == Runeerror && len == 1) ||
|
||||
(r >= 0xd800 && r <= 0xdfff))
|
||||
if((r == Runeerror && len == 1) || (r >= 0xd800 && r <= 0xdfff))
|
||||
return 0;
|
||||
tmp.r[tmp.n++] = r;
|
||||
src += len;
|
||||
@@ -75,14 +69,13 @@ scmp(Str *a, Str *b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* UTF-8 of s into buf[sz], NUL-terminated, whole runes only. */
|
||||
int
|
||||
stoutf(Str *s, char *buf, int sz)
|
||||
{
|
||||
char tmp[UTFmax];
|
||||
int i, n, len;
|
||||
|
||||
if(sz <= 0)
|
||||
return 0;
|
||||
n = 0;
|
||||
for(i = 0; i < s->n; i++){
|
||||
len = runetochar(tmp, &s->r[i]);
|
||||
|
||||
@@ -76,7 +76,6 @@ str_utf8_capacity(struct ct *t)
|
||||
int n;
|
||||
char *want;
|
||||
} cases[] = {
|
||||
{ 0, 0, nil },
|
||||
{ 1, 0, "" },
|
||||
{ 2, 1, "a" },
|
||||
{ 3, 1, "a" },
|
||||
@@ -95,10 +94,6 @@ str_utf8_capacity(struct ct *t)
|
||||
if(n != cases[i].n)
|
||||
CT_ERRORF(t, "size %d: want length %d, got %d",
|
||||
cases[i].size, cases[i].n, n);
|
||||
if(cases[i].size == 0){
|
||||
CT_EQ_INT(t, 'Z', buf[0]);
|
||||
continue;
|
||||
}
|
||||
if(strcmp(cases[i].want, buf) != 0)
|
||||
CT_ERRORF(t, "size %d: want \"%s\", got \"%s\"",
|
||||
cases[i].size, cases[i].want, buf);
|
||||
@@ -120,10 +115,6 @@ str_invalid_and_full_appends(struct ct *t)
|
||||
Str s;
|
||||
int i;
|
||||
|
||||
CT_CHECK(t, !sinit(&s, nil, 1));
|
||||
CT_EQ_INT(t, 0, s.n);
|
||||
CT_CHECK(t, !sinit(&s, "x", -1));
|
||||
CT_EQ_INT(t, 0, s.n);
|
||||
for(i = 0; i < Maxrunes; i++)
|
||||
s.r[i] = 'a';
|
||||
s.n = Maxrunes;
|
||||
|
||||
Reference in New Issue
Block a user