Skip to content

fix: safer integer conversion and external buffer support#6

Merged
wemeetagain merged 2 commits intomainfrom
nh/safer-integer-conversion
Mar 23, 2026
Merged

fix: safer integer conversion and external buffer support#6
wemeetagain merged 2 commits intomainfrom
nh/safer-integer-conversion

Conversation

@nazarhussain
Copy link
Copy Markdown
Contributor

  • Uses std.math.cast with proper error handling instead of @intCast (prevents runtime panic on overflow)
  • Adds external_buffer return hint support
  • Cleaner pointer-to-bytes conversion

@nazarhussain nazarhussain changed the title fix: safer integer conversion and external buffer support in to_from_… fix: safer integer conversion and external buffer support Mar 17, 2026
Copy link
Copy Markdown
Contributor

@spiral-ladder spiral-ladder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some questions

Comment thread src/to_from_value.zig
const n: i64 = if (i.bits <= 32)
@as(i64, try value.getValueUint32())
else
try value.getValueInt64();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarifying question: i know this was the same code previously, but how come value.getValueInt64() works here for >u32? does the napi api require an i64 result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

napi doesn't provide a napi_get_value_uint64 — the only 64-bit integer API is napi_get_value_int64 which returns i64. So for unsigned types >32 bits we're forced to read as i64 first, then std.math.cast handles the range check (rejecting negative values or values that overflow the target type).

Comment thread src/to_from_value.zig Outdated
return try env.createInt64(n);
}

const n = std.math.cast(u64, v) orelse return error.InvalidArg;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to do this intermediate cast?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to above feedback.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still not sure what the problem with std.math.cast(.., v) is below in the if and else.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the intermediate cast. Now the values are casted at the return statements.

Copy link
Copy Markdown

@lodekeeper-z lodekeeper-z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — replacing @intCast with std.math.cast is the right call for NAPI boundaries where JS can pass arbitrary values. The external_buffer support is a nice addition too.

@wemeetagain wemeetagain merged commit ff0f0c8 into main Mar 23, 2026
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.

4 participants