mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-02 12:02:47 -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:
@ -3488,7 +3488,7 @@ static void do_text_internal(
|
||||
bool opaque;
|
||||
bool is_cursor = false;
|
||||
static int *lpDx = NULL;
|
||||
static int lpDx_len = 0;
|
||||
static size_t lpDx_len = 0;
|
||||
int *lpDx_maybe;
|
||||
int len2; /* for SURROGATE PAIR */
|
||||
|
||||
@ -3699,9 +3699,7 @@ static void do_text_internal(
|
||||
}
|
||||
|
||||
if (len > lpDx_len) {
|
||||
lpDx_len = len * 9 / 8 + 16;
|
||||
lpDx = sresize(lpDx, lpDx_len, int);
|
||||
|
||||
sgrowarray(lpDx, lpDx_len, len);
|
||||
if (lpDx_maybe) lpDx_maybe = lpDx;
|
||||
}
|
||||
|
||||
@ -3780,14 +3778,10 @@ static void do_text_internal(
|
||||
lpDx[0] = -1;
|
||||
} else if (DIRECT_FONT(text[0])) {
|
||||
static char *directbuf = NULL;
|
||||
static int directlen = 0;
|
||||
int i;
|
||||
if (len > directlen) {
|
||||
directlen = len;
|
||||
directbuf = sresize(directbuf, directlen, char);
|
||||
}
|
||||
static size_t directlen = 0;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
sgrowarray(directbuf, directlen, len);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
directbuf[i] = text[i] & 0xFF;
|
||||
|
||||
ExtTextOut(wintw_hdc, x + xoffset,
|
||||
|
@ -541,7 +541,8 @@ HANDLE *handle_get_events(int *nevents)
|
||||
{
|
||||
HANDLE *ret;
|
||||
struct handle *h;
|
||||
int i, n, size;
|
||||
int i;
|
||||
size_t n, size;
|
||||
|
||||
/*
|
||||
* Go through our tree counting the handle objects currently
|
||||
@ -552,10 +553,7 @@ HANDLE *handle_get_events(int *nevents)
|
||||
if (handles_by_evtomain) {
|
||||
for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) {
|
||||
if (h->u.g.busy) {
|
||||
if (n >= size) {
|
||||
size += 32;
|
||||
ret = sresize(ret, size, HANDLE);
|
||||
}
|
||||
sgrowarray(ret, size, n);
|
||||
ret[n++] = h->u.g.ev_to_main;
|
||||
}
|
||||
}
|
||||
|
@ -223,16 +223,14 @@ HMODULE load_system32_dll(const char *libname)
|
||||
* path.)
|
||||
*/
|
||||
static char *sysdir = NULL;
|
||||
static size_t sysdirsize = 0;
|
||||
char *fullpath;
|
||||
HMODULE ret;
|
||||
|
||||
if (!sysdir) {
|
||||
int size = 0, len;
|
||||
do {
|
||||
size = 3*size/2 + 512;
|
||||
sysdir = sresize(sysdir, size, char);
|
||||
len = GetSystemDirectory(sysdir, size);
|
||||
} while (len >= size);
|
||||
size_t len;
|
||||
while ((len = GetSystemDirectory(sysdir, sysdirsize)) >= sysdirsize)
|
||||
sgrowarray(sysdir, sysdirsize, len);
|
||||
}
|
||||
|
||||
fullpath = dupcat(sysdir, "\\", libname, NULL);
|
||||
|
@ -262,7 +262,7 @@ int main(int argc, char **argv)
|
||||
{
|
||||
bool sending;
|
||||
SOCKET *sklist;
|
||||
int skcount, sksize;
|
||||
size_t skcount, sksize;
|
||||
int exitcode;
|
||||
bool errors;
|
||||
bool use_subsystem = false;
|
||||
@ -579,10 +579,7 @@ int main(int argc, char **argv)
|
||||
socket = next_socket(&socketstate)) i++;
|
||||
|
||||
/* Expand the buffer if necessary. */
|
||||
if (i > sksize) {
|
||||
sksize = i + 16;
|
||||
sklist = sresize(sklist, sksize, SOCKET);
|
||||
}
|
||||
sgrowarray(sklist, sksize, i);
|
||||
|
||||
/* Retrieve the sockets into sklist. */
|
||||
skcount = 0;
|
||||
|
@ -63,7 +63,7 @@ char *psftp_lcd(char *dir)
|
||||
char *psftp_getcwd(void)
|
||||
{
|
||||
char *ret = snewn(256, char);
|
||||
int len = GetCurrentDirectory(256, ret);
|
||||
size_t len = GetCurrentDirectory(256, ret);
|
||||
if (len > 256)
|
||||
ret = sresize(ret, len, char);
|
||||
GetCurrentDirectory(len, ret);
|
||||
|
@ -298,8 +298,7 @@ bool enum_settings_next(settings_e *e, strbuf *sb)
|
||||
success = (retd == ERROR_SUCCESS);
|
||||
break;
|
||||
}
|
||||
regbuf_size = regbuf_size * 5 / 4 + 256;
|
||||
regbuf = sresize(regbuf, regbuf_size, char);
|
||||
sgrowarray(regbuf, regbuf_size, regbuf_size);
|
||||
}
|
||||
|
||||
if (success)
|
||||
|
@ -160,11 +160,10 @@ void pgp_fingerprints(void)
|
||||
char *GetDlgItemText_alloc(HWND hwnd, int id)
|
||||
{
|
||||
char *ret = NULL;
|
||||
int size = 0;
|
||||
size_t size = 0;
|
||||
|
||||
do {
|
||||
size = size * 4 / 3 + 512;
|
||||
ret = sresize(ret, size, char);
|
||||
sgrowarray(ret, size, size);
|
||||
GetDlgItemText(hwnd, id, ret, size);
|
||||
} while (!memchr(ret, '\0', size-1));
|
||||
|
||||
|
Reference in New Issue
Block a user