1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-05 21:42:47 -05:00

Generalise strbuf_catf() into put_fmt().

marshal.h now provides a macro put_fmt() which allows you to write
arbitrary printf-formatted data to an arbitrary BinarySink.

We already had this facility for strbufs in particular, in the form of
strbuf_catf(). That was able to take advantage of knowing the inner
structure of a strbuf to minimise memory allocation (it would snprintf
directly into the strbuf's existing buffer if possible). For a general
black-box BinarySink we can't do that, so instead we dupvprintf into a
temporary buffer.

For consistency, I've removed strbuf_catf, and converted all uses of
it into the new put_fmt - and I've also added an extra vtable method
in the BinarySink API, so that put_fmt can still use strbuf_catf's
more efficient memory management when talking to a strbuf, and fall
back to the simpler strategy when that's not available.
This commit is contained in:
Simon Tatham
2021-11-19 10:23:32 +00:00
parent efee4e0eae
commit be8d3974ff
24 changed files with 217 additions and 193 deletions

View File

@ -232,9 +232,9 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
ret->superfamily = IP;
if (ret->ais->ai_canonname != NULL)
strbuf_catf(realhost, "%s", ret->ais->ai_canonname);
put_fmt(realhost, "%s", ret->ais->ai_canonname);
else
strbuf_catf(realhost, "%s", host);
put_fmt(realhost, "%s", host);
#else
if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
/*
@ -258,7 +258,7 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
}
/* This way we are always sure the h->h_name is valid :) */
strbuf_clear(realhost);
strbuf_catf(realhost, "%s", h->h_name);
put_fmt(realhost, "%s", h->h_name);
for (n = 0; h->h_addr_list[n]; n++);
ret->addresses = snewn(n, unsigned long);
ret->naddresses = n;
@ -273,7 +273,7 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
*/
ret->superfamily = IP;
strbuf_clear(realhost);
strbuf_catf(realhost, "%s", host);
put_fmt(realhost, "%s", host);
ret->addresses = snew(unsigned long);
ret->naddresses = 1;
ret->addresses[0] = ntohl(a);