The actual unescape implementation has two bugs: 1. quotation marks from the input string are not removed and are sent to the spidev, e.g: input string: \"\\xFE\\x01\" will be sent to the spidev as 0x22 0xfe 0x01 0x22 2. there is not format check for decimal input strings First bug makes spidev_test unusable when strict spi sequence is needed, second bug is not nice to have it in. This patch improves unescape function and fixes above listed bugs. Signed-off-by: Aleksandar Gerasimovski <aleksandar.gerasimovski@xxxxxxxxxxxxxxxxxxxxxx> --- tools/spi/spidev_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 83844f8..7c36677 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -89,7 +89,7 @@ static void hex_dump(const void *src, size_t length, size_t line_size, /* * Unescape - process hexadecimal escape character - * converts shell input "\x23" -> 0x23 + * converts shell input "\\x23" -> 0x23 */ static int unescape(char *_dst, char *_src, size_t len) { @@ -100,6 +100,10 @@ static int unescape(char *_dst, char *_src, size_t len) unsigned int ch; while (*src) { + if (*src == '"') { + src++; + continue; + } if (*src == '\\' && *(src+1) == 'x') { match = sscanf(src + 2, "%2x", &ch); if (!match) @@ -108,6 +112,9 @@ static int unescape(char *_dst, char *_src, size_t len) src += 4; *dst++ = (unsigned char)ch; } else { + match = sscanf(src, "%2d", &ch); + if (!match) + pabort("malformed input string"); *dst++ = *src++; } ret++; -- 1.8.3.1