Fix label sanitization (#14428)

This simplify and trim whitespaces left.
ie, starting, trailing and multiple consecutive whitespaces are now
removed.
This commit is contained in:
Filipe Azevedo 2022-09-18 19:25:21 +02:00 committed by GitHub
parent e8f4425eed
commit c10a4d632f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}