1 diff --git a/catgets/Makefile b/catgets/Makefile
2 index 4624a88..56de38b 100644
5 @@ -34,6 +34,7 @@ test-srcs = test-gencat
6 ifeq ($(run-built-tests),yes)
7 tests-special += $(objpfx)de/libc.cat $(objpfx)test1.cat $(objpfx)test2.cat \
8 $(objpfx)sample.SJIS.cat $(objpfx)test-gencat.out
9 +tests-special += $(objpfx)tst-catgets-mem.out
12 gencat-modules = xmalloc
13 @@ -50,9 +51,11 @@ catgets-CPPFLAGS := -DNLSPATH='"$(msgcatdir)/%L/%N:$(msgcatdir)/%L/LC_MESSAGES/%
15 generated += de.msg test1.cat test1.h test2.cat test2.h sample.SJIS.cat \
17 +generated += tst-catgets.mtrace tst-catgets-mem.out
21 -tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de
22 +tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de MALLOC_TRACE=$(objpfx)tst-catgets.mtrace
24 ifeq ($(run-built-tests),yes)
25 # This test just checks whether the program produces any error or not.
26 @@ -86,4 +89,8 @@ $(objpfx)test-gencat.out: test-gencat.sh $(objpfx)test-gencat \
27 $(objpfx)sample.SJIS.cat: sample.SJIS $(objpfx)gencat
28 $(built-program-cmd) -H $(objpfx)test-gencat.h < $(word 1,$^) > $@; \
31 +$(objpfx)tst-catgets-mem.out: $(objpfx)tst-catgets.out
32 + $(common-objpfx)malloc/mtrace $(objpfx)tst-catgets.mtrace > $@; \
35 diff --git a/catgets/catgets.c b/catgets/catgets.c
36 index cf93d56..4be452d 100644
37 --- a/catgets/catgets.c
38 +++ b/catgets/catgets.c
40 License along with the GNU C Library; if not, see
41 <http://www.gnu.org/licenses/>. */
47 @@ -35,6 +34,7 @@ catopen (const char *cat_name, int flag)
49 const char *env_var = NULL;
50 const char *nlspath = NULL;
53 if (strchr (cat_name, '/') == NULL)
55 @@ -54,7 +54,10 @@ catopen (const char *cat_name, int flag)
57 /* Append the system dependent directory. */
58 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
59 - char *tmp = alloca (len);
62 + if (__glibc_unlikely (tmp == NULL))
63 + return (nl_catd) -1;
65 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
67 @@ -65,16 +68,18 @@ catopen (const char *cat_name, int flag)
69 result = (__nl_catd) malloc (sizeof (*result));
71 - /* We cannot get enough memory. */
72 - return (nl_catd) -1;
74 - if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
76 + /* We cannot get enough memory. */
77 + result = (nl_catd) -1;
79 + else if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
81 /* Couldn't open the file. */
82 free ((void *) result);
83 - return (nl_catd) -1;
84 + result = (nl_catd) -1;
88 return (nl_catd) result;
91 diff --git a/catgets/open_catalog.c b/catgets/open_catalog.c
92 index e069416..9f4d776 100644
93 --- a/catgets/open_catalog.c
94 +++ b/catgets/open_catalog.c
95 @@ -47,6 +47,7 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
101 if (strchr (cat_name, '/') != NULL || nlspath == NULL)
102 fd = open_not_cancel_2 (cat_name, O_RDONLY);
103 @@ -57,23 +58,23 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
104 if (__glibc_unlikely (bufact + (n) >= bufmax)) \
106 char *old_buf = buf; \
107 - bufmax += 256 + (n); \
108 - buf = (char *) alloca (bufmax); \
109 - memcpy (buf, old_buf, bufact); \
110 + bufmax += (bufmax < 256 + (n)) ? 256 + (n) : bufmax; \
111 + buf = realloc (buf, bufmax); \
112 + if (__glibc_unlikely (buf == NULL)) \
119 /* The RUN_NLSPATH variable contains a colon separated list of
120 descriptions where we expect to find catalogs. We have to
121 recognize certain % substitutions and stop when we found the
122 first existing file. */
133 while (*run_nlspath != '\0')
135 @@ -188,7 +189,10 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
137 /* Avoid dealing with directories and block devices */
138 if (__builtin_expect (fd, 0) < 0)
145 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
146 goto close_unlock_return;
147 @@ -325,6 +329,7 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
148 /* Release the lock again. */
150 close_not_cancel_no_status (fd);
155 diff --git a/catgets/tst-catgets.c b/catgets/tst-catgets.c
156 index a0a4089..140de72 100644
157 --- a/catgets/tst-catgets.c
158 +++ b/catgets/tst-catgets.c
162 #include <nl_types.h>
166 +#include <sys/resource.h>
169 static const char *msgs[] =
170 @@ -12,6 +15,33 @@ static const char *msgs[] =
172 #define nmsgs (sizeof (msgs) / sizeof (msgs[0]))
175 +/* Test for unbounded alloca. */
183 + const int sz = 1024 * 1024;
185 + getrlimit (RLIMIT_STACK, &rl);
187 + setrlimit (RLIMIT_STACK, &rl);
189 + buf = malloc (sz + 1);
190 + memset (buf, 'A', sz);
192 + setenv ("NLSPATH", buf, 1);
194 + result = catopen (buf, NL_CAT_LOCALE);
195 + assert (result == (nl_catd) -1);
204 @@ -62,6 +92,7 @@ do_test (void)
208 + result += do_bz17905 ();
215 From 7565d2a862683a3c26ffb1f32351b8c5ab9f7b31 Mon Sep 17 00:00:00 2001
216 From: Paul Pluzhnikov <ppluzhnikov@google.com>
217 Date: Sat, 8 Aug 2015 15:54:40 -0700
218 Subject: [PATCH] Fix trailing space.
221 catgets/tst-catgets.c | 2 +-
222 1 file changed, 1 insertion(+), 1 deletion(-)
224 diff --git a/catgets/tst-catgets.c b/catgets/tst-catgets.c
225 index 140de72..0886938 100644
226 --- a/catgets/tst-catgets.c
227 +++ b/catgets/tst-catgets.c
228 @@ -30,7 +30,7 @@ do_bz17905 (void)
230 setrlimit (RLIMIT_STACK, &rl);
232 - buf = malloc (sz + 1);
233 + buf = malloc (sz + 1);
234 memset (buf, 'A', sz);
236 setenv ("NLSPATH", buf, 1);