Skip to content

Commit 54f9823

Browse files
committed
Fix: RTRIM changes input string
1 parent 6618409 commit 54f9823

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

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 (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)