Skip to content

Commit ecb7fb8

Browse files
Merge pull request #1168 from vojtechtrefny/master_check-parsed-part-type
part: Check partition parsed partition type before setting it
2 parents a43c36b + a42600f commit ecb7fb8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/plugins/part.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,14 +1723,15 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g
17231723
struct fdisk_partition *pa = NULL;
17241724
struct fdisk_parttype *ptype = NULL;
17251725
const gchar *label_name = NULL;
1726+
gchar *endptr = NULL;
17261727
gint status = 0;
17271728
gint part_id_int = 0;
17281729

17291730
/* check if part type/id is valid for MBR */
17301731
if (table_type == BD_PART_TABLE_MSDOS) {
1731-
part_id_int = g_ascii_strtoull (type_str, NULL, 0);
1732+
part_id_int = g_ascii_strtoull (type_str, &endptr, 0);
17321733

1733-
if (part_id_int == 0) {
1734+
if (part_id_int == 0 || endptr == NULL || *endptr != '\0') {
17341735
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL,
17351736
"Invalid partition id given: '%s'.", type_str);
17361737
return FALSE;
@@ -1772,6 +1773,18 @@ static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const g
17721773
return FALSE;
17731774
}
17741775

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+
17751788
status = fdisk_set_partition_type (cxt, part_num, ptype);
17761789
if (status != 0) {
17771790
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
@@ -1312,6 +1312,16 @@ def test_set_part_id(self):
13121312
with self.assertRaises(GLib.GError):
13131313
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0x85")
13141314

1315+
# some invalid ids
1316+
with self.assertRaises(GLib.GError):
1317+
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "83;id")
1318+
1319+
with self.assertRaises(GLib.GError):
1320+
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "0xfff")
1321+
1322+
with self.assertRaises(GLib.GError):
1323+
BlockDev.part_set_part_id (self.loop_devs[0], ps.path, "999")
1324+
13151325

13161326
class PartSetBootableFlagCase(PartTestCase):
13171327
def test_set_part_type(self):

0 commit comments

Comments
 (0)