forked from github/server
respect the input length when copying in str_escape_ex.
This commit is contained in:
parent
5af3ac1b40
commit
034872c44e
1 changed files with 14 additions and 2 deletions
|
@ -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)
|
||||
{
|
||||
size_t slen = strlen(str);
|
||||
const char *read = str;
|
||||
char *write = buffer;
|
||||
if (size < 1) return NULL;
|
||||
while (size > 1 && *read) {
|
||||
if (size < 1) {
|
||||
return NULL;
|
||||
}
|
||||
while (slen > 0 && size > 1 && *read) {
|
||||
const char *pos = strpbrk(read, chars);
|
||||
size_t len = size;
|
||||
if (pos) {
|
||||
|
@ -324,7 +327,11 @@ const char *str_escape_ex(const char *str, char *buffer, size_t size, const char
|
|||
if (len < size) {
|
||||
unsigned char ch = *(const unsigned char *)pos;
|
||||
if (len > 0) {
|
||||
if (len > slen) {
|
||||
len = slen;
|
||||
}
|
||||
memmove(write, read, len);
|
||||
slen -= len;
|
||||
write += len;
|
||||
read += 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;
|
||||
}
|
||||
++read;
|
||||
--slen;
|
||||
} else {
|
||||
/* end of buffer space */
|
||||
len = size - 1;
|
||||
if (len > 0) {
|
||||
if (len > slen) {
|
||||
len = slen;
|
||||
}
|
||||
memmove(write, read, len);
|
||||
write += len;
|
||||
size -= len;
|
||||
slen -= len;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue