Further harden glibc malloc metadata against 1-byte overflows.
[glibc.git] / nss / tst-field.c
blobd80be681c2e370bef5cf8a42946c33c36e65770e
1 /* Test for invalid field handling in file-style NSS databases. [BZ #18724]
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* This test needs to be statically linked because it access hidden
20 functions. */
22 #include <nss.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 static bool errors;
30 static void
31 check (const char *what, bool expr)
33 if (!expr)
35 printf ("FAIL: %s\n", what);
36 errors = true;
40 #define CHECK(expr) check (#expr, (expr))
42 static void
43 check_rewrite (const char *input, const char *expected)
45 char *to_free;
46 const char *result = __nss_rewrite_field (input, &to_free);
47 CHECK (result != NULL);
48 if (result != NULL && strcmp (result, expected) != 0)
50 printf ("FAIL: rewrite \"%s\" -> \"%s\", expected \"%s\"\n",
51 input, result, expected);
52 errors = true;
54 free (to_free);
57 static int
58 do_test (void)
60 CHECK (__nss_valid_field (NULL));
61 CHECK (__nss_valid_field (""));
62 CHECK (__nss_valid_field ("+"));
63 CHECK (__nss_valid_field ("-"));
64 CHECK (__nss_valid_field (" "));
65 CHECK (__nss_valid_field ("abcdef"));
66 CHECK (__nss_valid_field ("abc def"));
67 CHECK (__nss_valid_field ("abc\tdef"));
68 CHECK (!__nss_valid_field ("abcdef:"));
69 CHECK (!__nss_valid_field ("abcde:f"));
70 CHECK (!__nss_valid_field (":abcdef"));
71 CHECK (!__nss_valid_field ("abcdef\n"));
72 CHECK (!__nss_valid_field ("\nabcdef"));
73 CHECK (!__nss_valid_field (":"));
74 CHECK (!__nss_valid_field ("\n"));
76 CHECK (__nss_valid_list_field (NULL));
77 CHECK (__nss_valid_list_field ((char *[]) {(char *) "good", NULL}));
78 CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g,ood", NULL}));
79 CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g\nood", NULL}));
80 CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g:ood", NULL}));
82 check_rewrite (NULL, "");
83 check_rewrite ("", "");
84 check_rewrite ("abc", "abc");
85 check_rewrite ("abc\n", "abc ");
86 check_rewrite ("abc:", "abc ");
87 check_rewrite ("\nabc", " abc");
88 check_rewrite (":abc", " abc");
89 check_rewrite (":", " ");
90 check_rewrite ("\n", " ");
91 check_rewrite ("a:b:c", "a b c");
92 check_rewrite ("a\nb\nc", "a b c");
93 check_rewrite ("a\nb:c", "a b c");
94 check_rewrite ("aa\nbb\ncc", "aa bb cc");
95 check_rewrite ("aa\nbb:cc", "aa bb cc");
97 return errors;
100 #define TEST_FUNCTION do_test ()
101 #include "../test-skeleton.c"