mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user