Skip to content

Commit 14fc195

Browse files
authored
Merge pull request #200 from Joe7M/master
Fix: RTRIM changes input string
2 parents 6618409 + 463e277 commit 14fc195

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

samples/distro-examples/tests/strings.bas

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,9 @@ for t in number_test
209209
if (t[1] != bin(t[0])) then throw "err: bin(" + str(t[0]) + ") <> " + t[1]
210210
if (t[2] != oct(t[0])) then throw "err: oct(" + str(t[0]) + ") <> " + t[2]
211211
if (t[3] != hex(t[0])) then throw "err: hex(" + str(t[0]) + ") <> " + t[3]
212-
next
212+
next
213+
214+
REM Bug: RTRIM trimed input string on the right side
215+
s1 = " test "
216+
s2 = rtrim(s1)
217+
if(s1 != " test ") then throw "err: RTRIM changed input string"

src/common/blib_func.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,19 +1050,17 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
10501050
break;
10511051
}
10521052
p = arg->v.p.ptr;
1053+
uint32_t len = strlen(arg->v.p.ptr);
10531054
if (*p != '\0') {
1054-
while (*p) {
1055-
p++;
1056-
}
1057-
p--;
1058-
while (p >= arg->v.p.ptr && (is_wspace(*p))) {
1055+
p = p + len - 1;
1056+
while (p >= arg->v.p.ptr && is_wspace(*p)) {
1057+
len--;
10591058
p--;
10601059
}
1061-
p++;
1062-
*p = '\0';
10631060
}
1064-
r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1);
1065-
strcpy(r->v.p.ptr, arg->v.p.ptr);
1061+
r->v.p.ptr = (char *)malloc(len + 1);
1062+
strncpy(r->v.p.ptr, arg->v.p.ptr, len);
1063+
r->v.p.ptr[len] = '\0';
10661064
r->v.p.length = strlen(r->v.p.ptr) + 1;
10671065

10681066
// alltrim

0 commit comments

Comments
 (0)