Skip to content

Commit

Permalink
Add bounds checks for writing to an inline char[]
Browse files Browse the repository at this point in the history
* Raise IndexError if the written String is longer than the char[].
* Don't write a final \0 if we write as many characters as the char[] length.
  • Loading branch information
eregon committed Mar 28, 2020
1 parent b3956b1 commit c0a446a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ext/ffi_c/StructLayout.c
Expand Up @@ -350,8 +350,13 @@ array_field_put(VALUE self, VALUE pointer, VALUE value)
argv[0] = INT2FIX(f->offset);
argv[1] = value;

rb_funcall2(pointer, rb_intern("put_string"), 2, argv);

if (RSTRING_LEN(value) < array->length) {
rb_funcall2(pointer, rb_intern("put_string"), 2, argv);
} else if (RSTRING_LEN(value) == array->length) {
rb_funcall2(pointer, rb_intern("put_bytes"), 2, argv);
} else {
rb_raise(rb_eIndexError, "String is longer (%ld bytes) than the char array (%d bytes)", RSTRING_LEN(value), array->length);
}
} else {
#ifdef notyet
MemoryOp* op;
Expand Down

0 comments on commit c0a446a

Please sign in to comment.