Fixed in standard function library: String comparison was retrurning wrong result whith strings of different sizes but starting the same, or with zero length strings.
authorEdouard Tisserant
Thu, 18 Jun 2020 14:32:20 +0200
changeset 1091 6f4e7a8387ae
parent 1090 61fee9f5368a
child 1092 220ddcce851d
Fixed in standard function library: String comparison was retrurning wrong result whith strings of different sizes but starting the same, or with zero length strings.
lib/C/iec_std_functions.h
--- a/lib/C/iec_std_functions.h	Fri May 08 10:20:04 2020 +0200
+++ b/lib/C/iec_std_functions.h	Thu Jun 18 14:32:20 2020 +0200
@@ -890,7 +890,11 @@
 __iec_(TIME)
 #undef __iec_
 
-#define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len)
+inline int __str_cmp(uint8_t* str1, __strlen_t len1, uint8_t* str2, __strlen_t len2) { 
+    int cmp = memcmp(str1, str2, len1 < len2 ? len1 : len2);
+    return cmp ? cmp : (len1 > len2 ? 1 : (len1 < len2 ? - 1 : 0));
+}
+#define __STR_CMP(str1, str2) __str_cmp(str1.body, str1.len, str2.body, str2.len)
 
 /* Max for string data types */	
 __extrem_(MAX_STRING, STRING, __STR_CMP(op1,tmp) < 0) /* The explicitly typed standard functions */