You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/en/unsafe/generalities.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## *Unsafe* capacities
4
4
5
-
Language capabilities can be extended using unsafe code. The full list of these capacities is given in the [Rust reference](https://doc.rust-lang.org/reference/unsafety.html). Notice the following one's.
5
+
Language capabilities can be extended using unsafe code. The full list of these capacities is given in the [Rust reference](https://doc.rust-lang.org/reference/unsafety.html). Notice the following ones.
6
6
7
7
* Dereference a raw pointer
8
8
* Modify a static mutable variable
@@ -36,14 +36,14 @@ manipulations of memory pointers, the language provides the `unsafe` keyword.
36
36
37
37
> **Rule {{#check LANG-UNSAFE | Don't use unsafe blocks}}**
38
38
>
39
-
> For a secured development, the `unsafe` blocks must be avoided. Afterward,
39
+
> In a secure Rust development, the `unsafe` blocks must be avoided. In the following,
40
40
> we list the only cases where `unsafe` may be used, provided that they come
41
41
> with a proper justification:
42
42
>
43
-
> - The Foreign Function Interface (FFI) of Rust allows for describing
43
+
> - The Foreign Function Interface (FFI) of Rust allows describing
44
44
> functions whose implementations are written in C, using the `extern "C"`
45
45
> prefix. To use such a function, the `unsafe` keyword is required. “Safe”
46
-
> wrapper shall be defined to safely and seamlessly call C code.
46
+
> wrappers shall be defined to safely and seamlessly call C code.
47
47
>
48
48
> - For embedded device programming, registers and various other resources are
49
49
> often accessed through a fixed memory address. In this case, `unsafe` blocks
@@ -121,14 +121,14 @@ Consequently, even *safe* function must be handled carefully in *unsafe* context
121
121
122
122
#### Example
123
123
124
-
Suppose one wants to propose an API find object of a given type in the memory.
124
+
Suppose one wants to propose an API to find objects of a given type in the memory.
125
125
This API could ask implementing the following trait
126
126
127
127
```rust
128
128
traitLocatable {
129
-
/// Find object of type `Self` in the buffer `buf`.
130
-
///Returns the index of the first byte representing
131
-
/// an object of type `Self`
129
+
/// Find objects of type `Self` in the buffer `buf`.
130
+
///Return the index of the first byte representing
* If the `Locatable` implementation gives the wrong index, the `as_ref` function produce an *UB*.
170
-
* If the `Locatable` implementation gives an outbound index, the subsequent buffer overflow is an *UB*.
170
+
* If the `Locatable` implementation gives an out-of-bounds index, the subsequent buffer overflow is an *UB*.
171
171
172
172
</div>
173
173
174
-
For instance, the following `Locatable` implementation is wrong **but** it is of the responsibility of the API maker to take it into account.
174
+
For instance, the following `Locatable` implementation is wrong **but** it is the responsibility of the API maker to take it into account.
175
175
176
176
```rust
177
177
implLocatableforbool {
@@ -181,7 +181,7 @@ impl Locatable for bool {
181
181
}
182
182
```
183
183
184
-
The following program produces *UB*.
184
+
The following program produces a *UB*.
185
185
186
186
```rust,should_panic
187
187
fn main() {
@@ -246,7 +246,7 @@ in case these *safe* functions have bad behavior.
246
246
If they cannot protect their function against badly implemented *safe* functions/traits, they could either
247
247
248
248
* mark the function they *write* as `unsafe`: thus it is the user's responsibility to feed this function with correct arguments (by checking *unsafe* function documentation).
249
-
* mark the traits they *use* as `unsafe` : thus it is user's responsibility to simplement the trait properly (again reading the trait documentation).
249
+
* mark the traits they *use* as `unsafe` : thus it is user's responsibility to implement the trait properly (again reading the trait documentation).
> In a secure Rust development, `from_raw` should only be called on `into_raw`ed values.
127
128
128
129
<!-- -->
129
130
@@ -151,7 +152,7 @@ and, for `Box` smart pointer, conversion of C pointer into `Box` is [discouraged
151
152
## Uninitialized memory
152
153
153
154
By default, Rust forces all values to be initialized, preventing the use of
154
-
uninitialized memory (except if using `std::mem::uninitialized` or
155
+
uninitialized memory (except when using `std::mem::uninitialized` or
155
156
`std::mem::MaybeUninit`).
156
157
157
158
> **Rule {{#check MEM-UNINIT | Do not use uninitialized memory}}**
@@ -170,14 +171,14 @@ The use of uninitialized memory may result in two distinct security issues:
170
171
> `std::mem::MaybeUninit` is an improvement over `std::mem::uninitialized`.
171
172
> Indeed, it makes dropping uninitialized values a lot more difficult.
172
173
> However, it does not change the second issue: the non-drop of an initialized
173
-
> memory is as much likely. It is problematic, in particular when considering
174
+
> memory remains. It is problematic, in particular when considering
174
175
> the use of `Drop` to erase sensitive memory.
175
176
176
177
## Cyclic reference counted pointers (`Rc` and `Arc`)
177
178
178
-
Combining [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html), recurcivity and reference counted pointer into type definitions is unsafe. It can produce memory leaks which can result in DDoS attack or secret leaks.
179
+
Combining [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html), recursivity and reference counted pointer into type definitions is unsafe. It can produce memory leaks which can result in DDoS attacks or leaking secrets.
179
180
180
-
The following example show such a memory leak in safe Rust:
181
+
The following example shows such a memory leak in safe Rust:
181
182
182
183
```rust
183
184
use std::{cell::Cell, rc::Rc};
@@ -235,6 +236,6 @@ Hello, world!
235
236
==153637== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
0 commit comments