1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Rewrite some manual char-buffer-handling code.

In the course of recent refactorings I noticed a couple of cases where
we were doing old-fashioned preallocation of a char array with some
conservative maximum size, then writing into it via *p++ or similar
and hoping we got the calculation right.

Now we have strbuf and dupcat, so we shouldn't ever have to do that.
Fixed as many cases as I could find by searching for allocations of
the form 'snewn(foo, char)'.

Particularly worth a mention was the Windows GSSAPI setup code, which
was directly using the Win32 Registry API, and looks much more legible
using the windows/utils/registry.c wrappers. (But that was why I had
to enhance them in the previous commit so as to be able to open
registry keys read-only: without that, the open operation would
actually fail on this key, which is not user-writable.)

Also unix/askpass.c, which was doing a careful reallocation of its
buffer to avoid secrets being left behind in the vacated memory -
which is now just a matter of ensuring we called strbuf_new_nm().
This commit is contained in:
Simon Tatham 2022-09-13 15:00:26 +01:00
parent 7339e00f4a
commit 6cf6682c54
13 changed files with 174 additions and 291 deletions

View File

@ -358,28 +358,26 @@ static void proc_rec_opt(Telnet *telnet, int cmd, int option)
static void process_subneg(Telnet *telnet) static void process_subneg(Telnet *telnet)
{ {
unsigned char *b, *p, *q; unsigned char *p, *q;
int var, value, n, bsize; int var, value;
char *e, *eval, *ekey, *user;
switch (telnet->sb_opt) { switch (telnet->sb_opt) {
case TELOPT_TSPEED: case TELOPT_TSPEED:
if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) { if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) {
char *termspeed = conf_get_str(telnet->conf, CONF_termspeed); char *termspeed = conf_get_str(telnet->conf, CONF_termspeed);
b = snewn(20 + strlen(termspeed), unsigned char); strbuf *sb = strbuf_new();
b[0] = IAC; put_byte(sb, IAC);
b[1] = SB; put_byte(sb, SB);
b[2] = TELOPT_TSPEED; put_byte(sb, TELOPT_TSPEED);
b[3] = TELQUAL_IS; put_byte(sb, TELQUAL_IS);
strcpy((char *)(b + 4), termspeed); put_datapl(sb, ptrlen_from_asciz(termspeed));
n = 4 + strlen(termspeed); put_byte(sb, IAC);
b[n] = IAC; put_byte(sb, SE);
b[n + 1] = SE; telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
telnet->bufsize = sk_write(telnet->s, b, n + 2);
logevent(telnet->logctx, "server subnegotiation: SB TSPEED SEND"); logevent(telnet->logctx, "server subnegotiation: SB TSPEED SEND");
logeventf(telnet->logctx, logeventf(telnet->logctx,
"client subnegotiation: SB TSPEED IS %s", termspeed); "client subnegotiation: SB TSPEED IS %s", termspeed);
sfree(b); strbuf_free(sb);
} else } else
logevent(telnet->logctx, logevent(telnet->logctx,
"server subnegotiation: SB TSPEED <something weird>"); "server subnegotiation: SB TSPEED <something weird>");
@ -387,24 +385,24 @@ static void process_subneg(Telnet *telnet)
case TELOPT_TTYPE: case TELOPT_TTYPE:
if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) { if (telnet->sb_buf->len == 1 && telnet->sb_buf->u[0] == TELQUAL_SEND) {
char *termtype = conf_get_str(telnet->conf, CONF_termtype); char *termtype = conf_get_str(telnet->conf, CONF_termtype);
b = snewn(20 + strlen(termtype), unsigned char); strbuf *sb = strbuf_new();
b[0] = IAC; put_byte(sb, IAC);
b[1] = SB; put_byte(sb, SB);
b[2] = TELOPT_TTYPE; put_byte(sb, TELOPT_TTYPE);
b[3] = TELQUAL_IS; put_byte(sb, TELQUAL_IS);
for (n = 0; termtype[n]; n++) size_t tt_start = sb->len;
b[n + 4] = (termtype[n] >= 'a' && termtype[n] <= 'z' ? for (size_t n = 0; termtype[n]; n++)
termtype[n] + 'A' - 'a' : put_byte(sb, (termtype[n] >= 'a' && termtype[n] <= 'z' ?
termtype[n]); termtype[n] + 'A' - 'a' : termtype[n]));
b[n + 4] = IAC; size_t tt_end = sb->len;
b[n + 5] = SE; put_byte(sb, IAC);
telnet->bufsize = sk_write(telnet->s, b, n + 6); put_byte(sb, SE);
b[n + 4] = 0; telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
logevent(telnet->logctx, strbuf_shrink_to(sb, tt_end);
"server subnegotiation: SB TTYPE SEND"); logevent(telnet->logctx, "server subnegotiation: SB TTYPE SEND");
logeventf(telnet->logctx, logeventf(telnet->logctx, "client subnegotiation: SB TTYPE IS %s",
"client subnegotiation: SB TTYPE IS %s", b + 4); sb->s + tt_start);
sfree(b); strbuf_free(sb);
} else } else
logevent(telnet->logctx, logevent(telnet->logctx,
"server subnegotiation: SB TTYPE <something weird>\r\n"); "server subnegotiation: SB TTYPE <something weird>\r\n");
@ -446,49 +444,34 @@ static void process_subneg(Telnet *telnet)
value = RFC_VALUE; value = RFC_VALUE;
var = RFC_VAR; var = RFC_VAR;
} }
bsize = 20;
for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
NULL, &ekey);
eval != NULL;
eval = conf_get_str_strs(telnet->conf, CONF_environmt,
ekey, &ekey))
bsize += strlen(ekey) + strlen(eval) + 2;
user = get_remote_username(telnet->conf);
if (user)
bsize += 6 + strlen(user);
b = snewn(bsize, unsigned char); strbuf *sb = strbuf_new();
b[0] = IAC; put_byte(sb, IAC);
b[1] = SB; put_byte(sb, SB);
b[2] = telnet->sb_opt; put_byte(sb, telnet->sb_opt);
b[3] = TELQUAL_IS; put_byte(sb, TELQUAL_IS);
n = 4; char *ekey, *eval;
for (eval = conf_get_str_strs(telnet->conf, CONF_environmt, for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
NULL, &ekey); NULL, &ekey);
eval != NULL; eval != NULL;
eval = conf_get_str_strs(telnet->conf, CONF_environmt, eval = conf_get_str_strs(telnet->conf, CONF_environmt,
ekey, &ekey)) { ekey, &ekey)) {
b[n++] = var; put_byte(sb, var);
for (e = ekey; *e; e++) put_datapl(sb, ptrlen_from_asciz(ekey));
b[n++] = *e; put_byte(sb, value);
b[n++] = value; put_datapl(sb, ptrlen_from_asciz(eval));
for (e = eval; *e; e++)
b[n++] = *e;
} }
char *user = get_remote_username(telnet->conf);
if (user) { if (user) {
b[n++] = var; put_byte(sb, var);
b[n++] = 'U'; put_datalit(sb, "USER");
b[n++] = 'S'; put_byte(sb, value);
b[n++] = 'E'; put_datapl(sb, ptrlen_from_asciz(user));
b[n++] = 'R';
b[n++] = value;
for (e = user; *e; e++)
b[n++] = *e;
} }
b[n++] = IAC; put_byte(sb, IAC);
b[n++] = SE; put_byte(sb, SE);
telnet->bufsize = sk_write(telnet->s, b, n); telnet->bufsize = sk_write(telnet->s, sb->s, sb->len);
if (n == 6) { if (sb->len == 6) {
logeventf(telnet->logctx, logeventf(telnet->logctx,
"client subnegotiation: SB %s IS <nothing>", "client subnegotiation: SB %s IS <nothing>",
telopt(telnet->sb_opt)); telopt(telnet->sb_opt));
@ -505,7 +488,7 @@ static void process_subneg(Telnet *telnet)
if (user) if (user)
logeventf(telnet->logctx, " USER=%s", user); logeventf(telnet->logctx, " USER=%s", user);
} }
sfree(b); strbuf_free(sb);
sfree(user); sfree(user);
} }
break; break;

