- parted: Fix ending sector location when using kibi IEC suffix (bcl)

- tests: Fix formatting and snprintf warnings in tests. (bcl)
- ui: Add checks for prompt being NULL (bcl)
- strlist: Handle realloc error in wchar_to_str (bcl)
- libparted: Fix potential NULL dereference in ped_disk_next_partition (bcl)
- filesys: Check for null from close_fn (bcl)
This commit is contained in:
Brian C. Lane 2023-03-17 16:27:05 -07:00
parent dc75c40906
commit d747d1a8fe
7 changed files with 344 additions and 2 deletions

View File

@ -0,0 +1,28 @@
From c409dbf423d870ab26684cd6a6953c76c4a08d7f Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Wed, 15 Feb 2023 10:04:36 -0800
Subject: [PATCH 18/24] filesys: Check for null from close_fn
If the filesystem type name isn't known it can return a NULL.
---
libparted/fs/r/filesys.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libparted/fs/r/filesys.c b/libparted/fs/r/filesys.c
index 9dafd71..6e795bc 100644
--- a/libparted/fs/r/filesys.c
+++ b/libparted/fs/r/filesys.c
@@ -198,8 +198,9 @@ ped_file_system_close (PedFileSystem* fs)
{
PED_ASSERT (fs != NULL);
PedDevice *dev = fs->geom->dev;
+ close_fn_t fn = close_fn (fs->type->name);
- if (!(close_fn (fs->type->name) (fs)))
+ if (!fn || !(fn (fs)))
goto error_close_dev;
ped_device_close (dev);
return 1;
--
2.39.2

View File

@ -0,0 +1,30 @@
From 31db44c74a96f8e2b495205d18525449e9b29543 Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Wed, 15 Feb 2023 10:13:58 -0800
Subject: [PATCH 19/24] libparted: Fix potential NULL dereference in
ped_disk_next_partition
---
libparted/disk.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/libparted/disk.c b/libparted/disk.c
index e1a3489..6c82877 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1718,8 +1718,11 @@ ped_disk_next_partition (const PedDisk* disk, const PedPartition* part)
return part->part_list ? part->part_list : part->next;
if (part->next)
return part->next;
- if (part->type & PED_PARTITION_LOGICAL)
+ if (part->type & PED_PARTITION_LOGICAL) {
+ if (!ped_disk_extended_partition (disk))
+ return NULL;
return ped_disk_extended_partition (disk)->next;
+ }
return NULL;
}
--
2.39.2

View File

@ -0,0 +1,28 @@
From 9cb38a7444d02023d112ae8e9e38436104f75f64 Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Wed, 15 Feb 2023 10:32:59 -0800
Subject: [PATCH 20/24] strlist: Handle realloc error in wchar_to_str
It could return a NULL if the realloc fails. This handles the failure in
the same way as other failures in wchar_to_str, it exits immediately
with an error message.
---
parted/strlist.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/parted/strlist.c b/parted/strlist.c
index 71cba59..7901789 100644
--- a/parted/strlist.c
+++ b/parted/strlist.c
@@ -166,6 +166,8 @@ wchar_to_str (const wchar_t* str, size_t count)
goto error;
result = realloc (result, strlen (result) + 1);
+ if (!result)
+ goto error;
return result;
error:
--
2.39.2

View File

