summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2019-02-18 08:32:43 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2019-02-18 08:32:43 (GMT)
commit7d66d87691efd2004306c57624d61a0391056cc5 (patch) (side-by-side diff)
treeee01f286294717504cd2593bc7df75582b349bb3
parentbbdaff2c31a5cffe362cffdc1147546ac25a6f06 (diff)
downloaddico-7d66d87691efd2004306c57624d61a0391056cc5.tar.gz
dico-7d66d87691efd2004306c57624d61a0391056cc5.tar.bz2
Modify utf8 iterator to correctly handle non-zero-terminated strings.
* include/dico/utf8.h (utf8_iterator): New fields: length, err, end. (utf8_iter_err_p,utf8_iter_init): New prototype. * lib/utf8.c (utf8_iter0): Add boundary checking. Set end and error flags as appropriate. (utf8_iter_err_p): Rewrite. (utf8_iter_err_p): New function. (utf8_iter_init): New function. (utf8_iter_first): Rewrite using utf8_iter_init. (utf8_iter_next): Update length. * lib/linetrimstr.c (_linetrimstr_find_end): Don't assume zero-terminated string. * modules/dict.org/dictorg.c (revert_word): Likewise. * modules/outline/outline.c (revert_word): Likewise.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/dico/utf8.h5
-rw-r--r--lib/linetrimstr.c21
-rw-r--r--lib/utf8.c37
-rw-r--r--modules/dict.org/dictorg.c2
-rw-r--r--modules/outline/outline.c2
5 files changed, 46 insertions, 21 deletions
diff --git a/include/dico/utf8.h b/include/dico/utf8.h
index 953a4f1..807f6b7 100644
--- a/include/dico/utf8.h
+++ b/include/dico/utf8.h
@@ -27,12 +27,17 @@ struct utf8_iterator {
char *string;
char *curptr;
unsigned curwidth;
+ size_t length;
+ int end:1;
+ int err:1;
};
#define utf8_iter_isascii(itr) \
((itr).curwidth == 1 && isascii((itr).curptr[0]))
int utf8_iter_end_p(struct utf8_iterator *itr);
+int utf8_iter_err_p(struct utf8_iterator *itr);
+int utf8_iter_init(struct utf8_iterator *itr, char *ptr, size_t size);
int utf8_iter_first(struct utf8_iterator *itr, char *ptr);
int utf8_iter_next(struct utf8_iterator *itr);
diff --git a/lib/linetrimstr.c b/lib/linetrimstr.c
index 3ed8483..7cd100e 100644
--- a/lib/linetrimstr.c
+++ b/lib/linetrimstr.c
@@ -37,18 +37,14 @@ static size_t
_linetrimstr_find_end(struct _linetrimstr *s, const char *buf, size_t size,
size_t *psize)
{
- const char *end = buf + size;
struct utf8_iterator itr;
const char *wordstart = buf;
- utf8_iter_first(&itr, (char*)buf);
- do {
- for (; utf8_iter_isascii(itr) && ISWS(*itr.curptr);
+ utf8_iter_init(&itr, (char*)buf, size);
+ while (!utf8_iter_end_p(&itr)) {
+ for (; !utf8_iter_end_p(&itr)
+ && utf8_iter_isascii(itr) && ISWS(*itr.curptr);
utf8_iter_next(&itr)) {
- if (itr.curptr >= end) {
- *psize = itr.curptr - buf;
- return 0;
- }
if (*itr.curptr == '\n')
s->linelen = 0;
else if (++s->linelen >= s->maxlen) {
@@ -59,12 +55,9 @@ _linetrimstr_find_end(struct _linetrimstr *s, const char *buf, size_t size,
}
wordstart = itr.curptr;
- for (; !(utf8_iter_isascii(itr) && ISWS(*itr.curptr));
+ for (; !utf8_iter_end_p(&itr)
+ && !(utf8_iter_isascii(itr) && ISWS(*itr.curptr));
utf8_iter_next(&itr)) {
- if (itr.curptr >= end) {
- *psize = itr.curptr - buf;
- return 0;
- }
if (++s->linelen >= s->maxlen) {
size_t size = itr.string == wordstart ?
itr.curptr - buf : wordstart - itr.string;
@@ -75,7 +68,7 @@ _linetrimstr_find_end(struct _linetrimstr *s, const char *buf, size_t size,
}
}
}
- } while (itr.curptr < end);
+ }
*psize = itr.curptr - buf;
return 0;
}
diff --git a/lib/utf8.c b/lib/utf8.c
index 3ba6048..999bce1 100644
--- a/lib/utf8.c
+++ b/lib/utf8.c
@@ -1540,9 +1540,19 @@ utf8_strlen(const char *s)
static int
utf8_iter0(struct utf8_iterator *itr)
{
- size_t n = utf8_char_width(itr->curptr);
- if (n == 0)
+ size_t n;
+
+ if (itr->length == 0) {
+ itr->end = 1;
+ return 1;
+ }
+ n = utf8_char_width(itr->curptr);
+ if (n > itr->length) {
+ itr->end = 1;
+ itr->err = 1;
return 1;
+ }
+
itr->curwidth = n;
return 0;
}
@@ -1550,23 +1560,40 @@ utf8_iter0(struct utf8_iterator *itr)
int
utf8_iter_end_p(struct utf8_iterator *itr)
{
- return *itr->curptr == 0;
+ return itr->end;
}
int
-utf8_iter_first(struct utf8_iterator *itr, char *ptr)
+utf8_iter_err_p(struct utf8_iterator *itr)
+{
+ return itr->end;
+}
+
+int
+utf8_iter_init(struct utf8_iterator *itr, char *ptr, size_t size)
{
itr->string = ptr;
itr->curptr = ptr;
+ itr->length = size;
+ itr->curwidth = 0;
+ itr->err = 0;
+ itr->end = 0;
return utf8_iter0(itr);
}
int
+utf8_iter_first(struct utf8_iterator *itr, char *ptr)
+{
+ return utf8_iter_init(itr, ptr, strlen(ptr));
+}
+
+int
utf8_iter_next(struct utf8_iterator *itr)
{
- if (*itr->curptr == 0)
+ if (utf8_iter_end_p(itr))
return -1;
itr->curptr += itr->curwidth;
+ itr->length -= itr->curwidth;
return utf8_iter0(itr);
}
diff --git a/modules/dict.org/dictorg.c b/modules/dict.org/dictorg.c
index ee9c26f..7eacfbc 100644
--- a/modules/dict.org/dictorg.c
+++ b/modules/dict.org/dictorg.c
@@ -527,7 +527,7 @@ revert_word(char *dst, const char *src, size_t len)
char *p = dst + len;
*p = 0;
- for (utf8_iter_first(&itr, (char *)src);
+ for (utf8_iter_init(&itr, (char *)src, len);
!utf8_iter_end_p(&itr);
utf8_iter_next(&itr)) {
p -= itr.curwidth;
diff --git a/modules/outline/outline.c b/modules/outline/outline.c
index 18e7dd3..e27530a 100644
--- a/modules/outline/outline.c
+++ b/modules/outline/outline.c
@@ -267,7 +267,7 @@ revert_word(char *dst, const char *src, size_t len)
char *p = dst + len;
*p = 0;
- for (utf8_iter_first(&itr, (char *)src);
+ for (utf8_iter_init(&itr, (char *)src, len);
!utf8_iter_end_p(&itr);
utf8_iter_next(&itr)) {
p -= itr.curwidth;

Return to:

Send suggestions and report system problems to the System administrator.