Fix potential overflow in checkType

This commit is contained in:
Tatsuya Kinoshita
2022-12-20 21:16:53 +09:00
parent 419ca82d57
commit 4d813002c3

22
etc.c
View File

@@ -297,7 +297,9 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
char *sp = str, *ep; char *sp = str, *ep;
s = Strnew_size(s->length); s = Strnew_size(s->length);
do_copy = TRUE; do_copy = TRUE;
ep = bs ? (bs - 2) : endp; ep = endp;
if (bs && ep > bs - 2)
ep = bs - 2;
#ifdef USE_ANSI_COLOR #ifdef USE_ANSI_COLOR
if (es && ep > es - 2) if (es && ep > es - 2)
ep = es - 2; ep = es - 2;
@@ -318,6 +320,10 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
if (!do_copy) { if (!do_copy) {
for (; str < endp && IS_ASCII(*str); str++) { for (; str < endp && IS_ASCII(*str); str++) {
*(prop++) = PE_NORMAL | (IS_CNTRL(*str) ? PC_CTRL : PC_ASCII); *(prop++) = PE_NORMAL | (IS_CNTRL(*str) ? PC_CTRL : PC_ASCII);
#ifdef USE_ANSI_COLOR
if (color)
*(color++) = 0;
#endif
#ifdef USE_M17N #ifdef USE_M17N
*(plens++) = plen = 1; *(plens++) = plen = 1;
#endif #endif
@@ -383,6 +389,10 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
else { else {
Strshrink(s, plen); Strshrink(s, plen);
prop -= plen; prop -= plen;
#ifdef USE_ANSI_COLOR
if (color)
color -= plen;
#endif
plen = *(--plens); plen = *(--plens);
str += 2; str += 2;
} }
@@ -405,6 +415,10 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
else { else {
Strshrink(s, plen); Strshrink(s, plen);
prop -= plen; prop -= plen;
#ifdef USE_ANSI_COLOR
if (color)
color -= plen;
#endif
plen = *(--plens); plen = *(--plens);
str++; str++;
} }
@@ -416,6 +430,10 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
else { else {
Strshrink(s, 1); Strshrink(s, 1);
prop--; prop--;
#ifdef USE_ANSI_COLOR
if (color)
color--;
#endif
str++; str++;
} }
#endif #endif
@@ -460,6 +478,8 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
*(prop++) = mode; *(prop++) = mode;
#ifdef USE_M17N #ifdef USE_M17N
plen = get_mclen(str); plen = get_mclen(str);
if (str + plen > endp)
plen = endp - str;
*(plens++) = plen; *(plens++) = plen;
if (plen > 1) { if (plen > 1) {
mode = (mode & ~PC_WCHAR1) | PC_WCHAR2; mode = (mode & ~PC_WCHAR1) | PC_WCHAR2;