xframes-project / xframes

GPU-accelerated GUI development for Node.js and the browser
https://xframes.dev
MIT License
7 stars 0 forks source link

Avoid using strncpy in InputText constructor #42

Open coderabbitai[bot] opened 4 months ago

coderabbitai[bot] commented 4 months ago

The constructor in the InputText class uses strncpy, which can lead to buffer overflows. Consider using std::copy from the <algorithm> library to safely copy the string and ensure null termination.

Relevant code snippet:

#include <algorithm> // Include the algorithm library

InputText(ReactImgui* view, const int id, const std::string& defaultValue, const std::string& label, std::optional<BaseStyle>& style) : StyledWidget(view, id, style) {
    m_type = "InputText";
    m_bufferPointer = std::make_unique<char[]>(100);
    m_defaultValue = defaultValue;
    m_label = label;

    if (!defaultValue.empty()) {
        std::copy(defaultValue.c_str(), defaultValue.c_str() + std::min(defaultValue.size(), size_t(99)), m_bufferPointer.get());
        m_bufferPointer[std::min(defaultValue.size(), size_t(99))] = '\0'; // Ensure null termination
    }
}

References:

PR URL: https://github.com/andreamancuso/react-wasm/pull/41 Comment URL: https://github.com/andreamancuso/react-wasm/pull/41#discussion_r1695145756