len = *p_len;
- if (len < 2) /* just '\0'? */
+ if (len == 0)
return;
+ if (*str == '\0') {
+ *p_len = 0;
+ return;
+ }
+
p = str + len - 1;
for (i = len - 1; i >= 0; i--) {
- if (isspace(*p)) {
+ if (isspace(*p) || *p == '\0') {
*p = '\0';
len--;
p--;
*p_len = len;
if (str < p)
- for (; len > 0; len--, str++, p++)
+ for (; len >= 0; len--, str++, p++)
*str = *p;
}