[parted-devel] [PATCH 2/3] libparted: Replace abs() with llabs()

Shin'ichiro Kawasaki kawasaki at juno.dti.ne.jp
Sun Jun 30 13:49:43 BST 2019


GCC 9 fails to compile a few C source files which call abs() function.
An example of the error messages was as follows:

  CC       dos.lo
dos.c: In function '_best_solution':
dos.c:1773:13: error: absolute value function 'abs' given an argument of type 'long long int' but has parameter of type 'int' which may cause truncation of value [-Werror=absolute-value]
 1773 |   a_delta = abs (part->geom.start - a->start);
      |             ^~~
dos.c:1774:13: error: absolute value function 'abs' given an argument of type 'long long int' but has parameter of type 'int' which may cause truncation of value [-Werror=absolute-value]
 1774 |   b_delta = abs (part->geom.start - b->start);
      |             ^~~
cc1: all warnings being treated as errors

To avoid the errors, replace abs() function calls with llabs().

Signed-off-by: Shin'ichiro Kawasaki <kawasaki at juno.dti.ne.jp>
---
 libparted/cs/natmath.c | 2 +-
 libparted/disk.c       | 2 +-
 libparted/filesys.c    | 2 +-
 libparted/labels/dos.c | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/libparted/cs/natmath.c b/libparted/cs/natmath.c
index 74d3213..b0bb862 100644
--- a/libparted/cs/natmath.c
+++ b/libparted/cs/natmath.c
@@ -432,7 +432,7 @@ closest (PedSector sector, PedSector a, PedSector b)
 	if (b == -1)
 		return a;
 
-	if (abs (sector - a) < abs (sector - b))
+	if (llabs (sector - a) < llabs (sector - b))
 		return a;
 	else
 		return b;
diff --git a/libparted/disk.c b/libparted/disk.c
index fe82f44..959a2a9 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -615,7 +615,7 @@ ped_disk_check (const PedDisk* disk)
 		if (!geom)
 			continue;
 
-		length_error = abs (walk->geom.length - geom->length);
+		length_error = llabs (walk->geom.length - geom->length);
 		max_length_error = PED_MAX (4096, walk->geom.length / 100);
                 bool ok = (ped_geometry_test_inside (&walk->geom, geom)
                            && length_error <= max_length_error);
diff --git a/libparted/filesys.c b/libparted/filesys.c
index 0f8e821..0012466 100644
--- a/libparted/filesys.c
+++ b/libparted/filesys.c
@@ -211,7 +211,7 @@ _geometry_error (const PedGeometry* a, const PedGeometry* b)
 	PedSector	start_delta = a->start - b->start;
 	PedSector	end_delta = a->end - b->end;
 
-	return abs (start_delta) + abs (end_delta);
+	return llabs (start_delta) + llabs (end_delta);
 }
 
 static PedFileSystemType*
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index d736b9e..6abb662 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -1770,8 +1770,8 @@ _best_solution (const PedPartition* part, const PedCHSGeometry* bios_geom,
 		PedSector	a_delta;
 		PedSector	b_delta;
 
-		a_delta = abs (part->geom.start - a->start);
-		b_delta = abs (part->geom.start - b->start);
+		a_delta = llabs (part->geom.start - a->start);
+		b_delta = llabs (part->geom.start - b->start);
 
 		if (a_delta < b_delta)
 			goto choose_a;
-- 
2.22.0




More information about the parted-devel mailing list