Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/plugins/part.c
Original file line number Diff line number Diff line change
Expand Up @@ -1725,13 +1725,13 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g
const gchar *label_name = NULL;
gchar *endptr = NULL;
gint status = 0;
gint part_id_int = 0;
gint64 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, &endptr, 0);
part_id_int = g_ascii_strtoll (type_str, &endptr, 0);

if (part_id_int == 0 || endptr == NULL || *endptr != '\0') {
if (part_id_int <= 0 || part_id_int > 0xFF || endptr == NULL || *endptr != '\0') {
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL,
"Invalid partition id given: '%s'.", type_str);
return FALSE;
Expand Down Expand Up @@ -1773,18 +1773,6 @@ 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,
Expand Down
10 changes: 10 additions & 0 deletions tests/part_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,13 @@ def test_set_part_id(self):
ps = BlockDev.part_get_part_spec (self.loop_devs[0], ps.path)
self.assertEqual(ps.id, "0x8e")

# 0xc8 -- unknown to libfdisk, but still valid
succ = BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0xc8")
self.assertTrue(succ)

ps = BlockDev.part_get_part_spec (self.loop_devs[0], ps.path)
self.assertEqual(ps.id, "0xc8")

# we can't change part id to extended partition id
with self.assertRaises(GLib.GError):
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0x85")
Expand All @@ -1322,6 +1329,9 @@ def test_set_part_id(self):
with self.assertRaises(GLib.GError):
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "999")

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


class PartSetBootableFlagCase(PartTestCase):
def test_set_part_type(self):
Expand Down