Skip to content

Return default value on failed cast in metacall_value wrappers - #951

Closed
pranab-mandal wants to merge 1 commit into
metacall:developfrom
pranab-mandal:fix/value-cast-null-guard
Closed

pranab-mandal wants to merge 1 commit into
metacall:developfrom
pranab-mandal:fix/value-cast-null-guard

Conversation

@pranab-mandal

Copy link
Copy Markdown

Description

In source/metacall/source/metacall_value.c, metacall_value_cast_* wrapper functions lacked an early return check when value_type_cast() returned NULL.

When casting an incompatible type (such as string to integer), value_type_cast() returns NULL. The previous code did not update *v, but still proceeded to call value_to_*(*v). Because value_to_* directly reads memory offsets without checking runtime type tags, it reinterpreted raw string characters as an integer (e.g. "Hell" became 1819043144 / 0x6C6C6548).

Fix

  • Added an early NULL check across all 15 metacall_value_cast_* wrappers in metacall_value.c to return type-safe defaults (0, 0L, 0.0f, 0.0, '\0', or NULL) immediately without touching *v.
  • Added unit test string_to_int_cast_returns_null in reflect_value_cast_int_test.cpp.
  • Added author name to CONTRIBUTORS.

Fixes #933

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective.
  • My changes generate no new warnings.
  • I have disclosed AI assistance:
    • AI-ASSISTED: Yes
    • Tool/Model: Antigravity / Claude 3.7 Sonnet/Gemini 3.8
    • Usage Scope: Used as an assistant for root-cause debugging, verifying memory ownership semantics in reflect_value_type_cast.c, and structuring the regression test. Every change has been reviewed, tested, and understood by the author.

@viferga

viferga commented Nov 9, 2026

Copy link
Copy Markdown
Member

This change has not been discussed.

@viferga viferga closed this Nov 9, 2026
@pranab-mandal

pranab-mandal commented Nov 9, 2026 •

Copy link
Copy Markdown
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

## 🐛 Bug Report: Missing null check in metacall_value_cast_* causes memory reinterpretation on unsupported casts(e.g returns little endian)

2 participants