1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-02 03:52:49 -05:00

Rename 'ret' variables passed from allocation to return.

I mentioned recently (in commit 9e7d4c53d8) message that I'm no
longer fond of the variable name 'ret', because it's used in two quite
different contexts: it's the return value from a subroutine you just
called (e.g. 'int ret = read(fd, buf, len);' and then check for error
or EOF), or it's the value you're preparing to return from the
_containing_ routine (maybe by assigning it a default value and then
conditionally modifying it, or by starting at NULL and reallocating,
or setting it just before using the 'goto out' cleanup idiom). In the
past I've occasionally made mistakes by forgetting which meaning the
variable had, or accidentally conflating both uses.

If all else fails, I now prefer 'retd' (short for 'returned') in the
former situation, and 'toret' (obviously, the value 'to return') in
the latter case. But even better is to pick a name that actually says
something more specific about what the thing actually is.

One particular bad habit throughout this codebase is to have a set of
functions that deal with some object type (say 'Foo'), all *but one*
of which take a 'Foo *foo' parameter, but the foo_new() function
starts with 'Foo *ret = snew(Foo)'. If all the rest of them think the
canonical name for the ambient Foo is 'foo', so should foo_new()!

So here's a no-brainer start on cutting down on the uses of 'ret': I
looked for all the cases where it was being assigned the result of an
allocation, and renamed the variable to be a description of the thing
being allocated. In the case of a new() function belonging to a
family, I picked the same name as the rest of the functions in its own
family, for consistency. In other cases I picked something sensible.

One case where it _does_ make sense not to use your usual name for the
variable type is when you're cloning an existing object. In that case,
_neither_ of the Foo objects involved should be called 'foo', because
it's ambiguous! They should be named so you can see which is which. In
the two cases I found here, I've called them 'orig' and 'copy'.

As in the previous refactoring, many thanks to clang-rename for the
help.
This commit is contained in:
Simon Tatham
2022-09-13 14:53:36 +01:00
parent 6cf6682c54
commit 20f818af12
19 changed files with 508 additions and 511 deletions

View File

@ -54,10 +54,10 @@ struct crcda_ctx {
struct crcda_ctx *crcda_make_context(void)
{
struct crcda_ctx *ret = snew(struct crcda_ctx);
ret->h = NULL;
ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
return ret;
struct crcda_ctx *ctx = snew(struct crcda_ctx);
ctx->h = NULL;
ctx->n = HASH_MINSIZE / HASH_ENTRYSIZE;
return ctx;
}
void crcda_free_context(struct crcda_ctx *ctx)

View File

@ -736,7 +736,7 @@ struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
{
sfree(req);
if (pktin->type == SSH_FXP_NAME) {
struct fxp_names *ret;
struct fxp_names *names;
unsigned long i;
i = get_uint32(pktin);
@ -766,28 +766,28 @@ struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
return NULL;
}
ret = snew(struct fxp_names);
ret->nnames = i;
ret->names = snewn(ret->nnames, struct fxp_name);
for (i = 0; i < (unsigned long)ret->nnames; i++) {
ret->names[i].filename = mkstr(get_string(pktin));
ret->names[i].longname = mkstr(get_string(pktin));
get_fxp_attrs(pktin, &ret->names[i].attrs);
names = snew(struct fxp_names);
names->nnames = i;
names->names = snewn(names->nnames, struct fxp_name);
for (i = 0; i < (unsigned long)names->nnames; i++) {
names->names[i].filename = mkstr(get_string(pktin));
names->names[i].longname = mkstr(get_string(pktin));
get_fxp_attrs(pktin, &names->names[i].attrs);
}
if (get_err(pktin)) {
fxp_internal_error("malformed FXP_NAME packet");
for (i = 0; i < (unsigned long)ret->nnames; i++) {
sfree(ret->names[i].filename);
sfree(ret->names[i].longname);
for (i = 0; i < (unsigned long)names->nnames; i++) {
sfree(names->names[i].filename);
sfree(names->names[i].longname);
}
sfree(ret->names);
sfree(ret);
sfree(names->names);
sfree(names);
sfree(pktin);
return NULL;
}
sftp_pkt_free(pktin);
return ret;
return names;
} else {
fxp_got_status(pktin);
sftp_pkt_free(pktin);
@ -840,14 +840,14 @@ void fxp_free_names(struct fxp_names *names)
/*
* Duplicate an fxp_name structure.
*/
struct fxp_name *fxp_dup_name(struct fxp_name *name)
struct fxp_name *fxp_dup_name(struct fxp_name *orig)
{
struct fxp_name *ret;
ret = snew(struct fxp_name);
ret->filename = dupstr(name->filename);
ret->longname = dupstr(name->longname);
ret->attrs = name->attrs; /* structure copy */
return ret;
struct fxp_name *copy;
copy = snew(struct fxp_name);
copy->filename = dupstr(orig->filename);
copy->longname = dupstr(orig->longname);
copy->attrs = orig->attrs; /* structure copy */
return copy;
}
/*