Simplify parseval() by allocating a buffer the size of the input string,

which will always be big enough to hold the output string.

Obtained from:	OpenBSD (revision 1.36)
This commit is contained in:
Marcelo Araujo 2017-05-26 03:27:06 +00:00
parent 8bf4606408
commit 85a9cb4440
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318915

View File

@ -1212,30 +1212,19 @@ char *
parseval(char *p, size_t len)
{
char hex[3];
char *cp = p, *buffer, *newbuffer;
size_t size, newsize, i, j;
char *buffer;
size_t i, j;
size = 50;
if ((buffer = calloc(1, size)) == NULL)
if ((buffer = calloc(1, len + 1)) == NULL)
return NULL;
for (i = j = 0; j < len; i++) {
if (i >= size) {
newsize = size + 1024;
if ((newbuffer = realloc(buffer, newsize)) == NULL) {
free(buffer);
return (NULL);
}
buffer = newbuffer;
size = newsize;
}
if (cp[j] == '\\') {
strlcpy(hex, cp + j + 1, sizeof(hex));
if (p[j] == '\\') {
strlcpy(hex, p + j + 1, sizeof(hex));
buffer[i] = (char)strtoumax(hex, NULL, 16);
j += 3;
} else {
buffer[i] = cp[j];
buffer[i] = p[j];
j++;
}
}