Menu

Feature suggestion: warn about deref-by-assignment

CHR
2020-10-07
2021-01-23
  • CHR

    CHR - 2020-10-07

    std::(w)string does not handle assignment of (or construction from) nullptr char strings.

    std::string f(const char* c)
    {
        std::string s;
        s = c;
        c = nullptr;
        s += c;
        return s;
    }
    

    I think it would be nice to get a warning in such cases,

     
  • CHR

    CHR - 2021-01-22

    MSVC reports this as C6387 (at least when constructing from a pointer that could be null):
    https://docs.microsoft.com/en-us/cpp/code-quality/c6387?view=msvc-160
    Thoughts?

     

    Last edit: CHR 2021-01-22
  • Daniel Marjamäki

    of course it would be good to get warnings about it.

    I think there is an old ticket about passing nullptr to std::string constructor. But as far as I remember that does not mention = and +=.

    but not sure how to proceed. The Library::container has a variable stdStringLike maybe if that is true it's ok to always warn for such operations with nullptr, I do not know.

    Are there similar problems for other string classes QString, CString, wxString, ..?

    Configuration of constructors is not possible right now as we don't support overloading yet :-(
    I think it would be difficult to fix overloading. But not impossible.

    As far as I know overloaded operators operator= and operator+= can not be configured but that would be nice.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.