From ef59ad7d4f401bb9125cced5078d7e6e6b47585e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 29 Jan 2021 16:16:20 +0100 Subject: [PATCH] Fix subStr related exceptions --- tasmota/support.ino | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 3ae2b6105..c2381f289 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -253,8 +253,9 @@ uint32_t ChrCount(const char *str, const char *delim) { } // Function to return a substring defined by a delimiter at an index -char* subStr(char* dest, char* str, const char *delim, int index) -{ +char* subStr(char* dest, char* str, const char *delim, int index) { +/* + // Exceptions on empty fields char *act; char *sub = nullptr; char *ptr; @@ -266,6 +267,22 @@ char* subStr(char* dest, char* str, const char *delim, int index) sub = strtok_r(act, delim, &ptr); if (sub == nullptr) break; } + return sub; +*/ + uint32_t len = strlen(str) -1; + // Since strtok consumes the first arg, make a copy + strncpy(dest, str, len +2); + // Since strtok_r will exception if last char is delim change to space + if (strchr(delim, dest[len]) != nullptr) { dest[len] = ' '; } + + char *ptr = dest; + char *sub; + int i = index; + while ( ptr && ((sub = strtok_r(ptr, delim, &ptr))) && (--i) ) { +// Serial.printf("%s|", sub); + } +// Serial.printf("%s|=\n|", sub); + sub = Trim(sub); return sub; }