]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: move version_is_valid() into generic code
authorLennart Poettering <lennart@poettering.net>
Thu, 2 Mar 2023 10:09:42 +0000 (11:09 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Jun 2023 17:02:31 +0000 (19:02 +0200)
While we are at it, replace the sloppy use of filename_is_valid() by the
less sloppy filename_part_is_valid() (as added by the preceeding
commit), since we don#t want to be too restrictive here. (After all,
version strings invalid as standalone filenames might be valid as part
of filenames, and hence we should allow them).

src/basic/string-util.c
src/basic/string-util.h
src/sysupdate/meson.build
src/sysupdate/sysupdate-pattern.c
src/sysupdate/sysupdate-transfer.c
src/sysupdate/sysupdate-util.c
src/sysupdate/sysupdate-util.h [deleted file]
src/test/test-string-util.c

index 41d264318a27f4dd99bd789580726b0f91d305a5..1eedcb66f7cf3e2c562d1f789aa4822462d99469 100644 (file)
@@ -16,6 +16,7 @@
 #include "macro.h"
 #include "memory-util.h"
 #include "memstream-util.h"
+#include "path-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
@@ -1296,3 +1297,17 @@ char *startswith_strv(const char *string, char **strv) {
 
         return found;
 }
+
+bool version_is_valid(const char *s) {
+        if (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 "," and "_". */
+        if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
+                return false;
+
+        return true;
+}
index b5028c9e00080ac0c026a2bab0ee902353e62703..0ff5b46bba93d74dee0cb132f1356f9f57049d2c 100644 (file)
@@ -275,3 +275,5 @@ char *startswith_strv(const char *string, char **strv);
 
 #define STARTSWITH_SET(p, ...)                                  \
         startswith_strv(p, STRV_MAKE(__VA_ARGS__))
+
+bool version_is_valid(const char *s);
index 2f8c2305dac57f029dc8d1875e011978abee4712..1bd6bbf9a97e3ddcbcce14cbebb6ec02d05d04c5 100644 (file)
@@ -15,8 +15,6 @@ systemd_sysupdate_sources = files(
         'sysupdate-transfer.h',
         'sysupdate-update-set.c',
         'sysupdate-update-set.h',
-        'sysupdate-util.c',
-        'sysupdate-util.h',
         'sysupdate.c',
         'sysupdate.h',
 )
index 6d9c8d8f8b9b02a938ce6caf79962aa9f8ba0482..c9e2067c58e7e34e7d68af051e802b104c2e22b2 100644 (file)
@@ -8,7 +8,6 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "sysupdate-pattern.h"
-#include "sysupdate-util.h"
 
 typedef enum PatternElementType {
         PATTERN_LITERAL,
index bbc3a5bcaa0475bad88fb069f00f04294636b1fb..dd2b1e45ebd39e25cc0910eecfe12e04fb8d32db 100644 (file)
@@ -24,7 +24,6 @@
 #include "sysupdate-pattern.h"
 #include "sysupdate-resource.h"
 #include "sysupdate-transfer.h"
-#include "sysupdate-util.h"
 #include "sysupdate.h"
 #include "tmpfile-util.h"
 #include "web-util.h"
index c7a23015ce52b882f590429cd3df74e5eb5ee119..eacc592f622f5ca3c7c9dff796bc48e187d737ad 100644 (file)
@@ -2,16 +2,3 @@
 
 #include "path-util.h"
 #include "sysupdate-util.h"
-
-bool version_is_valid(const char *s) {
-        if (isempty(s))
-                return false;
-
-        if (!filename_is_valid(s))
-                return false;
-
-        if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
-                return false;
-
-        return true;
-}
diff --git a/src/sysupdate/sysupdate-util.h b/src/sysupdate/sysupdate-util.h
deleted file mode 100644 (file)
index afa3a9d..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include <stdbool.h>
-
-bool version_is_valid(const char *s);
index 2d3e40321e52249330c7533fb1da62e03d7d2db8..e8fcc5a13c98c1ebc24c3407255773b407847d73 100644 (file)
@@ -1264,4 +1264,14 @@ TEST(strstrafter) {
         assert_se(!strstrafter(buffer, "-"));
 }
 
+TEST(version_is_valid) {
+        assert_se(!version_is_valid(NULL));
+        assert_se(!version_is_valid(""));
+        assert_se(version_is_valid("0"));
+        assert_se(version_is_valid("5"));
+        assert_se(version_is_valid("999999"));
+        assert_se(version_is_valid("999999.5"));
+        assert_se(version_is_valid("6.2.12-300.fc38.x86_64"));
+}
+
 DEFINE_TEST_MAIN(LOG_DEBUG);