Skip to content

Commit

Permalink
Revert using strncmp instead of strcmp to make the function robust an…
Browse files Browse the repository at this point in the history
…d safe from buffer overruns. #33449 (#33482)

* Add tests for Nullable String attribute

* Use strcmp instead of strncmp to compare a null-terminated arg
  • Loading branch information
yufengwangca authored May 16, 2024
1 parent fef9ee3 commit 22b9c96
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool HandleNullableOptional(Argument & arg, char * argValue, std::function<bool(
if (arg.isNullable())
{
auto * nullable = reinterpret_cast<chip::app::DataModel::Nullable<T> *>(arg.value);
if (argValue != nullptr && strncmp(argValue, "null", 4) == 0)
if (strcmp(argValue, "null") == 0)
{
nullable->SetNull();
return true;
Expand Down
2 changes: 1 addition & 1 deletion examples/fabric-admin/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool HandleNullableOptional(Argument & arg, char * argValue, std::function<bool(
if (arg.isNullable())
{
auto * nullable = reinterpret_cast<chip::app::DataModel::Nullable<T> *>(arg.value);
if (argValue != nullptr && strncmp(argValue, "null", 4) == 0)
if (strcmp(argValue, "null") == 0)
{
nullable->SetNull();
return true;
Expand Down
14 changes: 13 additions & 1 deletion src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2896,7 +2896,7 @@ tests:
constraints:
notValue: nullableOctetStrTestValue

# Tests for Char String attribute
# Tests for Nullable Char String attribute

- label: "Read attribute NULLABLE_CHAR_STRING Default Value"
command: "readAttribute"
Expand Down Expand Up @@ -2968,6 +2968,18 @@ tests:
response:
error: UNSUPPORTED_CLUSTER

- label: "Write attribute NULLABLE_CHAR_STRING - Value starting with null"
command: "writeAttribute"
attribute: "nullable_char_string"
arguments:
value: "nulla"

- label: "Read attribute NULLABLE_CHAR_STRING"
command: "readAttribute"
attribute: "nullable_char_string"
response:
value: "nulla"

# Tests for command with optional arguments
- label:
"Send a command that takes an optional parameter but do not set it."
Expand Down

0 comments on commit 22b9c96

Please sign in to comment.