Back to all reviewers

Prevent null vulnerabilities

opencv/opencv
Based on 6 comments
C++

Use container classes and smart pointers instead of manual memory allocation to prevent null pointer dereferences and memory leaks. **Instead of raw memory allocation:**

Null Handling C++

Reviewer Prompt

Use container classes and smart pointers instead of manual memory allocation to prevent null pointer dereferences and memory leaks.

Instead of raw memory allocation:

// Problematic: Potential memory leak if exceptions occur
uint32_t* buffer = new uint32_t[size];
// ... operations that might throw
delete[] buffer;  // May never be reached

Prefer container classes:

// Better: Automatic cleanup even with exceptions
std::vector<uint32_t> buffer(size);
// or
cv::AutoBuffer<uint32_t> buffer(size);  // May use stack space for small allocations

For custom resource types, use smart pointers with appropriate deleters:

// Problematic: Manual resource management
WebPAnimDecoder* decoder = WebPAnimDecoderNew(&webp_data, &dec_options);
// ... operations
WebPAnimDecoderDelete(decoder);  // May be forgotten or skipped due to early returns

// Better: Automatic cleanup with custom deleter
struct UniquePtrDeleter {
    void operator()(WebPAnimDecoder* decoder) const { WebPAnimDecoderDelete(decoder); }
};
std::unique_ptr<WebPAnimDecoder, UniquePtrDeleter> decoder(WebPAnimDecoderNew(&webp_data, &dec_options));

When null is invalid, use references instead of pointers:

// Problematic: Needs null check
uint32_t read_chunk(CHUNK* pChunk);

// Better: NULL is not possible
uint32_t read_chunk(CHUNK& pChunk);

Proper memory management not only prevents memory leaks but also eliminates many null pointer scenarios, making your code more robust and easier to maintain.

6
Comments Analyzed
C++
Primary Language
Null Handling
Category

Source Discussions