25
pscp.c
View File

@ -2120,7 +2120,6 @@ static void get_dir_list(int argc, char *argv[])
{ {
char *wsrc, *host, *user; char *wsrc, *host, *user;
const char *src; const char *src;
char *cmd, *p;
const char *q; const char *q;
char c; char c;
@ -2150,24 +2149,18 @@ static void get_dir_list(int argc, char *argv[])
user = NULL; user = NULL;
} }
cmd = snewn(4 * strlen(src) + 100, char); strbuf *cmd = strbuf_new();
strcpy(cmd, "ls -la '"); put_datalit(cmd, "ls -la '");
p = cmd + strlen(cmd);
for (q = src; *q; q++) { for (q = src; *q; q++) {
if (*q == '\'') { if (*q == '\'')
*p++ = '\''; put_datalit(cmd, "'\\''");
*p++ = '\\'; else
*p++ = '\''; put_byte(cmd, *q);
*p++ = '\'';
} else {
*p++ = *q;
}
} }
*p++ = '\''; put_datalit(cmd, "'");
*p = '\0';
do_cmd(host, user, cmd); do_cmd(host, user, cmd->s);
sfree(cmd); strbuf_free(cmd);
if (using_sftp) { if (using_sftp) {
scp_sftp_listdir(src); scp_sftp_listdir(src);

View File

@ -267,19 +267,10 @@ static bool gppmap(settings_r *sesskey, const char *name,
static void wmap(settings_w *sesskey, char const *outkey, Conf *conf, static void wmap(settings_w *sesskey, char const *outkey, Conf *conf,
int primary, bool include_values) int primary, bool include_values)
{ {
char *buf, *p, *key, *realkey; char *key, *realkey;
const char *val, *q; const char *val, *q;
int len;
len = 1; /* allow for NUL */ strbuf *sb = strbuf_new();
for (val = conf_get_str_strs(conf, primary, NULL, &key);
val != NULL;
val = conf_get_str_strs(conf, primary, key, &key))
len += 2 + 2 * (strlen(key) + strlen(val)); /* allow for escaping */
buf = snewn(len, char);
p = buf;
for (val = conf_get_str_strs(conf, primary, NULL, &key); for (val = conf_get_str_strs(conf, primary, NULL, &key);
val != NULL; val != NULL;
@ -304,19 +295,19 @@ static void wmap(settings_w *sesskey, char const *outkey, Conf *conf,
realkey = NULL; realkey = NULL;
} }
if (p != buf) if (sb->len)
*p++ = ','; put_byte(sb, ',');
for (q = key; *q; q++) { for (q = key; *q; q++) {
if (*q == '=' || *q == ',' || *q == '\\') if (*q == '=' || *q == ',' || *q == '\\')
*p++ = '\\'; put_byte(sb, '\\');
*p++ = *q; put_byte(sb, *q);
} }
if (include_values) { if (include_values) {
*p++ = '='; put_byte(sb, '=');
for (q = val; *q; q++) { for (q = val; *q; q++) {
if (*q == '=' || *q == ',' || *q == '\\') if (*q == '=' || *q == ',' || *q == '\\')
*p++ = '\\'; put_byte(sb, '\\');
*p++ = *q; put_byte(sb, *q);
} }
} }
@ -325,9 +316,8 @@ static void wmap(settings_w *sesskey, char const *outkey, Conf *conf,
key = realkey; key = realkey;
} }
} }
*p = '\0'; write_setting_s(sesskey, outkey, sb->s);
write_setting_s(sesskey, outkey, buf); strbuf_free(sb);
sfree(buf);
} }
static int key2val(const struct keyvalwhere *mapping, static int key2val(const struct keyvalwhere *mapping,
@ -456,34 +446,18 @@ static void wprefs(settings_w *sesskey, const char *name,
const struct keyvalwhere *mapping, int nvals, const struct keyvalwhere *mapping, int nvals,
Conf *conf, int primary) Conf *conf, int primary)
{ {
char *buf, *p; strbuf *sb = strbuf_new();
int i, maxlen;
for (maxlen = i = 0; i < nvals; i++) { for (int i = 0; i < nvals; i++) {
const char *s = val2key(mapping, nvals, const char *s = val2key(mapping, nvals,
conf_get_int_int(conf, primary, i)); conf_get_int_int(conf, primary, i));
if (s) { if (s)
maxlen += (maxlen > 0 ? 1 : 0) + strlen(s); put_fmt(sb, "%s%s", (sb->len ? "," : ""), s);
}
} }
buf = snewn(maxlen + 1, char); write_setting_s(sesskey, name, sb->s);
p = buf;
for (i = 0; i < nvals; i++) { strbuf_free(sb);
const char *s = val2key(mapping, nvals,
conf_get_int_int(conf, primary, i));
if (s) {
p += sprintf(p, "%s%s", (p > buf ? "," : ""), s);
}
}
assert(p - buf == maxlen);
*p = '\0';
write_setting_s(sesskey, name, buf);
sfree(buf);
} }
static void write_setting_b(settings_w *handle, const char *key, bool value) static void write_setting_b(settings_w *handle, const char *key, bool value)

View File

@ -799,20 +799,17 @@ static void scp_source_process_stack(ScpSource *scp)
scp->head = node; /* put back the unfinished READDIR */ scp->head = node; /* put back the unfinished READDIR */
node = NULL; /* and prevent it being freed */ node = NULL; /* and prevent it being freed */
} else { } else {
ptrlen subpath; strbuf *subpath = strbuf_new();
subpath.len = node->pathname.len + 1 + scp->reply.name.len; put_datapl(subpath, node->pathname);
char *subpath_space = snewn(subpath.len, char); put_byte(subpath, '/');
subpath.ptr = subpath_space; put_datapl(subpath, scp->reply.name);
memcpy(subpath_space, node->pathname.ptr, node->pathname.len);
subpath_space[node->pathname.len] = '/';
memcpy(subpath_space + node->pathname.len + 1,
scp->reply.name.ptr, scp->reply.name.len);
scp->head = node; /* put back the unfinished READDIR */ scp->head = node; /* put back the unfinished READDIR */
node = NULL; /* and prevent it being freed */ node = NULL; /* and prevent it being freed */
scp_source_push_name(scp, subpath, scp->reply.attrs, NULL); scp_source_push_name(scp, ptrlen_from_strbuf(subpath),
scp->reply.attrs, NULL);
sfree(subpath_space); strbuf_free(subpath);
} }
} else if (node->attrs.permissions & PERMS_DIRECTORY) { } else if (node->attrs.permissions & PERMS_DIRECTORY) {
assert(scp->recursive || node->wildcard); assert(scp->recursive || node->wildcard);

View File

@ -1588,19 +1588,17 @@ static void term_copy_stuff_from_conf(Terminal *term)
*/ */
{ {
char *answerback = conf_get_str(term->conf, CONF_answerback); char *answerback = conf_get_str(term->conf, CONF_answerback);
int maxlen = strlen(answerback);
term->answerback = snewn(maxlen, char); strbuf_clear(term->answerback);
term->answerbacklen = 0;
while (*answerback) { while (*answerback) {
char *n; char *n;
char c = ctrlparse(answerback, &n); char c = ctrlparse(answerback, &n);
if (n) { if (n) {
term->answerback[term->answerbacklen++] = c; put_byte(term->answerback, c);
answerback = n; answerback = n;
} else { } else {
term->answerback[term->answerbacklen++] = *answerback++; put_byte(term->answerback, *answerback++);
} }
} }
} }
@ -1980,6 +1978,7 @@ Terminal *term_init(Conf *myconf, struct unicode_data *ucsdata, TermWin *win)
term->termstate = TOPLEVEL; term->termstate = TOPLEVEL;
term->selstate = NO_SELECTION; term->selstate = NO_SELECTION;
term->curstype = 0; term->curstype = 0;
term->answerback = strbuf_new();
term_copy_stuff_from_conf(term); term_copy_stuff_from_conf(term);
@ -2095,7 +2094,7 @@ void term_free(Terminal *term)
sfree(term->ltemp); sfree(term->ltemp);
sfree(term->wcFrom); sfree(term->wcFrom);
sfree(term->wcTo); sfree(term->wcTo);
sfree(term->answerback); strbuf_free(term->answerback);
for (i = 0; i < term->bidi_cache_size; i++) { for (i = 0; i < term->bidi_cache_size; i++) {
sfree(term->pre_bidi_cache[i].chars); sfree(term->pre_bidi_cache[i].chars);
@ -3807,7 +3806,7 @@ static void term_out(Terminal *term, bool called_from_term_data)
if (term->ldisc) { if (term->ldisc) {
strbuf *buf = term_input_data_from_charset( strbuf *buf = term_input_data_from_charset(
term, DEFAULT_CODEPAGE, term, DEFAULT_CODEPAGE,
term->answerback, term->answerbacklen); term->answerback->s, term->answerback->len);
ldisc_send(term->ldisc, buf->s, buf->len, false); ldisc_send(term->ldisc, buf->s, buf->len, false);
strbuf_free(buf); strbuf_free(buf);
} }

View File

@ -300,8 +300,7 @@ struct terminal_tag {
* the former every time. * the former every time.
*/ */
bool ansi_colour; bool ansi_colour;
char *answerback; strbuf *answerback;
int answerbacklen;
bool no_arabicshaping; bool no_arabicshaping;
int beep; int beep;
bool bellovl; bool bellovl;

View File

@ -46,8 +46,7 @@ struct askpass_ctx {
GdkColor cols[3]; GdkColor cols[3];
#endif #endif
char *error_message; /* if we finish without a passphrase */ char *error_message; /* if we finish without a passphrase */
char *passphrase; /* if we finish with one */ strbuf *passphrase; /* if we finish with one */
int passlen, passsize;
#if GTK_CHECK_VERSION(3,20,0) #if GTK_CHECK_VERSION(3,20,0)
GdkSeat *seat; /* for gdk_seat_grab */ GdkSeat *seat; /* for gdk_seat_grab */
#elif GTK_CHECK_VERSION(3,0,0) #elif GTK_CHECK_VERSION(3,0,0)
@ -107,48 +106,30 @@ static void visually_acknowledge_keypress(struct askpass_ctx *ctx)
ctx->active_area = new_active; ctx->active_area = new_active;
} }
static int last_char_len(struct askpass_ctx *ctx) static size_t last_char_start(struct askpass_ctx *ctx)
{ {
/* /*
* GTK always encodes in UTF-8, so we can do this in a fixed way. * GTK always encodes in UTF-8, so we can do this in a fixed way.
*/ */
int i; assert(ctx->passphrase->len > 0);
assert(ctx->passlen > 0); size_t i = ctx->passphrase->len - 1;
i = ctx->passlen - 1; while ((unsigned)(ctx->passphrase->u[i] - 0x80) < 0x40) {
while ((unsigned)((unsigned char)ctx->passphrase[i] - 0x80) < 0x40) {
if (i == 0) if (i == 0)
break; break;
i--; i--;
} }
return ctx->passlen - i; return i;
} }
static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str) static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str)
{ {
int len = strlen(str); put_datapl(ctx->passphrase, ptrlen_from_asciz(str));
if (ctx->passlen + len >= ctx->passsize) {
/* Take some care with buffer expansion, because there are
* pieces of passphrase in the old buffer so we should ensure
* realloc doesn't leave a copy lying around in the address
* space. */
int oldsize = ctx->passsize;
char *newbuf;
ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
newbuf = snewn(ctx->passsize, char);
memcpy(newbuf, ctx->passphrase, oldsize);
smemclr(ctx->passphrase, oldsize);
sfree(ctx->passphrase);
ctx->passphrase = newbuf;
}
strcpy(ctx->passphrase + ctx->passlen, str);
ctx->passlen += len;
visually_acknowledge_keypress(ctx); visually_acknowledge_keypress(ctx);
} }
static void cancel_askpass(struct askpass_ctx *ctx, const char *msg) static void cancel_askpass(struct askpass_ctx *ctx, const char *msg)
{ {
smemclr(ctx->passphrase, ctx->passsize); strbuf_free(ctx->passphrase);
ctx->passphrase = NULL; ctx->passphrase = NULL;
ctx->error_message = dupstr(msg); ctx->error_message = dupstr(msg);
gtk_main_quit(); gtk_main_quit();
@ -182,7 +163,7 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
if (event->type == GDK_KEY_PRESS) { if (event->type == GDK_KEY_PRESS) {
if (!strcmp(event->string, "\x15")) { if (!strcmp(event->string, "\x15")) {
/* Ctrl-U. Wipe out the whole line */ /* Ctrl-U. Wipe out the whole line */
ctx->passlen = 0; strbuf_clear(ctx->passphrase);
visually_acknowledge_keypress(ctx); visually_acknowledge_keypress(ctx);
} else if (!strcmp(event->string, "\x17")) { } else if (!strcmp(event->string, "\x17")) {
/* Ctrl-W. Delete back to the last space->nonspace /* Ctrl-W. Delete back to the last space->nonspace
@ -190,20 +171,21 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
* way (mimicking terminal drivers), and don't attempt * way (mimicking terminal drivers), and don't attempt
* to second-guess exciting Unicode space * to second-guess exciting Unicode space
* characters. */ * characters. */
while (ctx->passlen > 0) { while (ctx->passphrase->len > 0) {
char deleted, prior; char deleted, prior;
ctx->passlen -= last_char_len(ctx); size_t newlen = last_char_start(ctx);
deleted = ctx->passphrase[ctx->passlen]; deleted = ctx->passphrase->s[newlen];
prior = (ctx->passlen == 0 ? ' ' : strbuf_shrink_to(ctx->passphrase, newlen);
ctx->passphrase[ctx->passlen-1]); prior = (ctx->passphrase->len == 0 ? ' ' :
ctx->passphrase->s[ctx->passphrase->len-1]);
if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior)) if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior))
break; break;
} }
visually_acknowledge_keypress(ctx); visually_acknowledge_keypress(ctx);
} else if (event->keyval == GDK_KEY_BackSpace) { } else if (event->keyval == GDK_KEY_BackSpace) {
/* Backspace. Delete one character. */ /* Backspace. Delete one character. */
if (ctx->passlen > 0) if (ctx->passphrase->len > 0)
ctx->passlen -= last_char_len(ctx); strbuf_shrink_to(ctx->passphrase, last_char_start(ctx));
visually_acknowledge_keypress(ctx); visually_acknowledge_keypress(ctx);
#if !GTK_CHECK_VERSION(2,0,0) #if !GTK_CHECK_VERSION(2,0,0)
} else if (event->string[0]) { } else if (event->string[0]) {
@ -427,9 +409,7 @@ static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
int i; int i;
GtkBox *action_area; GtkBox *action_area;
ctx->passlen = 0; ctx->passphrase = strbuf_new_nm();
ctx->passsize = 2048;
ctx->passphrase = snewn(ctx->passsize, char);
/* /*
* Create widgets. * Create widgets.
@ -553,11 +533,6 @@ static void gtk_askpass_cleanup(struct askpass_ctx *ctx)
#endif #endif
gtk_grab_remove(ctx->promptlabel); gtk_grab_remove(ctx->promptlabel);
if (ctx->passphrase) {
assert(ctx->passlen < ctx->passsize);
ctx->passphrase[ctx->passlen] = '\0';
}
gtk_widget_destroy(ctx->dialog); gtk_widget_destroy(ctx->dialog);
} }
@ -612,7 +587,7 @@ char *gtk_askpass_main(const char *display, const char *wintitle,
if (ctx->passphrase) { if (ctx->passphrase) {
*success = true; *success = true;
return ctx->passphrase; return strbuf_to_str(ctx->passphrase);
} else { } else {
*success = false; *success = false;
return ctx->error_message; return ctx->error_message;

View File

@ -594,9 +594,7 @@ void dlg_listbox_addwithid(dlgcontrol *ctrl, dlgparam *dp,
cols = cols ? cols : 1; cols = cols ? cols : 1;
for (i = 0; i < cols; i++) { for (i = 0; i < cols; i++) {
int collen = strcspn(text, "\t"); int collen = strcspn(text, "\t");
char *tmpstr = snewn(collen+1, char); char *tmpstr = mkstr(make_ptrlen(text, collen));
memcpy(tmpstr, text, collen);
tmpstr[collen] = '\0';
gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1); gtk_list_store_set(uc->listmodel, &iter, i+1, tmpstr, -1);
sfree(tmpstr); sfree(tmpstr);
text += collen; text += collen;

View File

@ -1228,9 +1228,8 @@ Backend *pty_backend_create(
char *shellname; char *shellname;
if (conf_get_bool(conf, CONF_login_shell)) { if (conf_get_bool(conf, CONF_login_shell)) {
const char *p = strrchr(shell, '/'); const char *p = strrchr(shell, '/');
shellname = snewn(2+strlen(shell), char);
p = p ? p+1 : shell; p = p ? p+1 : shell;
sprintf(shellname, "-%s", p); shellname = dupprintf("-%s", p);
} else } else
shellname = (char *)shell; shellname = (char *)shell;
execl(shell, shellname, (void *)NULL); execl(shell, shellname, (void *)NULL);

View File

@ -321,7 +321,6 @@ static int keycmp(void *av, void *bv)
void provide_xrm_string(const char *string, const char *progname) void provide_xrm_string(const char *string, const char *progname)
{ {
const char *p, *q; const char *p, *q;
char *key;
struct skeyval *xrms, *ret; struct skeyval *xrms, *ret;
p = q = strchr(string, ':'); p = q = strchr(string, ':');
@ -330,14 +329,13 @@ void provide_xrm_string(const char *string, const char *progname)
" \"%s\"\n", progname, string); " \"%s\"\n", progname, string);
return; return;
} }
q++; xrms = snew(struct skeyval);
while (p > string && p[-1] != '.' && p[-1] != '*') while (p > string && p[-1] != '.' && p[-1] != '*')
p--; p--;
xrms = snew(struct skeyval); xrms->key = mkstr(make_ptrlen(p, q-p));
key = snewn(q-p, char);
memcpy(key, p, q-p); q++;
key[q-p-1] = '\0';
xrms->key = key;
while (*q && isspace((unsigned char)*q)) while (*q && isspace((unsigned char)*q))
q++; q++;
xrms->value = dupstr(q); xrms->value = dupstr(q);

View File

@ -393,13 +393,11 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
INT *pwidths, nfit; INT *pwidths, nfit;
SIZE size; SIZE size;
const char *p; const char *p;
char *ret, *q;
RECT r; RECT r;
HFONT oldfont, newfont; HFONT oldfont, newfont;
ret = snewn(1+strlen(text), char); strbuf *sb = strbuf_new();
p = text; p = text;
q = ret;
pwidths = snewn(1+strlen(text), INT); pwidths = snewn(1+strlen(text), INT);
/* /*
@ -432,7 +430,7 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
* Either way, we stop wrapping, copy the remainder of * Either way, we stop wrapping, copy the remainder of
* the input string unchanged to the output, and leave. * the input string unchanged to the output, and leave.
*/ */
strcpy(q, p); put_datapl(sb, ptrlen_from_asciz(p));
break; break;
} }
@ -449,9 +447,8 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
} }
} }
strncpy(q, p, nfit); put_data(sb, p, nfit);
q[nfit] = '\n'; put_byte(sb, '\n');
q += nfit+1;
p += nfit; p += nfit;
while (*p && isspace((unsigned char)*p)) while (*p && isspace((unsigned char)*p))
@ -467,7 +464,7 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
sfree(pwidths); sfree(pwidths);
return ret; return strbuf_to_str(sb);
} }
/* /*
@ -1181,30 +1178,24 @@ void progressbar(struct ctlpos *cp, int id)
*/ */
static char *shortcut_escape(const char *text, char shortcut) static char *shortcut_escape(const char *text, char shortcut)
{ {
char *ret;
char const *p;
char *q;
if (!text) if (!text)
return NULL; /* sfree won't choke on this */ return NULL; /* sfree won't choke on this */
ret = snewn(2*strlen(text)+1, char); /* size potentially doubles! */ strbuf *sb = strbuf_new();
shortcut = tolower((unsigned char)shortcut); shortcut = tolower((unsigned char)shortcut);
p = text; const char *p = text;
q = ret;
while (*p) { while (*p) {
if (shortcut != NO_SHORTCUT && if (shortcut != NO_SHORTCUT &&
tolower((unsigned char)*p) == shortcut) { tolower((unsigned char)*p) == shortcut) {
*q++ = '&'; put_byte(sb, '&');
shortcut = NO_SHORTCUT; /* stop it happening twice */ shortcut = NO_SHORTCUT; /* stop it happening twice */
} else if (*p == '&') { } else if (*p == '&') {
*q++ = '&'; put_byte(sb, '&');
} }
*q++ = *p++; put_byte(sb, *p++);
} }
*q = '\0'; return strbuf_to_str(sb);
return ret;
} }
void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c) void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)

