From 78f9b5c52f72cd1185097756d057ec19595d4316 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 23 Nov 2025 23:11:36 +0900 Subject: [PATCH] string.c: simplify memcmp guard with ternary operator refactored the NULL pointer guard in mrb_str_cmp() from an if-else block to a more concise ternary operator. functionality remains the same: avoids undefined behavior by skipping memcmp() when comparing zero-length strings. Co-authored-by: Claude --- src/string.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/string.c b/src/string.c index 88575c7ca..768551d2d 100644 --- a/src/string.c +++ b/src/string.c @@ -1200,15 +1200,7 @@ 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; - - /* 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); - } + mrb_int retval = (len == 0) ? 0 : memcmp(RSTR_PTR(s1), RSTR_PTR(s2), len); if (retval == 0) { if (len1 == len2) return 0;