diff --git a/src/dns/nm-dns-manager.c b/src/dns/nm-dns-manager.c index c4402d5dc5..0266c0708f 100644 --- a/src/dns/nm-dns-manager.c +++ b/src/dns/nm-dns-manager.c @@ -594,10 +594,39 @@ create_resolv_conf (const char *const*searches, g_string_append (str, "# Generated by NetworkManager\n"); if (searches && searches[0]) { + gsize search_base_idx; + g_string_append (str, "search"); + search_base_idx = str->len; + for (i = 0; searches[i]; i++) { + const char *s = searches[i]; + gsize l = strlen (s); + + if ( l == 0 + || NM_STRCHAR_ANY (s, ch, NM_IN_SET (ch, ' ', '\t', '\n'))) { + /* there should be no such characters in the search entry. Also, + * because glibc parser would treat them as line/word separator. + * + * Skip the value silently. */ + continue; + } + + if (search_base_idx > 0) { + if (str->len - search_base_idx + 1 + l > 254) { + /* this entry crosses the 256 character boundery. Older glibc versions + * would truncate the entry at this point. + * + * Fill the line with spaces to cross the 256 char boundary and continue + * afterwards. This way, the truncation happens between two search entries. */ + while (str->len - search_base_idx < 257) + g_string_append_c (str, ' '); + search_base_idx = 0; + } + } + g_string_append_c (str, ' '); - g_string_append (str, searches[i]); + g_string_append_len (str, s, l); } g_string_append_c (str, '\n'); } diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 34b183a65d..01d964322c 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -1884,7 +1884,7 @@ test_dns_create_resolv_conf (void) NULL, NULL, "# Generated by NetworkManager\n" - "search a2x456789.b2x456789.c2x456789.d2x456789.e2x456789.f2x456789.g2x456789.h2x456789.i2x456789.j2x4567890 a2y456789.b2y456789.c2y456789.d2y456789.e2y456789.f2y456789.g2y456789.h2y456789.i2y456789.j2y4567890 a2z456789.b2z456789.c2z456789.d2z456789.e2z456789.f2z456789.g2z456789.h2z456789.i2z456789.j2z4567890\n" + "search a2x456789.b2x456789.c2x456789.d2x456789.e2x456789.f2x456789.g2x456789.h2x456789.i2x456789.j2x4567890 a2y456789.b2y456789.c2y456789.d2y456789.e2y456789.f2y456789.g2y456789.h2y456789.i2y456789.j2y4567890 a2z456789.b2z456789.c2z456789.d2z456789.e2z456789.f2z456789.g2z456789.h2z456789.i2z456789.j2z4567890\n" ""); }