From a42600f935596b5df21aa88d17c60580e4c104aa Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Thu, 19 Feb 2026 15:28:45 +0100 Subject: [PATCH] part: Check partition parsed partition type before setting it --- src/plugins/part.c | 17 +++++++++++++++-- tests/part_test.py | 10 ++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/plugins/part.c b/src/plugins/part.c index 35ac5c09..30334a5d 100644 --- a/src/plugins/part.c +++ b/src/plugins/part.c @@ -1723,14 +1723,15 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g struct fdisk_partition *pa = NULL; struct fdisk_parttype *ptype = NULL; const gchar *label_name = NULL; + gchar *endptr = NULL; gint status = 0; gint part_id_int = 0; /* check if part type/id is valid for MBR */ if (table_type == BD_PART_TABLE_MSDOS) { - part_id_int = g_ascii_strtoull (type_str, NULL, 0); + part_id_int = g_ascii_strtoull (type_str, &endptr, 0); - if (part_id_int == 0) { + if (part_id_int == 0 || endptr == NULL || *endptr != '\0') { g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, "Invalid partition id given: '%s'.", type_str); return FALSE; @@ -1772,6 +1773,18 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g return FALSE; } + if (table_type == BD_PART_TABLE_MSDOS) { + /* for GPT types unknown by libfdisk might still be valid */ + status = fdisk_parttype_is_unknown (ptype); + if (status != 0) { + g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, + "Invalid partition type given: '%s'.", type_str); + fdisk_unref_parttype (ptype); + fdisk_unref_partition (pa); + return FALSE; + } + } + status = fdisk_set_partition_type (cxt, part_num, ptype); if (status != 0) { g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, diff --git a/tests/part_test.py b/tests/part_test.py index 2bea2e05..44b54bbe 100644 --- a/tests/part_test.py +++ b/tests/part_test.py @@ -1312,6 +1312,16 @@ def test_set_part_id(self): with self.assertRaises(GLib.GError): BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0x85") + # some invalid ids + with self.assertRaises(GLib.GError): + BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "83;id") + + with self.assertRaises(GLib.GError): + BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0xfff") + + with self.assertRaises(GLib.GError): + BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "999") + class PartSetBootableFlagCase(PartTestCase): def test_set_part_type(self):