-
Notifications
You must be signed in to change notification settings - Fork 45
Fix assignment to array within struct #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix assignment to array within struct #26
Conversation
…gth]`. # Conflicts: # test/fiddle/test_import.rb
lib/fiddle/struct.rb
Outdated
|
|
||
| def []=(index, value) | ||
| if index < 0 || index >= size | ||
| raise RangeError, 'index %d outside of array bounds 0...%d' % [index, size] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe IndexError?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I changed this.
lib/fiddle/struct.rb
Outdated
| raise RangeError, 'index %d outside of array bounds 0...%d' % [index, size] | ||
| end | ||
|
|
||
| to_ptr[index * @align, @size] = [value].pack(@pack_format) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why @align, shouldn't be @size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this was wrong. I fixed the error, but I was unable to make it fail in a test case.
The test did not fail because, on my system, @align == @size for all primitive types. Therefore the result of using @align yields a correct value on my system, but could fail on other architectures. I modified the test case to attempt to write at several array indices, and then check that the underlying memory can be correctly unpacked. I think the modified test case covers the issue better than the original, even though I did not experience a failure, so I committed it.
# Conflicts: # lib/fiddle/struct.rb
This pull request was split from #14 as requested by @nobu . It depends on #25 because that pull request makes it possible to access the underlying memory of a struct, which is required in order for this pull request to work properly.
When a member of a struct is an array,
CStructEntitywould return a temporary array containing those values. Assignment to that array was a no-op, which led to confusing behavior because an assignment to the array would be "ignored". I fixed this so that assignment to the returned array correctly updates the member within the struct.For example: