修复 xy_str_strip 误释放的问题

This commit is contained in:
Mikachu2333
2025-10-06 03:00:59 +08:00
parent 5588beb542
commit 41783b8531

View File

@@ -614,23 +614,25 @@ xy_str_delete_suffix (const char *str, const char *suffix)
static char * static char *
xy_str_strip (const char *str) xy_str_strip (const char *str)
{ {
char *new = xy_strdup (str); if (!str)
return xy_strdup ("");
while (strchr ("\n\r\v\t\f ", new[0])) const char *start = str;
{ while (*start && strchr ("\n\r\v\t\f ", *start))
new += 1; start++;
}
size_t len = strlen (new); if ('\0' == *start)
return xy_strdup ("");
char *last = new + len - 1; const char *end = start + strlen (start) - 1;
while (end >= start && strchr ("\n\r\v\t\f ", *end))
end--;
while (strchr ("\n\r\v\t\f ", *last)) size_t len = (size_t) (end - start + 1);
{ char *ret = xy_malloc0 (len + 1);
*last = '\0'; memcpy (ret, start, len);
last -= 1; ret[len] = '\0';
} return ret;
return new;
} }
typedef struct typedef struct