C/C++ Code Smells
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: Smart pointers
16void process() {
17 auto data = std::make_unique<int[]>(100);
18 // ... use data ...
19 // automatically cleaned up
20}
Buffer Overflow
1// β Bad
2char buffer[10];
3strcpy(buffer, user_input); // Dangerous!
4
5// β
Good
6char buffer[10];
7strncpy(buffer, user_input, sizeof(buffer) - 1);
8buffer[sizeof(buffer) - 1] = '\0';
9
10// β
Better (C++)
11std::string buffer = user_input;
Use After Free
1// β Bad
2int* ptr = new int(42);
3delete ptr;
4*ptr = 10; // Use after free!
5
6// β
Good
7auto ptr = std::make_unique<int>(42);
8// Can't use after it's freed
Raw Pointers
1// β Bad
2Widget* createWidget() {
3 return new Widget();
4}
5
6// β
Good
7std::unique_ptr<Widget> createWidget() {
8 return std::make_unique<Widget>();
9}
Not Using const
1// β Bad
2void process(std::vector<int>& data) {
3 for (int i = 0; i < data.size(); i++) {
4 std::cout << data[i];
5 }
6}
7
8// β
Good
9void process(const std::vector<int>& data) {
10 for (const auto& item : data) {
11 std::cout << item;
12 }
13}
Related Snippets
- C/C++ Secure Coding
Secure coding practices for C/C++ applications. Buffer Overflow Prevention 1// β β¦ - C/C++ Vulnerability Checks
Tools for checking vulnerabilities in C/C++ code. Valgrind 1# Install 2sudo apt β¦ - Common Antipatterns
Common software antipatterns to avoid across all languages and architectures. β¦ - Common Code Smells
Common code smells to watch for during code reviews with examples and fixes. β¦ - Developer Pre-Submission Checklist
Comprehensive checklist for developers before submitting a pull request. Code β¦ - Go Code Smells
Common code smells in Go and how to fix them. Ignoring Errors 1// β Bad 2result, β¦ - Go Secure Coding
Secure coding practices for Go applications. SQL Injection Prevention 1// β β¦ - Go Vulnerability Checks
Tools for checking vulnerabilities in Go code. Govulncheck 1# Install 2go β¦ - Haskell Code Smells
Common code smells in Haskell and how to fix them. Partial Functions 1-- β Bad: β¦ - Haskell Secure Coding
Secure coding practices for Haskell applications. SQL Injection Prevention 1-- β β¦ - Haskell Vulnerability Checks
Tools for checking vulnerabilities in Haskell code. Cabal Outdated 1# Check β¦ - Python Code Smells
Common code smells in Python and how to fix them. Mutable Default Arguments 1# β β¦ - Python Secure Coding
Secure coding practices for Python applications. SQL Injection Prevention 1# β β¦ - Python Vulnerability Checks
Tools for checking vulnerabilities in Python code. Safety - Dependency Scanner β¦ - Reviewer Checklist
Comprehensive checklist for code reviewers to ensure thorough and constructive β¦ - Rust Code Smells
Common code smells in Rust and how to fix them. Unwrap/Expect Abuse 1// β Bad β¦ - Rust Secure Coding
Secure coding practices for Rust applications. SQL Injection Prevention 1// β β¦ - Rust Vulnerability Checks
Tools for checking vulnerabilities in Rust code. Cargo Audit 1# Install 2cargo β¦ - TypeScript Code Smells
Common code smells in TypeScript and how to fix them. Using any 1// β Bad β¦ - TypeScript Secure Coding
Secure coding practices for TypeScript applications. XSS Prevention 1// β β¦ - TypeScript Vulnerability Checks
Tools for checking vulnerabilities in TypeScript/JavaScript code. npm audit 1# β¦