rust-lang / rust-lang/rfcs

Warn when lacking custom drop implementation for stack overflowing structures

Open
#3,132 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Disclaimer

Maybe this is tricky to implement, or maybe it's already been posted somewhere, but the only resource I've found on this is (ironically) a stack overflow post. Or maybe it's just not as important as I think it is.

edit : I'm aware that this problem is pretty obvious, so it probably comes from older choices, but in the context of Rust safeness, I find it particularly dangerous.

Motivation

Rust being a language known to be "safe" (and it is), I find it quite unfortunate that an auto-implemented function can potentially lead to a stack overflow without even warning the user. I mean, if I were the CISO of a company, I would definitely ban the use of recursive structures. But if a team working on critical areas doesn't know this, it could potentially lead to unpredictable crashes with the consequences that would include.

Idea

Since Rust automatically handles deallocation of structures, I think it would be nice to warn users when deallocation can potentially lead to a stack overflow. This means warning the user when a custom deallocation function is not implemented for what we consider a potentially recursive or stack overflowing structure.

How to know

A simple solution might be to warn the user when a structure contains one or more boxes and does not have a custom deallocation function.

I felt like this should always be an Option, but it could also be a Box or a Box<Box<.... Box....>>.

So a more complex, but also more specific, solution might be to find cycle referencing types, with cycle detection in a graph.
For nested structures, we could say that it is impossible to make many nested boxes and therefore not handle the case.

We could also change the algorithm behind deallocation and use a recursive tree search algorithm, but the trade-off would be really terrible, and even more so with the drop function which is probably one of the most called functions, so impossible.

I don't have many ideas left, but I think the first one is probably the easiest and most convenient to use, the only problem being the false positive rate...

One way to solve this problem could be to take the problem the other way around. We could make sure that there would be no stack overflow if the number of nested boxes was less than a threshold and there was no cyclic reference between multiple types.

Conclusion

I agree that this is a complex problem, since we don't want too many false positives. But I really think the idea that an auto-implemented function is not robust to overflow is a bad concept.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked Stack Overflow discussion and the issue's proposed approaches for Rust drop behavior. Determine whether warning criteria can be specified without unacceptable false positives, then document one agreed design and its expected behavior in an RFC.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.