1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Replace more ad-hoc growing char buffers with strbuf.

I've fixed a handful of these where I found them in passing, but when
I went systematically looking, there were a lot more that I hadn't
found!

A particular highlight of this collection is the code that formats
Windows clipboard data in RTF, which was absolutely crying out for
strbuf_catf, and now it's got it.
This commit is contained in:
Simon Tatham
2019-02-11 06:58:07 +00:00
parent e3e4315033
commit d07d7d66f6
13 changed files with 194 additions and 311 deletions

View File

@ -1084,8 +1084,7 @@ struct openssh_new_key {
/* This too points to a position within keyblob */
ptrlen private;
unsigned char *keyblob;
int keyblob_len, keyblob_size;
strbuf *keyblob;
};
static struct openssh_new_key *load_openssh_new_key(const Filename *filename,
@ -1103,8 +1102,7 @@ static struct openssh_new_key *load_openssh_new_key(const Filename *filename,
unsigned key_index;
ret = snew(struct openssh_new_key);
ret->keyblob = NULL;
ret->keyblob_len = ret->keyblob_size = 0;
ret->keyblob = strbuf_new();
fp = f_open(filename, "r", false);
if (!fp) {
@ -1153,14 +1151,7 @@ static struct openssh_new_key *load_openssh_new_key(const Filename *filename,
goto error;
}
if (ret->keyblob_len + len > ret->keyblob_size) {
ret->keyblob_size = ret->keyblob_len + len + 256;
ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
unsigned char);
}
memcpy(ret->keyblob + ret->keyblob_len, out, len);
ret->keyblob_len += len;
put_data(ret->keyblob, out, len);
smemclr(out, sizeof(out));
}
@ -1175,12 +1166,12 @@ static struct openssh_new_key *load_openssh_new_key(const Filename *filename,
fclose(fp);
fp = NULL;
if (ret->keyblob_len == 0 || !ret->keyblob) {
if (ret->keyblob->len == 0 || !ret->keyblob) {
errmsg = "key body not present";
goto error;
}
BinarySource_BARE_INIT(src, ret->keyblob, ret->keyblob_len);
BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(ret->keyblob));
if (strcmp(get_asciz(src), "openssh-key-v1") != 0) {
errmsg = "new-style OpenSSH magic number missing\n";
@ -1288,10 +1279,7 @@ static struct openssh_new_key *load_openssh_new_key(const Filename *filename,
}
smemclr(base64_bit, sizeof(base64_bit));
if (ret) {
if (ret->keyblob) {
smemclr(ret->keyblob, ret->keyblob_size);
sfree(ret->keyblob);
}
strbuf_free(ret->keyblob);
smemclr(ret, sizeof(*ret));
sfree(ret);
}
@ -1308,8 +1296,7 @@ static bool openssh_new_encrypted(const Filename *filename)
if (!key)
return false;
ret = (key->cipher != ON_E_NONE);
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
strbuf_free(key->keyblob);
smemclr(key, sizeof(*key));
sfree(key);
return ret;
@ -1483,8 +1470,7 @@ static ssh2_userkey *openssh_new_read(
ssh_key_free(retkey->key);
sfree(retkey);
}
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
strbuf_free(key->keyblob);
smemclr(key, sizeof(*key));
sfree(key);
if (errmsg_p) *errmsg_p = errmsg;
@ -1714,8 +1700,7 @@ static bool openssh_auto_write(
struct sshcom_key {
char comment[256]; /* allowing any length is overkill */
unsigned char *keyblob;
int keyblob_len, keyblob_size;
strbuf *keyblob;
};
static struct sshcom_key *load_sshcom_key(const Filename *filename,
@ -1733,8 +1718,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename,
ret = snew(struct sshcom_key);
ret->comment[0] = '\0';
ret->keyblob = NULL;
ret->keyblob_len = ret->keyblob_size = 0;
ret->keyblob = strbuf_new();
fp = f_open(filename, "r", false);
if (!fp) {
@ -1831,14 +1815,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename,
goto error;
}
if (ret->keyblob_len + len > ret->keyblob_size) {
ret->keyblob_size = ret->keyblob_len + len + 256;
ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
unsigned char);
}
memcpy(ret->keyblob + ret->keyblob_len, out, len);
ret->keyblob_len += len;
put_data(ret->keyblob, out, len);
}
p++;
@ -1849,7 +1826,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename,
line = NULL;
}
if (ret->keyblob_len == 0 || !ret->keyblob) {
if (ret->keyblob->len == 0) {
errmsg = "key body not present";
goto error;
}
@ -1868,10 +1845,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename,
line = NULL;
}
if (ret) {
if (ret->keyblob) {
smemclr(ret->keyblob, ret->keyblob_size);
sfree(ret->keyblob);
}
strbuf_free(ret->keyblob);
smemclr(ret, sizeof(*ret));
sfree(ret);
}
@ -1890,7 +1864,7 @@ static bool sshcom_encrypted(const Filename *filename, char **comment)
if (!key)
goto done;
BinarySource_BARE_INIT(src, key->keyblob, key->keyblob_len);
BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(key->keyblob));
if (get_uint32(src) != SSHCOM_MAGIC_NUMBER)
goto done; /* key is invalid */
@ -1905,8 +1879,7 @@ static bool sshcom_encrypted(const Filename *filename, char **comment)
done:
if (key) {
*comment = dupstr(key->comment);
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
strbuf_free(key->keyblob);
smemclr(key, sizeof(*key));
sfree(key);
} else {
@ -1983,7 +1956,7 @@ static ssh2_userkey *sshcom_read(
if (!key)
return NULL;
BinarySource_BARE_INIT(src, key->keyblob, key->keyblob_len);
BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(key->keyblob));
if (get_uint32(src) != SSHCOM_MAGIC_NUMBER) {
errmsg = "key does not begin with magic number";
@ -2158,8 +2131,7 @@ static ssh2_userkey *sshcom_read(
if (blob) {
strbuf_free(blob);
}
smemclr(key->keyblob, key->keyblob_size);
sfree(key->keyblob);
strbuf_free(key->keyblob);
smemclr(key, sizeof(*key));
sfree(key);
if (errmsg_p) *errmsg_p = errmsg;