Skip to content

Commit 1509bab

Browse files
author
Mikachu2333
committed
修复 xy_str_strip 误释放的问题
1 parent 92a9d42 commit 1509bab

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/xy.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -610,23 +610,25 @@ xy_str_delete_suffix (const char *str, const char *suffix)
610610
static char *
611611
xy_str_strip (const char *str)
612612
{
613-
char *new = xy_strdup (str);
613+
if (!str)
614+
return xy_strdup ("");
614615

615-
while (strchr ("\n\r\v\t\f ", new[0]))
616-
{
617-
new += 1;
618-
}
616+
const char *start = str;
617+
while (*start && strchr ("\n\r\v\t\f ", *start))
618+
start++;
619619

620-
size_t len = strlen (new);
620+
if ('\0' == *start)
621+
return xy_strdup ("");
621622

622-
char *last = new + len - 1;
623+
const char *end = start + strlen (start) - 1;
624+
while (end >= start && strchr ("\n\r\v\t\f ", *end))
625+
end--;
623626

624-
while (strchr ("\n\r\v\t\f ", *last))
625-
{
626-
*last = '\0';
627-
last -= 1;
628-
}
629-
return new;
627+
size_t len = (size_t) (end - start + 1);
628+
char *ret = xy_malloc0 (len + 1);
629+
memcpy (ret, start, len);
630+
ret[len] = '\0';
631+
return ret;
630632
}
631633

632634
typedef struct

0 commit comments

Comments
 (0)