harden dcngettext plural processing
authorRich Felker <dalias@aerifal.cx>
Tue, 29 Jul 2014 16:25:41 +0000 (12:25 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 29 Jul 2014 16:25:41 +0000 (12:25 -0400)
while the __mo_lookup backend can verify that the translated message
ends with a null terminator, is has no way to know nplurals and thus
no way to verify that sufficiently many null terminators are present
in the string to satisfy all plural forms. the code in dcngettext was
already attempting to avoid reading past the end of the mo file
mapping, but failed to do so because the strlen call itself could
over-read. using strnlen instead allows us to avoid the problem.

src/locale/dcngettext.c

index 51e6522..d3462fd 100644 (file)
@@ -229,8 +229,9 @@ notrans:
                unsigned long plural = __pleval(p->plural_rule, n);
                if (plural > p->nplurals) goto notrans;
                while (plural--) {
-                       size_t l = strlen(trans);
-                       if (l+1 >= p->map_size - (trans - (char *)p->map))
+                       size_t rem = p->map_size - (trans - (char *)p->map);
+                       size_t l = strnlen(trans, rem);
+                       if (l+1 >= rem)
                                goto notrans;
                        trans += l+1;
                }