Skip to content
Merged
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
17 changes: 15 additions & 2 deletions src/plugins/part.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/part_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down