Skip to content

Commit a7ab00f

Browse files
committed
part: Fix partition ID validation
Follow up for a42600f. We want to be able to set all valid IDs even those unknown to libfdisk.
1 parent 80490b9 commit a7ab00f

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/plugins/part.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,13 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g
17251725
const gchar *label_name = NULL;
17261726
gchar *endptr = NULL;
17271727
gint status = 0;
1728-
gint part_id_int = 0;
1728+
guint64 part_id_int = 0;
17291729

17301730
/* check if part type/id is valid for MBR */
17311731
if (table_type == BD_PART_TABLE_MSDOS) {
1732-
part_id_int = g_ascii_strtoull (type_str, &endptr, 0);
1732+
part_id_int = g_ascii_strtoll (type_str, &endptr, 0);
17331733

1734-
if (part_id_int == 0 || endptr == NULL || *endptr != '\0') {
1734+
if (part_id_int <= 0 || part_id_int > 0xFF || endptr == NULL || *endptr != '\0') {
17351735
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL,
17361736
"Invalid partition id given: '%s'.", type_str);
17371737
return FALSE;
@@ -1773,18 +1773,6 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g
17731773
return FALSE;
17741774
}
17751775

1776-
if (table_type == BD_PART_TABLE_MSDOS) {
1777-
/* for GPT types unknown by libfdisk might still be valid */
1778-
status = fdisk_parttype_is_unknown (ptype);
1779-
if (status != 0) {
1780-
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL,
1781-
"Invalid partition type given: '%s'.", type_str);
1782-
fdisk_unref_parttype (ptype);
1783-
fdisk_unref_partition (pa);
1784-
return FALSE;
1785-
}
1786-
}
1787-
17881776
status = fdisk_set_partition_type (cxt, part_num, ptype);
17891777
if (status != 0) {
17901778
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL,

tests/part_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,13 @@ def test_set_part_id(self):
13081308
ps = BlockDev.part_get_part_spec (self.loop_devs[0], ps.path)
13091309
self.assertEqual(ps.id, "0x8e")
13101310

1311+
# 0xc8 -- unknown to libfdisk, but still valid
1312+
succ = BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0xc8")
1313+
self.assertTrue(succ)
1314+
1315+
ps = BlockDev.part_get_part_spec (self.loop_devs[0], ps.path)
1316+
self.assertEqual(ps.id, "0xc8")
1317+
13111318
# we can't change part id to extended partition id
13121319
with self.assertRaises(GLib.GError):
13131320
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0x85")
@@ -1322,6 +1329,9 @@ def test_set_part_id(self):
13221329
with self.assertRaises(GLib.GError):
13231330
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "999")
13241331

1332+
with self.assertRaises(GLib.GError):
1333+
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "-1")
1334+
13251335

13261336
class PartSetBootableFlagCase(PartTestCase):
13271337
def test_set_part_type(self):

0 commit comments

Comments
 (0)