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 
10 static 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.  */
20 static int
do_bz17905(void)21 do_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, &rl);
30   rl.rlim_cur = sz;
31   setrlimit (RLIMIT_STACK, &rl);
32 
33   buf = malloc (sz + 1);
34   memset (buf, 'A', sz);
35   buf[sz] = '\0';
36   setenv ("NLSPATH", buf, 1);
37 
38   result = catopen (buf, NL_CAT_LOCALE);
39   assert (result == (nl_catd) -1);
40 
41   free (buf);
42   return 0;
43 }
44 
45 #define ROUNDS 5
46 
47 static int
do_test(void)48 do_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 ("libc", 0);
59       size_t cnt;
60 
61       if (cd == (nl_catd) -1)
62 	{
63 	  printf ("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 (cd, 1, 1 + cnt,
74 			   "+#+# if this comes backs it's an error");
75 
76 	  if (trans == NULL)
77 	    {
78 	      printf ("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 ("expected \"%s\", got \"%s\"\n", msgs[cnt], trans);
84 	      result = 1;
85 	    }
86 	}
87 
88       if (catclose (cd) != 0)
89 	{
90 	  printf ("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