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

Make dupcat() into a variadic macro.

Up until now, it's been a variadic _function_, whose argument list
consists of 'const char *' ASCIZ strings to concatenate, terminated by
one containing a null pointer. Now, that function is dupcat_fn(), and
it's wrapped by a C99 variadic _macro_ called dupcat(), which
automatically suffixes the null-pointer terminating argument.

This has three benefits. Firstly, it's just less effort at every call
site. Secondly, it protects against the risk of accidentally leaving
off the NULL, causing arbitrary words of stack memory to be
dereferenced as char pointers. And thirdly, it protects against the
more subtle risk of writing a bare 'NULL' as the terminating argument,
instead of casting it explicitly to a pointer. That last one is
necessary because C permits the macro NULL to expand to an integer
constant such as 0, so NULL by itself may not have pointer type, and
worse, it may not be marshalled in a variadic argument list in the
same way as a pointer. (For example, on a 64-bit machine it might only
occupy 32 bits. And yet, on another 64-bit platform, it might work
just fine, so that you don't notice the mistake!)

I was inspired to do this by happening to notice one of those bare
NULL terminators, and thinking I'd better check if there were any
more. Turned out there were quite a few. Now there are none.
This commit is contained in:
Simon Tatham
2019-10-14 19:42:37 +01:00
parent 283bd541a6
commit 1547c9c1ec
27 changed files with 73 additions and 85 deletions

View File

@ -3654,7 +3654,7 @@ void old_keyfile_warning(void)
void nonfatal_message_box(void *window, const char *msg)
{
char *title = dupcat(appname, " Error", NULL);
char *title = dupcat(appname, " Error");
create_message_box(
window, title, msg,
string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
@ -3694,7 +3694,7 @@ static void licence_clicked(GtkButton *button, gpointer data)
{
char *title;
title = dupcat(appname, " Licence", NULL);
title = dupcat(appname, " Licence");
assert(aboutbox != NULL);
create_message_box(aboutbox, title, LICENCE_TEXT("\n\n"),
string_width("LONGISH LINE OF TEXT SO THE LICENCE"
@ -3716,7 +3716,7 @@ void about_box(void *window)
aboutbox = our_dialog_new();
gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
title = dupcat("About ", appname, NULL);
title = dupcat("About ", appname);
gtk_window_set_title(GTK_WINDOW(aboutbox), title);
sfree(title);
@ -3932,7 +3932,7 @@ void showeventlog(eventlog_stuff *es, void *parentwin)
c->listbox.percentages[2] = 65;
es->window = window = our_dialog_new();
title = dupcat(appname, " Event Log", (const char *)NULL);
title = dupcat(appname, " Event Log");
gtk_window_set_title(GTK_WINDOW(window), title);
sfree(title);
w0 = layout_ctrls(&es->dp, NULL, &es->scs, s0, GTK_WINDOW(window));
@ -4025,7 +4025,7 @@ void logevent_dlg(eventlog_stuff *es, const char *string)
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
sfree(*location);
*location = dupcat(timebuf, string, NULL);
*location = dupcat(timebuf, string);
if (es->window) {
dlg_listbox_add(es->listctrl, &es->dp, *location);
}

View File

@ -461,7 +461,7 @@ static unifont *x11font_create(GtkWidget *widget, const char *name,
reg = XGetAtomName(disp, (Atom)registry_ret);
enc = XGetAtomName(disp, (Atom)encoding_ret);
if (reg && enc) {
char *encoding = dupcat(reg, "-", enc, NULL);
char *encoding = dupcat(reg, "-", enc);
pubcs = realcs = charset_from_xenc(encoding);
/*
@ -1280,8 +1280,7 @@ static char *x11font_size_increment(unifont *font, int increment)
if (xlfd_best) {
char *bare_returned_name = xlfd_recompose(xlfd_best);
returned_name = dupcat(
xfont->u.vt->prefix, ":", bare_returned_name,
(const char *)NULL);
xfont->u.vt->prefix, ":", bare_returned_name);
sfree(bare_returned_name);
}
@ -2042,8 +2041,7 @@ static char *pangofont_size_increment(unifont *font, int increment)
} else {
pango_font_description_set_size(desc, size);
newname = pango_font_description_to_string(desc);
retname = dupcat(pfont->u.vt->prefix, ":",
newname, (const char *)NULL);
retname = dupcat(pfont->u.vt->prefix, ":", newname);
g_free(newname);
}
@ -3780,15 +3778,14 @@ char *unifontsel_get_name(unifontsel *fontsel)
name = fs->selected->fontclass->scale_fontname
(GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
if (name) {
char *ret = dupcat(fs->selected->fontclass->prefix, ":",
name, NULL);
char *ret = dupcat(fs->selected->fontclass->prefix, ":", name);
sfree(name);
return ret;
}
}
return dupcat(fs->selected->fontclass->prefix, ":",
fs->selected->realname, NULL);
fs->selected->realname);
}
#endif /* GTK_CHECK_VERSION(2,0,0) */

View File

@ -235,7 +235,7 @@ static void post_fatal_message_box(void *vctx, int result)
static void common_connfatal_message_box(
GtkFrontend *inst, const char *msg, post_dialog_fn_t postfn)
{
char *title = dupcat(appname, " Fatal Error", NULL);
char *title = dupcat(appname, " Fatal Error");
GtkWidget *dialog = create_message_box(
inst->window, title, msg,
string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
@ -661,7 +661,7 @@ gint delete_window(GtkWidget *widget, GdkEvent *event, GtkFrontend *inst)
* case we'll just re-emphasise that one.
*/
if (!find_and_raise_dialog(inst, DIALOG_SLOT_WARN_ON_CLOSE)) {
char *title = dupcat(appname, " Exit Confirmation", NULL);
char *title = dupcat(appname, " Exit Confirmation");
GtkWidget *dialog = create_message_box(
inst->window, title,
"Are you sure you want to close this session?",
@ -1080,8 +1080,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
char *old = state_string;
state_string = dupcat(state_string,
state_string[0] ? "|" : "",
mod_bits[i].name,
(char *)NULL);
mod_bits[i].name);
sfree(old);
val &= ~mod_bits[i].mod_bit;
@ -4596,7 +4595,7 @@ void change_settings_menuitem(GtkMenuItem *item, gpointer data)
if (find_and_raise_dialog(inst, DIALOG_SLOT_RECONFIGURE))
return;
title = dupcat(appname, " Reconfiguration", NULL);
title = dupcat(appname, " Reconfiguration");
ctx = snew(struct after_change_settings_dialog_ctx);
ctx->inst = inst;
@ -5489,7 +5488,7 @@ void new_session_window(Conf *conf, const char *geometry_string)
paste_clipboard_menuitem);
MKMENUITEM("Copy All", copy_all_menuitem);
MKSEP();
s = dupcat("About ", appname, NULL);
s = dupcat("About ", appname);
MKMENUITEM(s, about_menuitem);
sfree(s);
#undef MKMENUITEM

View File

@ -27,7 +27,7 @@ void platform_get_x11_auth(struct X11Display *disp, Conf *conf)
if (!xauthfile) {
xauthfile = getenv("HOME");
if (xauthfile) {
xauthfile = dupcat(xauthfile, "/.Xauthority", NULL);
xauthfile = dupcat(xauthfile, "/.Xauthority");
needs_free = true;
}
}
@ -127,7 +127,7 @@ int platform_make_x11_server(Plug *plug, const char *progname, int mindisp,
if (!tmpdir || !*tmpdir)
tmpdir = "/tmp";
authfilename = dupcat(tmpdir, "/", progname, "-Xauthority-XXXXXX", NULL);
authfilename = dupcat(tmpdir, "/", progname, "-Xauthority-XXXXXX");
{
int oldumask = umask(077);

View File

@ -329,7 +329,7 @@ static char *askpass_tty(const char *prompt)
p->to_server = false;
p->from_server = false;
p->name = dupstr("Pageant passphrase prompt");
add_prompt(p, dupcat(prompt, ": ", (const char *)NULL), false);
add_prompt(p, dupcat(prompt, ": "), false);
ret = console_get_userpass_input(p);
assert(ret >= 0);

View File

@ -1134,7 +1134,7 @@ Backend *pty_backend_create(
for (val = conf_get_str_strs(conf, CONF_environmt, NULL, &key);
val != NULL;
val = conf_get_str_strs(conf, CONF_environmt, key, &key)) {
char *varval = dupcat(key, "=", val, NULL);
char *varval = dupcat(key, "=", val);
putenv(varval);
/*
* We must not free varval, since putenv links it

View File

@ -47,7 +47,7 @@ const struct BackendVtable *select_backend(Conf *conf)
void initial_config_box(Conf *conf, post_dialog_fn_t after, void *afterctx)
{
char *title = dupcat(appname, " Configuration", NULL);
char *title = dupcat(appname, " Configuration");
create_config_box(title, conf, false, 0, after, afterctx);
sfree(title);
}
@ -57,7 +57,7 @@ const bool dup_check_launchable = true;
char *make_default_wintitle(char *hostname)
{
return dupcat(hostname, " - ", appname, NULL);
return dupcat(hostname, " - ", appname);
}
/*

View File

@ -460,7 +460,7 @@ char *dir_file_cat(const char *dir, const char *file)
ptrlen dir_pl = ptrlen_from_asciz(dir);
return dupcat(
dir, ptrlen_endswith(dir_pl, PTRLEN_LITERAL("/"), NULL) ? "" : "/",
file, NULL);
file);
}
/*

View File

@ -273,7 +273,7 @@ int platform_ssh_share(const char *pi_name, Conf *conf,
/*
* Acquire a lock on a file in that directory.
*/
lockname = dupcat(dirname, "/lock", (char *)NULL);
lockname = dupcat(dirname, "/lock");
lockfd = open(lockname, O_CREAT | O_RDWR | O_TRUNC, 0600);
if (lockfd < 0) {
*logtext = dupprintf("%s: open: %s", lockname, strerror(errno));
@ -348,11 +348,11 @@ void platform_ssh_share_cleanup(const char *name)
return;
}
filename = dupcat(dirname, "/socket", (char *)NULL);
filename = dupcat(dirname, "/socket");
remove(filename);
sfree(filename);
filename = dupcat(dirname, "/lock", (char *)NULL);
filename = dupcat(dirname, "/lock");
remove(filename);
sfree(filename);

View File

@ -449,7 +449,7 @@ FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
* provided name string (e.g. "Font") to a suffixed one
* ("FontName").
*/
char *suffname = dupcat(name, "Name", NULL);
char *suffname = dupcat(name, "Name");
char *tmp;
if ((tmp = read_setting_s(handle, suffname)) != NULL) {
@ -463,7 +463,7 @@ FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
/* Fall back to old-style name. */
tmp = read_setting_s(handle, name);
if (tmp && *tmp) {
char *tmp2 = dupcat("server:", tmp, NULL);
char *tmp2 = dupcat("server:", tmp);
FontSpec *fs = fontspec_new(tmp2);
sfree(tmp2);
sfree(tmp);
@ -491,7 +491,7 @@ void write_setting_fontspec(settings_w *handle, const char *name, FontSpec *fs)
* writing our settings back out we simply always generate the
* new-style name.
*/
char *suffname = dupcat(name, "Name", NULL);
char *suffname = dupcat(name, "Name");
write_setting_s(handle, suffname, fs->name);
sfree(suffname);
}