Skip to content

Commit

Permalink
Remove unnecessary null terminator (#1360)
Browse files Browse the repository at this point in the history
* Remove unnecessary null terminator

* Return guaranteed null-terminated string in TWStringUTF8Bytes

Co-authored-by: Catenocrypt <[email protected]>
  • Loading branch information
10gic and Catenocrypt authored Apr 9, 2021
1 parent fb9ce77 commit 0f2c369
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/TrustWalletCore/TWString.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ size_t TWStringSize(TWString *_Nonnull string);
/// Returns the byte at the provided index.
char TWStringGet(TWString *_Nonnull string, size_t index);

/// Returns the raw pointer to the string's UTF8 bytes.
/// Returns the raw pointer to the string's UTF8 bytes (null-terminated).
const char *_Nonnull TWStringUTF8Bytes(TWString *_Nonnull string);

/// Deletes a string created with a `TWStringCreate*` method. After delete it must not be used (can segfault)!
Expand Down
4 changes: 1 addition & 3 deletions src/interface/TWString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ TWString *_Nonnull TWStringCreateWithUTF8Bytes(const char *_Nonnull bytes) {

TWString *_Nonnull TWStringCreateWithRawBytes(const uint8_t *_Nonnull bytes, size_t size) {
auto s = new std::string(bytes, bytes + size);
// append null terminator
s->append(size, '\0');
return s;
}

Expand All @@ -32,7 +30,7 @@ char TWStringGet(TWString *_Nonnull string, size_t index) {

const char *_Nonnull TWStringUTF8Bytes(TWString *_Nonnull string) {
auto s = reinterpret_cast<const std::string*>(string);
return s->data();
return s->c_str();
}

void TWStringDelete(TWString *_Nonnull string) {
Expand Down

0 comments on commit 0f2c369

Please sign in to comment.