diff --git a/Error Handling.md b/Error Handling.md index 2fa1759..785168c 100644 --- a/Error Handling.md +++ b/Error Handling.md @@ -6,7 +6,7 @@ Rust has a number of features for handling errors. One feature is that Rust requ This requirement ensures that errors are handled before the possiblity could arise This can be split into two major categories - - *Recoverable* - File a file not found, just need to report the problem to the user and retry the operation + - [*Recoverable*](#recoverable-errors) - File a file not found, just need to report the problem to the user and retry the operation - [*Unrecoverable*](#unrecoverable-errors) - A symptom of bugs, like trying to access a location beyond the end of an array. Need to immediately stop the program Many languages dont distinguish between the two kinds of errors and handle them the same way using mechanisms such as exceptions @@ -62,7 +62,55 @@ fn main() { } ``` +Here we are tryin to access the 100th element, this is out of range and therefore Rust will initiate a error +In C, attempting to read beyond hte end of a data structure is undefined behavior, and you might get whatever is at the memory location, this would be something "random" + +This is considered a *buffer overread* and can lead to security vulnerabilities, this would allow an attacker to be able to manipulate the index in such a way that they shouldnt be allowed to sore in that data structure. + +Rust protects yo from this kind of vulnerability by casuing a panic if you try to read something out of range. + +The `note:` line tells us that we can set the `RUST_BACKTRACE` environment variable to get a backtrace to show exactly what happened to casue the error. + +The key to reading a backtrace is to start at the top and read until you see the files you wrote, that is where the problem originates. + +The lines above that spot are code that our code has called, and the lines below are the code that called your code. +These before-and-after lines might include core Rust code, std lib code or crates that you are using + +You can set the backtrace by setting the `RUST_BACKTRACE` environment variable to any value except 0 + +Example +``` +RUST_BACKTRACE=1 cargo run +thread 'main' panicked at src/main.rs:4:6: +index out of bounds: the len is 3 but the index is 99 +stack backtrace: + 0: rust_begin_unwind + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5 + 1: core::panicking::panic_fmt + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14 + 2: core::panicking::panic_bounds_check + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:208:5 + 3: >::index + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:255:10 + 4: core::slice::index:: for [T]>::index + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:18:9 + 5: as core::ops::index::Index>::index + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/vec/mod.rs:2770:9 + 6: panic::main + at ./src/main.rs:4:6 + 7: core::ops::function::FnOnce::call_once + at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5 +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. +``` + +In order to a backtarace with this info, debug symbols must be enabled + +Debug symbols are enabled by defualt when using `cargo build` or `cargo run` without the `--release` flag + +In line 6 of the backtrace points to the line in our project that causes the problem, that would be line 4 of *src/main.rs* + +If we dont want our program to panc thne we sould start our investigation at the inidcated line we wrote ### Unwinding the Stack or Aborting in Response to a Panic *unwinding* in rust means that it walks back up the stack and cleans up the data form each function it encounters. @@ -81,4 +129,6 @@ example of this ```toml [profile.release] panic = 'abort' -``` \ No newline at end of file +``` + +## Recoverable Errors