Daniele Esposti's Blog

How to NOT write an exception handler

Exception handlers are important but more important is to code them in the correct way. Today I’ll show you an example of how to NOT write an exception handler. This example is taken from a real piece of code I found laying around in the code base, so “True story...


Disassembly C code for fun – Part 9: arrays

Arrays are a list of fixed number of elements of the same type stored into a continuos block of memory. String are a type of array with a fixed number of char with a NULL as the last element of the array. The assembly generated by an array will be...


Mocking objects in unit tests

Unit testing is important to ensure the stability and the correctness of the code throughout all lifespan of the project. However, testing some code requires a connection to external services (like SMTP, web, etc.) which in all cases must be avoided to simplify the test and development environment and to...


Disassembly C code for fun – Part 8: strings

Following the last post about the buffer overflow protection, today we finally talk about strings. Strings We’ll start back from the last piece of code used in the previous post: #include <stdio.h> int main() { char text[] = "hello world"; return 0; } and its disassembly: 0x0000000100000ed0 <main+0>: push %rbp...


Detect if a number is power of 2

Easy post today: simple method to detect if a number is a power of 2 in O(1) complexity. Yes, O(1), no naive loops involved :-). The code is very simple: bool powerof2(unsigned int n) { return n > 0 & !(n & (n - 1)); } The code looks cryptic,...