diff --git a/puttymem.h b/puttymem.h index 941aded3..b927f1ba 100644 --- a/puttymem.h +++ b/puttymem.h @@ -49,4 +49,19 @@ void safefree(void *); ((type *)snrealloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr), \ (n), sizeof(type))) +/* + * For cases where you want to allocate a struct plus a subsidiary + * data buffer in one step, this macro lets you add a constant to the + * amount malloced. + * + * Since the return value is already cast to the struct type, a + * pointer to that many bytes of extra data can be conveniently + * obtained by simply adding 1 to the returned pointer! + * snew_plus_get_aux is a handy macro that does that and casts the + * result to void *, so you can assign it straight to wherever you + * wanted it. + */ +#define snew_plus(type, extra) ((type *)snmalloc(1, sizeof(type) + (extra))) +#define snew_plus_get_aux(ptr) ((void *)((ptr) + 1)) + #endif diff --git a/sshshare.c b/sshshare.c index 895c99c8..d5faa3ab 100644 --- a/sshshare.c +++ b/sshshare.c @@ -932,17 +932,14 @@ static void share_closing(Plug plug, const char *error_msg, int error_code, static void share_xchannel_add_message( struct share_xchannel *xc, int type, const void *data, int len) { - unsigned char *block; struct share_xchannel_message *msg; /* - * Be a little tricksy here by allocating a single memory block - * containing both the 'struct share_xchannel_message' and the - * actual data. Simplifies freeing it later. + * Allocate the 'struct share_xchannel_message' and the actual + * data in one unit. */ - block = smalloc(sizeof(struct share_xchannel_message) + len); - msg = (struct share_xchannel_message *)block; - msg->data = block + sizeof(struct share_xchannel_message); + msg = snew_plus(struct share_xchannel_message, len); + msg->data = snew_plus_get_aux(msg); msg->datalen = len; msg->type = type; memcpy(msg->data, data, len); diff --git a/unix/gtkfont.c b/unix/gtkfont.c index 4dd7919c..086669e2 100644 --- a/unix/gtkfont.c +++ b/unix/gtkfont.c @@ -258,7 +258,6 @@ struct xlfd_decomposed { static struct xlfd_decomposed *xlfd_decompose(const char *xlfd) { - void *mem; char *p, *components[14]; struct xlfd_decomposed *dec; int i; @@ -266,15 +265,14 @@ static struct xlfd_decomposed *xlfd_decompose(const char *xlfd) if (!xlfd) return NULL; - mem = smalloc(sizeof(struct xlfd_decomposed) + strlen(xlfd) + 1); - p = ((char *)mem) + sizeof(struct xlfd_decomposed); + dec = snew_plus(struct xlfd_decomposed, strlen(xlfd) + 1); + p = snew_plus_get_aux(dec); strcpy(p, xlfd); - dec = (struct xlfd_decomposed *)mem; for (i = 0; i < 14; i++) { if (*p != '-') { /* Malformed XLFD: not enough '-' */ - sfree(mem); + sfree(dec); return NULL; } *p++ = '\0'; @@ -283,7 +281,7 @@ static struct xlfd_decomposed *xlfd_decompose(const char *xlfd) } if (*p) { /* Malformed XLFD: too many '-' */ - sfree(mem); + sfree(dec); return NULL; }