From c10a4d632fb32aa93c5ac97687f2c13925875009 Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Sun, 18 Sep 2022 19:25:21 +0200 Subject: [PATCH] Fix label sanitization (#14428) This simplify and trim whitespaces left. ie, starting, trailing and multiple consecutive whitespaces are now removed. --- libretro-common/playlists/label_sanitization.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libretro-common/playlists/label_sanitization.c b/libretro-common/playlists/label_sanitization.c index 81c04b6457..50a95ee513 100644 --- a/libretro-common/playlists/label_sanitization.c +++ b/libretro-common/playlists/label_sanitization.c @@ -81,13 +81,23 @@ void label_sanitize(char *label, bool (*left)(char*), bool (*right)(char*)) if ((*left)(&label[lindex])) copy = false; else - new_label[rindex++] = label[lindex]; + { + const bool whitespace = label[lindex] == ' ' && (rindex == 0 || new_label[rindex - 1] == ' '); + + /* Simplify consecutive whitespaces */ + if (!whitespace) + new_label[rindex++] = label[lindex]; + } } else if ((*right)(&label[lindex])) copy = true; } - new_label[rindex] = '\0'; + /* Trim trailing whitespace */ + if (rindex > 0 && new_label[rindex - 1] == ' ') + new_label[rindex - 1] = '\0'; + else + new_label[rindex] = '\0'; strlcpy(label, new_label, PATH_MAX_LENGTH); }