1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-18 03:20:59 -05:00

Giant const-correctness patch of doom!

Having found a lot of unfixed constness issues in recent development,
I thought perhaps it was time to get proactive, so I compiled the
whole codebase with -Wwrite-strings. That turned up a huge load of
const problems, which I've fixed in this commit: the Unix build now
goes cleanly through with -Wwrite-strings, and the Windows build is as
close as I could get it (there are some lingering issues due to
occasional Windows API functions like AcquireCredentialsHandle not
having the right constness).

Notable fallout beyond the purely mechanical changing of types:
 - the stuff saved by cmdline_save_param() is now explicitly
   dupstr()ed, and freed in cmdline_run_saved.
 - I couldn't make both string arguments to cmdline_process_param()
   const, because it intentionally writes to one of them in the case
   where it's the argument to -pw (in the vain hope of being at least
   slightly friendly to 'ps'), so elsewhere I had to temporarily
   dupstr() something for the sake of passing it to that function
 - I had to invent a silly parallel version of const_cmp() so I could
   pass const string literals in to lookup functions.
 - stripslashes() in pscp.c and psftp.c has the annoying strchr nature
This commit is contained in:
Simon Tatham
2015-05-15 11:15:42 +01:00
parent fb4fbe1158
commit 89da2ddf56
65 changed files with 559 additions and 450 deletions

42
conf.c
View File

@ -39,6 +39,16 @@ struct key {
} secondary;
};
/* Variant form of struct key which doesn't contain dynamic data, used
* for lookups. */
struct constkey {
int primary;
union {
int i;
const char *s;
} secondary;
};
struct value {
union {
int intval;
@ -88,6 +98,29 @@ static int conf_cmp(void *av, void *bv)
}
}
static int conf_cmp_constkey(void *av, void *bv)
{
struct key *a = (struct key *)av;
struct constkey *b = (struct constkey *)bv;
if (a->primary < b->primary)
return -1;
else if (a->primary > b->primary)
return +1;
switch (subkeytypes[a->primary]) {
case TYPE_INT:
if (a->secondary.i < b->secondary.i)
return -1;
else if (a->secondary.i > b->secondary.i)
return +1;
return 0;
case TYPE_STR:
return strcmp(a->secondary.s, b->secondary.s);
default:
return 0;
}
}
/*
* Free any dynamic data items pointed to by a 'struct key'. We
* don't free the structure itself, since it's probably part of a
@ -286,7 +319,7 @@ char *conf_get_str_str(Conf *conf, int primary, const char *secondary)
char *conf_get_str_strs(Conf *conf, int primary,
char *subkeyin, char **subkeyout)
{
struct key key;
struct constkey key;
struct conf_entry *entry;
assert(subkeytypes[primary] == TYPE_STR);
@ -297,7 +330,7 @@ char *conf_get_str_strs(Conf *conf, int primary,
entry = findrel234(conf->tree, &key, NULL, REL234_GT);
} else {
key.secondary.s = "";
entry = findrel234(conf->tree, &key, NULL, REL234_GE);
entry = findrel234(conf->tree, &key, conf_cmp_constkey, REL234_GE);
}
if (!entry || entry->key.primary != primary)
return NULL;
@ -307,7 +340,7 @@ char *conf_get_str_strs(Conf *conf, int primary,
char *conf_get_str_nthstrkey(Conf *conf, int primary, int n)
{
struct key key;
struct constkey key;
struct conf_entry *entry;
int index;
@ -315,7 +348,8 @@ char *conf_get_str_nthstrkey(Conf *conf, int primary, int n)
assert(valuetypes[primary] == TYPE_STR);
key.primary = primary;
key.secondary.s = "";
entry = findrelpos234(conf->tree, &key, NULL, REL234_GE, &index);
entry = findrelpos234(conf->tree, &key, conf_cmp_constkey,
REL234_GE, &index);
if (!entry || entry->key.primary != primary)
return NULL;
entry = index234(conf->tree, index + n);