@ -0,0 +1,50 @@
From 09c95d2feab0c087339886134dfe2dc04094348a Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Wed, 15 Feb 2023 11:15:28 -0800
Subject: [PATCH 21/24] ui: Add checks for prompt being NULL
Also removes a cast from const char* to char* when passing to readline
that doesn't appear to be necessary any longer.
Added asserts to make sure prompt isn't NULL after strdup and realloc
calls.
---
parted/ui.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/parted/ui.c b/parted/ui.c
index df14e55..9e5ced2 100644
--- a/parted/ui.c
+++ b/parted/ui.c
@@ -564,8 +564,7 @@ _readline (const char* prompt, const StrList* possibilities)
wipe_line ();
#ifdef HAVE_LIBREADLINE
if (!opt_script_mode) {
- /* XXX: why isn't prompt const? */
- line = readline ((char*) prompt);
+ line = readline (prompt);
if (line)
_add_history_unique (line);
} else
@@ -781,6 +780,8 @@ realloc_and_cat (char* str, const char* append)
int length = strlen (str) + strlen (append) + 1;
char* new_str = realloc (str, length);
+ PED_ASSERT(new_str != NULL);
+
strcat (new_str, append);
return new_str;
}
@@ -789,7 +790,9 @@ static char*
_construct_prompt (const char* head, const char* def,
const StrList* possibilities)
{
+ PED_ASSERT(head != NULL);
char* prompt = strdup (head);
+ PED_ASSERT(prompt != NULL);
if (def && possibilities)
PED_ASSERT (str_list_match_any (possibilities, def));
--
2.39.2

View File

@ -0,0 +1,46 @@
From d272bb5b5343fd288a204314654a7fbaea84528d Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Wed, 15 Feb 2023 11:52:42 -0800
Subject: [PATCH 22/24] tests: Fix formatting and snprintf warnings in tests.
The assert message includes sector values, which are long long int, so
use the proper formatting of %lld.
The snprintf warning complained about trying to write 258 bytes so I
bumped the buffer size up to 259. The return value is already being
checked for truncation so this is just to keep the compiler happy
without having to suppress the warning.
---
libparted/tests/disk.c | 2 +-
libparted/tests/symlink.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libparted/tests/disk.c b/libparted/tests/disk.c
index f7b16a5..a2e304c 100644
--- a/libparted/tests/disk.c
+++ b/libparted/tests/disk.c
@@ -75,7 +75,7 @@ START_TEST (test_duplicate)
ck_assert_msg(part->geom.start == part_dup->geom.start &&
part->geom.end == part_dup->geom.end,
"Duplicated partition %d doesn't match. "
- "Details are start: %d/%d end: %d/%d\n",
+ "Details are start: %lld/%lld end: %lld/%lld\n",
*i, part->geom.start, part_dup->geom.start,
part->geom.end, part_dup->geom.end);
}
diff --git a/libparted/tests/symlink.c b/libparted/tests/symlink.c
index da6bef8..7be02cd 100644
--- a/libparted/tests/symlink.c
+++ b/libparted/tests/symlink.c
@@ -61,7 +61,7 @@ START_TEST (test_symlink)
here, but as /dev/mapper is root owned this is a non issue */
close (tmp_fd);
unlink (ln);
- char temp_disk_path[256];
+ char temp_disk_path[259];
int r = snprintf(temp_disk_path, sizeof temp_disk_path, "%s/%s",
cwd,
temporary_disk);
--
2.39.2

View File

