Fix segmentation fault in tr(1)

and make the parser stricter.
This commit is contained in:
FRIGN 2015-01-24 23:00:34 +01:00
parent c369844ae2
commit ee843a2e09

4
tr.c
View File

@ -75,7 +75,7 @@ resolveescapes(Rune *r, size_t len)
{ {
size_t i, off, m, factor, q; size_t i, off, m, factor, q;
for (i = 0; i < len - 1; i++) { for (i = 0; i < len; i++) {
if (r[i] != '\\') if (r[i] != '\\')
continue; continue;
off = 0; off = 0;
@ -97,7 +97,7 @@ resolveescapes(Rune *r, size_t len)
if (r[m] < '0' || r[m] > '7') if (r[m] < '0' || r[m] > '7')
break; break;
if (m == i + 1) if (m == i + 1)
continue; eprintf("tr: invalid escape sequence '\\%c'\n", r[i + 1]);
off += m - i - 1; off += m - i - 1;
for (--m, q = 0, factor = 1; m > i; m--) { for (--m, q = 0, factor = 1; m > i; m--) {
q += (r[m] - '0') * factor; q += (r[m] - '0') * factor;