Skip to content

Commit 758b628

Browse files
committed
deploy: 737a09e
1 parent 01c1cab commit 758b628

65 files changed

Lines changed: 911 additions & 227 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<link rel="stylesheet" type="text/css" href="_static/graphviz.css?v=4ae1632d" />
1313
<link rel="stylesheet" type="text/css" href="_static/sphinx-data-viewer/jsonview.bundle.css?v=f6ef2277" />
1414
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/libs/html/datatables.min.css?v=4b4fd840" />
15-
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
16-
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
1715
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_toggle.css?v=5c6620df" />
1816
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/needstable.css?v=5e1b6797" />
1917
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_core.css?v=f5b60a78" />
18+
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
19+
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
2020
<link rel="stylesheet" type="text/css" href="_static/sphinx-needs/modern.css?v=803738c0" />
2121

2222

_sources/coding-guidelines/associated-items.rst.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,88 @@
55

66
Associated Items
77
================
8+
9+
.. guideline:: Recursive function are not allowed
10+
:id: gui_ot2Zt3dd6of1
11+
:category: required
12+
:status: draft
13+
:release: 1.3.0-latest
14+
:fls: fls_vjgkg8kfi93
15+
:decidability: undecidable
16+
:scope: system
17+
:tags: stack-overflow
18+
19+
Any function shall not call itself directly or indirectly
20+
21+
.. rationale::
22+
:id: rat_gvoKeVSKK8fD
23+
:status: draft
24+
25+
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 <https://en.wikipedia.org/wiki/Tail_call>`_\ , 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 <https://github.com/phi-go/rfcs/blob/guaranteed-tco/text/0000-explicit-tail-calls.md>`_\ , 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.
26+
27+
.. non_compliant_example::
28+
:id: non_compl_ex_MxqhjfkStJJy
29+
:status: draft
30+
31+
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.
32+
33+
.. code-block:: rust
34+
35+
// Recursive enum to represent a string or a list of `MyEnum`
36+
enum MyEnum {
37+
Str(String),
38+
List(Vec<MyEnum>),
39+
}
40+
41+
// Concatenates strings from a nested structure of `MyEnum` using recursion.
42+
fn concat_strings(input: &[MyEnum]) -> String {
43+
let mut result = String::new();
44+
for item in input {
45+
match item {
46+
MyEnum::Str(s) => result.push_str(s),
47+
MyEnum::List(list) => result.push_str(&concat_strings(list)),
48+
}
49+
}
50+
result
51+
}
52+
53+
.. compliant_example::
54+
:id: compl_ex_9pK3h65rfceO
55+
:status: draft
56+
57+
The following code implements the same functionality using iteration instead of recursion. The ``stack`` variable is used to maintain the processing context at each step of the loop. This approach provides explicit control over memory usage. If the stack grows beyond a predefined limit due to the structure or size of the input, the function returns an error rather than risking a stack overflow or out-of-memory exception. This ensures more predictable and robust behavior in resource-constrained environments.
58+
59+
.. code-block:: rust
60+
61+
// Recursive enum to represent a string or a list of `MyEnum`
62+
enum MyEnum {
63+
Str(String),
64+
List(Vec<MyEnum>),
65+
}
66+
67+
/// Concatenates strings from a nested structure of `MyEnum` without using recursion.
68+
/// Returns an error if the stack size exceeds `MAX_STACK_SIZE`.
69+
fn concat_strings_non_recursive(input: &[MyEnum]) -> Result<String, &'static str> {
70+
const MAX_STACK_SIZE: usize = 1000;
71+
let mut result = String::new();
72+
let mut stack = Vec::new();
73+
74+
// Add all items to the stack
75+
stack.extend(input.iter());
76+
77+
while let Some(item) = stack.pop() {
78+
match item {
79+
MyEnum::Str(s) => result.insert_str(0, s),
80+
MyEnum::List(list) => {
81+
// Add list items to the stack
82+
for sub_item in list.iter() {
83+
stack.push(sub_item);
84+
if stack.len() > MAX_STACK_SIZE {
85+
return Err("Too big structure");
86+
}
87+
}
88+
}
89+
}
90+
}
91+
Ok(result)
92+
}

