Fixed FIND Standard Character String Function. Now returns 0 in case empty string is given either for IN1 or IN2.
authorEdouard Tisserant
Fri, 08 May 2020 10:20:04 +0200
changeset 1090 61fee9f5368a
parent 1089 25271e1a3426
child 1091 6f4e7a8387ae
Fixed FIND Standard Character String Function. Now returns 0 in case empty string is given either for IN1 or IN2.
lib/C/iec_std_functions.h
--- a/lib/C/iec_std_functions.h	Fri Aug 30 10:27:45 2019 +0200
+++ b/lib/C/iec_std_functions.h	Fri May 08 10:20:04 2020 +0200
@@ -1442,6 +1442,7 @@
 static inline __strlen_t __pfind(STRING* IN1, STRING* IN2){
     UINT count1 = 0; /* offset of first matching char in IN1 */
     UINT count2 = 0; /* count of matching char */
+    if(!(IN2->len > 0 && IN1->len >= IN2->len)) return 0;
     while(count1 + count2 < IN1->len && count2 < IN2->len)
     {
         if(IN1->body[count1 + count2] != IN2->body[count2]){
@@ -1452,7 +1453,7 @@
             count2++;
         }
     }
-    return count2 == IN2->len -1 ? 0 : count1 + 1;
+    return count2 == IN2->len ? count1 + 1 : 0;
 }
 
 #define __iec_(TYPENAME) \