Skip to content

BLERemoteCharacteristic writeValue fails to write std::strings containing null character '\0' #4472

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

Closed
snosrap opened this issue Nov 1, 2020 · 3 comments

Comments

@snosrap
Copy link
Contributor

snosrap commented Nov 1, 2020

Unlike C-style strings, std::strings can contain the null character. For example std::string("hello\0world").

However, in BLERemoteCharacteristic.cpp, the std::string newValue is converted to a C-style string before being sent to writeValue. This means that any characters after a null character in newValue will not be sent to writeValue. For example, std::string("hello\0world") will be converted into the C-style/null-terminated string "hello\0".

writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response);

A fix would be to use the data() method on std::string and, more importantly, the length() method on std::string. newValue.length() will give the total length of the string (including any null characters), however strlen will only return the length up to the first null character.

void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
	writeValue((uint8_t*)newValue.data(), newValue.length(), response);
} // writeValue
@snosrap
Copy link
Contributor Author

snosrap commented Nov 1, 2020

@chegewara, I'm not sure I understand your comment. Are you suggesting I not use writeValue(std::string newValue, bool response = false) in cases where newValue may contain null characters?

@chegewara
Copy link
Contributor

Sorry, i didnt read carefully your OP. Yes, this change is good.

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