1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 11:02:48 -05:00

Introduced wrapper macros snew(), snewn() and sresize() for the

malloc functions, which automatically cast to the same type they're
allocating the size of. Should prevent any future errors involving
mallocing the size of the wrong structure type, and will also make
life easier if we ever need to turn the PuTTY core code from real C
into C++-friendly C. I haven't touched the Mac frontend in this
checkin because I couldn't compile or test it.

[originally from svn r3014]
This commit is contained in:
Simon Tatham
2003-03-29 16:14:26 +00:00
parent 70729da988
commit d36a4c3685
60 changed files with 385 additions and 389 deletions

View File

@ -207,6 +207,10 @@ void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
{
struct dlgparam *dp = (struct dlgparam *)dlg;
struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
/*
* This is an internal allocation routine, so it's allowed to
* use smalloc directly.
*/
uc->privdata = smalloc(size);
uc->privdata_needs_free = FALSE;
return uc->privdata;
@ -379,7 +383,7 @@ void dlg_listbox_addwithid(union control *ctrl, void *dlg,
assert(ncols <=
(uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
percents = smalloc(ncols * sizeof(gint));
percents = snewn(ncols, gint);
percents[ncols-1] = 100;
for (i = 0; i < ncols-1; i++) {
percents[i] = uc->ctrl->listbox.percentages[i];
@ -1244,7 +1248,7 @@ GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
continue; /* no actual control created */
}
uc = smalloc(sizeof(struct uctrl));
uc = snew(struct uctrl);
uc->ctrl = ctrl;
uc->privdata = NULL;
uc->privdata_needs_free = FALSE;
@ -1309,7 +1313,7 @@ GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
group = NULL;
uc->nbuttons = ctrl->radio.nbuttons;
uc->buttons = smalloc(uc->nbuttons * sizeof(GtkWidget *));
uc->buttons = snewn(uc->nbuttons, GtkWidget *);
for (i = 0; i < ctrl->radio.nbuttons; i++) {
GtkWidget *b;
@ -2038,8 +2042,8 @@ int do_config_box(const char *title)
if (nselparams >= selparamsize) {
selparamsize += 16;
selparams = srealloc(selparams,
selparamsize * sizeof(*selparams));
selparams = sresize(selparams, selparamsize,
struct selparam);
}
selparams[nselparams].dp = &dp;
selparams[nselparams].panels = PANELS(panels);
@ -2059,7 +2063,7 @@ int do_config_box(const char *title)
}
dp.ntreeitems = nselparams;
dp.treeitems = smalloc(dp.ntreeitems * sizeof(GtkWidget *));
dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
for (index = 0; index < nselparams; index++) {
gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",

View File

@ -1283,7 +1283,7 @@ void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
wchar_t *tmp = data;
int tmplen = len;
inst->pasteout_data_utf8 = smalloc(len*6);
inst->pasteout_data_utf8 = snewn(len*6, char);
inst->pasteout_data_utf8_len = len*6;
inst->pasteout_data_utf8_len =
charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
@ -1294,15 +1294,15 @@ void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
inst->pasteout_data_utf8 = NULL;
} else {
inst->pasteout_data_utf8 =
srealloc(inst->pasteout_data_utf8,
inst->pasteout_data_utf8_len);
sresize(inst->pasteout_data_utf8,
inst->pasteout_data_utf8_len, char);
}
} else {
inst->pasteout_data_utf8 = NULL;
inst->pasteout_data_utf8_len = 0;
}
inst->pasteout_data = smalloc(len*6);
inst->pasteout_data = snewn(len*6, char);
inst->pasteout_data_len = len*6;
inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
data, len, inst->pasteout_data,
@ -1313,7 +1313,7 @@ void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
inst->pasteout_data = NULL;
} else {
inst->pasteout_data =
srealloc(inst->pasteout_data, inst->pasteout_data_len);
sresize(inst->pasteout_data, inst->pasteout_data_len, char);
}
if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
@ -1414,7 +1414,7 @@ void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
if (inst->pastein_data)
sfree(inst->pastein_data);
inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
inst->pastein_data = snewn(seldata->length, wchar_t);
inst->pastein_data_len = seldata->length;
inst->pastein_data_len =
mb_to_wc((seldata->type == inst->utf8_string_atom ?
@ -1530,7 +1530,7 @@ Context get_ctx(void *frontend)
if (!inst->area->window)
return NULL;
dctx = smalloc(sizeof(*dctx));
dctx = snew(struct draw_ctx);
dctx->inst = inst;
dctx->gc = gdk_gc_new(inst->area->window);
return dctx;
@ -1627,7 +1627,7 @@ void do_text_internal(Context ctx, int x, int y, char *text, int len,
wchar_t *wcs;
int i;
wcs = smalloc(sizeof(wchar_t) * (len+1));
wcs = snewn(len+1, wchar_t);
for (i = 0; i < len; i++) {
wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
}
@ -1654,7 +1654,7 @@ void do_text_internal(Context ctx, int x, int y, char *text, int len,
* and (c) the clip rectangle should prevent it causing
* trouble anyway.
*/
gwcs = smalloc(sizeof(GdkWChar) * (len*2+1));
gwcs = snewn(len*2+1, GdkWChar);
memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
/*
* FIXME: when we have a wide-char equivalent of
@ -1668,7 +1668,7 @@ void do_text_internal(Context ctx, int x, int y, char *text, int len,
gwcs, len*2);
sfree(gwcs);
} else {
gcs = smalloc(sizeof(GdkWChar) * (len+1));
gcs = snewn(len+1, gchar);
wc_to_mb(inst->fontinfo[fontid].charset, 0,
wcs, len, gcs, len, ".", NULL, NULL);
gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
@ -2129,7 +2129,7 @@ int do_cmdline(int argc, char **argv, int do_everything, Config *cfg)
if (--argc > 0) {
int i;
pty_argv = smalloc((argc+1) * sizeof(char *));
pty_argv = snewn(argc+1, char *);
++argv;
for (i = 0; i < argc; i++)
pty_argv[i] = argv[i];
@ -2293,7 +2293,7 @@ int main(int argc, char **argv)
/*
* Create an instance structure and initialise to zeroes
*/
inst = smalloc(sizeof(*inst));
inst = snew(struct gui_data);
memset(inst, 0, sizeof(*inst));
inst->alt_keycode = -1; /* this one needs _not_ to be zero */

View File

@ -498,7 +498,7 @@ static char *pty_init(void *frontend, void **backend_handle, Config *cfg,
char *shellname;
if (cfg->login_shell) {
char *p = strrchr(shell, '/');
shellname = smalloc(2+strlen(shell));
shellname = snewn(2+strlen(shell), char);
p = p ? p+1 : shell;
sprintf(shellname, "-%s", p);
} else

View File

@ -29,7 +29,7 @@ void platform_get_x11_auth(char *display, int *protocol,
if (!fp)
return; /* assume no auth */
localbuf = smalloc(maxsize);
localbuf = snewn(maxsize, char);
while (1) {
/*

View File

@ -80,7 +80,7 @@ void agent_query(void *in, int inlen, void **out, int *outlen)
}
retsize += 4;
assert(retbuf == sizebuf);
retbuf = smalloc(retsize);
retbuf = snewn(retsize, char);
memcpy(retbuf, sizebuf, 4);
}
}

View File

@ -119,7 +119,7 @@ char *error_string(int error)
SockAddr sk_namelookup(const char *host, char **canonicalname)
{
SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
SockAddr ret = snew(struct SockAddr_tag);
unsigned long a;
struct hostent *h = NULL;
char realhost[8192];
@ -195,14 +195,14 @@ SockAddr sk_namelookup(const char *host, char **canonicalname)
}
ret->address = ntohl(a);
realhost[lenof(realhost)-1] = '\0';
*canonicalname = smalloc(1+strlen(realhost));
*canonicalname = snewn(1+strlen(realhost), char);
strcpy(*canonicalname, realhost);
return ret;
}
SockAddr sk_nonamelookup(const char *host)
{
SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
SockAddr ret = snew(struct SockAddr_tag);
ret->error = NULL;
ret->family = AF_UNSPEC;
strncpy(ret->hostname, host, lenof(ret->hostname));
@ -324,7 +324,7 @@ Socket sk_register(void *sock, Plug plug)
/*
* Create Socket structure.
*/
ret = smalloc(sizeof(struct Socket_tag));
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
@ -367,7 +367,7 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
/*
* Create Socket structure.
*/
ret = smalloc(sizeof(struct Socket_tag));
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
@ -525,7 +525,7 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only)
/*
* Create Socket structure.
*/
ret = smalloc(sizeof(struct Socket_tag));
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;

View File

@ -446,13 +446,13 @@ int main(int argc, char **argv)
while (*p) {
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
command = srealloc(command, cmdsize);
command = sresize(command, cmdsize, char);
}
command[cmdlen++]=*p++;
}
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
command = srealloc(command, cmdsize);
command = sresize(command, cmdsize, char);
}
command[cmdlen++]=' '; /* always add trailing space */
if (--argc) p = *++argv;
@ -631,7 +631,7 @@ int main(int argc, char **argv)
/* Expand the sklist buffer if necessary. */
if (i > sksize) {
sksize = i + 16;
sklist = srealloc(sklist, sksize * sizeof(*sklist));
sklist = sresize(sklist, sksize, int);
}
/*

View File

@ -12,7 +12,7 @@ struct printer_job_tag {
printer_job *printer_start_job(char *printer)
{
printer_job *ret = smalloc(sizeof(printer_job));
printer_job *ret = snew(printer_job);
/*
* On Unix, we treat the printer string as the name of a
* command to pipe to - typically lpr, of course.

View File

@ -75,8 +75,8 @@ void provide_xrm_string(char *string)
q++;
while (p > string && p[-1] != '.' && p[-1] != '*')
p--;
xrms = smalloc(sizeof(struct xrm_string));
key = smalloc(q-p);
xrms = snew(struct xrm_string);
key = snewn(q-p, char);
memcpy(key, p, q-p);
key[q-p-1] = '\0';
xrms->key = key;
@ -199,14 +199,14 @@ static void make_filename(char *filename, int index)
*/
static char *fgetline(FILE *fp)
{
char *ret = smalloc(512);
char *ret = snewn(512, char);
int size = 512, len = 0;
while (fgets(ret + len, size - len, fp)) {
len += strlen(ret + len);
if (ret[len-1] == '\n')
break; /* got a newline, we're done */
size = len + 512;
ret = srealloc(ret, size);
ret = sresize(ret, size, char);
}
if (len == 0) { /* first fgets returned NULL */
sfree(ret);