@ -0,0 +1,146 @@
From 3f6b723a7d610d98afb0c9d0cfefe4700712e4db Mon Sep 17 00:00:00 2001
From: "Brian C. Lane" <bcl@redhat.com>
Date: Fri, 3 Mar 2023 14:35:22 -0800
Subject: [PATCH] parted: Fix ending sector location when using kibi IEC suffix
This fixes a bug when using KiB to specify the ending location of a
partition. It was not subtracting 1s like it does with the other units
because it was looking for a 'k' not a 'K'.
This also fixes a quirk of the suffix checking code, it would check for
matching case, but converting to the actual IEC value was case
insensitive. This now uses common functions for the matching so that
case doesn't matter.
It also adds tests to check for the fix.
The only change in behavior is that using KiB to specify the ending
location of a partition will now correctly create the end 1s lower than
the specified location like it does for MiB, GiB, etc.
---
parted/parted.c | 29 ++++++++++++++++++----------
tests/t0207-IEC-binary-notation.sh | 31 ++++++++++++++++++++++++++++++
tests/t0208-mkpart-end-in-IEC.sh | 2 +-
3 files changed, 51 insertions(+), 11 deletions(-)
diff --git a/parted/parted.c b/parted/parted.c
index 84187b7..84465d5 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -583,16 +583,27 @@ void _strip_trailing_spaces(char *str)
str[i]='\0';
}
-/* Return true, if str ends with [kMGTPEZY]iB, i.e. IEC units. */
+/* Return true if the unit is one of the supported IEC unit values */
+static bool
+_is_unit_IEC(const PedUnit unit) {
+ return (unit == PED_UNIT_KIBIBYTE) || (unit == PED_UNIT_MEBIBYTE) ||
+ (unit == PED_UNIT_GIBIBYTE) || (unit == PED_UNIT_TEBIBYTE);
+}
+
+/* Return true, if str ends with IEC units. */
static bool
_string_ends_with_iec_unit(const char *str)
{
- /* 3 characters for the IEC unit and at least 1 digit */
- if (!str || strlen(str) < 4)
- return false;
+ /* 3 characters for the IEC unit and at least 1 digit */
+ if (!str || strlen(str) < 4)
+ return false;
- char const *p = str + strlen(str) - 3;
- return strchr ("kMGTPEZY", *p) && c_strcasecmp (p+1, "iB") == 0;
+ char const *p = str + strlen(str) - 3;
+ PedUnit unit = ped_unit_get_by_name(p);
+ if (unit == -1) {
+ return false;
+ }
+ return _is_unit_IEC(unit);
}
/* Return true if str ends with explicit unit identifier.
@@ -612,7 +623,7 @@ _string_has_unit_suffix(const char *str)
return false;
}
-/* If the selected unit is one of kiB, MiB, GiB or TiB and the partition is not
+/* If the selected unit is one of KiB, MiB, GiB or TiB and the partition is not
* only 1 sector long, then adjust the end so that it is one sector before the
* given position. Also adjust range_end accordingly. Thus next partition can
* start immediately after this one.
@@ -636,9 +647,7 @@ _adjust_end_if_iec (PedSector* start, PedSector* end,
_strip_trailing_spaces(end_input);
PedUnit unit = ped_unit_get_default();
if (_string_ends_with_iec_unit(end_input) ||
- (!_string_has_unit_suffix(end_input) &&
- ((unit == PED_UNIT_KIBIBYTE) || (unit == PED_UNIT_MEBIBYTE) ||
- (unit == PED_UNIT_GIBIBYTE) || (unit == PED_UNIT_TEBIBYTE)))) {
+ (!_string_has_unit_suffix(end_input) && _is_unit_IEC(unit))) {
*end -= 1;
range_end->start -= 1;
range_end->end -= 1;
diff --git a/tests/t0207-IEC-binary-notation.sh b/tests/t0207-IEC-binary-notation.sh
index d9bbad6..b8a789e 100644
--- a/tests/t0207-IEC-binary-notation.sh
+++ b/tests/t0207-IEC-binary-notation.sh
@@ -28,6 +28,7 @@ parted --align=none -s $dev mklabel gpt mkpart p1 $((64*1024))B $((1024*1024-$ss
compare /dev/null err || fail=1
parted -m -s $dev u s p > exp || fail=1
+# Test using MiB
rm $dev
dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
parted --align=none -s $dev mklabel gpt mkpart p1 64KiB 1MiB \
@@ -37,4 +38,34 @@ parted -m -s $dev u s p > out || fail=1
compare exp out || fail=1
+# Test using lower case kib and mib
+rm $dev
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 64kib 1mib \
+ > err 2>&1 || fail=1
+compare /dev/null err || fail=1
+parted -m -s $dev u s p > out || fail=1
+
+compare exp out || fail=1
+
+# Test using KiB
+rm $dev
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 64KiB 1024KiB \
+ > err 2>&1 || fail=1
+compare /dev/null err || fail=1
+parted -m -s $dev u s p > out || fail=1
+
+compare exp out || fail=1
+
+# Test using kiB
+rm $dev
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 64kiB 1024kiB \
+ > err 2>&1 || fail=1
+compare /dev/null err || fail=1
+parted -m -s $dev u s p > out || fail=1
+
+compare exp out || fail=1
+
Exit $fail
diff --git a/tests/t0208-mkpart-end-in-IEC.sh b/tests/t0208-mkpart-end-in-IEC.sh
index 118ec72..a3db2c3 100644
--- a/tests/t0208-mkpart-end-in-IEC.sh
+++ b/tests/t0208-mkpart-end-in-IEC.sh
@@ -25,7 +25,7 @@ dev=dev-file
dd if=/dev/null of=$dev bs=1M seek=$n_mbs || fail=1
# create 1st partition
-parted --align=none -s $dev mklabel gpt mkpart p1 1MiB 2MiB > err 2>&1 || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 1MiB 2048KiB > err 2>&1 || fail=1
compare /dev/null err || fail=1 # expect no output
#parted -m -s $dev u s p > exp || fail=1
--
2.39.2

View File

@ -1,7 +1,7 @@
Summary: The GNU disk partition manipulation program Summary: The GNU disk partition manipulation program
Name: parted Name: parted
Version: 3.5 Version: 3.5
Release: 10%{?dist} Release: 11%{?dist}
License: GPL-3.0-or-later License: GPL-3.0-or-later
URL: http://www.gnu.org/software/parted URL: http://www.gnu.org/software/parted
@ -28,6 +28,12 @@ Patch0014: 0014-gpt-Add-no_automount-partition-flag.patch
Patch0015: 0015-tests-XFS-requires-a-minimum-size-of-300M.patch Patch0015: 0015-tests-XFS-requires-a-minimum-size-of-300M.patch
Patch0016: 0016-libparted-Fix-problem-with-creating-1s-partitions.patch Patch0016: 0016-libparted-Fix-problem-with-creating-1s-partitions.patch
Patch0017: 0017-tests-Fixing-libparted-test-framework-usage.patch Patch0017: 0017-tests-Fixing-libparted-test-framework-usage.patch
Patch0018: 0018-filesys-Check-for-null-from-close_fn.patch
Patch0019: 0019-libparted-Fix-potential-NULL-dereference-in-ped_disk.patch
Patch0020: 0020-strlist-Handle-realloc-error-in-wchar_to_str.patch
Patch0021: 0021-ui-Add-checks-for-prompt-being-NULL.patch
Patch0022: 0022-tests-Fix-formatting-and-snprintf-warnings-in-tests.patch
Patch0023: 0023-parted-Fix-ending-sector-location-when-using-kibi-IE.patch
BuildRequires: gcc BuildRequires: gcc
BuildRequires: e2fsprogs-devel BuildRequires: e2fsprogs-devel
@ -132,7 +138,15 @@ make check
%changelog %changelog
* Tue Feb 07 2023 Brian C. Lane <bcl@redhat.com> - 3.5-9.bcl.1 * Fri Mar 17 2023 Brian C. Lane <bcl@redhat.com> - 3.5-11
- parted: Fix ending sector location when using kibi IEC suffix (bcl)
- tests: Fix formatting and snprintf warnings in tests. (bcl)
- ui: Add checks for prompt being NULL (bcl)
- strlist: Handle realloc error in wchar_to_str (bcl)
- libparted: Fix potential NULL dereference in ped_disk_next_partition (bcl)
- filesys: Check for null from close_fn (bcl)
* Tue Feb 07 2023 Brian C. Lane <bcl@redhat.com> - 3.5-10
- libparted: Fix problem with creating 1s partitions - libparted: Fix problem with creating 1s partitions
- tests: Fixing libparted test framework usage - tests: Fixing libparted test framework usage