]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: gpt: use ul_strtou16 for attribute bit parsing
authorJamie Magee <jamie.magee@gmail.com>
Mon, 4 May 2026 15:47:59 +0000 (08:47 -0700)
committerJamie Magee <jamie.magee@gmail.com>
Mon, 4 May 2026 15:47:59 +0000 (08:47 -0700)
Replace bare strtol() with ul_strtou16() which folds the errno/end-pointer/empty-input checks into one call and uses a uint16_t result type matching the bit-index domain. Also add udevadm settle between write/read sfdisk calls in the new attribute subtests.

libfdisk/src/gpt.c
tests/ts/sfdisk/gpt

index 1c98d859ecac47e4b9ee75d858e2150f3ffe2289..2511361f656dcd068ff0ca4ea393c208a378b246 100644 (file)
@@ -1851,24 +1851,40 @@ static int gpt_entry_attrs_from_string(
                } else if (isdigit((unsigned char) *p)
                           || (strncmp(p, "GUID:", 5) == 0
                               && isdigit((unsigned char) *(p + 5)))) {
-                       char *end = NULL;
+                       const char *num_start, *num_end;
+                       char buf[32];
+                       size_t len;
+                       uint16_t val;
                        int is_guid = (*p == 'G');
 
                        if (is_guid)
                                p += 5;
 
-                       errno = 0;
-                       bit = strtol(p, &end, 0);
-                       if (errno || !end || end == p)
-                               bit = -1;
-                       else if (bit >= GPT_ATTRBIT_GUID_FIRST
-                                && bit < GPT_ATTRBIT_GUID_FIRST + GPT_ATTRBIT_GUID_COUNT)
-                               p = end;
-                       else if (!is_guid && bit >= GPT_ATTRBIT_REQ
-                                && bit <= GPT_ATTRBIT_LEGACY)
-                               p = end;
-                       else
+                       num_start = p;
+                       num_end = p;
+                       while (*num_end && *num_end != ',' && !isblank(*num_end))
+                               num_end++;
+                       len = num_end - num_start;
+
+                       if (len == 0 || len >= sizeof(buf)) {
                                bit = -1;
+                       } else {
+                               memcpy(buf, num_start, len);
+                               buf[len] = '\0';
+
+                               if (ul_strtou16(buf, &val, 0) != 0)
+                                       bit = -1;
+                               else if (val >= GPT_ATTRBIT_GUID_FIRST
+                                        && val < GPT_ATTRBIT_GUID_FIRST + GPT_ATTRBIT_GUID_COUNT) {
+                                       bit = val;
+                                       p = num_end;
+                               } else if (!is_guid && val >= GPT_ATTRBIT_REQ
+                                          && val <= GPT_ATTRBIT_LEGACY) {
+                                       bit = val;
+                                       p = num_end;
+                               } else
+                                       bit = -1;
+                       }
                }
 
                if (bit < 0) {
index 10290485bc86872d83f14c6cce0b4d790d2e2590..80659c2ef8c8cb219564b251ae916664f7b1019e 100755 (executable)
@@ -132,6 +132,7 @@ ts_finalize_subtest
 
 ts_init_subtest "attrs-numeric-named"
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 "0,1,2" >> $TS_OUTPUT 2>> $TS_ERRLOG
+udevadm settle
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 >> $TS_OUTPUT 2>> $TS_ERRLOG
 ts_fdisk_clean $TS_DEVICE
 udevadm settle
@@ -140,6 +141,7 @@ ts_finalize_subtest
 
 ts_init_subtest "attrs-numeric-mixed"
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 "2,GUID:60" >> $TS_OUTPUT 2>> $TS_ERRLOG
+udevadm settle
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 >> $TS_OUTPUT 2>> $TS_ERRLOG
 ts_fdisk_clean $TS_DEVICE
 udevadm settle
@@ -148,6 +150,7 @@ ts_finalize_subtest
 
 ts_init_subtest "attrs-numeric-hex"
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 "0x2" >> $TS_OUTPUT 2>> $TS_ERRLOG
+udevadm settle
 $TS_CMD_SFDISK --part-attrs ${TS_DEVICE} 2 >> $TS_OUTPUT 2>> $TS_ERRLOG
 ts_fdisk_clean $TS_DEVICE
 udevadm settle