]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
gpt: generalize validator for GPT partition labels
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Jan 2021 14:50:14 +0000 (15:50 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Mar 2021 05:50:24 +0000 (14:50 +0900)
This adds a proper validator function.

No change in behaviour, just some minor refactoring (this should be
useful elsewhere later on though)

src/partition/repart.c
src/shared/gpt.c
src/shared/gpt.h

index 45e82cd0eb5fb732aec30ecada1a69361911a16b..1964d9c95710bf48eb9a06442485164638a2eae3 100644 (file)
@@ -960,7 +960,6 @@ static int config_parse_label(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char16_t *recoded = NULL;
         _cleanup_free_ char *resolved = NULL;
         char **label = data;
         int r;
@@ -981,11 +980,14 @@ static int config_parse_label(
                 return 0;
         }
 
-        recoded = utf8_to_utf16(resolved, strlen(resolved));
-        if (!recoded)
-                return log_oom();
-
-        if (char16_strlen(recoded) > 36) {
+        r = gpt_partition_label_valid(resolved);
+        if (r < 0) {
+                log_syntax(unit, LOG_WARNING, filename, line, r,
+                           "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
+                           resolved, rvalue);
+                return 0;
+        }
+        if (!r) {
                 log_syntax(unit, LOG_WARNING, filename, line, 0,
                            "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
                            resolved, rvalue);
index 15ea2f0a1f1af01660cc1a0efa2e7049a06a30af..a96f5ee02daba899a757806dcbe8cd9f856c381a 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "gpt.h"
 #include "string-util.h"
+#include "utf8.h"
 
 const GptPartitionType gpt_partition_type_table[] = {
         { GPT_ROOT_X86,              "root-x86"              },
@@ -95,3 +96,13 @@ int gpt_partition_type_uuid_from_string(const char *s, sd_id128_t *ret) {
 
         return sd_id128_from_string(s, ret);
 }
+
+int gpt_partition_label_valid(const char *s) {
+        _cleanup_free_ char16_t *recoded = NULL;
+
+        recoded = utf8_to_utf16(s, strlen(s));
+        if (!recoded)
+                return -ENOMEM;
+
+        return char16_strlen(recoded) <= 36;
+}
index 315cde6be3224f38f645ed1e2741aa8e52dca2ce..2e0f50c3c648fc83798261ff301524dd63676c96 100644 (file)
@@ -126,3 +126,5 @@ typedef struct GptPartitionType {
 } GptPartitionType;
 
 extern const GptPartitionType gpt_partition_type_table[];
+
+int gpt_partition_label_valid(const char *s);