Sitemap
Mohammad Abir Abbas

Mohammad Abir Abbas

Multi-LLM architect · Rust & React engineer · Wavelink NFC × Abaya Track builder · Fractional consultant · HackerNoon/Rustacean featured · abir.getwaved.ai

Secure Coding Paradigms

3 min readOct 4, 2023

--

Part 1 : Simple Control Flow, Fixed upper bounds for loops, No Dynamic Memory Allocation

Today we are going to delve into secure coding practices being ignored by a lot of developers. Only writing codes without adhering to security can cost a business or the industry huge loss in production or open to huge attacks. As IOT and other devices are multiplying day by day and with lot of people joining the coding world it’s imperative that we all start paying more attention.

Get Mohammad Abir Abbas’s stories in your inbox

Join Medium for free to get updates from this writer.

For the comparison we will follow the Nasa Top 10 coding rules as it is related with safety critical systems(like rockets). We will discuss the key rules to follow and provide coding examples to highlight the differences.

Rule №1 — Simple Control Flow

In C++, maintaining a simple control flow is essential for secure coding. Avoiding complex nested conditions and excessive branching improves code readability and reduces the likelihood of logical errors. Let’s take a look at an example:

Press enter or click to view image in full size
Simple Control Flow

In Python, a similar control flow can be achieved with a more readable syntax:

Press enter or click to view image in full size
Simple Control flow(python)

Rule №2 — Fixed Upper Bound for Loops

In both C++ and Python, it is important to have a fixed upper bound for loops to prevent potential buffer overflows and infinite loops. Let’s consider a loop that iterates over an array:

Press enter or click to view image in full size

Similarly, in Python, we can achieve a fixed upper bound loop:

Press enter or click to view image in full size

Rule №3 — No Dynamic Memory Allocation

C++ provides powerful memory management capabilities, including dynamic memory allocation with new and delete. However, in the context of secure coding, it is recommended to avoid dynamic memory allocation whenever possible. Instead, stack-based variables or smart pointers to manage memory is preferred automatically. Here's an example:

Press enter or click to view image in full size

In Python, dynamic memory allocation is inherent to the language, as it relies on garbage collection. We can simply assign a value to a variable without worrying about memory deallocation:

Press enter or click to view image in full size

We will continue this series of writing for future. If you read the above feel free to share on how you have improved your codebases by commenting. Feel free to leave out your feedback on how to improve such writings.

Do reach out to me through mabbas or email mabbas@students.42wolfsbur.de

--

--

Mohammad Abir Abbas
Mohammad Abir Abbas

Written by Mohammad Abir Abbas

Multi-LLM architect · Rust & React engineer · Wavelink NFC × Abaya Track builder · Fractional consultant · HackerNoon/Rustacean featured · abir.getwaved.ai