tst-catgets.c 1.8 KB

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