Repository navigation
Fix a SEGV from Numo::RObject#hton swapping the bytes of object references - #41
Conversation
In little-endian builds, hton is an alias of swap_byte on Numo::NArray. RObject undefines swap_byte, but undefining a method does not remove its aliases, so hton stayed reachable. It swapped the bytes of each VALUE, and the GC crashed when it marked the result. Undefine hton as well, and reject RObject in nary_swap_byte so a bound method object cannot get around the undef. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
/opencode review |
Review: PR #41 — Fix
|
Purpose
Numo::RObject#htonswaps the bytes of each element. RObject elements are Ruby object references, so the result holds broken references, and the GC crashes when it marks them.Summary of Changes
htonis an alias ofswap_byteonNumo::NArrayin little-endian builds.Numo::RObjectundefinesswap_byte,to_networkand the other conversions, but undefining a method does not remove its aliases, sohtonstayed reachable.Numo::RObjectnow undefineshtontoo, andnary_swap_byteraisesTypeErrorfor RObject. The second part covers a bound method object, such asNumo::NArray.instance_method(:swap_byte).bind_call(a), as #30 did for binary I/O.Cumo found and fixed the same in sonots/cumo#638.
Testing
Before the fix,
htonon an RObject array, a view and a subclass, andswap_byteandhtonthroughbind_call, all crashed in the GC when the result was kept. After the fix, they raiseNoMethodErrororTypeError. Throughbind_call,to_networkandto_swappedalready raisedNoMethodError, because they callswap_byte.to_vacsandto_hostreturn the array unchanged.htonon DFloat, Int32, SComplex and Bit gives the same results as before.I also checked every alias on
Numo::NArraythat points to a methodNumo::RObjectundefines. Onlyhtonwas left.to_stringandfrom_stringwere already undefined by #30.The new test fails on the code before the fix.
test_narray,test_bit,test_extra,test_mathandtest_ractorall pass.Related Issues
None.
🤖 Generated with Claude Code