-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[CHIPTOOL] Fix AddArgument for ByteSpan when content is hexstring #38838
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -710,6 +710,37 @@ size_t Command::AddArgument(const char * name, chip::CharSpan * value, const cha | |
size_t Command::AddArgument(const char * name, chip::ByteSpan * value, const char * desc, uint8_t flags) | ||
{ | ||
Argument arg; | ||
|
||
// if the value is a hex string, we need to convert it from hex to bytes | ||
if ((value->size() > kHexStringPrefixLen) && IsHexString((const char *) value->data())) | ||
{ | ||
uint8_t * buffer = nullptr; | ||
size_t size; | ||
|
||
CHIP_ERROR err = HexToBytes( | ||
chip::CharSpan((const char *) (&value->data()[kHexStringPrefixLen]), (value->size() - kHexStringPrefixLen)), | ||
[&buffer](size_t allocSize) { | ||
buffer = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(allocSize, sizeof(uint8_t))); | ||
return buffer; | ||
}, | ||
&size); | ||
|
||
if (err != CHIP_NO_ERROR) | ||
{ | ||
if (buffer != nullptr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's log the error. |
||
{ | ||
chip::Platform::MemoryFree(buffer); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// The buffer is now filled with the decoded bytes, so we can set the value | ||
// to point to the buffer and set the size accordingly | ||
chip::ByteSpan newvalue = new chip::ByteSpan(buffer, size); | ||
value = &newvalue; | ||
} | ||
|
||
arg.type = ArgumentType::OctetString; | ||
arg.name = name; | ||
arg.value = reinterpret_cast<void *>(value); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes no sense. The way this works is that AddArgument registers a memory location where the value will live later when we have a value. At the point where this is called,
value->size()
is 0 at best (if it's a non-optional argument), and garbage at worst, becausevalue
might in fact be a reinterpret_cast pointer to anOptional<ByteSpan>
here (if the kOptional flag is set).The hex string detection should (and does) happen during the actual argument decoding, which is what will fill in the ByteSpan here. In particular, this will happen in
Command::InitArgument
.