1#include <assert.h>
2#include <mcheck.h>
3#include <nl_types.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/resource.h>
8
9
10static const char *msgs[] =
11{
12#define INPUT(str)
13#define OUTPUT(str) str,
14#include <intl/msgs.h>
15};
16#define nmsgs (sizeof (msgs) / sizeof (msgs[0]))
17
18
19/* Test for unbounded alloca. */
20static int
21do_bz17905 (void)
22{
23 char *buf;
24 struct rlimit rl;
25 nl_catd result __attribute__ ((unused));
26
27 const int sz = 1024 * 1024;
28
29 getrlimit (RLIMIT_STACK, rlimits: &rl);
30 rl.rlim_cur = sz;
31 setrlimit (RLIMIT_STACK, rlimits: &rl);
32
33 buf = malloc (size: sz + 1);
34 memset (buf, 'A', sz);
35 buf[sz] = '\0';
36 setenv (name: "NLSPATH", value: buf, replace: 1);
37
38 result = catopen (cat_name: buf, NL_CAT_LOCALE);
39 assert (result == (nl_catd) -1);
40
41 free (ptr: buf);
42 return 0;
43}
44
45#define ROUNDS 5
46
47static int
48do_test (void)
49{
50 int rnd;
51 int result = 0;
52
53 mtrace ();
54
55 /* We do this a few times to stress the memory handling. */
56 for (rnd = 0; rnd < ROUNDS; ++rnd)
57 {
58 nl_catd cd = catopen (cat_name: "libc", flag: 0);
59 size_t cnt;
60
61 if (cd == (nl_catd) -1)
62 {
63 printf (format: "cannot load catalog: %m\n");
64 result = 1;
65 break;
66 }
67
68 /* Go through all the messages and compare the result. */
69 for (cnt = 0; cnt < nmsgs; ++cnt)
70 {
71 char *trans;
72
73 trans = catgets (catalog: cd, set: 1, number: 1 + cnt,
74 string: "+#+# if this comes backs it's an error");
75
76 if (trans == NULL)
77 {
78 printf (format: "catgets return NULL for %zd\n", cnt);
79 result = 1;
80 }
81 else if (strcmp (trans, msgs[cnt]) != 0 && msgs[cnt][0] != '\0')
82 {
83 printf (format: "expected \"%s\", got \"%s\"\n", msgs[cnt], trans);
84 result = 1;
85 }
86 }
87
88 if (catclose (catalog: cd) != 0)
89 {
90 printf (format: "catclose failed: %m\n");
91 result = 1;
92 }
93 }
94
95 result += do_bz17905 ();
96 return result;
97}
98
99#define TEST_FUNCTION do_test ()
100#include "../test-skeleton.c"
101

source code of glibc/catgets/tst-catgets.c