Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::string can't be returned as NULL #44

Open
brechtsanders opened this issue Dec 11, 2021 · 5 comments
Open

std::string can't be returned as NULL #44

brechtsanders opened this issue Dec 11, 2021 · 5 comments

Comments

@brechtsanders
Copy link

In StringTokenizer::GetNextToken() in Utilities/StringTokenizer.cpp the line return(NULL); is not correct as the function returns a std::string which can't be set to NULL..

In fact, GCC 12 will give the following error:

Utilities/StringTokenizer.cpp:238:28: error: use of deleted function 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::nullptr_t) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::nullptr_t = std::nullptr_t]'

Changing the line return(NULL); to return(""); fixes this.

P.S.: I know GCC 12 isn't released yet, but the current stage 3 prerelease already works quite well.

@marxin
Copy link

marxin commented Jan 15, 2022

Thanks for the report. I can confirm that GCC 12 is going to contain the change (adding libstdc++ maintainer @jwakely).

@jwakely
Copy link

jwakely commented Jan 15, 2022

The report is correct, constructing a std::string from a null pointer is undefined behaviour, and would throw in older versions of GCC (was this function ever even called?)

return "" or return std::string() or (since C++11) return {} would work

@marxin
Copy link

marxin commented Jan 19, 2022

Note this is going to be an error only if -std=c++23 is used.

@jwakely
Copy link

jwakely commented Jan 19, 2022

But it's still undefined in all earlier versions of C++ if that code ever executes!

@jwakely
Copy link

jwakely commented Jan 19, 2022

You should still fix it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants