diff --git a/samples/distro-examples/tests/strings.bas b/samples/distro-examples/tests/strings.bas index dbf0fb9a..b403065b 100644 --- a/samples/distro-examples/tests/strings.bas +++ b/samples/distro-examples/tests/strings.bas @@ -209,4 +209,9 @@ for t in number_test if (t[1] != bin(t[0])) then throw "err: bin(" + str(t[0]) + ") <> " + t[1] if (t[2] != oct(t[0])) then throw "err: oct(" + str(t[0]) + ") <> " + t[2] if (t[3] != hex(t[0])) then throw "err: hex(" + str(t[0]) + ") <> " + t[3] -next \ No newline at end of file +next + +REM Bug: RTRIM trimed input string on the right side +s1 = " test " +s2 = rtrim(s1) +if(s1 != " test ") then throw "err: RTRIM changed input string" diff --git a/src/common/blib_func.c b/src/common/blib_func.c index 2b553f87..79994c2f 100644 --- a/src/common/blib_func.c +++ b/src/common/blib_func.c @@ -1050,19 +1050,17 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) { break; } p = arg->v.p.ptr; + uint32_t len = strlen(arg->v.p.ptr); if (*p != '\0') { - while (*p) { - p++; - } - p--; - while (p >= arg->v.p.ptr && (is_wspace(*p))) { + p = p + len - 1; + while (p >= arg->v.p.ptr && is_wspace(*p)) { + len--; p--; } - p++; - *p = '\0'; } - r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1); - strcpy(r->v.p.ptr, arg->v.p.ptr); + r->v.p.ptr = (char *)malloc(len + 1); + strncpy(r->v.p.ptr, arg->v.p.ptr, len); + r->v.p.ptr[len] = '\0'; r->v.p.length = strlen(r->v.p.ptr) + 1; // alltrim