From eb053e7446607f5e70215bf508499ef6bab3aa4a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 2 Apr 2026 15:37:37 +0900 Subject: [Feature #21979] Allow negative offset in unpack --- pack.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'pack.c') diff --git a/pack.c b/pack.c index 94bb510e0d..24221bc3d6 100644 --- a/pack.c +++ b/pack.c @@ -1024,9 +1024,10 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset) StringValue(fmt); rb_must_asciicompat(fmt); - if (offset < 0) rb_raise(rb_eArgError, "offset can't be negative"); len = RSTRING_LEN(str); - if (offset > len) rb_raise(rb_eArgError, "offset outside of string"); + if (offset < 0 ? (offset += len) < 0 : offset > len) { + rb_raise(rb_eArgError, "offset outside of string"); + } s = RSTRING_PTR(str); send = s + len; -- cgit v1.2.3