summaryrefslogtreecommitdiff
path: root/pack.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2026-04-02 15:37:37 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2026-05-11 13:54:09 +0900
commiteb053e7446607f5e70215bf508499ef6bab3aa4a (patch)
tree964783ef8ca9555f69ef946aba3c9ff0bdae26d7 /pack.c
parent793f4c9572b9d888f0d40fb9d4ec783ea23486e4 (diff)
[Feature #21979] Allow negative offset in unpack
Diffstat (limited to 'pack.c')
-rw-r--r--pack.c5
1 files changed, 3 insertions, 2 deletions
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;