mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-pack: avoid integer overflow in pack_hex buffer calculation
rewrite ceiling division to avoid signed overflow. the expression (count + 1) / 2 triggers undefined behavior when count == INT_MAX. use count / 2 + (count & 1) instead, which computes the same result without intermediate overflow. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1009,7 +1009,8 @@ pack_hex(mrb_state *mrb, mrb_value src, mrb_value dst, mrb_int didx, int count,
|
||||
}
|
||||
|
||||
/* calculate output buffer size needed - one byte per two hex chars */
|
||||
int output_bytes = (count + 1) / 2;
|
||||
/* use count/2 + (count&1) to avoid overflow when count == INT_MAX */
|
||||
int output_bytes = count / 2 + (count & 1);
|
||||
dst = str_len_ensure(mrb, dst, didx + output_bytes);
|
||||
char *dptr = RSTRING_PTR(dst) + didx;
|
||||
char *dptr0 = dptr;
|
||||
|
||||
Reference in New Issue
Block a user