Repository navigation
C: render Duration as int64_t nanoseconds (v0.11.0-kixelated.7+v0.32.2) - #6
Conversation
The C backend maps uniffi's Duration to <namespace>_duration, a typedef of int64_t nanoseconds mirroring the C++ binding's std::chrono::nanoseconds. The C++ converter now fails on a negative duration, which reached Rust as a huge one, and on one from Rust past INT64_MAX nanoseconds, which overflowed. Both abort under the expected style the C backend builds on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Refusing it in write threw after lower had allocated the buffer, which leaked under the exceptions style; memcheck caught it in chronological-test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ca955d8b53
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (secs > static_cast<uint64_t>(max_secs.count()) || | ||
| (secs == static_cast<uint64_t>(max_secs.count()) && nanos > max_nanos)) { | ||
| {% call macros::fail("Duration does not fit in int64_t nanoseconds") %} |
There was a problem hiding this comment.
Free the owning RustBuffer when duration validation throws
Under the C++ exceptions style, receiving a Rust duration above INT64_MAX nanoseconds now throws from read(), bypassing the rustbuffer_free(buf) that follows it in lift(). RustStream does not own or release the buffer, so catching this exception leaks the returned Rust allocation on every call. The same problem affects enclosing records and collections whose readers encounter an oversized duration, potentially leaking a much larger buffer. Make the owning lift paths exception-safe so rejecting an out-of-range duration also releases its buffer.
Useful? React with 👍 / 👎.
Pins the fork commit that renders UniFFI's Duration in the generated C, kixelated/uniffi-bindgen-cpp#6, until it is tagged v0.11.0-kixelated.7+v0.32.2. Finishes quest/m1/c/duration.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The C backend renders uniffi's builtin
Durationinstead of refusing it, so moq-ffi can export typed durations without losing the generated C. Pinned as a draft by moq-dev/moq#6683.C
Durationis<namespace>_duration, atypedef int64_tcount of nanoseconds, mirroring the C++ binding'sstd::chrono::duration<int64_t, std::nano>. The typedef puts the unit in every field, argument, and return; it is emitted only when the interface uses a duration.Option<Duration>is<namespace>_optional_duration { has_value, value }, and lists and maps work as for any scalar.C++ (also reached by C)
The C++ converter, which the C backend lowers and lifts through, silently corrupted out-of-range values:
u64, so Rust saw a duration of about 584 billion years.INT64_MAXnanoseconds (about 292 years) overflowedint64_t, which is undefined.Both now fail through the existing
macros::fail: a throw under the exceptions style, an abort with a message under the expected style the C backend builds on. Every value in between round-trips as before. The negative check sits inallocation_size, which every lower runs before it allocates anything. The first version checked inwrite, which threw after the buffer was allocated, and memcheck caught the leak inchronological-test.Tests
c_apifixture:CapiTimerwith aDurationfield, anOption<Duration>, and aVec<Duration>.capi_twiceandcapi_halvecross a duration and an optional one in both directions, andcapi_make_durationbuilds one on the Rust side. The C test round-trips values across the seconds boundary and exactlyINT64_MAX.c-negative-duration-testandc-long-duration-testrunc_api-testwith an argument and match the abort message, the wayexpected-value-on-error-testdoes. Both fail on the old converter.chronological-testexpects a negative duration to throw under the exceptions style; it aborted on the old converter.capi_double(it becomesdoubleunderstrip_function_prefix) andcapi_duration(it clashes with the new typedef), so those checks cover durations too.Run locally: the full ctest suite (60 tests, including valgrind memcheck) on Linux with GCC 16,
cargo fmt --check, andcargo clippy(no new warnings). CI passes on Linux (GCC, Clang), macOS (GCC, Clang), and Windows (MSVC).Known gap, not new
Under the exceptions style, a throw while lowering a later argument still leaks the buffers of arguments already lowered. Every converter that can throw has this problem, so it is left for a separate change.
Release
As in #5, a commit bumps the version to
v0.11.0-kixelated.7+v0.32.2and moves the changelog entry under it, so the moq pin's hashes stay the same once the tag exists. The leak fix landed after it and changes no version. Merging and tagging are the maintainer's call; nothing is tagged.(Written by Claude Opus 5.5)