diff --git a/src/string.c b/src/string.c index 9918b2903..88575c7ca 100644 --- a/src/string.c +++ b/src/string.c @@ -1200,7 +1200,16 @@ mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2) mrb_int len1 = RSTR_LEN(s1); mrb_int len2 = RSTR_LEN(s2); mrb_int len = lesser(len1, len2); - mrb_int retval = memcmp(RSTR_PTR(s1), RSTR_PTR(s2), len); + mrb_int retval; + + /* avoid UB: memcmp requires non-NULL pointers even when size is 0 */ + if (len == 0) { + retval = 0; + } + else { + retval = memcmp(RSTR_PTR(s1), RSTR_PTR(s2), len); + } + if (retval == 0) { if (len1 == len2) return 0; if (len1 > len2) return 1;