-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Chapter
Associated Items
Guideline Title
Guideline Test
Category
Advisory
Status
Draft
Release Begin
1.1.1
Release End
1.1.1
FLS Paragraph ID
fls_vjgkg8kfi93
Decidability
Decidable
Scope
Module
Tags
stack-overflow
Amplification
Any function shall not call itself directly or indirectly
Exception(s)
- Top-level point
- Nested point
- Deeper nested point
- Nested point
Rationale
Recursive functions can easily cause stack overflows, which may result in exceptions or, in some cases, undefined behavior (typically some embedded systems). Although the Rust compiler supports tail call optimization, this optimization is not guaranteed and depends on the specific implementation and function structure. There is an open RFC to guarantee tail call optimization in the Rust compiler, but this feature has not yet been stabilized. Until tail call optimization is guaranteed and stabilized, developers should avoid using recursive functions to prevent potential stack overflows and ensure program reliability.
-
A list item
- Another sub-item
- Another sub item.
-
Intro
-
Reason 1
Context and explanation for Reason 1 -
Reason 2
Context and explanation for Reason 2
GGGGGGGGGGGGGGGGGGGGGGGG
- Intro
- Reason 1
- Context and explanation for Reason 1
- Reason 2
- Context and explanation for Reason 2
GGGGGGGGGGGGGGGGGGGGG
-
Intro
-
Reason 1
Context and explanation for Reason 1 -
Reason 2
Context and explanation for Reason 2
GGGGGGGGG nested bullets GGGGGGGGGG
-
Intro
-
Reason 1
- Context and explanation for Reason 1
-
Reason 2
- Context and explanation for Reason 2
-
Intro
This section gives an overview. -
Reason 1
Context and explanation for Reason 1.-
Detail 1.1
Extra info about detail 1.1.- Sub-detail 1.1.1
Even deeper explanation for 1.1.1.
- Sub-detail 1.1.1
-
Detail 1.2
Another branch of reasoning.
-
-
Reason 2
Context and explanation for Reason 2.-
Detail 2.1
Supporting evidence. -
Detail 2.2
More elaboration with a note:This could be important for implementation.
-
Non-Compliant Example - Prose
The below function concat_strings is not complaint because it call itself and depending on depth of data provided as input it could generate an stack overflow exception or undefine behavior.
Non-Compliant Example - Code
// Recursive enum to represent a string or a list of `MyEnum`
enum MyEnum {
Str(String),
List(Vec<MyEnum>),
}
// Concatenates strings from a nested structure of `MyEnum` using recursion.
fn concat_strings(input: &[MyEnum]) -> String {
let mut result = String::new();
for item in input {
match item {
MyEnum::Str(s) => result.push_str(s),
MyEnum::List(list) => result.push_str(&concat_strings(list)),
}
}
result
}
Compliant Example - Prose
tete
Compliant Example - Code
// Recursive enum to represent a string or a list of `MyEnum`
enum MyEnum {
Str(String),
List(Vec<MyEnum>),
}
/// Concatenates strings from a nested structure of `MyEnum` without using recursion.
/// Returns an error if the stack size exceeds `MAX_STACK_SIZE`.
fn concat_strings_non_recursive(input: &[MyEnum]) -> Result<String, &'static str> {
const MAX_STACK_SIZE: usize = 1000;
let mut result = String::new();
let mut stack = Vec::new();
// Add all items to the stack
stack.extend(input.iter());
while let Some(item) = stack.pop() {
match item {
MyEnum::Str(s) => result.insert_str(0, s),
MyEnum::List(list) => {
// Add list items to the stack
for sub_item in list.iter() {
stack.push(sub_item);
if stack.len() > MAX_STACK_SIZE {
return Err("Too big structure");
}
}
}
}
}
Ok(result)
}