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

etl::string is invalid if calling assign() with itself #1031

Closed
jbenczarski opened this issue Feb 20, 2025 · 5 comments
Closed

etl::string is invalid if calling assign() with itself #1031

jbenczarski opened this issue Feb 20, 2025 · 5 comments

Comments

@jbenczarski
Copy link

If you assign a string to itself, the first character of the string is set to '0' because initialise() is called during assign(). Is this intentional?

Simple self assignment:

etl::string<32> str1 = "hello world";
str1.assign(str1);

Assignment to itself via string_view of itself:

etl::string<32> str = "hello world";
auto sv = etl::string_view(str);
str.assign(sv.begin(), sv.end());

@jwellbelove
Copy link
Contributor

jwellbelove commented Feb 20, 2025

The first could be solved with a this != &other test.

The second is more problematic. The only thing you could efficiently check is a data() != view.data() test, but that would fail where view.begin() isn't at the start of the string.

@jbenczarski
Copy link
Author

Well, the issue only exists when you assign starting from (str/view).begin().

While not very efficient, would it work if assign did not call initialise() and instead only set current_size = 0? I believe it would fix my issue, but I'm not sure of all the ramifications.

@jwellbelove
Copy link
Contributor

I think that could work. It would have to erase the unused portion of the string if the 'secure' flag was set.

@jwellbelove
Copy link
Contributor

jwellbelove commented Feb 22, 2025

After some thought and experimentation, it is possible to stop a self assign from destroying the string, but it isn't possible to get etl::string_view parameter to work in all circumstances (reverse iterators).

The only option would be to throw an error if the address of the first element is within the address range of the string's buffer. (This has it's own issues)

The STL doesn't do this, the standard just says that elements must not be within the address range of the string's buffer.

Caveat Emptor.

@jwellbelove
Copy link
Contributor

Fixed 20.40.0

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

2 participants