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]);
|
||||
|
||||
Reference in New Issue
Block a user