Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
None
-
None
-
None
Description
The pn_inspect() method returns a string representation of the inspected AMQP type.
While testing an array of type char in which the first char had value 0x0, it was observed that this method is not correctly printing the array, but is being truncated:
"@PN_CHAR["
and prevents effective checking of the array contents in the test.
The formatting of simple AMQP types is set in the pni_inspect_atom() method. The use of the %c formatting code to print the char is causing null chars to terminate the string which is being treated as a c-string. The following suggested change fixes this issue and prefixes each char with 'U' to indicate it is UTF-32:
diff --git a/c/src/core/codec.c b/c/src/core/codec.c index b50f286b..1f9185f7 100644 --- a/c/src/core/codec.c +++ b/c/src/core/codec.c @@ -129,7 +129,8 @@ int pni_inspect_atom(pn_atom_t *atom, pn_string_t *str) case PN_INT: return pn_string_addf(str, "%" PRIi32, atom->u.as_int); case PN_CHAR: - return pn_string_addf(str, "%c", atom->u.as_char); + if (isprint(atom->u.as_char)) return pn_string_addf(str, "U'%c'", atom->u.as_char); + return pn_string_addf(str, "U'\\x%" PRIx32 "'", atom->u.as_char); case PN_ULONG: return pn_string_addf(str, "%" PRIu64, atom->u.as_ulong); case PN_LONG:
and the array prints as:
"@PN_CHAR[U'\x0', U'5', U'a', U'Z', U'\x7f']"
The following are improved:
- Each char is surrounded by single quotes '\''
- Each char is prefixed by 'U' to indicate it is UTF-32 (see https://en.cppreference.com/w/cpp/language/character_literal)
- Printable chars print as the char itself, non-printable chars print as the hex ordinal value.
Thoughts?