Common code smells in C/C++ and how to fix them. Memory Leaks 1// ❌ Bad 2void process() { 3 int* data = new int[100]; 4 // ... use data ... 5 // forgot to delete! 6} 7 8// ✅ Good: Use RAII 9void process() { 10 std::vector<int> data(100); 11 // ... use data ... 12 // automatically cleaned up 13} 14 15// ✅ Good: …
Read MoreSecure coding practices for C/C++ applications. Buffer Overflow Prevention 1// ❌ Vulnerable 2char buffer[100]; 3gets(buffer); // Never use! 4sprintf(buffer, "%s", user_input); 5 6// ✅ Secure 7char buffer[100]; 8fgets(buffer, sizeof(buffer), stdin); 9snprintf(buffer, sizeof(buffer), "%s", user_input); …
Read MoreTools for checking vulnerabilities in C/C++ code. Valgrind 1# Install 2sudo apt install valgrind 3 4# Check memory leaks 5valgrind --leak-check=full ./program 6 7# Detailed output 8valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./program AddressSanitizer (ASan) 1# Compile with ASan 2gcc …
Read More