authselect-1.0.2-3: resolve rhbz#1655025 (invalid backup)

Resolves: rhbz#1655025
This commit is contained in:
Pavel Březina 2018-11-30 14:04:16 +01:00
parent 5b3eb25d34
commit 034c678c9d
4 changed files with 404 additions and 1 deletions

View File

@ -0,0 +1,221 @@
From 7cadfbf00aed1ba88bf593b02ee5946ba5f54bde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 28 Nov 2018 13:45:33 +0100
Subject: [PATCH 1/3] util: remove duplicate values correctly in
string_array_del_value
---
src/lib/util/string_array.c | 27 ++++--
src/tests/test_util_string_array.c | 146 ++++++++++++++++++++++++++++-
2 files changed, 163 insertions(+), 10 deletions(-)
diff --git a/src/lib/util/string_array.c b/src/lib/util/string_array.c
index e56d66bdcce7c8a1cf99f9b91068614c4b8d3d81..a8afa5ab8edbb26d6f946619f9ce0b83c511bb8c 100644
--- a/src/lib/util/string_array.c
+++ b/src/lib/util/string_array.c
@@ -140,24 +140,33 @@ string_array_add_value(char **array, const char *value, bool unique)
char **
string_array_del_value(char **array, const char *value)
{
- bool found = false;
- int i;
+ size_t count;
+ size_t pos;
+ size_t i;
- if (!string_array_has_value(array, value)) {
- return array;
+ if (array == NULL) {
+ return NULL;
}
- for (i = 0; array[i] != NULL; i++) {
- if (strcmp(value, array[i]) == 0) {
+ count = string_array_count(array);
+ for (i = 0; i < count; i++) {
+ if (strcmp(array[i], value) == 0) {
free(array[i]);
- found = true;
+ array[i] = NULL;
}
+ }
- if (found) {
- array[i] = array[i + 1];
+ for (i = 0, pos = 0; i < count; i++) {
+ if (array[i] != NULL) {
+ array[pos] = array[i];
+ pos++;
}
}
+ for (; pos < count; pos++) {
+ array[pos] = NULL;
+ }
+
return array;
}
diff --git a/src/tests/test_util_string_array.c b/src/tests/test_util_string_array.c
index 4f6812e9396f04bbbfb72bda8a9022501f074faf..249cb96acea3c4feac910702572cafb1025d9496 100644
--- a/src/tests/test_util_string_array.c
+++ b/src/tests/test_util_string_array.c
@@ -32,11 +32,155 @@ void test_string_array_create(void **state)
string_array_free(array);
}
+void test_string_array_del_value__single(void **state)
+{
+ char **array;
+ const char *values[] = {"1", "2", "3", "4", "5", NULL};
+ const char *expected[] = {"1", "3", "4", "5", NULL};
+ int i;
+
+ array = string_array_create(10);
+ assert_non_null(array);
+
+ /* Fill array. */
+ for (i = 0; values[i] != NULL; i++) {
+ array = string_array_add_value(array, values[i], false);
+ assert_non_null(array);
+ assert_non_null(array[i]);
+ }
+ assert_null(array[i]);
+
+ /* Delete value. */
+ array = string_array_del_value(array, "2");
+ assert_non_null(array);
+
+ /* Test values. */
+ for (i = 0; expected[i] != NULL; i++) {
+ assert_non_null(array[i]);
+ assert_string_equal(array[i], expected[i]);
+ }
+ assert_null(array[i]);
+
+ string_array_free(array);
+}
+
+void test_string_array_del_value__single_repeated(void **state)
+{
+ char **array;
+ const char *values[] = {"1", "2", "2", "3", "2", "4", "2", "5", NULL};
+ const char *expected[] = {"1", "3", "4", "5", NULL};
+ int i;
+
+ array = string_array_create(10);
+ assert_non_null(array);
+
+ /* Fill array. */
+ for (i = 0; values[i] != NULL; i++) {
+ array = string_array_add_value(array, values[i], false);
+ assert_non_null(array);
+ assert_non_null(array[i]);
+ }
+ assert_null(array[i]);
+
+ /* Delete value. */
+ array = string_array_del_value(array, "2");
+ assert_non_null(array);
+
+ /* Test values. */
+ for (i = 0; expected[i] != NULL; i++) {
+ assert_non_null(array[i]);
+ assert_string_equal(array[i], expected[i]);
+ }
+ assert_null(array[i]);
+
+ string_array_free(array);
+}
+
+void test_string_array_del_value__multiple(void **state)
+{
+ char **array;
+ const char *values[] = {"1", "2", "3", "4", "5", NULL};
+ const char *expected[] = {"1", "4", NULL};
+ int i;
+
+ array = string_array_create(10);
+ assert_non_null(array);
+
+ /* Fill array. */
+ for (i = 0; values[i] != NULL; i++) {
+ array = string_array_add_value(array, values[i], false);
+ assert_non_null(array);
+ assert_non_null(array[i]);
+ }
+ assert_null(array[i]);
+
+ /* Delete value. */
+ array = string_array_del_value(array, "2");
+ assert_non_null(array);
+
+ array = string_array_del_value(array, "3");
+ assert_non_null(array);
+
+ array = string_array_del_value(array, "5");
+ assert_non_null(array);
+
+ /* Test values. */
+ for (i = 0; expected[i] != NULL; i++) {
+ assert_non_null(array[i]);
+ assert_string_equal(array[i], expected[i]);
+ }
+ assert_null(array[i]);
+
+ string_array_free(array);
+}
+
+void test_string_array_del_value__multiple_repeated(void **state)
+{
+ char **array;
+ const char *values[] = {"1", "2", "2", "3", "3", "2", "4", "2", "5", "5", NULL};
+ const char *expected[] = {"1", "4", NULL};
+ int i;
+
+ array = string_array_create(10);
+ assert_non_null(array);
+
+ /* Fill array. */
+ for (i = 0; values[i] != NULL; i++) {
+ array = string_array_add_value(array, values[i], false);
+ assert_non_null(array);
+ assert_non_null(array[i]);
+ }
+ assert_null(array[i]);
+
+ /* Delete value. */
+ array = string_array_del_value(array, "2");
+ assert_non_null(array);
+
+ array = string_array_del_value(array, "3");
+ assert_non_null(array);
+
+ array = string_array_del_value(array, "5");
+ assert_non_null(array);
+
+ /* Test values. */
+ for (i = 0; expected[i] != NULL; i++) {
+ assert_non_null(array[i]);
+ assert_string_equal(array[i], expected[i]);
+ }
+ assert_null(array[i]);
+
+ string_array_free(array);
+}
+
int main(int argc, const char *argv[])
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_string_array_create)
+ cmocka_unit_test(test_string_array_create),
+ cmocka_unit_test(test_string_array_del_value__single),
+ cmocka_unit_test(test_string_array_del_value__single_repeated),
+ cmocka_unit_test(test_string_array_del_value__multiple),
+ cmocka_unit_test(test_string_array_del_value__multiple_repeated)
};
return cmocka_run_group_tests(tests, NULL, NULL);
--
2.17.2

View File

@ -0,0 +1,149 @@
From 137a80790f2a81b9b405717579a1682f36f8655f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 28 Nov 2018 13:59:51 +0100
Subject: [PATCH 2/3] util: do not return value from string_array_del_value
It is not needed.
---
src/lib/authselect.c | 9 ++-------
src/lib/util/string_array.c | 6 +++---
src/lib/util/string_array.h | 2 +-
src/tests/test_util_string_array.c | 28 ++++++++--------------------
4 files changed, 14 insertions(+), 31 deletions(-)
diff --git a/src/lib/authselect.c b/src/lib/authselect.c
index e0b8b1246b0e7139494d90cca4e0ebed3eb66376..0f8d4a8b6d0b0faef81daf176486108ed0ea74db 100644
--- a/src/lib/authselect.c
+++ b/src/lib/authselect.c
@@ -179,7 +179,7 @@ authselect_apply_changes(void)
WARN("Profile feature [%s] is no longer supported, removing it...",
features[i]);
- features = string_array_del_value(features, features[i]);
+ string_array_del_value(features, features[i]);
i--;
}
@@ -247,15 +247,10 @@ authselect_feature_disable(const char *feature)
return ret;
}
- features = string_array_del_value(features, feature);
- if (features == NULL) {
- ret = ENOMEM;
- goto done;
- }
+ string_array_del_value(features, feature);
ret = authselect_activate(profile_id, (const char **)features, false);
-done:
string_array_free(features);
free(profile_id);
diff --git a/src/lib/util/string_array.c b/src/lib/util/string_array.c
index a8afa5ab8edbb26d6f946619f9ce0b83c511bb8c..e8871dc067fbf3d461d1ee9579813ddc81eef676 100644
--- a/src/lib/util/string_array.c
+++ b/src/lib/util/string_array.c
@@ -137,7 +137,7 @@ string_array_add_value(char **array, const char *value, bool unique)
return string_array_add_value_safe(array, value, strlen(value), unique);
}
-char **
+void
string_array_del_value(char **array, const char *value)
{
size_t count;
@@ -145,7 +145,7 @@ string_array_del_value(char **array, const char *value)
size_t i;
if (array == NULL) {
- return NULL;
+ return;
}
count = string_array_count(array);
@@ -167,7 +167,7 @@ string_array_del_value(char **array, const char *value)
array[pos] = NULL;
}
- return array;
+ return;
}
char **
diff --git a/src/lib/util/string_array.h b/src/lib/util/string_array.h
index ba9760b5d66a9619ca8edea5e3418c5cfbbec929..5842db174563982528e20354138ef5792346fb37 100644
--- a/src/lib/util/string_array.h
+++ b/src/lib/util/string_array.h
@@ -115,7 +115,7 @@ string_array_add_value(char **array, const char *value, bool unique);
*
* @return Array without the value.
*/
-char **
+void
string_array_del_value(char **array, const char *value);
/**
diff --git a/src/tests/test_util_string_array.c b/src/tests/test_util_string_array.c
index 249cb96acea3c4feac910702572cafb1025d9496..ad76f8b190b823210b5e30ae828dce6518596e3b 100644
--- a/src/tests/test_util_string_array.c
+++ b/src/tests/test_util_string_array.c
@@ -51,8 +51,7 @@ void test_string_array_del_value__single(void **state)
assert_null(array[i]);
/* Delete value. */
- array = string_array_del_value(array, "2");
- assert_non_null(array);
+ string_array_del_value(array, "2");
/* Test values. */
for (i = 0; expected[i] != NULL; i++) {
@@ -83,8 +82,7 @@ void test_string_array_del_value__single_repeated(void **state)
assert_null(array[i]);
/* Delete value. */
- array = string_array_del_value(array, "2");
- assert_non_null(array);
+ string_array_del_value(array, "2");
/* Test values. */
for (i = 0; expected[i] != NULL; i++) {
@@ -115,14 +113,9 @@ void test_string_array_del_value__multiple(void **state)
assert_null(array[i]);
/* Delete value. */
- array = string_array_del_value(array, "2");
- assert_non_null(array);
-
- array = string_array_del_value(array, "3");
- assert_non_null(array);
-
- array = string_array_del_value(array, "5");
- assert_non_null(array);
+ string_array_del_value(array, "2");
+ string_array_del_value(array, "3");
+ string_array_del_value(array, "5");
/* Test values. */
for (i = 0; expected[i] != NULL; i++) {
@@ -153,14 +146,9 @@ void test_string_array_del_value__multiple_repeated(void **state)
assert_null(array[i]);
/* Delete value. */
- array = string_array_del_value(array, "2");
- assert_non_null(array);
-
- array = string_array_del_value(array, "3");
- assert_non_null(array);
-
- array = string_array_del_value(array, "5");
- assert_non_null(array);
+ string_array_del_value(array, "2");
+ string_array_del_value(array, "3");
+ string_array_del_value(array, "5");
/* Test values. */
for (i = 0; expected[i] != NULL; i++) {
--
2.17.2

View File

@ -0,0 +1,26 @@
From 7a9bc11d444220fdb63f6538c5c8a9fe88454520 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 30 Nov 2018 11:51:38 +0100
Subject: [PATCH 3/3] util: fix buffer error in textfile_copy()
Resolves:
https://github.com/pbrezina/authselect/issues/123
---
src/lib/util/textfile.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/lib/util/textfile.c b/src/lib/util/textfile.c
index 7ee5df677b519f2433d9dfa235ad72551f7ded67..3394042eaf0db9508675cbd0aedc0cc13ea6546c 100644
--- a/src/lib/util/textfile.c
+++ b/src/lib/util/textfile.c
@@ -249,7 +249,7 @@ textfile_copy(const char *source,
/* eof not error */
}
- bytes_written = fwrite(buf, sizeof(char), sizeof(buf), fdest);
+ bytes_written = fwrite(buf, sizeof(char), bytes_read, fdest);
if (bytes_written != bytes_read) {
if (ferror(fdest) != 0) {
ret = EIO;
--
2.17.2

View File

@ -1,6 +1,6 @@
Name: authselect
Version: 1.0.2
Release: 1%{?dist}
Release: 2%{?dist}
Summary: Configures authentication and identity sources from supported profiles
URL: https://github.com/pbrezina/authselect
@ -12,6 +12,10 @@ Source1: translations.tar.gz
%global makedir %{_builddir}/%{name}-%{version}
Patch0001: 0001-util-remove-duplicate-values-correctly-in-string_arr.patch
Patch0002: 0002-util-do-not-return-value-from-string_array_del_value.patch
Patch0003: 0003-util-fix-buffer-error-in-textfile_copy.patch
BuildRequires: autoconf
BuildRequires: automake
BuildRequires: findutils
@ -250,6 +254,9 @@ sed -i -E '/^\w+=$/d' %{_sysconfdir}/security/pwquality.conf.d/10-authconfig-pwq
exit 0
%changelog
* Mon Dec 3 2018 Pavel Březina <pbrezina@redhat.com> - 1.0.2-2
- Resolves rhbz#1655025 (invalid backup).
* Fri Nov 23 2018 Pavel Březina <pbrezina@redhat.com> - 1.0.2-1
- Rebase to 1.0.2