View File

@ -300,35 +300,21 @@ static INT_PTR CALLBACK LogProc(HWND hwnd, UINT msg,
LB_GETSELITEMS, LB_GETSELITEMS,
selcount, selcount,
(LPARAM) selitems); (LPARAM) selitems);
int i; static const unsigned char sel_nl[] = SEL_NL;
int size;
char *clipdata;
static unsigned char sel_nl[] = SEL_NL;
if (count == 0) { /* can't copy zero stuff */ if (count == 0) { /* can't copy zero stuff */
MessageBeep(0); MessageBeep(0);
break; break;
} }
size = 0; strbuf *sb = strbuf_new();
for (i = 0; i < count; i++) for (int i = 0; i < count; i++) {
size += char *q = getevent(selitems[i]);
strlen(getevent(selitems[i])) + sizeof(sel_nl); put_datapl(sb, ptrlen_from_asciz(q));
put_data(sb, sel_nl, sizeof(sel_nl));
clipdata = snewn(size, char);
if (clipdata) {
char *p = clipdata;
for (i = 0; i < count; i++) {
char *q = getevent(selitems[i]);
int qlen = strlen(q);
memcpy(p, q, qlen);
p += qlen;
memcpy(p, sel_nl, sizeof(sel_nl));
p += sizeof(sel_nl);
}
write_aclip(hwnd, CLIP_SYSTEM, clipdata, size);
sfree(clipdata);
} }
write_aclip(hwnd, CLIP_SYSTEM, sb->s, sb->len);
strbuf_free(sb);
sfree(selitems); sfree(selitems);
for (i = 0; i < (ninitial + ncircular); i++) for (i = 0; i < (ninitial + ncircular); i++)

