mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as _almost_ completely separate types. I'm still willing to use C's implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine, no need to spell it out as blob.len != 0), but generally, if a variable is going to be conceptually a boolean, I like to declare it bool and assign to it using 'true' or 'false' rather than 0 or 1. PuTTY is an exception, because it predates the C99 bool, and I've stuck to its existing coding style even when adding new code to it. But it's been annoying me more and more, so now that I've decided C99 bool is an acceptable thing to require from our toolchain in the first place, here's a quite thorough trawl through the source doing 'boolification'. Many variables and function parameters are now typed as bool rather than int; many assignments of 0 or 1 to those variables are now spelled 'true' or 'false'. I managed this thorough conversion with the help of a custom clang plugin that I wrote to trawl the AST and apply heuristics to point out where things might want changing. So I've even managed to do a decent job on parts of the code I haven't looked at in years! To make the plugin's work easier, I pushed platform front ends generally in the direction of using standard 'bool' in preference to platform-specific boolean types like Windows BOOL or GTK's gboolean; I've left the platform booleans in places they _have_ to be for the platform APIs to work right, but variables only used by my own code have been converted wherever I found them. In a few places there are int values that look very like booleans in _most_ of the places they're used, but have a rarely-used third value, or a distinction between different nonzero values that most users don't care about. In these cases, I've _removed_ uses of 'true' and 'false' for the return values, to emphasise that there's something more subtle going on than a simple boolean answer: - the 'multisel' field in dialog.h's list box structure, for which the GTK front end in particular recognises a difference between 1 and 2 but nearly everything else treats as boolean - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you something about the specific location of the urgent pointer, but most clients only care about 0 vs 'something nonzero' - the return value of wc_match, where -1 indicates a syntax error in the wildcard. - the return values from SSH-1 RSA-key loading functions, which use -1 for 'wrong passphrase' and 0 for all other failures (so any caller which already knows it's not loading an _encrypted private_ key can treat them as boolean) - term->esc_query, and the 'query' parameter in toggle_mode in terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h, but can also hold -1 for some other intervening character that we don't support. In a few places there's an integer that I haven't turned into a bool even though it really _can_ only take values 0 or 1 (and, as above, tried to make the call sites consistent in not calling those values true and false), on the grounds that I thought it would make it more confusing to imply that the 0 value was in some sense 'negative' or bad and the 1 positive or good: - the return value of plug_accepting uses the POSIXish convention of 0=success and nonzero=error; I think if I made it bool then I'd also want to reverse its sense, and that's a job for a separate piece of work. - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1 represent the default and alternate screens. There's no obvious reason why one of those should be considered 'true' or 'positive' or 'success' - they're just indices - so I've left it as int. ssh_scp_recv had particularly confusing semantics for its previous int return value: its call sites used '<= 0' to check for error, but it never actually returned a negative number, just 0 or 1. Now the function and its call sites agree that it's a bool. In a couple of places I've renamed variables called 'ret', because I don't like that name any more - it's unclear whether it means the return value (in preparation) for the _containing_ function or the return value received from a subroutine call, and occasionally I've accidentally used the same variable for both and introduced a bug. So where one of those got in my way, I've renamed it to 'toret' or 'retd' (the latter short for 'returned') in line with my usual modern practice, but I haven't done a thorough job of finding all of them. Finally, one amusing side effect of doing this is that I've had to separate quite a few chained assignments. It used to be perfectly fine to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a the 'true' defined by stdbool.h, that idiom provokes a warning from gcc: 'suggest parentheses around assignment used as truth value'!
This commit is contained in:
@ -23,10 +23,10 @@ struct ssh2_userauth_state {
|
||||
|
||||
PacketProtocolLayer *transport_layer, *successor_layer;
|
||||
Filename *keyfile;
|
||||
int tryagent, change_username;
|
||||
bool tryagent, change_username;
|
||||
char *hostname, *fullhostname;
|
||||
char *default_username;
|
||||
int try_ki_auth, try_gssapi_auth, try_gssapi_kex_auth, gssapi_fwd;
|
||||
bool try_ki_auth, try_gssapi_auth, try_gssapi_kex_auth, gssapi_fwd;
|
||||
|
||||
ptrlen session_id;
|
||||
enum {
|
||||
@ -39,28 +39,28 @@ struct ssh2_userauth_state {
|
||||
AUTH_TYPE_KEYBOARD_INTERACTIVE,
|
||||
AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
|
||||
} type;
|
||||
int need_pw, can_pubkey, can_passwd, can_keyb_inter;
|
||||
bool need_pw, can_pubkey, can_passwd, can_keyb_inter;
|
||||
int userpass_ret;
|
||||
int tried_pubkey_config, done_agent;
|
||||
bool tried_pubkey_config, done_agent;
|
||||
struct ssh_connection_shared_gss_state *shgss;
|
||||
#ifndef NO_GSSAPI
|
||||
int can_gssapi;
|
||||
int can_gssapi_keyex_auth;
|
||||
int tried_gssapi;
|
||||
int tried_gssapi_keyex_auth;
|
||||
bool can_gssapi;
|
||||
bool can_gssapi_keyex_auth;
|
||||
bool tried_gssapi;
|
||||
bool tried_gssapi_keyex_auth;
|
||||
time_t gss_cred_expiry;
|
||||
Ssh_gss_buf gss_buf;
|
||||
Ssh_gss_buf gss_rcvtok, gss_sndtok;
|
||||
Ssh_gss_stat gss_stat;
|
||||
#endif
|
||||
int kbd_inter_refused;
|
||||
bool kbd_inter_refused;
|
||||
prompts_t *cur_prompt;
|
||||
int num_prompts;
|
||||
char *username;
|
||||
char *password;
|
||||
int got_username;
|
||||
bool got_username;
|
||||
strbuf *publickey_blob;
|
||||
int privatekey_available, privatekey_encrypted;
|
||||
bool privatekey_available, privatekey_encrypted;
|
||||
char *publickey_algorithm;
|
||||
char *publickey_comment;
|
||||
void *agent_response_to_free;
|
||||
@ -71,7 +71,7 @@ struct ssh2_userauth_state {
|
||||
ptrlen pk, alg, comment;
|
||||
int len;
|
||||
PktOut *pktout;
|
||||
int want_user_input;
|
||||
bool want_user_input;
|
||||
|
||||
agent_pending_query *auth_agent_query;
|
||||
bufchain banner;
|
||||
@ -81,11 +81,11 @@ struct ssh2_userauth_state {
|
||||
|
||||
static void ssh2_userauth_free(PacketProtocolLayer *);
|
||||
static void ssh2_userauth_process_queue(PacketProtocolLayer *);
|
||||
static int ssh2_userauth_get_specials(
|
||||
static bool ssh2_userauth_get_specials(
|
||||
PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
|
||||
static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
|
||||
SessionSpecialCode code, int arg);
|
||||
static int ssh2_userauth_want_user_input(PacketProtocolLayer *ppl);
|
||||
static bool ssh2_userauth_want_user_input(PacketProtocolLayer *ppl);
|
||||
static void ssh2_userauth_got_user_input(PacketProtocolLayer *ppl);
|
||||
static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
|
||||
|
||||
@ -114,11 +114,10 @@ static const struct PacketProtocolLayerVtable ssh2_userauth_vtable = {
|
||||
PacketProtocolLayer *ssh2_userauth_new(
|
||||
PacketProtocolLayer *successor_layer,
|
||||
const char *hostname, const char *fullhostname,
|
||||
Filename *keyfile, int tryagent,
|
||||
const char *default_username, int change_username,
|
||||
int try_ki_auth,
|
||||
int try_gssapi_auth, int try_gssapi_kex_auth,
|
||||
int gssapi_fwd, struct ssh_connection_shared_gss_state *shgss)
|
||||
Filename *keyfile, bool tryagent,
|
||||
const char *default_username, bool change_username,
|
||||
bool try_ki_auth, bool try_gssapi_auth, bool try_gssapi_kex_auth,
|
||||
bool gssapi_fwd, struct ssh_connection_shared_gss_state *shgss)
|
||||
{
|
||||
struct ssh2_userauth_state *s = snew(struct ssh2_userauth_state);
|
||||
memset(s, 0, sizeof(*s));
|
||||
@ -1101,7 +1100,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
|
||||
s->num_prompts = get_uint32(pktin);
|
||||
for (i = 0; i < s->num_prompts; i++) {
|
||||
ptrlen prompt;
|
||||
int echo;
|
||||
bool echo;
|
||||
static char noprompt[] =
|
||||
"<server failed to send prompt>: ";
|
||||
|
||||
@ -1213,7 +1212,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
|
||||
/*
|
||||
* Plain old password authentication.
|
||||
*/
|
||||
int changereq_first_time; /* not live over crReturn */
|
||||
bool changereq_first_time; /* not live over crReturn */
|
||||
|
||||
s->ppl.bpp->pls->actx = SSH2_PKTCTX_PASSWORD;
|
||||
|
||||
@ -1296,7 +1295,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
|
||||
* Loop until the server accepts it.
|
||||
*/
|
||||
|
||||
int got_new = false; /* not live over crReturn */
|
||||
bool got_new = false; /* not live over crReturn */
|
||||
ptrlen prompt; /* not live over crReturn */
|
||||
|
||||
{
|
||||
@ -1634,7 +1633,7 @@ static PktOut *ssh2_userauth_gss_packet(
|
||||
}
|
||||
#endif
|
||||
|
||||
static int ssh2_userauth_get_specials(
|
||||
static bool ssh2_userauth_get_specials(
|
||||
PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
|
||||
{
|
||||
/* No specials provided by this layer. */
|
||||
@ -1647,7 +1646,7 @@ static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
|
||||
/* No specials provided by this layer. */
|
||||
}
|
||||
|
||||
static int ssh2_userauth_want_user_input(PacketProtocolLayer *ppl)
|
||||
static bool ssh2_userauth_want_user_input(PacketProtocolLayer *ppl)
|
||||
{
|
||||
struct ssh2_userauth_state *s =
|
||||
container_of(ppl, struct ssh2_userauth_state, ppl);
|
||||
|
Reference in New Issue
Block a user