mruby-pack: optimize BER decoding overflow checking

- calculate maximum safe bytes upfront to reduce checking frequency
- only check overflow when approaching byte limits or value limits
- maintain same overflow detection accuracy with better performance
- reduces per-iteration overhead for common BER decoding cases

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-13 07:25:00 +09:00
parent 9051c48431
commit 7a98bd3da0
+6 -1
View File
@@ -438,11 +438,16 @@ unpack_BER(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary,
if (srclen == 0) return 0;
/* calculate maximum safe bytes before potential overflow */
const int max_safe_bytes = (sizeof(mrb_int) * 8 - 1) / 7; /* conservative estimate */
int i;
for (i = 1; p < e; p++, i++) {
if (n > (MRB_INT_MAX >> 7)) {
/* check overflow before we might exceed safe limits */
if (i > max_safe_bytes || n > (MRB_INT_MAX >> 7)) {
mrb_raise(mrb, E_RANGE_ERROR, "BER unpacking 'w' overflow");
}
n <<= 7;
n |= *p & 0x7f;
if ((*p & 0x80) == 0) break;