respect the input length when copying in str_escape_ex.

This commit is contained in:
Enno Rehling 2018-05-27 15:15:31 +02:00
parent 5af3ac1b40
commit 034872c44e

View file

@ -312,10 +312,13 @@ char *str_unescape(char *str) {
const char *str_escape_ex(const char *str, char *buffer, size_t size, const char *chars) const char *str_escape_ex(const char *str, char *buffer, size_t size, const char *chars)
{ {
size_t slen = strlen(str);
const char *read = str; const char *read = str;
char *write = buffer; char *write = buffer;
if (size < 1) return NULL; if (size < 1) {
while (size > 1 && *read) { return NULL;
}
while (slen > 0 && size > 1 && *read) {
const char *pos = strpbrk(read, chars); const char *pos = strpbrk(read, chars);
size_t len = size; size_t len = size;
if (pos) { if (pos) {
@ -324,7 +327,11 @@ const char *str_escape_ex(const char *str, char *buffer, size_t size, const char
if (len < size) { if (len < size) {
unsigned char ch = *(const unsigned char *)pos; unsigned char ch = *(const unsigned char *)pos;
if (len > 0) { if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len); memmove(write, read, len);
slen -= len;
write += len; write += len;
read += len; read += len;
size -= len; size -= len;
@ -376,13 +383,18 @@ const char *str_escape_ex(const char *str, char *buffer, size_t size, const char
else size = 1; else size = 1;
} }
++read; ++read;
--slen;
} else { } else {
/* end of buffer space */ /* end of buffer space */
len = size - 1; len = size - 1;
if (len > 0) { if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len); memmove(write, read, len);
write += len; write += len;
size -= len; size -= len;
slen -= len;
break; break;
} }
} }