mruby-string-ext/string.c (mrb_str_ord): fail with invalid UTF-8.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-05-25 11:17:49 +09:00
parent bb99d7b2ca
commit f08e66e53e
+4 -2
View File
@@ -1034,7 +1034,7 @@ utf8code(unsigned char* p)
}
}
}
return p[0];
return -1;
}
static mrb_value
@@ -1042,7 +1042,9 @@ mrb_str_ord(mrb_state* mrb, mrb_value str)
{
if (RSTRING_LEN(str) == 0)
mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
return mrb_fixnum_value(utf8code((unsigned char*) RSTRING_PTR(str)));
mrb_int c = utf8code((unsigned char*) RSTRING_PTR(str));
if (c < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid UTF-8 byte sequence");
return mrb_fixnum_value(c);
}
#else
static mrb_value