]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: check alignment reset return codes
authorKarel Zak <kzak@redhat.com>
Mon, 2 Sep 2024 12:21:40 +0000 (14:21 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 2 Sep 2024 12:21:40 +0000 (14:21 +0200)
Traditionally, there was only recalculation based on disk properties.
However, since libfdisk now supports additional script headers to
modify the default alignment, we need to check return codes. This is
because users can specify nonsensical values. If we ignore the return
codes, the library will report the issue multiple times and
return an error to the application when creating a new disk label.

Old version:
  >>> Script header accepted.
  >>> Last LBA specified by script is out of range.
      Last LBA specified by script is out of range.
      Last LBA specified by script is out of range.
  Failed to apply script headers, disk label not created: Numerical result out of range

Fixed version:
  >>> Script header accepted.
  >>> Last LBA specified by script is out of range.
  Failed to apply script headers, disk label not created: Numerical result out of range.

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/alignment.c
libfdisk/src/context.c
libfdisk/src/gpt.c
libfdisk/src/label.c
libfdisk/src/script.c

index 3ae7219132e0b8f3c8a99a418b37ccc5f756c1e1..2c7a00486694deb7908aa6ddcaef3ee430198b16 100644 (file)
@@ -227,6 +227,8 @@ int fdisk_override_geometry(struct fdisk_context *cxt,
                            unsigned int heads,
                            unsigned int sectors)
 {
+       int rc = 0;
+
        if (!cxt)
                return -EINVAL;
        if (heads)
@@ -239,14 +241,15 @@ int fdisk_override_geometry(struct fdisk_context *cxt,
        else
                recount_geometry(cxt);
 
-       fdisk_reset_alignment(cxt);
+       rc = fdisk_reset_alignment(cxt);
 
-       DBG(CXT, ul_debugobj(cxt, "override C/H/S: %u/%u/%u",
+       DBG(CXT, ul_debugobj(cxt, "override C/H/S: %u/%u/%u [rc=%d]",
                (unsigned) cxt->geom.cylinders,
                (unsigned) cxt->geom.heads,
-               (unsigned) cxt->geom.sectors));
+               (unsigned) cxt->geom.sectors,
+               rc));
 
-       return 0;
+       return rc;
 }
 
 /**
@@ -365,6 +368,8 @@ int fdisk_has_user_device_geometry(struct fdisk_context *cxt)
 
 int fdisk_apply_user_device_properties(struct fdisk_context *cxt)
 {
+       int rc;
+
        if (!cxt)
                return -EINVAL;
 
@@ -395,7 +400,9 @@ int fdisk_apply_user_device_properties(struct fdisk_context *cxt)
        else if (cxt->user_geom.heads || cxt->user_geom.sectors)
                recount_geometry(cxt);
 
-       fdisk_reset_alignment(cxt);
+       rc = fdisk_reset_alignment(cxt);
+       if (rc)
+               return rc;
 
        if (cxt->user_grain) {
                unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
@@ -467,8 +474,7 @@ int fdisk_reset_device_properties(struct fdisk_context *cxt)
        if (rc)
                return rc;
 
-       fdisk_apply_user_device_properties(cxt);
-       return 0;
+       return fdisk_apply_user_device_properties(cxt);
 }
 
 /*
index 463a60f863a3501b49a8a090b957199e3b3cdb6f..2bf9e2ecd5104b438d2993a8a16e7ab9bff4dc1c 100644 (file)
@@ -303,8 +303,7 @@ int __fdisk_switch_label(struct fdisk_context *cxt, struct fdisk_label *lb)
        cxt->label = lb;
        DBG(CXT, ul_debugobj(cxt, "--> switching context to %s!", lb->name));
 
-       fdisk_apply_label_device_properties(cxt);
-       return 0;
+       return fdisk_apply_label_device_properties(cxt);
 }
 
 /**
index 3e383286bd5717fb61761b2e644237b3e499ed15..374246ce6be20ccbba6bca7c83cd922a9581e9e8 100644 (file)
@@ -3185,8 +3185,11 @@ static int gpt_reset_alignment(struct fdisk_context *cxt)
        } else {
                /* estimate ranges for GPT */
                uint64_t first, last;
+               int rc;
 
-               count_first_last_lba(cxt, &first, &last, NULL);
+               rc = count_first_last_lba(cxt, &first, &last, NULL);
+               if (rc)
+                       return rc;
                if (cxt->first_lba < first)
                        cxt->first_lba = first;
                if (cxt->last_lba > last)
index dc0ad9b04f0b0d9f90d5c156db2d7e70259c74e2..3b6a614411e94eb9b2826c4c39b1502c31bd0f49 100644 (file)
@@ -346,7 +346,7 @@ int fdisk_list_disklabel(struct fdisk_context *cxt)
  */
 int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
 {
-       int haslabel = 0;
+       int haslabel = 0, rc;
        struct fdisk_label *lb;
 
        if (!cxt)
@@ -375,13 +375,19 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
        if (!lb->op->create)
                return -ENOSYS;
 
-       __fdisk_switch_label(cxt, lb);
+       rc = __fdisk_switch_label(cxt, lb);
+       if (rc)
+               return rc;
+
        assert(cxt->label == lb);
 
-       if (haslabel && !cxt->parent)
-               fdisk_reset_device_properties(cxt);
+       if (haslabel && !cxt->parent) {
+               rc = fdisk_reset_device_properties(cxt);
+               if (rc)
+                       return rc;
+       }
 
-       DBG(CXT, ul_debugobj(cxt, "create a new %s label", lb->name));
+       DBG(CXT, ul_debugobj(cxt, "creating a new %s label", lb->name));
        return lb->op->create(cxt);
 }
 
index e357fad9a4a1f3dd9956bf3893542bd44c256cbc..913ae32ebef89825885e96941ca105d0245045ea 100644 (file)
@@ -1570,8 +1570,12 @@ int fdisk_apply_script_headers(struct fdisk_context *cxt, struct fdisk_script *d
                        return rc;
        }
 
-       if (fdisk_has_user_device_properties(cxt))
-               fdisk_apply_user_device_properties(cxt);
+       if (fdisk_has_user_device_properties(cxt)) {
+               rc = fdisk_apply_user_device_properties(cxt);
+               if (rc)
+                       return rc;
+       }
+
 
        /* create empty label */
        name = fdisk_script_get_header(dp, "label");