Skip to content

Commit

Permalink
Fix octetStringEscapedForCLiteral to handle non-ASCII better. (#13138)
Browse files Browse the repository at this point in the history
There were several bugs in octetStringEscapedForCLiteral:

1. It used \x escapes, but decimal charcode numbers.  Since it was
   only tested for charcode 0, this was not obvious.
2. It did not escape '"', which could break things.
3. It did not escape non-control non-ASCII characters (anything with
   charcode > 127).

As a result the string "\r\n\xff\"\xa0" in YAML would get represented
in the C++ as:

  "\x13\x10ÿ" "

and after this change is represented as:

  "\x0d\x0a\xff\x22\xa0"
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Apr 29, 2022
1 parent 47ebc40 commit bee83b3
Show file tree
Hide file tree
Showing 4 changed files with 2,968 additions and 2,843 deletions.
12 changes: 12 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,18 @@ tests:
response:
value: "Tes\x00ti\x00ng"

- label: "Write attribute OCTET_STRING with weird chars"
command: "writeAttribute"
attribute: "octet_string"
arguments:
value: "\r\n\xff\"\xa0"

- label: "Read attribute OCTET_STRING with weird chars"
command: "readAttribute"
attribute: "octet_string"
response:
value: "\r\n\xff\"\xa0"

- label: "Write attribute OCTET_STRING"
command: "writeAttribute"
attribute: "octet_string"
Expand Down
6 changes: 4 additions & 2 deletions src/app/zap-templates/common/ClusterTestGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,11 @@ function expectedValueHasProp(value, name)

function octetStringEscapedForCLiteral(value)
{
return value.replace(/\p{Control}/gu, ch => {
// Escape control characters, things outside the ASCII range, and single
// quotes (because that's our string terminator).
return value.replace(/\p{Control}|\P{ASCII}|"/gu, ch => {
let code = ch.charCodeAt(0);
code = code.toString();
code = code.toString(16);
if (code.length == 1) {
code = "0" + code;
}
Expand Down
Loading

0 comments on commit bee83b3

Please sign in to comment.