Repository navigation
Return default value on failed cast in metacall_value wrappers - #951
pranab-mandal wants to merge 1 commit into
Conversation
…value_cast_* wrappers
|
This change has not been discussed. |
|
Hey @viferga, sorry for jumping straight to a PR without discussing the approach first! I've been looking into issue #933. Right now, if a type cast fails (like casting a string to an int), value_type_cast() returns NULL, but the wrapper functions keep going and try to read the memory anyway ,which ends up returning garbage values or reinterpreted memory. My initial thought in the PR was to add an early NULL check and return a default type safe value (like 0 or NULL). However, I realize silently returning a default value might mask errors or break language interop downstream. How would you prefer to handle these failed casts architecturally? Should we return a specific error/sentinel value, raise a runtime error, or is there another pattern in metacall/core you'd like me to follow here? |
Description
In
source/metacall/source/metacall_value.c,metacall_value_cast_*wrapper functions lacked an early return check whenvalue_type_cast()returnedNULL.When casting an incompatible type (such as string to integer),
value_type_cast()returnsNULL. The previous code did not update*v, but still proceeded to callvalue_to_*(*v). Becausevalue_to_*directly reads memory offsets without checking runtime type tags, it reinterpreted raw string characters as an integer (e.g."Hell"became1819043144/0x6C6C6548).Fix
NULLcheck across all 15metacall_value_cast_*wrappers inmetacall_value.cto return type-safe defaults (0,0L,0.0f,0.0,'\0', orNULL) immediately without touching*v.string_to_int_cast_returns_nullinreflect_value_cast_int_test.cpp.CONTRIBUTORS.Fixes #933
Type of change
Checklist:
reflect_value_type_cast.c, and structuring the regression test. Every change has been reviewed, tested, and understood by the author.