mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
Replace several ad-hoc string formatters with strbuf.
uxnet.c's sk_namelookup and the sorting-key construction in pangofont_enum_fonts() were both using s[n]printf and strncpy into buffers that had no real need to be fixed-size; format_telnet_command and the GTK Event Log selection-data builder were doing their own sresize loops, but now we have strbuf they can just use that and save redoing the same work.
This commit is contained in:
parent
915be1f6f0
commit
4251d28f71
98
proxy.c
98
proxy.c
@ -1276,15 +1276,8 @@ int proxy_socks5_negotiate (ProxySocket *p, int change)
|
||||
char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
{
|
||||
char *fmt = conf_get_str(conf, CONF_proxy_telnet_command);
|
||||
char *ret = NULL;
|
||||
int retlen = 0, retsize = 0;
|
||||
int so = 0, eo = 0;
|
||||
#define ENSURE(n) do { \
|
||||
if (retsize < retlen + n) { \
|
||||
retsize = retlen + n + 512; \
|
||||
ret = sresize(ret, retsize, char); \
|
||||
} \
|
||||
} while (0)
|
||||
strbuf *buf = strbuf_new();
|
||||
|
||||
/* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
|
||||
* %%, %host, %port, %user, and %pass
|
||||
@ -1302,11 +1295,8 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
|
||||
/* if there was any unescaped text before the escape
|
||||
* character, send that now */
|
||||
if (eo != so) {
|
||||
ENSURE(eo - so);
|
||||
memcpy(ret + retlen, fmt + so, eo - so);
|
||||
retlen += eo - so;
|
||||
}
|
||||
if (eo != so)
|
||||
put_data(buf, fmt + so, eo - so);
|
||||
|
||||
so = eo++;
|
||||
|
||||
@ -1323,32 +1313,27 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
switch (fmt[eo]) {
|
||||
|
||||
case '\\':
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '\\';
|
||||
put_byte(buf, '\\');
|
||||
eo++;
|
||||
break;
|
||||
|
||||
case '%':
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '%';
|
||||
put_byte(buf, '%');
|
||||
eo++;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '\r';
|
||||
put_byte(buf, '\r');
|
||||
eo++;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '\n';
|
||||
put_byte(buf, '\n');
|
||||
eo++;
|
||||
break;
|
||||
|
||||
case 't':
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '\t';
|
||||
put_byte(buf, '\t');
|
||||
eo++;
|
||||
break;
|
||||
|
||||
@ -1371,16 +1356,14 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
/* non hex character, so we abort and just
|
||||
* send the whole thing unescaped (including \x)
|
||||
*/
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '\\';
|
||||
put_byte(buf, '\\');
|
||||
eo = so + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* we only extract two hex characters */
|
||||
if (i == 1) {
|
||||
ENSURE(1);
|
||||
ret[retlen++] = v;
|
||||
put_byte(buf, v);
|
||||
eo++;
|
||||
break;
|
||||
}
|
||||
@ -1392,9 +1375,7 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
break;
|
||||
|
||||
default:
|
||||
ENSURE(2);
|
||||
memcpy(ret+retlen, fmt + so, 2);
|
||||
retlen += 2;
|
||||
put_data(buf, fmt + so, 2);
|
||||
eo++;
|
||||
break;
|
||||
}
|
||||
@ -1406,61 +1387,37 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
*/
|
||||
|
||||
if (fmt[eo] == '%') {
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '%';
|
||||
put_byte(buf, '%');
|
||||
eo++;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "host", 4) == 0) {
|
||||
char dest[512];
|
||||
int destlen;
|
||||
sk_getaddr(addr, dest, lenof(dest));
|
||||
destlen = strlen(dest);
|
||||
ENSURE(destlen);
|
||||
memcpy(ret+retlen, dest, destlen);
|
||||
retlen += destlen;
|
||||
put_data(buf, dest, strlen(dest));
|
||||
eo += 4;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "port", 4) == 0) {
|
||||
char portstr[8], portlen;
|
||||
portlen = sprintf(portstr, "%i", port);
|
||||
ENSURE(portlen);
|
||||
memcpy(ret + retlen, portstr, portlen);
|
||||
retlen += portlen;
|
||||
strbuf_catf(buf, "%d", port);
|
||||
eo += 4;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "user", 4) == 0) {
|
||||
char *username = conf_get_str(conf, CONF_proxy_username);
|
||||
int userlen = strlen(username);
|
||||
ENSURE(userlen);
|
||||
memcpy(ret+retlen, username, userlen);
|
||||
retlen += userlen;
|
||||
const char *username = conf_get_str(conf, CONF_proxy_username);
|
||||
put_data(buf, username, strlen(username));
|
||||
eo += 4;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "pass", 4) == 0) {
|
||||
char *password = conf_get_str(conf, CONF_proxy_password);
|
||||
int passlen = strlen(password);
|
||||
ENSURE(passlen);
|
||||
memcpy(ret+retlen, password, passlen);
|
||||
retlen += passlen;
|
||||
const char *password = conf_get_str(conf, CONF_proxy_password);
|
||||
put_data(buf, password, strlen(password));
|
||||
eo += 4;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "proxyhost", 9) == 0) {
|
||||
char *host = conf_get_str(conf, CONF_proxy_host);
|
||||
int phlen = strlen(host);
|
||||
ENSURE(phlen);
|
||||
memcpy(ret+retlen, host, phlen);
|
||||
retlen += phlen;
|
||||
const char *host = conf_get_str(conf, CONF_proxy_host);
|
||||
put_data(buf, host, strlen(host));
|
||||
eo += 9;
|
||||
}
|
||||
else if (strnicmp(fmt + eo, "proxyport", 9) == 0) {
|
||||
int port = conf_get_int(conf, CONF_proxy_port);
|
||||
char pport[50];
|
||||
int pplen;
|
||||
sprintf(pport, "%d", port);
|
||||
pplen = strlen(pport);
|
||||
ENSURE(pplen);
|
||||
memcpy(ret+retlen, pport, pplen);
|
||||
retlen += pplen;
|
||||
strbuf_catf(buf, "%d", port);
|
||||
eo += 9;
|
||||
}
|
||||
else {
|
||||
@ -1468,8 +1425,7 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
* don't advance eo, so that we'll consider the
|
||||
* text immediately following the % as unescaped.
|
||||
*/
|
||||
ENSURE(1);
|
||||
ret[retlen++] = '%';
|
||||
put_byte(buf, '%');
|
||||
}
|
||||
}
|
||||
|
||||
@ -1479,16 +1435,10 @@ char *format_telnet_command(SockAddr *addr, int port, Conf *conf)
|
||||
|
||||
/* if there is any unescaped text at the end of the line, send it */
|
||||
if (eo != so) {
|
||||
ENSURE(eo - so);
|
||||
memcpy(ret + retlen, fmt + so, eo - so);
|
||||
retlen += eo - so;
|
||||
put_data(buf, fmt + so, eo - so);
|
||||
}
|
||||
|
||||
ENSURE(1);
|
||||
ret[retlen] = '\0';
|
||||
return ret;
|
||||
|
||||
#undef ENSURE
|
||||
return strbuf_to_str(buf);
|
||||
}
|
||||
|
||||
int proxy_telnet_negotiate (ProxySocket *p, int change)
|
||||
|
@ -3763,7 +3763,7 @@ struct eventlog_stuff {
|
||||
char **events_initial;
|
||||
char **events_circular;
|
||||
int ninitial, ncircular, circular_first;
|
||||
char *seldata;
|
||||
strbuf *seldata;
|
||||
int sellen;
|
||||
bool ignore_selchange;
|
||||
};
|
||||
@ -3773,8 +3773,6 @@ static void eventlog_destroy(GtkWidget *widget, gpointer data)
|
||||
eventlog_stuff *es = (eventlog_stuff *)data;
|
||||
|
||||
es->window = NULL;
|
||||
sfree(es->seldata);
|
||||
es->seldata = NULL;
|
||||
dlg_cleanup(&es->dp);
|
||||
ctrl_free_box(es->eventbox);
|
||||
}
|
||||
@ -3803,7 +3801,6 @@ static void eventlog_list_handler(union control *ctrl, dlgparam *dp,
|
||||
dlg_update_done(ctrl, dp);
|
||||
} else if (event == EVENT_SELCHANGE) {
|
||||
int i;
|
||||
int selsize = 0;
|
||||
|
||||
/*
|
||||
* If this SELCHANGE event is happening as a result of
|
||||
@ -3817,36 +3814,15 @@ static void eventlog_list_handler(union control *ctrl, dlgparam *dp,
|
||||
/*
|
||||
* Construct the data to use as the selection.
|
||||
*/
|
||||
sfree(es->seldata);
|
||||
es->seldata = NULL;
|
||||
es->sellen = 0;
|
||||
es->seldata->len = 0;
|
||||
for (i = 0; i < es->ninitial; i++) {
|
||||
if (dlg_listbox_issel(ctrl, dp, i)) {
|
||||
int extralen = strlen(es->events_initial[i]);
|
||||
|
||||
if (es->sellen + extralen + 2 > selsize) {
|
||||
selsize = es->sellen + extralen + 512;
|
||||
es->seldata = sresize(es->seldata, selsize, char);
|
||||
}
|
||||
|
||||
strcpy(es->seldata + es->sellen, es->events_initial[i]);
|
||||
es->sellen += extralen;
|
||||
es->seldata[es->sellen++] = '\n';
|
||||
}
|
||||
if (dlg_listbox_issel(ctrl, dp, i))
|
||||
strbuf_catf(es->seldata, "%s\n", es->events_initial[i]);
|
||||
}
|
||||
for (i = 0; i < es->ncircular; i++) {
|
||||
if (dlg_listbox_issel(ctrl, dp, es->ninitial + i)) {
|
||||
int j = (es->circular_first + i) % LOGEVENT_CIRCULAR_MAX;
|
||||
int extralen = strlen(es->events_circular[j]);
|
||||
|
||||
if (es->sellen + extralen + 2 > selsize) {
|
||||
selsize = es->sellen + extralen + 512;
|
||||
es->seldata = sresize(es->seldata, selsize, char);
|
||||
}
|
||||
|
||||
strcpy(es->seldata + es->sellen, es->events_circular[j]);
|
||||
es->sellen += extralen;
|
||||
es->seldata[es->sellen++] = '\n';
|
||||
strbuf_catf(es->seldata, "%s\n", es->events_circular[j]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3867,7 +3843,7 @@ void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
|
||||
eventlog_stuff *es = (eventlog_stuff *)data;
|
||||
|
||||
gtk_selection_data_set(seldata, gtk_selection_data_get_target(seldata), 8,
|
||||
(unsigned char *)es->seldata, es->sellen);
|
||||
es->seldata->u, es->seldata->len);
|
||||
}
|
||||
|
||||
gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
|
||||
@ -3891,9 +3867,6 @@ gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
|
||||
#endif
|
||||
es->ignore_selchange = false;
|
||||
|
||||
sfree(es->seldata);
|
||||
es->sellen = 0;
|
||||
es->seldata = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -3983,6 +3956,7 @@ eventlog_stuff *eventlogstuff_new(void)
|
||||
{
|
||||
eventlog_stuff *es = snew(eventlog_stuff);
|
||||
memset(es, 0, sizeof(*es));
|
||||
es->seldata = strbuf_new();
|
||||
return es;
|
||||
}
|
||||
|
||||
@ -4000,6 +3974,7 @@ void eventlogstuff_free(eventlog_stuff *es)
|
||||
sfree(es->events_circular[i]);
|
||||
sfree(es->events_circular);
|
||||
}
|
||||
strbuf_free(es->seldata);
|
||||
|
||||
sfree(es);
|
||||
}
|
||||
|
@ -1882,8 +1882,7 @@ static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
|
||||
* If so, go through them one by one.
|
||||
*/
|
||||
for (k = 0; k < nsizes; k++) {
|
||||
char *fullname;
|
||||
char stylekey[128];
|
||||
char *fullname, *stylekey;
|
||||
|
||||
pango_font_description_set_size(desc, sizes[k]);
|
||||
|
||||
@ -1893,27 +1892,27 @@ static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
|
||||
* Construct the sorting key for font styles.
|
||||
*/
|
||||
{
|
||||
char *p = stylekey;
|
||||
int n;
|
||||
strbuf *buf = strbuf_new();
|
||||
|
||||
n = pango_font_description_get_weight(desc);
|
||||
int weight = pango_font_description_get_weight(desc);
|
||||
/* Weight: normal, then lighter, then bolder */
|
||||
if (n <= PANGO_WEIGHT_NORMAL)
|
||||
n = PANGO_WEIGHT_NORMAL - n;
|
||||
p += sprintf(p, "%4d", n);
|
||||
if (weight <= PANGO_WEIGHT_NORMAL)
|
||||
weight = PANGO_WEIGHT_NORMAL - weight;
|
||||
strbuf_catf(buf, "%4d", weight);
|
||||
|
||||
n = pango_font_description_get_style(desc);
|
||||
p += sprintf(p, " %2d", n);
|
||||
strbuf_catf(buf, " %2d",
|
||||
pango_font_description_get_style(desc));
|
||||
|
||||
n = pango_font_description_get_stretch(desc);
|
||||
int stretch = pango_font_description_get_stretch(desc);
|
||||
/* Stretch: closer to normal sorts earlier */
|
||||
n = 2 * abs(PANGO_STRETCH_NORMAL - n) +
|
||||
(n < PANGO_STRETCH_NORMAL);
|
||||
p += sprintf(p, " %2d", n);
|
||||
stretch = 2 * abs(PANGO_STRETCH_NORMAL - stretch) +
|
||||
(stretch < PANGO_STRETCH_NORMAL);
|
||||
strbuf_catf(buf, " %2d", stretch);
|
||||
|
||||
n = pango_font_description_get_variant(desc);
|
||||
p += sprintf(p, " %2d", n);
|
||||
|
||||
strbuf_catf(buf, " %2d",
|
||||
pango_font_description_get_variant(desc));
|
||||
|
||||
stylekey = strbuf_to_str(buf);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1926,6 +1925,7 @@ static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
|
||||
(sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
|
||||
flags, &pangofont_vtable);
|
||||
|
||||
sfree(stylekey);
|
||||
g_free(fullname);
|
||||
}
|
||||
if (sizes != &dummysize)
|
||||
|
21
unix/uxnet.c
21
unix/uxnet.c
@ -195,12 +195,11 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
|
||||
struct hostent *h = NULL;
|
||||
int n;
|
||||
#endif
|
||||
char realhost[8192];
|
||||
strbuf *realhost;
|
||||
|
||||
/* Clear the structure and default to IPv4. */
|
||||
memset(ret, 0, sizeof(SockAddr));
|
||||
ret->superfamily = UNRESOLVED;
|
||||
*realhost = '\0';
|
||||
ret->error = NULL;
|
||||
ret->refcount = 1;
|
||||
|
||||
@ -225,11 +224,12 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
|
||||
return ret;
|
||||
}
|
||||
ret->superfamily = IP;
|
||||
*realhost = '\0';
|
||||
|
||||
realhost = strbuf_new();
|
||||
if (ret->ais->ai_canonname != NULL)
|
||||
strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1);
|
||||
strbuf_catf(realhost, "%s", ret->ais->ai_canonname);
|
||||
else
|
||||
strncat(realhost, host, sizeof(realhost) - 1);
|
||||
strbuf_catf(realhost, "%s", host);
|
||||
#else
|
||||
if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
|
||||
/*
|
||||
@ -248,10 +248,12 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
|
||||
h_errno == TRY_AGAIN ?
|
||||
"Temporary name service failure" :
|
||||
"gethostbyname: unknown error");
|
||||
strbuf_free(realhost);
|
||||
return ret;
|
||||
}
|
||||
/* This way we are always sure the h->h_name is valid :) */
|
||||
strncpy(realhost, h->h_name, sizeof(realhost));
|
||||
realhost->len = 0;
|
||||
strbuf_catf(realhost, "%s", h->h_name);
|
||||
for (n = 0; h->h_addr_list[n]; n++);
|
||||
ret->addresses = snewn(n, unsigned long);
|
||||
ret->naddresses = n;
|
||||
@ -265,15 +267,14 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
|
||||
* success return from inet_addr.
|
||||
*/
|
||||
ret->superfamily = IP;
|
||||
strncpy(realhost, host, sizeof(realhost));
|
||||
realhost->len = 0;
|
||||
strbuf_catf(realhost, "%s", host);
|
||||
ret->addresses = snew(unsigned long);
|
||||
ret->naddresses = 1;
|
||||
ret->addresses[0] = ntohl(a);
|
||||
}
|
||||
#endif
|
||||
realhost[lenof(realhost)-1] = '\0';
|
||||
*canonicalname = snewn(1+strlen(realhost), char);
|
||||
strcpy(*canonicalname, realhost);
|
||||
*canonicalname = strbuf_to_str(realhost);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user