[parted-devel] [PATCH] parted: Add alignment error details to align-check (#726856)

Brian C. Lane bcl at redhat.com
Fri Feb 12 22:01:51 UTC 2016


When align-check is used in interactive mode and it isn't aligned print
the error details. Script mode behavior is unchanged.

Also cleanup pointer usage and handle asprintf failure by using a constant
string in the error report - "unknown (malloc failure)".
---
 parted/parted.c | 56 +++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 17 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index 81b2250..255ff9a 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -184,7 +184,7 @@ static int _print_list ();
 static void _done (PedDevice* dev, PedDisk *diskp);
 static bool partition_align_check (PedDisk const *disk,
                         PedPartition const *part, enum AlignmentType a_type,
-                        char *align_err);
+                        char **align_err);
 
 static void
 _timer_handler (PedTimer* timer, void* context)
@@ -795,13 +795,16 @@ do_mkpart (PedDevice** dev, PedDisk** diskp)
                                  ? PED_EXCEPTION_OK
                                  : PED_EXCEPTION_IGNORE_CANCEL),
                                 _("The resulting partition is not properly "
-                                  "aligned for best performance: %s"), align_err) ==
+                                  "aligned for best performance: %s"),
+                                align_err ? align_err : _("unknown (malloc failed)")) ==
                             PED_EXCEPTION_CANCEL) {
-                                free(align_err);
+                                if (align_err)
+                                    free(align_err);
                                 /* undo partition addition */
                                 goto error_remove_part;
                         }
-                    free(align_err);
+                    if (align_err)
+                        free(align_err);
                 }
         } else {
                 ped_exception_leave_all();
@@ -1633,11 +1636,14 @@ do_select (PedDevice** dev, PedDisk** diskp)
 
    If align_err is not NULL a string describing why the check failed
    will be allocated and returned. It is up to the caller to free this.
-   Pass NULL if not error description is needed.
+   Pass NULL if no error description is needed.
+
+   If allocating the error string fails *align_err will be set to NULL, the
+   caller should always check for this.
 */
 static bool
 partition_align_check (PedDisk const *disk, PedPartition const *part,
-		       enum AlignmentType a_type, char *align_err)
+		       enum AlignmentType a_type, char **align_err)
 {
   PED_ASSERT (part->disk == disk);
   PedDevice const *dev = disk->dev;
@@ -1646,17 +1652,20 @@ partition_align_check (PedDisk const *disk, PedPartition const *part,
 		      ? ped_device_get_minimum_alignment (dev)
 		      : ped_device_get_optimum_alignment (dev));
   if (pa == NULL)
-    return true;
+      return true;
 
   PED_ASSERT (pa->grain_size != 0);
   bool ok = (part->geom.start % pa->grain_size == pa->offset);
 
   /* If it isn't aligned and the caller wants an explanation,
      show them the math.  */
-  if (!ok && align_err)
-      asprintf(align_err, "%llds %% %llds != %llds", part->geom.start,
-                                                     pa->grain_size,
-                                                     pa->offset);
+  if (!ok && align_err) {
+      if (asprintf(align_err,
+                   "%llds %% %llds != %llds",
+                   part->geom.start, pa->grain_size, pa->offset) < 0) {
+          *align_err = NULL;
+      }
+  }
   free (pa);
   return ok;
 }
@@ -1677,12 +1686,25 @@ do_align_check (PedDevice **dev, PedDisk** diskp)
   if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
     goto error;
 
-  bool aligned = partition_align_check (*diskp, part, align_type, NULL);
-  if (!opt_script_mode)
-    printf(aligned ? _("%d aligned\n") : _("%d not aligned\n"), part->num);
-
-  if (opt_script_mode)
-    return aligned ? 1 : 0;
+  char *align_err = NULL;
+  bool aligned = partition_align_check (*diskp, part, align_type, &align_err);
+
+  /* Don't print the error in script mode */
+  if (opt_script_mode) {
+      if (align_err)
+          free(align_err);
+      return aligned ? 1 : 0;
+  }
+
+  if (aligned)
+      printf(_("%d aligned\n"), part->num);
+  else
+      printf(_("%d not aligned: %s\n"),
+             part->num,
+             align_err ? align_err : _("unknown (malloc failed)"));
+
+  if (align_err)
+      free(align_err);
 
   /* Always return 1 in interactive mode, to be consistent
      with the other modes.  */
-- 
2.5.0




More information about the parted-devel mailing list