提交 b2ac85ad 编写于 作者: P Pauli

Rework the append_buf function

It won't overflow the buffer and will allocate new buffers sufficiently large to
hold new strings longer than the expansion factor.
Reviewed-by: NRichard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3847)
上级 9a0953ed
...@@ -48,28 +48,38 @@ const OPTIONS engine_options[] = { ...@@ -48,28 +48,38 @@ const OPTIONS engine_options[] = {
static int append_buf(char **buf, int *size, const char *s) static int append_buf(char **buf, int *size, const char *s)
{ {
if (*buf == NULL) { const int expand = 256;
*size = 256; int len = strlen(s) + 1;
*buf = app_malloc(*size, "engine buffer"); char *p = *buf;
**buf = '\0';
} if (p == NULL) {
*size = ((len + expand - 1) / expand) * expand;
p = *buf = app_malloc(*size, "engine buffer");
} else {
const int blen = strlen(p);
if (blen > 0)
len += 2 + blen;
if (len > *size) {
*size = ((len + expand - 1) / expand) * expand;
p = OPENSSL_realloc(p, *size);
if (p == NULL) {
OPENSSL_free(*buf);
*buf = NULL;
return 0;
}
*buf = p;
}
if (strlen(*buf) + strlen(s) >= (unsigned int)*size) { if (blen > 0) {
char *tmp; p += blen;
*size += 256; *p++ = ',';
tmp = OPENSSL_realloc(*buf, *size); *p++ = ' ';
if (tmp == NULL) {
OPENSSL_free(*buf);
*buf = NULL;
return 0;
} }
*buf = tmp;
} }
if (**buf != '\0') strcpy(p, s);
strcat(*buf, ", ");
strcat(*buf, s);
return 1; return 1;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册