_sources/coding-guidelines/expressions.rst.txt

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,139 @@ Expressions
8282
8383
fn with_base(_: &Base) { ... }
8484
85+
.. guideline:: Do not use integer type as divisor
86+
:id: gui_7y0GAMmtMhch
87+
:category: advisory
88+
:status: draft
89+
:release: latest
90+
:fls: fls_Q9dhNiICGIfr
91+
:decidability: decidable
92+
:scope: module
93+
:tags: numerics, subset
94+
95+
This guideline applies when a `DivisionExpression
96+
<https://rust-lang.github.io/fls/expressions.html#syntax_divisionexpression>`_ or `RemainderExpression
97+
<https://rust-lang.github.io/fls/expressions.html#syntax_remainderexpression>`_ is used with a RightOperand of
98+
`integer type <https://rust-lang.github.io/fls/types-and-traits.html#integer-types>`_.
99+
100+
.. rationale::
101+
:id: rat_vLFlPWSCHRje
102+
:status: draft
103+
104+
The built-in semantics for these expressions can result in panics when division by zero occurs. It is
105+
recommended to either:
106+
107+
* Use checked division functions, which ensure the programmer handles the case when the divisor is zero, or
108+
* To create divisors using :std:`std::num::NonZero`, which then allows the programmer to perform those
109+
operations knowing that their divisor is not zero.
110+
111+
**Note:** since the compiler can assume the value of a :std:`std::num::NonZero`
112+
variable to not be zero, checks for zero when dividing by it can be elided in the
113+
final binary, increasing overall performance beyond what normal division can have.
114+
115+
.. non_compliant_example::
116+
:id: non_compl_ex_0XeioBrgfh5z
117+
:status: draft
118+
119+
When either the division or remainder are performed, the right operand is evaluated to zero and the
120+
program panics.
121+
122+
.. code-block:: rust
123+
124+
let x = 0;
125+
let y = 5 / x; // This line will panic.
126+
let z = 5 % x; // This line would also panic.
127+
128+
.. compliant_example::
129+
:id: compl_ex_k1CD6xoZxhXb
130+
:status: draft
131+
132+
There is no compliant way to divide with an integer type. Here, instead, the developer explicitly:
133+
134+
* Uses a checked division function, which ensures a zero divisor is handled separately, and
135+
* Creates a divisor using :std:`std::num::NonZero`, which outsources the check for zero to the
136+
construction of that struct. It's worth noting that such a divisor can be used multiple times after it's been created, whilst keeping the guarantee that such divisions will be safe.
137+
138+
.. code-block:: rust
139+
140+
let x = 0;
141+
if let Some(divisor) = match NonZero::<u32>::new(x) {
142+
let result = 5 / divisor;
143+
}
144+
let result = match 5u32.checked_rem(x) {
145+
None => 0,
146+
Some(r) => r,
147+
}
148+
149+
150+
.. guideline:: Do not divide by 0
151+
:id: gui_kMbiWbn8Z6g5
152+
:category: required
153+
:status: draft
154+
:release: latest
155+
:fls: fls_Q9dhNiICGIfr
156+
:decidability: undecidable
157+
:scope: system
158+
:tags: numerics, defect
159+
160+
This guideline applies when unsigned integer or two’s complement division is performed during the
161+
evaluation of an `ArithmeticExpression
162+
<https://rust-lang.github.io/fls/expressions.html#arithmetic-expressions>`_.
163+
164+
This includes the evaluation of a `RemainderExpression
165+
<https://rust-lang.github.io/fls/expressions.html#syntax_remainderexpression>`_, which uses unsigned integer or two's
166+
complement division.
167+
168+
This rule does not apply to evaluation of a :std:`core::ops::Div` trait on types other than `integer
169+
types <https://rust-lang.github.io/fls/types-and-traits.html#integer-types>`_.
170+
171+
.. rationale::
172+
:id: rat_h84NjY2tLSBW
173+
:status: draft
174+
175+
Integer division by zero results in a panic, which is an abnormal program state and may terminate the
176+
process. The use of :std:`std::num::NonZero` as the divisor is a recommended way to avoid the
177+
undecidability of this guideline.
178+
179+
.. non_compliant_example::
180+
:id: non_compl_ex_LLs3vY8aGz0F
181+
:status: draft
182+
183+
When the division is performed, the right operand is evaluated to zero and the program panics.
184+
185+
.. code-block:: rust
186+
187+
let x = 0;
188+
let y = 5 / x; // This line will panic.
189+
190+
.. compliant_example::
191+
:id: compl_ex_Ri9pP5Ch3kbb
192+
:status: draft
193+
194+
There is no compliant way to perform integer division by zero. A checked division will prevent any
195+
division by zero from happening. The programmer can then handle the returned :std:`std::option::Option`.
196+
197+
The check for zero can also be performed manually. However, as the complexity of the control
198+
flow leading to the invariant increases, it becomes increasingly harder to reason about it. For both programmers and static analysis tools.
199+
200+
.. code-block:: rust
201+
202+
// Example 1: using the checked division API
203+
let result = match 5u8.checked_div(0) {
204+
None => 0
205+
Some(r) => r
206+
};
207+
208+
// Example 2: performing zero-checks by hand
209+
let x = 0;
210+
let y = if x != 0 {
211+
5 / x
212+
} else {
213+
0
214+
};
215+
216+
217+
85218
86219
.. guideline:: The 'as' operator should not be used with numeric operands
87220
:id: gui_ADHABsmK9FXz

