Skip to content

transpile: Do not leak main argument strings#1447

Merged
kkysen merged 4 commits into
immunant:masterfrom
Rua:main-leak
Dec 7, 2025
Merged

transpile: Do not leak main argument strings#1447
kkysen merged 4 commits into
immunant:masterfrom
Rua:main-leak

Conversation

@Rua

@Rua Rua commented Nov 7, 2025

Copy link
Copy Markdown
Contributor

I opted to store the final strings as Vec<u8> rather than as CString, because the latter does not have a way to get a mutable pointer/reference without leaking it. This is because of the nul termination invariant that CString must uphold.

@Rua Rua force-pushed the main-leak branch 4 times, most recently from e10b712 to 2f26db6 Compare November 8, 2025 09:22

@kkysen kkysen left a comment

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.

Can you add a snapshot test for this, too?

@Rua Rua force-pushed the main-leak branch 2 times, most recently from 326ac44 to ba0f652 Compare November 11, 2025 09:04

@kkysen kkysen left a comment

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.

Sorry, can you also do the thing where the snapshot test is added first before the fix, then updated along with the fix? Thanks!

@kkysen kkysen self-requested a review November 16, 2025 01:19
Comment thread c2rust-transpile/tests/snapshots/snapshots__transpile@main.c.snap
Comment thread c2rust-transpile/tests/snapshots/snapshots__transpile@main.c.snap

@kkysen kkysen left a comment

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.

LGTM. Sorry it took me a while to get back around to this. Just a couple minor comment things I think would be helpful.

vec![mk().method_call_expr(
mk().method_call_expr(
mk().call_expr(
// TODO(kkysen) change `"std"` to `"alloc"` after `#![feature(alloc_c_string)]` is stabilized in `1.63.0`

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.

Can you keep this comment?

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.

Actually, I think we can switch to it now. rust-lang/rust#99277 was merged on 2022-07-16, before 2022-08-08.

@Rua Rua Dec 7, 2025

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.

Changing std to alloc doesn't seem to work:

failed to resolve: use of unresolved module or unlinked crate alloc

I guess that it's not as simple as just swapping out one for the other.

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.

Ah, I think we also need to add extern crate alloc;. We can do that separately then, and just leave the comment here for now.

Comment thread c2rust-transpile/src/translator/main_function.rs Outdated
Comment thread c2rust-transpile/src/translator/main_function.rs Outdated
Comment thread c2rust-transpile/src/translator/main_function.rs Outdated
Comment thread c2rust-transpile/src/translator/main_function.rs Outdated
Comment thread c2rust-transpile/src/translator/main_function.rs Outdated
@kkysen kkysen merged commit 924e703 into immunant:master Dec 7, 2025
5 checks passed
kkysen added a commit that referenced this pull request Dec 7, 2025
…` generated is detected as a test

When testing `main.c`, `main.rs` is generated, and `cargo` thinks
`c2rust-transpile/tests/snapshots/main.rs` is a second `snapshots` test,
in addition to `c2rust-transpile/tests/snapshots.rs`:

```shell
❯ cargo test -p c2rust-transpile
error: failed to load manifest for workspace member `/home/kkysen/work/c2rust/c2rust`

Caused by:
  failed to load manifest for dependency `c2rust-transpile`

Caused by:
  failed to parse manifest at `/home/kkysen/work/c2rust/c2rust-transpile/Cargo.toml`

Caused by:
  found duplicate test name snapshots, but all test targets must have a unique name
```

This was from #1447, but wasn't caught in CI because it only runs `cargo test` a single time.
kkysen added a commit that referenced this pull request Dec 8, 2025
…generated is detected as a test

When testing `main.c`, `main.rs` is generated, and `cargo` thinks
`c2rust-transpile/tests/snapshots/main.rs` is a second `snapshots` test,
in addition to `c2rust-transpile/tests/snapshots.rs`:

```shell
❯ cargo test -p c2rust-transpile
error: failed to load manifest for workspace member `/home/kkysen/work/c2rust/c2rust`

Caused by:
  failed to load manifest for dependency `c2rust-transpile`

Caused by:
  failed to parse manifest at `/home/kkysen/work/c2rust/c2rust-transpile/Cargo.toml`

Caused by:
  found duplicate test name snapshots, but all test targets must have a unique name
```

This was from #1447, but wasn't caught in CI because it only runs `cargo test` a single time.
kkysen added a commit that referenced this pull request Dec 8, 2025
…generated is detected as a test (#1499)

When testing `main.c`, `main.rs` is generated, and `cargo` thinks
`c2rust-transpile/tests/snapshots/main.rs` is a second `snapshots` test,
in addition to `c2rust-transpile/tests/snapshots.rs`:

```shell
❯ cargo test -p c2rust-transpile
error: failed to load manifest for workspace member `/home/kkysen/work/c2rust/c2rust`

Caused by:
  failed to load manifest for dependency `c2rust-transpile`

Caused by:
  failed to parse manifest at `/home/kkysen/work/c2rust/c2rust-transpile/Cargo.toml`

Caused by:
  found duplicate test name snapshots, but all test targets must have a unique name
```

This was from #1447, but wasn't caught in CI because it only runs `cargo
test` a single time.
@Rua Rua deleted the main-leak branch December 9, 2025 10:52
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.

Generated wrapper main leaks memory of args

2 participants