1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32: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

@ -776,7 +776,7 @@ static void win_gui_eventlog(LogPolicy *lp, const char *string)
if (*location)
sfree(*location);
*location = dupcat(timebuf, string, (const char *)NULL);
*location = dupcat(timebuf, string);
if (logbox) {
int count;
SendDlgItemMessage(logbox, IDN_LIST, LB_ADDSTRING,

View File

@ -308,7 +308,7 @@ static Ssh_gss_stat ssh_sspi_import_name(struct ssh_gss_library *lib,
if (host == NULL) return SSH_GSS_FAILURE;
/* copy it into form host/FQDN */
pStr = dupcat("host/", host, NULL);
pStr = dupcat("host/", host);
*srv_name = (Ssh_gss_name) pStr;

View File

@ -432,7 +432,7 @@ static IShellLink *make_shell_link(const char *appname,
* behaviour change in which an argument string starting with
* '@' causes the SetArguments method to silently do the wrong
* thing. */
param_string = dupcat(" @", sessionname, NULL);
param_string = dupcat(" @", sessionname);
} else {
param_string = dupstr("");
}
@ -440,8 +440,7 @@ static IShellLink *make_shell_link(const char *appname,
sfree(param_string);
if (sessionname) {
desc_string = dupcat("Connect to PuTTY session '",
sessionname, "'", NULL);
desc_string = dupcat("Connect to PuTTY session '", sessionname, "'");
} else {
assert(appname);
desc_string = dupprintf("Run %.*s",

View File

@ -233,7 +233,7 @@ HMODULE load_system32_dll(const char *libname)
sgrowarray(sysdir, sysdirsize, len);
}
fullpath = dupcat(sysdir, "\\", libname, NULL);
fullpath = dupcat(sysdir, "\\", libname);
ret = LoadLibrary(fullpath);
sfree(fullpath);
return ret;

View File

@ -451,7 +451,7 @@ static void prompt_add_keyfile(void)
char *dir = filelist;
char *filewalker = filelist + strlen(dir) + 1;
while (*filewalker != '\0') {
char *filename = dupcat(dir, "\\", filewalker, NULL);
char *filename = dupcat(dir, "\\", filewalker);
Filename *fn = filename_from_str(filename);
win_add_keyfile(fn);
filename_free(fn);

View File

@ -274,7 +274,7 @@ DirHandle *open_directory(const char *name, const char **errmsg)
DirHandle *ret;
/* Enumerate files in dir `foo'. */
findfile = dupcat(name, "/*", NULL);
findfile = dupcat(name, "/*");
h = FindFirstFile(findfile, &fdat);
if (h == INVALID_HANDLE_VALUE) {
*errmsg = win_strerror(GetLastError());
@ -395,7 +395,7 @@ WildcardMatcher *begin_wildcard_matching(const char *name)
(fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
ret->name = NULL;
else
ret->name = dupcat(ret->srcpath, fdat.cFileName, NULL);
ret->name = dupcat(ret->srcpath, fdat.cFileName);
return ret;
}
@ -413,7 +413,7 @@ char *wildcard_get_filename(WildcardMatcher *dir)
(fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
dir->name = NULL;
else
dir->name = dupcat(dir->srcpath, fdat.cFileName, NULL);
dir->name = dupcat(dir->srcpath, fdat.cFileName);
}
if (dir->name) {
@ -455,7 +455,7 @@ char *dir_file_cat(const char *dir, const char *file)
return dupcat(
dir, (ptrlen_endswith(dir_pl, PTRLEN_LITERAL("\\"), NULL) ||
ptrlen_endswith(dir_pl, PTRLEN_LITERAL("/"), NULL)) ? "" : "\\",
file, NULL);
file);
}
/* ----------------------------------------------------------------------

View File

@ -175,7 +175,7 @@ FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
if (!fontname)
return NULL;
settingname = dupcat(name, "IsBold", NULL);
settingname = dupcat(name, "IsBold");
isbold = read_setting_i(handle, settingname, -1);
sfree(settingname);
if (isbold == -1) {
@ -183,7 +183,7 @@ FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
return NULL;
}
settingname = dupcat(name, "CharSet", NULL);
settingname = dupcat(name, "CharSet");
charset = read_setting_i(handle, settingname, -1);
sfree(settingname);
if (charset == -1) {
@ -191,7 +191,7 @@ FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
return NULL;
}
settingname = dupcat(name, "Height", NULL);
settingname = dupcat(name, "Height");
height = read_setting_i(handle, settingname, INT_MIN);
sfree(settingname);
if (height == INT_MIN) {
@ -210,13 +210,13 @@ void write_setting_fontspec(settings_w *handle,
char *settingname;
write_setting_s(handle, name, font->name);
settingname = dupcat(name, "IsBold", NULL);
settingname = dupcat(name, "IsBold");
write_setting_i(handle, settingname, font->isbold);
sfree(settingname);
settingname = dupcat(name, "CharSet", NULL);
settingname = dupcat(name, "CharSet");
write_setting_i(handle, settingname, font->charset);
sfree(settingname);
settingname = dupcat(name, "Height", NULL);
settingname = dupcat(name, "Height");
write_setting_i(handle, settingname, font->height);
sfree(settingname);
}
@ -551,15 +551,13 @@ static HANDLE access_random_seed(int action)
char profile[MAX_PATH + 1];
if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, profile)) &&
try_random_seed_and_free(dupcat(profile, "\\PUTTY.RND",
(const char *)NULL),
try_random_seed_and_free(dupcat(profile, "\\PUTTY.RND"),
action, &rethandle))
return rethandle;
if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, profile)) &&
try_random_seed_and_free(dupcat(profile, "\\PUTTY.RND",
(const char *)NULL),
try_random_seed_and_free(dupcat(profile, "\\PUTTY.RND"),
action, &rethandle))
return rethandle;
}
@ -582,8 +580,7 @@ static HANDLE access_random_seed(int action)
if (drvlen < lenof(drv) && pathlen < lenof(path) && pathlen > 0 &&
try_random_seed_and_free(
dupcat(drv, path, "\\PUTTY.RND", (const char *)NULL),
action, &rethandle))
dupcat(drv, path, "\\PUTTY.RND"), action, &rethandle))
return rethandle;
}
@ -595,8 +592,7 @@ static HANDLE access_random_seed(int action)
DWORD len = GetWindowsDirectory(windir, sizeof(windir));
if (len < lenof(windir) &&
try_random_seed_and_free(
dupcat(windir, "\\PUTTY.RND", (const char *)NULL),
action, &rethandle))
dupcat(windir, "\\PUTTY.RND"), action, &rethandle))
return rethandle;
}