Giter Site home page Giter Site logo

memory-safety's Introduction

Memory Safety

๐Ÿ”– Requirements

๐Ÿšฉ Starting

#Clone the project
git clone https://github.com/arielroque/memory-safety.git

#Enter in the folder 
cd memory-safety

๐Ÿ”ฌ Tests

Undefined Behavior

Let's test how C/C++ language handles undefinied behavior

Undefined Behavior: It is when the compiler does not know how to handle with a case and just displays an sort of memory address

Test: We will use a C/C++ code that try access a invalid position of one array

# Test with C
gcc ./c/undefined_behavior.c -o ./c/undefined_behavior
./c/undefined_behavior

# Test with C++
#g++ ./c++/undefined_behavior.cpp -o ./c++/undefined_behavior
#./c++/undefined_behavior

You will see something like that

Position 1 have:1
Position 2 have:2
Position 3 have:3
Position 4 have:4
Position 5 have:-702137232

Buffer Overflow

Let's test how C/C++ handles with buffer overflow

Buffer Overflow: It is when the software use a memory that is not allocated to be used in the first place. All information stored in this positions will be lost when the system allocated to another process.

Test: In this case, we will update the previous code and add a pointer to a invalid position by setting a value.

# Test with C
gcc ./c/buffer_overflow.c -o ./c/buffer_overflow
./c/buffer_overflow

# Test with C++
#g++ ./c++/buffer_overflow.cpp -o ./c++/buffer_overflow
#./c++/buffer_overflow

You will see something like that

Position 1 have: 1 
Position 2 have: 2 
Position 3 have: 3 
Position 4 have: 4 
Position 5 have: 2000 

Building with Rust

Let`s see how Rust handle with undefined behavior when we try to compile the code

cargo build --manifest-path=rust/Cargo.toml

You will see something like that

error: this operation will panic at runtime
  --> src/main.rs:11:36
   |
11 |     println!("Position 5 have: {}",array[4]);
   |                                    ^^^^^^^^ index out of bounds: the length is 4 but the index is 4
   |
   = note: `#[deny(unconditional_panic)]` on by default

error: could not compile `rust` due to previous error

memory-safety's People

Contributors

arielroque avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.