linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: tools: fix input string formatting
@ 2021-02-05  8:04 Aleksandar Gerasimovski
  2021-02-05 16:20 ` Mark Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Aleksandar Gerasimovski @ 2021-02-05  8:04 UTC (permalink / raw)
  To: broonie, linux-spi

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@hitachi-powergrids.com>
---
 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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-02-08 17:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-05  8:04 [PATCH] spi: tools: fix input string formatting Aleksandar Gerasimovski
2021-02-05 16:20 ` Mark Brown
2021-02-06 10:57   ` Aleksandar Gerasimovski
2021-02-08 10:09     ` Mark Brown
2021-02-08 17:08       ` Aleksandar Gerasimovski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).