string-util: replace version_is_valid()/version_is_valid_version_spec() by a common call
Let's take inspiration from string_is_safe() and take a flags field that allows fine tuning the validation. Then port over all current users of either function to the new logic. Note that this *does* change behaviour in various cases: 1. Generally: we'll now always accept the full UAPI.10 alphabet, including the "~" and "^" characters. As far as I can see there's no downside to this liberalization as none of the current consumers of the two functions uses these characters for anything else. 2. systemd-analyze compare-version will now accept version strings with "_" and "+" without complaining. I see no downside here, it just normalizes these debugging tools, to make them accept what most our other tools accept. 3. "bootctl link" will not accept empty version strings anymore Which is a bugfix I guess. 4. vpick will now refuse "_" and "+" in version strings. It kinda already did, because when parsing versions from filenames it uses "_" and "+" as name, architecture and attempt counter separators. We now systematically refuse it everywhere else in vpick too. This is hence a clean-up. Fixes: #28906 Replaces: #42815 #41937
This commit is contained in:
+33
-15
@@ -1462,29 +1462,47 @@ char* find_line_after_internal(const char *haystack, const char *needle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool version_is_valid(const char *s) {
|
||||
if (isempty(s))
|
||||
bool version_is_valid(const char *s, VersionFlags flags) {
|
||||
|
||||
/* Validates a version string superficially. This does not proces the version string in any
|
||||
* semantical way, it mostly just validates that its charset is reasonable. */
|
||||
|
||||
if (FLAGS_SET(flags, VERSION_ALLOW_EMPTY) ? !s : isempty(s))
|
||||
return false;
|
||||
|
||||
if (!filename_part_is_valid(s))
|
||||
return false;
|
||||
|
||||
/* This is a superset of the characters used by semver. We additionally allow "_". */
|
||||
if (!in_charset(s, ALPHANUMERICAL ".-+_"))
|
||||
return false;
|
||||
/* We always allow all characters specified by the UAPI.10 Version Specification, i.e. 0-9, a-z, A-Z,
|
||||
* ".", "-", "~", "^".
|
||||
*
|
||||
* If the relevant flags are set we'll also allow "+" and "_" separators.
|
||||
*
|
||||
* Note that with SemVer allows 0-9, a-z, A-Z, "+", "-", ".", hence with VERSION_ALLOW_PLUS we
|
||||
* implement a superset of it.
|
||||
*
|
||||
* If you wonder when to use which flags: when validating foreign versions (e.g. distribution
|
||||
* versions in /etc/os-release or so) validate liberally, i.e. add
|
||||
* VERSION_ALLOW_UNDERSCORE|VERSION_ALLOW_PLUS. When validating our own versioned objects (e.g. vpick
|
||||
* or so) validate more strictly, and in particular refuse characters such as "_" and "+" that may be
|
||||
* used for separating component names or boot attempt counters. Also: first – if appropriate – split
|
||||
* the string into individual components. For example, if the string consists of a name and a
|
||||
* version, separated by some character, only pass the version part to this function. The name part
|
||||
* may pass verification, but it's cleaner to not rely on that.
|
||||
*
|
||||
* For details about UAPI.10 see:
|
||||
*
|
||||
* → https://uapi-group.org/specifications/specs/version_format_specification/ */
|
||||
|
||||
return true;
|
||||
}
|
||||
char charset[] = ALPHANUMERICAL ".-~^" /* plus room for the two chars below: */ "\0\0";
|
||||
size_t l = strlen(charset);
|
||||
|
||||
bool version_is_valid_versionspec(const char *s) {
|
||||
if (FLAGS_SET(flags, VERSION_ALLOW_UNDERSCORE))
|
||||
charset[l++] = '_';
|
||||
if (FLAGS_SET(flags, VERSION_ALLOW_PLUS))
|
||||
charset[l++] = '+';
|
||||
|
||||
if (!filename_part_is_valid(s))
|
||||
return false;
|
||||
|
||||
if (!in_charset(s, ALPHANUMERICAL ".-~^"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return in_charset(s, charset);
|
||||
}
|
||||
|
||||
ssize_t strlevenshtein(const char *x, const char *y) {
|
||||
|
||||
Reference in New Issue
Block a user