[Openvpn-devel,v1] auth-pam: keep the text after the last match in searchandreplace()

Message ID 20261130172552.29874-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v1] auth-pam: keep the text after the last match in searchandreplace() |

Commit Message

Gert Doering Nov. 30, 2026, 5:25 p.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

The text following the last occurrence of the search string was never
copied, so a name/value like "USERNAME@example.com" was answered with
the bare username.

Copy the rest of the string after the loop and add a test for it.

Change-Id: I9b0ea680539846b287d5a5b77f981437f7dc7835
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2374
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2374
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <frank@lichtenheld.com>
  

Patch

diff --git a/src/plugins/auth-pam/utils.c b/src/plugins/auth-pam/utils.c
index 01cc4de..22c4eea 100644
--- a/src/plugins/auth-pam/utils.c
+++ b/src/plugins/auth-pam/utils.c
@@ -88,6 +88,7 @@ 
         searching = scratch + searchforlen;
         scratch = strstr(searching, searchfor);
     }
+    memcpy(target, searching, strlen(searching));
     return strdup(temp);
 }
 
diff --git a/tests/unit_tests/plugins/auth-pam/test_search_and_replace.c b/tests/unit_tests/plugins/auth-pam/test_search_and_replace.c
index 89f0642..cee8ebc 100644
--- a/tests/unit_tests/plugins/auth-pam/test_search_and_replace.c
+++ b/tests/unit_tests/plugins/auth-pam/test_search_and_replace.c
@@ -64,6 +64,17 @@ 
 }
 
 static void
+replace_with_trailing_text__trailing_text_is_kept(void **state)
+{
+    char *replaced = searchandreplace("aXbXcd", "X", "YY");
+
+    assert_non_null(replaced);
+    assert_string_equal("aYYbYYcd", replaced);
+
+    free(replaced);
+}
+
+static void
 pattern_not_found__returns_original(void **state)
 {
     char *replaced = searchandreplace("abc", "X", "Y");
@@ -84,6 +95,7 @@ 
         cmocka_unit_test(replace_single_char__one_time__match_is_replaced),
         cmocka_unit_test(replace_single_char__multiple_times__match_all_matches_are_replaced),
         cmocka_unit_test(replace_longer_text__multiple_times__match_all_matches_are_replaced),
+        cmocka_unit_test(replace_with_trailing_text__trailing_text_is_kept),
         cmocka_unit_test(pattern_not_found__returns_original),
     };