Skip to content

Commit 6708768

Browse files
author
Thies C. Arntzen
committed
xml_utf8_encode: make sure the returned data is zero-terminated
1 parent 1e3553b commit 6708768

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

ext/xml/xml.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ xml_utf8_encode(const char *s, int len, int *newlen, const XML_Char *encoding)
492492
}
493493
/* This is the theoretical max (will never get beyond len * 2 as long
494494
* as we are converting from single-byte characters, though) */
495-
newbuf = emalloc(len * 4);
495+
newbuf = emalloc(len * 4 + 1);
496496
while (pos > 0) {
497497
c = encoder ? encoder((unsigned char)(*s)) : (unsigned short)(*s);
498498
if (c < 0x80) {
@@ -513,9 +513,8 @@ xml_utf8_encode(const char *s, int len, int *newlen, const XML_Char *encoding)
513513
pos--;
514514
s++;
515515
}
516-
if (*newlen < len * 4) {
517-
newbuf = erealloc(newbuf, *newlen);
518-
}
516+
newbuf[*newlen] = 0;
517+
newbuf = erealloc(newbuf, (*newlen)+1);
519518
return newbuf;
520519
}
521520
/* }}} */

0 commit comments

Comments
 (0)