mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-01 03:22:48 -05:00
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the lines of if (logical_array_len >= physical_array_size) { physical_array_size = logical_array_len * 5 / 4 + 256; array = sresize(array, physical_array_size, ElementType); } which happens at a zillion call sites throughout this code base, with different random choices of the geometric factor and additive constant, sometimes forgetting them completely, and generally doing a lot of repeated work. The new macro sgrowarray(array,size,n) has the semantics: here are the array pointer and its physical size for you to modify, now please ensure that the nth element exists, so I can write into it. And sgrowarrayn(array,size,n,m) is the same except that it ensures that the array has size at least n+m (so sgrowarray is just the special case where m=1). Now that this is a single centralised implementation that will be used everywhere, I've also gone to more effort in the implementation, with careful overflow checks that would have been painful to put at all the previous call sites. This commit also switches over every use of sresize(), apart from a few where I really didn't think it would gain anything. A consequence of that is that a lot of array-size variables have to have their types changed to size_t, because the macros require that (they address-take the size to pass to the underlying function).
This commit is contained in:
@ -93,7 +93,7 @@ struct dlgparam {
|
||||
GtkWidget *currtreeitem, **treeitems;
|
||||
int ntreeitems;
|
||||
#else
|
||||
int nselparams;
|
||||
size_t nselparams;
|
||||
struct selparam *selparams;
|
||||
#endif
|
||||
struct controlbox *ctrlbox;
|
||||
@ -2918,7 +2918,7 @@ GtkWidget *create_config_box(const char *title, Conf *conf,
|
||||
struct Shortcuts scs;
|
||||
|
||||
struct selparam *selparams = NULL;
|
||||
int nselparams = 0, selparamsize = 0;
|
||||
size_t nselparams = 0, selparamsize = 0;
|
||||
|
||||
dp = snew(struct dlgparam);
|
||||
dp->after = after;
|
||||
@ -3040,11 +3040,7 @@ GtkWidget *create_config_box(const char *title, Conf *conf,
|
||||
page_num);
|
||||
}
|
||||
|
||||
if (nselparams >= selparamsize) {
|
||||
selparamsize += 16;
|
||||
selparams = sresize(selparams, selparamsize,
|
||||
struct selparam);
|
||||
}
|
||||
sgrowarray(selparams, selparamsize, nselparams);
|
||||
selparams[nselparams].dp = dp;
|
||||
selparams[nselparams].panels = GTK_NOTEBOOK(panels);
|
||||
selparams[nselparams].panel = panelvbox;
|
||||
@ -3243,8 +3239,7 @@ static void dlgparam_destroy(GtkWidget *widget, gpointer data)
|
||||
ctrl_free_box(dp->ctrlbox);
|
||||
#if GTK_CHECK_VERSION(2,0,0)
|
||||
if (dp->selparams) {
|
||||
int i;
|
||||
for (i = 0; i < dp->nselparams; i++)
|
||||
for (size_t i = 0; i < dp->nselparams; i++)
|
||||
if (dp->selparams[i].treepath)
|
||||
gtk_tree_path_free(dp->selparams[i].treepath);
|
||||
sfree(dp->selparams);
|
||||
|
@ -170,7 +170,7 @@ void launch_duplicate_session(Conf *conf)
|
||||
for (i = 0; pty_argv[i]; i++)
|
||||
put_asciz(serialised, pty_argv[i]);
|
||||
|
||||
sprintf(option, "---[%d,%d]", pipefd[0], serialised->len);
|
||||
sprintf(option, "---[%d,%zu]", pipefd[0], serialised->len);
|
||||
noncloexec(pipefd[0]);
|
||||
fork_and_exec_self(pipefd[1], option, NULL);
|
||||
close(pipefd[0]);
|
||||
|
10
unix/uxnet.c
10
unix/uxnet.c
@ -1584,18 +1584,16 @@ int net_service_lookup(char *service)
|
||||
|
||||
char *get_hostname(void)
|
||||
{
|
||||
int len = 128;
|
||||
size_t size = 0;
|
||||
char *hostname = NULL;
|
||||
do {
|
||||
len *= 2;
|
||||
hostname = sresize(hostname, len, char);
|
||||
if ((gethostname(hostname, len) < 0) &&
|
||||
(errno != ENAMETOOLONG)) {
|
||||
sgrowarray(hostname, size, size);
|
||||
if ((gethostname(hostname, size) < 0) && (errno != ENAMETOOLONG)) {
|
||||
sfree(hostname);
|
||||
hostname = NULL;
|
||||
break;
|
||||
}
|
||||
} while (strlen(hostname) >= len-1);
|
||||
} while (strlen(hostname) >= size-1);
|
||||
return hostname;
|
||||
}
|
||||
|
||||
|
@ -734,7 +734,8 @@ void run_agent(void)
|
||||
unsigned long now;
|
||||
int *fdlist;
|
||||
int fd;
|
||||
int i, fdsize, fdstate;
|
||||
int i, fdstate;
|
||||
size_t fdsize;
|
||||
int termination_pid = -1;
|
||||
bool errors = false;
|
||||
Conf *conf;
|
||||
@ -882,10 +883,7 @@ void run_agent(void)
|
||||
fd = next_fd(&fdstate, &rwx)) i++;
|
||||
|
||||
/* Expand the fdlist buffer if necessary. */
|
||||
if (i > fdsize) {
|
||||
fdsize = i + 16;
|
||||
fdlist = sresize(fdlist, fdsize, int);
|
||||
}
|
||||
sgrowarray(fdlist, fdsize, i);
|
||||
|
||||
/*
|
||||
* Add all currently open fds to the select sets, and store
|
||||
|
@ -568,7 +568,8 @@ int main(int argc, char **argv)
|
||||
bool sending;
|
||||
int *fdlist;
|
||||
int fd;
|
||||
int i, fdsize, fdstate;
|
||||
int i, fdstate;
|
||||
size_t fdsize;
|
||||
int exitcode;
|
||||
bool errors;
|
||||
enum TriState sanitise_stdout = AUTO, sanitise_stderr = AUTO;
|
||||
@ -909,10 +910,7 @@ int main(int argc, char **argv)
|
||||
fd = next_fd(&fdstate, &rwx)) i++;
|
||||
|
||||
/* Expand the fdlist buffer if necessary. */
|
||||
if (i > fdsize) {
|
||||
fdsize = i + 16;
|
||||
fdlist = sresize(fdlist, fdsize, int);
|
||||
}
|
||||
sgrowarray(fdlist, fdsize, i);
|
||||
|
||||
/*
|
||||
* Add all currently open fds to the select sets, and store
|
||||
|
@ -360,11 +360,12 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int *fdlist;
|
||||
int fd;
|
||||
int i, fdsize, fdstate;
|
||||
int i, fdstate;
|
||||
size_t fdsize;
|
||||
unsigned long now;
|
||||
|
||||
ssh_key **hostkeys = NULL;
|
||||
int nhostkeys = 0, hostkeysize = 0;
|
||||
size_t nhostkeys = 0, hostkeysize = 0;
|
||||
RSAKey *hostkey1 = NULL;
|
||||
|
||||
AuthPolicy ap;
|
||||
@ -434,10 +435,7 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nhostkeys >= hostkeysize) {
|
||||
hostkeysize = nhostkeys * 5 / 4 + 16;
|
||||
hostkeys = sresize(hostkeys, hostkeysize, ssh_key *);
|
||||
}
|
||||
sgrowarray(hostkeys, hostkeysize, nhostkeys);
|
||||
hostkeys[nhostkeys++] = key;
|
||||
} else if (keytype == SSH_KEYTYPE_SSH1) {
|
||||
if (hostkey1) {
|
||||
@ -578,10 +576,7 @@ int main(int argc, char **argv)
|
||||
fd = next_fd(&fdstate, &rwx)) i++;
|
||||
|
||||
/* Expand the fdlist buffer if necessary. */
|
||||
if (i > fdsize) {
|
||||
fdsize = i + 16;
|
||||
fdlist = sresize(fdlist, fdsize, int);
|
||||
}
|
||||
sgrowarray(fdlist, fdsize, i);
|
||||
|
||||
/*
|
||||
* Add all currently open fds to the select sets, and store
|
||||
|
@ -98,7 +98,7 @@ char *psftp_lcd(char *dir)
|
||||
char *psftp_getcwd(void)
|
||||
{
|
||||
char *buffer, *ret;
|
||||
int size = 256;
|
||||
size_t size = 256;
|
||||
|
||||
buffer = snewn(size, char);
|
||||
while (1) {
|
||||
@ -113,8 +113,7 @@ char *psftp_getcwd(void)
|
||||
* Otherwise, ERANGE was returned, meaning the buffer
|
||||
* wasn't big enough.
|
||||
*/
|
||||
size = size * 3 / 2;
|
||||
buffer = sresize(buffer, size, char);
|
||||
sgrowarray(buffer, size, size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -447,7 +446,8 @@ char *dir_file_cat(const char *dir, const char *file)
|
||||
static int ssh_sftp_do_select(bool include_stdin, bool no_fds_ok)
|
||||
{
|
||||
fd_set rset, wset, xset;
|
||||
int i, fdsize, *fdlist;
|
||||
int i, *fdlist;
|
||||
size_t fdsize;
|
||||
int fd, fdcount, fdstate, rwx, ret, maxfd;
|
||||
unsigned long now = GETTICKCOUNT();
|
||||
unsigned long next;
|
||||
@ -467,10 +467,7 @@ static int ssh_sftp_do_select(bool include_stdin, bool no_fds_ok)
|
||||
return -1; /* doom */
|
||||
|
||||
/* Expand the fdlist buffer if necessary. */
|
||||
if (i > fdsize) {
|
||||
fdsize = i + 16;
|
||||
fdlist = sresize(fdlist, fdsize, int);
|
||||
}
|
||||
sgrowarray(fdlist, fdsize, i);
|
||||
|
||||
FD_ZERO(&rset);
|
||||
FD_ZERO(&wset);
|
||||
@ -571,7 +568,8 @@ int ssh_sftp_loop_iteration(void)
|
||||
char *ssh_sftp_get_cmdline(const char *prompt, bool no_fds_ok)
|
||||
{
|
||||
char *buf;
|
||||
int buflen, bufsize, ret;
|
||||
size_t buflen, bufsize;
|
||||
int ret;
|
||||
|
||||
fputs(prompt, stdout);
|
||||
fflush(stdout);
|
||||
@ -587,10 +585,7 @@ char *ssh_sftp_get_cmdline(const char *prompt, bool no_fds_ok)
|
||||
return NULL; /* woop woop */
|
||||
}
|
||||
if (ret > 0) {
|
||||
if (buflen >= bufsize) {
|
||||
bufsize = buflen + 512;
|
||||
buf = sresize(buf, bufsize, char);
|
||||
}
|
||||
sgrowarray(buf, bufsize, buflen);
|
||||
ret = read(0, buf+buflen, 1);
|
||||
if (ret < 0) {
|
||||
perror("read");
|
||||
|
@ -28,7 +28,7 @@ typedef struct UnixSftpServer UnixSftpServer;
|
||||
struct UnixSftpServer {
|
||||
unsigned *fdseqs;
|
||||
bool *fdsopen;
|
||||
int fdsize;
|
||||
size_t fdsize;
|
||||
|
||||
tree234 *dirhandles;
|
||||
int last_dirhandle_index;
|
||||
@ -74,9 +74,8 @@ static void uss_free(SftpServer *srv)
|
||||
{
|
||||
UnixSftpServer *uss = container_of(srv, UnixSftpServer, srv);
|
||||
struct uss_dirhandle *udh;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < uss->fdsize; i++)
|
||||
for (size_t i = 0; i < uss->fdsize; i++)
|
||||
if (uss->fdsopen[i])
|
||||
close(i);
|
||||
sfree(uss->fdseqs);
|
||||
@ -118,9 +117,8 @@ static void uss_return_new_handle(
|
||||
{
|
||||
assert(fd >= 0);
|
||||
if (fd >= uss->fdsize) {
|
||||
int old_size = uss->fdsize;
|
||||
uss->fdsize = fd * 5 / 4 + 32;
|
||||
uss->fdseqs = sresize(uss->fdseqs, uss->fdsize, unsigned);
|
||||
size_t old_size = uss->fdsize;
|
||||
sgrowarray(uss->fdseqs, uss->fdsize, fd);
|
||||
uss->fdsopen = sresize(uss->fdsopen, uss->fdsize, bool);
|
||||
while (old_size < uss->fdsize) {
|
||||
uss->fdseqs[old_size] = 0;
|
||||
|
@ -34,12 +34,11 @@ struct x11_err_to_ignore {
|
||||
|
||||
struct x11_err_to_ignore *errs;
|
||||
|
||||
int nerrs, errsize;
|
||||
size_t nerrs, errsize;
|
||||
|
||||
static int x11_error_handler(Display *thisdisp, XErrorEvent *err)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < nerrs; i++) {
|
||||
for (size_t i = 0; i < nerrs; i++) {
|
||||
if (thisdisp == errs[i].display &&
|
||||
err->serial == errs[i].serial &&
|
||||
err->error_code == errs[i].error_code) {
|
||||
@ -65,7 +64,7 @@ void x11_ignore_error(Display *disp, unsigned char errcode)
|
||||
*/
|
||||
{
|
||||
unsigned long last = LastKnownRequestProcessed(disp);
|
||||
int i, j;
|
||||
size_t i, j;
|
||||
for (i = j = 0; i < nerrs; i++) {
|
||||
if (errs[i].display == disp && errs[i].serial <= last)
|
||||
continue;
|
||||
@ -74,10 +73,7 @@ void x11_ignore_error(Display *disp, unsigned char errcode)
|
||||
nerrs = j;
|
||||
}
|
||||
|
||||
if (nerrs >= errsize) {
|
||||
errsize = nerrs * 5 / 4 + 16;
|
||||
errs = sresize(errs, errsize, struct x11_err_to_ignore);
|
||||
}
|
||||
sgrowarray(errs, errsize, nerrs);
|
||||
errs[nerrs].display = disp;
|
||||
errs[nerrs].error_code = errcode;
|
||||
errs[nerrs].serial = NextRequest(disp);
|
||||
|
Reference in New Issue
Block a user