View File

@ -118,7 +118,6 @@ static void add_library_to_never_unload_tree(HMODULE module)
struct ssh_gss_liblist *ssh_gss_setup(Conf *conf) struct ssh_gss_liblist *ssh_gss_setup(Conf *conf)
{ {
HMODULE module; HMODULE module;
HKEY regkey;
struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist); struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
char *path; char *path;
static HMODULE kernel32_module; static HMODULE kernel32_module;
@ -137,55 +136,47 @@ struct ssh_gss_liblist *ssh_gss_setup(Conf *conf)
/* MIT Kerberos GSSAPI implementation */ /* MIT Kerberos GSSAPI implementation */
module = NULL; module = NULL;
if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MIT\\Kerberos", &regkey) HKEY regkey = open_regkey_ro(HKEY_LOCAL_MACHINE,
== ERROR_SUCCESS) { "SOFTWARE\\MIT\\Kerberos");
DWORD type, size; if (regkey) {
LONG ret; char *installdir = get_reg_sz(regkey, "InstallDir");
char *buffer; if (installdir) {
char *bindir = dupcat(installdir, "\\bin");
/* Find out the string length */ if(p_AddDllDirectory) {
ret = RegQueryValueEx(regkey, "InstallDir", NULL, &type, NULL, &size); /* Add MIT Kerberos' path to the DLL search path,
* it loads its own DLLs further down the road */
if (ret == ERROR_SUCCESS && type == REG_SZ) { wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, bindir);
buffer = snewn(size + 20, char); p_AddDllDirectory(dllPath);
ret = RegQueryValueEx(regkey, "InstallDir", NULL, sfree(dllPath);
&type, (LPBYTE)buffer, &size);
if (ret == ERROR_SUCCESS && type == REG_SZ) {
strcat (buffer, "\\bin");
if(p_AddDllDirectory) {
/* Add MIT Kerberos' path to the DLL search path,
* it loads its own DLLs further down the road */
wchar_t *dllPath =
dup_mb_to_wc(DEFAULT_CODEPAGE, 0, buffer);
p_AddDllDirectory(dllPath);
sfree(dllPath);
}
strcat (buffer, "\\gssapi"MIT_KERB_SUFFIX".dll");
module = LoadLibraryEx (buffer, NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32 |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
LOAD_LIBRARY_SEARCH_USER_DIRS);
/*
* The MIT Kerberos DLL suffers an internal segfault
* for some reason if you unload and reload one within
* the same process. So, make sure that after we load
* this library, we never free it.
*
* Or rather: after we've loaded it once, if any
* _further_ load returns the same module handle, we
* immediately free it again (to prevent the Windows
* API's internal reference count growing without
* bound). But on the other hand we never free it in
* ssh_gss_cleanup.
*/
if (library_is_in_never_unload_tree(module))
FreeLibrary(module);
add_library_to_never_unload_tree(module);
} }
sfree(buffer);
char *dllfile = dupcat(bindir, "\\gssapi"MIT_KERB_SUFFIX".dll");
module = LoadLibraryEx(dllfile, NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32 |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
LOAD_LIBRARY_SEARCH_USER_DIRS);
/*
* The MIT Kerberos DLL suffers an internal segfault for
* some reason if you unload and reload one within the
* same process. So, make sure that after we load this
* library, we never free it.
*
* Or rather: after we've loaded it once, if any _further_
* load returns the same module handle, we immediately
* free it again (to prevent the Windows API's internal
* reference count growing without bound). But on the
* other hand we never free it in ssh_gss_cleanup.
*/
if (library_is_in_never_unload_tree(module))
FreeLibrary(module);
add_library_to_never_unload_tree(module);
sfree(dllfile);
sfree(bindir);
sfree(installdir);
} }
RegCloseKey(regkey); close_regkey(regkey);
} }
if (module) { if (module) {
struct ssh_gss_library *lib = struct ssh_gss_library *lib =