From 22b9c96b691d3bb5a5d3db510db353b3b2bc2f74 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Thu, 16 May 2024 14:29:31 +0000 Subject: [PATCH] Revert using strncmp instead of strcmp to make the function robust and safe from buffer overruns. #33449 (#33482) * Add tests for Nullable String attribute * Use strcmp instead of strncmp to compare a null-terminated arg --- examples/chip-tool/commands/common/Command.cpp | 2 +- examples/fabric-admin/commands/common/Command.cpp | 2 +- src/app/tests/suites/TestCluster.yaml | 14 +++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/chip-tool/commands/common/Command.cpp b/examples/chip-tool/commands/common/Command.cpp index 1dff5e83f666b5..2c4f1eb3540e9f 100644 --- a/examples/chip-tool/commands/common/Command.cpp +++ b/examples/chip-tool/commands/common/Command.cpp @@ -171,7 +171,7 @@ bool HandleNullableOptional(Argument & arg, char * argValue, std::function *>(arg.value); - if (argValue != nullptr && strncmp(argValue, "null", 4) == 0) + if (strcmp(argValue, "null") == 0) { nullable->SetNull(); return true; diff --git a/examples/fabric-admin/commands/common/Command.cpp b/examples/fabric-admin/commands/common/Command.cpp index 7e5689130e3e97..f0521a9c259a8b 100644 --- a/examples/fabric-admin/commands/common/Command.cpp +++ b/examples/fabric-admin/commands/common/Command.cpp @@ -171,7 +171,7 @@ bool HandleNullableOptional(Argument & arg, char * argValue, std::function *>(arg.value); - if (argValue != nullptr && strncmp(argValue, "null", 4) == 0) + if (strcmp(argValue, "null") == 0) { nullable->SetNull(); return true; diff --git a/src/app/tests/suites/TestCluster.yaml b/src/app/tests/suites/TestCluster.yaml index 017d6ce6b6daf4..540683a5422f13 100644 --- a/src/app/tests/suites/TestCluster.yaml +++ b/src/app/tests/suites/TestCluster.yaml @@ -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" @@ -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."