Skip to content

Commit 2703c32

Browse files
mamenobu
authored andcommitted
Prevent potential buffer overrun in onigmo
A code pattern `p + enclen(enc, p, pend)` may lead to a buffer overrun if incomplete bytes of a UTF-8 character is placed at the end of a string. Because this pattern is used in several places in onigmo, this change fixes the issue in the side of `enclen`: the function should not return a number that is larger than `pend - p`. Co-Authored-By: Nobuyoshi Nakada <nobu@ruby-lang.org>
1 parent ed58c53 commit 2703c32

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

include/ruby/onigmo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ int onigenc_ascii_only_case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, c
356356
#define ONIGENC_PRECISE_MBC_ENC_LEN(enc,p,e) (enc)->precise_mbc_enc_len(p,e,enc)
357357

358358
ONIG_EXTERN
359-
int onigenc_mbclen_approximate(const OnigUChar* p,const OnigUChar* e, const struct OnigEncodingTypeST* enc);
359+
int onigenc_mbclen(const OnigUChar* p,const OnigUChar* e, const struct OnigEncodingTypeST* enc);
360360

361-
#define ONIGENC_MBC_ENC_LEN(enc,p,e) onigenc_mbclen_approximate(p,e,enc)
361+
#define ONIGENC_MBC_ENC_LEN(enc,p,e) onigenc_mbclen(p,e,enc)
362362
#define ONIGENC_MBC_MAXLEN(enc) ((enc)->max_enc_len)
363363
#define ONIGENC_MBC_MAXLEN_DIST(enc) ONIGENC_MBC_MAXLEN(enc)
364364
#define ONIGENC_MBC_MINLEN(enc) ((enc)->min_enc_len)

regenc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ onigenc_set_default_encoding(OnigEncoding enc)
5151
return 0;
5252
}
5353

54+
extern int
55+
onigenc_mbclen(const OnigUChar* p,const OnigUChar* e, OnigEncoding enc)
56+
{
57+
int ret = ONIGENC_PRECISE_MBC_ENC_LEN(enc, p, e);
58+
if (ONIGENC_MBCLEN_CHARFOUND_P(ret)) {
59+
ret = ONIGENC_MBCLEN_CHARFOUND_LEN(ret);
60+
if (ret > (int)(e - p)) ret = (int)(e - p); // just for case
61+
return ret;
62+
}
63+
else if (ONIGENC_MBCLEN_NEEDMORE_P(ret)) {
64+
return (int)(e - p);
65+
}
66+
return p < e ? 1 : 0;
67+
}
68+
5469
extern int
5570
onigenc_mbclen_approximate(const OnigUChar* p,const OnigUChar* e, OnigEncoding enc)
5671
{

regparse.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3799,7 +3799,6 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
37993799
}
38003800
else { /* string */
38013801
p = tok->backp + enclen(enc, tok->backp, end);
3802-
if (p > end) return ONIGERR_END_PATTERN_AT_ESCAPE;
38033802
}
38043803
}
38053804
break;

0 commit comments

Comments
 (0)