From 8f852eaf9c870de2078649410f029e0846117de2 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 22 Aug 2023 09:58:00 +0200 Subject: types: improve comparison reliability of binary strings The existing `ucv_compare()` implementation utilized `strcmp()` to compare two ucode string values, which may lead to incorrect results for strings containing null bytes as the comparison prematurely aborts when encountering the first null. Rework the string comparison logic to use `memcmp()` for comparing both ucv strings with each other in order to ensure that expressions such as `"" == "\u0000"` lead to the expected `false` result. Ref: https://github.com/openwrt/luci/issues/6530 Signed-off-by: Jo-Philipp Wich --- .../99_bugs/42_types_binary_string_comparison | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/custom/99_bugs/42_types_binary_string_comparison (limited to 'tests/custom/99_bugs') diff --git a/tests/custom/99_bugs/42_types_binary_string_comparison b/tests/custom/99_bugs/42_types_binary_string_comparison new file mode 100644 index 0000000..bc566c6 --- /dev/null +++ b/tests/custom/99_bugs/42_types_binary_string_comparison @@ -0,0 +1,28 @@ +When comparing strings with embedded null bytes, ensure that the comparison +takes the entire string length into account. + +-- Testcase -- +printf("%.J\n", [ + "" == "\u0000", + "" < "\u0000", + "" > "\u0000", + "foo\u0000bar" == "foo\u0000baz", + "foo\u0000bar" < "foo\u0000baz", + "foo\u0000bar" > "foo\u0000baz", +]); +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ + false, + true, + false, + false, + true, + false +] +-- End -- -- cgit v1.2.3