_sources/coding-guidelines/macros.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ Macros
479479
:status: draft
480480

481481
The following is a macro refers to Vec using a global path. Even if there is a different struct called
482-
`Vec` defined in the scope of the macro usage, this macro will unambigiously use the `Vec` from the
482+
`Vec` defined in the scope of the macro usage, this macro will unambiguously use the `Vec` from the
483483
Standard Library.
484484

485485
.. code-block:: rust

_sources/compliance/compliance-meaning.rst.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55

66
Compliance Meaning
77
==================
8+
9+
We follow MISRA Compliance 2020.

_sources/process/style-guideline.rst.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ A unique identifier for each guideline. Guideline identifiers **MUST** begin wit
105105
These identifiers are considered **stable** across releases and **MUST NOT** be removed.
106106
See ``status`` below for more.
107107

108-
**MUST** be generated using the ``generate-guideline-templates.py`` script to ensure
108+
**MUST** be generated using the ``generate_guideline_templates.py`` script to ensure
109109
compliance.
110110

111111
``category``
@@ -370,7 +370,7 @@ A unique identifier for each rationale. Rationale identifiers **MUST** begin wit
370370
These identifiers are considered **stable** across releases and **MUST NOT** be removed.
371371
See ``status`` below for more.
372372

373-
**MUST** be generated using the ``generate-guideline-templates.py`` script to ensure
373+
**MUST** be generated using the ``generate_guideline_templates.py`` script to ensure
374374
compliance.
375375

376376
``rationale`` ``status``
@@ -414,7 +414,7 @@ A unique identifier for each ``non_compliant_example``. ``non_compliant_example`
414414
These identifiers are considered **stable** across releases and **MUST NOT** be removed.
415415
See ``status`` below for more.
416416

417-
**MUST** be generated using the ``generate-guideline-templates.py`` script to ensure
417+
**MUST** be generated using the ``generate_guideline_templates.py`` script to ensure
418418
compliance.
419419

420420
``non_compliant_example`` ``status``
@@ -487,7 +487,7 @@ A unique identifier for each ``compliant_example``. ``compliant_example`` identi
487487
These identifiers are considered **stable** across releases and **MUST NOT** be removed.
488488
See ``status`` below for more.
489489

490-
**MUST** be generated using the ``generate-guideline-templates.py`` script to ensure
490+
**MUST** be generated using the ``generate_guideline_templates.py`` script to ensure
491491
compliance.
492492

493493
``compliant_example`` ``status``

appendices/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<link rel="stylesheet" type="text/css" href="../_static/graphviz.css?v=4ae1632d" />
1313
<link rel="stylesheet" type="text/css" href="../_static/sphinx-data-viewer/jsonview.bundle.css?v=f6ef2277" />
1414
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/libs/html/datatables.min.css?v=4b4fd840" />
15-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
16-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
1715
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_toggle.css?v=5c6620df" />
1816
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/needstable.css?v=5e1b6797" />
1917
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_core.css?v=f5b60a78" />
18+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
19+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
2020
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/modern.css?v=803738c0" />
2121

2222

appendices/licenses.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<link rel="stylesheet" type="text/css" href="../_static/graphviz.css?v=4ae1632d" />
1313
<link rel="stylesheet" type="text/css" href="../_static/sphinx-data-viewer/jsonview.bundle.css?v=f6ef2277" />
1414
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/libs/html/datatables.min.css?v=4b4fd840" />
15-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
16-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
1715
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_toggle.css?v=5c6620df" />
1816
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/needstable.css?v=5e1b6797" />
1917
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_core.css?v=f5b60a78" />
18+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
19+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
2020
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/modern.css?v=803738c0" />
2121

2222

appendices/standards-matrices.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<link rel="stylesheet" type="text/css" href="../_static/graphviz.css?v=4ae1632d" />
1313
<link rel="stylesheet" type="text/css" href="../_static/sphinx-data-viewer/jsonview.bundle.css?v=f6ef2277" />
1414
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/libs/html/datatables.min.css?v=4b4fd840" />
15-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
16-
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
1715
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_toggle.css?v=5c6620df" />
1816
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/needstable.css?v=5e1b6797" />
1917
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_core.css?v=f5b60a78" />
18+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_links.css?v=2150a916" />
19+
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/common_css/need_style.css?v=92936fa5" />
2020
<link rel="stylesheet" type="text/css" href="../_static/sphinx-needs/modern.css?v=803738c0" />
2121

2222

0 commit comments

Comments
 (0)