Introduced wrapper macros snew(), snewn() and sresize() for the
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 29 Mar 2003 16:14:26 +0000 (16:14 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 29 Mar 2003 16:14:26 +0000 (16:14 +0000)
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.

git-svn-id: svn://svn.tartarus.org/sgt/putty@3014 cda61777-01e9-0310-a592-d414129be87e

60 files changed:
cmdline.c
config.c
dialog.c
import.c
ldisc.c
ldiscucs.c
logging.c
misc.c
pageant.c
pageantc.c
plink.c
portfwd.c
printing.c
proxy.c
psftp.c
puttygen.c
puttymem.h
raw.c
rlogin.c
scp.c
settings.c
sftp.c
sizetip.c
ssh.c
sshaes.c
sshblowf.c
sshbn.c
sshcrcda.c
sshdes.c
sshdh.c
sshdss.c
sshmd5.c
sshpubk.c
sshrand.c
sshrsa.c
sshsha.c
sshzlib.c
telnet.c
terminal.c
terminal.h
testback.c
tree234.c
unicode.c
unix/gtkdlg.c
unix/pterm.c
unix/pty.c
unix/ux_x11.c
unix/uxagentc.c
unix/uxnet.c
unix/uxplink.c
unix/uxprint.c
unix/uxstore.c
wincfg.c
winctrls.c
windlg.c
window.c
winnet.c
winstore.c
winutils.c
x11fwd.c

index 2cad4fa..8b181f6 100644 (file)
--- a/cmdline.c
+++ b/cmdline.c
@@ -43,9 +43,8 @@ static void cmdline_save_param(char *p, char *value, int pri)
 {
     if (saves[pri].nsaved >= saves[pri].savesize) {
        saves[pri].savesize = saves[pri].nsaved + 32;
-       saves[pri].params =
-           srealloc(saves[pri].params,
-                    saves[pri].savesize*sizeof(*saves[pri].params));
+       saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
+                                   struct cmdline_saved_param);
     }
     saves[pri].params[saves[pri].nsaved].p = p;
     saves[pri].params[saves[pri].nsaved].value = value;
@@ -234,7 +233,7 @@ int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
                d = 0;
            if (cmdlen >= cmdsize) {
                cmdsize = cmdlen + 512;
-               command = srealloc(command, cmdsize);
+               command = sresize(command, cmdsize, char);
            }
            command[cmdlen++] = d;
        } while (c != EOF);
index ed2bee6..99a65ee 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1141,7 +1141,7 @@ void setup_config_box(struct controlbox *b, struct sesslist *sesslist,
                                charclass_handler, P(ccd));
     ccd->listbox->listbox.multisel = 1;
     ccd->listbox->listbox.ncols = 4;
-    ccd->listbox->listbox.percentages = smalloc(4*sizeof(int));
+    ccd->listbox->listbox.percentages = snewn(4, int);
     ccd->listbox->listbox.percentages[0] = 15;
     ccd->listbox->listbox.percentages[1] = 25;
     ccd->listbox->listbox.percentages[2] = 20;
@@ -1345,7 +1345,7 @@ void setup_config_box(struct controlbox *b, struct sesslist *sesslist,
                                       environ_handler, P(ed));
            ed->listbox->listbox.height = 3;
            ed->listbox->listbox.ncols = 2;
-           ed->listbox->listbox.percentages = smalloc(2*sizeof(int));
+           ed->listbox->listbox.percentages = snewn(2, int);
            ed->listbox->listbox.percentages[0] = 30;
            ed->listbox->listbox.percentages[1] = 70;
        }
@@ -1525,7 +1525,7 @@ void setup_config_box(struct controlbox *b, struct sesslist *sesslist,
                                    portfwd_handler, P(pfd));
        pfd->listbox->listbox.height = 3;
        pfd->listbox->listbox.ncols = 2;
-       pfd->listbox->listbox.percentages = smalloc(2*sizeof(int));
+       pfd->listbox->listbox.percentages = snewn(2, int);
        pfd->listbox->listbox.percentages[0] = 20;
        pfd->listbox->listbox.percentages[1] = 80;
        ctrl_tabdelay(s, pfd->rembutton);
index e217470..94572db 100644 (file)
--- a/dialog.c
+++ b/dialog.c
@@ -41,7 +41,7 @@ int ctrl_path_compare(char *p1, char *p2)
 
 struct controlbox *ctrl_new_box(void)
 {
-    struct controlbox *ret = smalloc(sizeof(struct controlbox));
+    struct controlbox *ret = snew(struct controlbox);
 
     ret->nctrlsets = ret->ctrlsetsize = 0;
     ret->ctrlsets = NULL;
@@ -128,7 +128,7 @@ struct controlset *ctrl_settitle(struct controlbox *b,
                                 char *path, char *title)
 {
     
-    struct controlset *s = smalloc(sizeof(struct controlset));
+    struct controlset *s = snew(struct controlset);
     int index = ctrl_find_set(b, path, 1);
     s->pathname = dupstr(path);
     s->boxname = NULL;
@@ -138,8 +138,7 @@ struct controlset *ctrl_settitle(struct controlbox *b,
     s->ctrls = NULL;
     if (b->nctrlsets >= b->ctrlsetsize) {
        b->ctrlsetsize = b->nctrlsets + 32;
-       b->ctrlsets = srealloc(b->ctrlsets,
-                              b->ctrlsetsize*sizeof(*b->ctrlsets));
+       b->ctrlsets = sresize(b->ctrlsets, b->ctrlsetsize,struct controlset *);
     }
     if (index < b->nctrlsets)
        memmove(&b->ctrlsets[index+1], &b->ctrlsets[index],
@@ -162,7 +161,7 @@ struct controlset *ctrl_getset(struct controlbox *b,
            return b->ctrlsets[index];
        index++;
     }
-    s = smalloc(sizeof(struct controlset));
+    s = snew(struct controlset);
     s->pathname = dupstr(path);
     s->boxname = dupstr(name);
     s->boxtitle = boxtitle ? dupstr(boxtitle) : NULL;
@@ -171,8 +170,7 @@ struct controlset *ctrl_getset(struct controlbox *b,
     s->ctrls = NULL;
     if (b->nctrlsets >= b->ctrlsetsize) {
        b->ctrlsetsize = b->nctrlsets + 32;
-       b->ctrlsets = srealloc(b->ctrlsets,
-                              b->ctrlsetsize*sizeof(*b->ctrlsets));
+       b->ctrlsets = sresize(b->ctrlsets, b->ctrlsetsize,struct controlset *);
     }
     if (index < b->nctrlsets)
        memmove(&b->ctrlsets[index+1], &b->ctrlsets[index],
@@ -186,10 +184,14 @@ struct controlset *ctrl_getset(struct controlbox *b,
 void *ctrl_alloc(struct controlbox *b, size_t size)
 {
     void *p;
+    /*
+     * This is an internal allocation routine, so it's allowed to
+     * use smalloc directly.
+     */
     p = smalloc(size);
     if (b->nfrees >= b->freesize) {
        b->freesize = b->nfrees + 32;
-       b->frees = srealloc(b->frees, b->freesize*sizeof(*b->frees));
+       b->frees = sresize(b->frees, b->freesize, void *);
     }
     b->frees[b->nfrees++] = p;
     return p;
@@ -199,10 +201,10 @@ static union control *ctrl_new(struct controlset *s, int type,
                               intorptr helpctx, handler_fn handler,
                               intorptr context)
 {
-    union control *c = smalloc(sizeof(union control));
+    union control *c = snew(union control);
     if (s->ncontrols >= s->ctrlsize) {
        s->ctrlsize = s->ncontrols + 32;
-       s->ctrls = srealloc(s->ctrls, s->ctrlsize * sizeof(*s->ctrls));
+       s->ctrls = sresize(s->ctrls, s->ctrlsize, union control *);
     }
     s->ctrls[s->ncontrols++] = c;
     /*
@@ -230,7 +232,7 @@ union control *ctrl_columns(struct controlset *s, int ncolumns, ...)
     } else {
        va_list ap;
        int i;
-       c->columns.percentages = smalloc(ncolumns * sizeof(int));
+       c->columns.percentages = snewn(ncolumns, int);
        va_start(ap, ncolumns);
        for (i = 0; i < ncolumns; i++)
            c->columns.percentages[i] = va_arg(ap, int);
@@ -300,11 +302,11 @@ union control *ctrl_radiobuttons(struct controlset *s, char *label,
     va_end(ap);
     c->radio.nbuttons = i;
     if (c->radio.shortcut == NO_SHORTCUT)
-       c->radio.shortcuts = smalloc(c->radio.nbuttons * sizeof(char));
+       c->radio.shortcuts = snewn(c->radio.nbuttons, char);
     else
        c->radio.shortcuts = NULL;
-    c->radio.buttons = smalloc(c->radio.nbuttons * sizeof(char *));
-    c->radio.buttondata = smalloc(c->radio.nbuttons * sizeof(intorptr));
+    c->radio.buttons = snewn(c->radio.nbuttons, char *);
+    c->radio.buttondata = snewn(c->radio.nbuttons, intorptr);
     /*
      * Second pass along variable argument list to actually fill in
      * the structure.
index d00f31f..1210cc5 100644 (file)
--- a/import.c
+++ b/import.c
@@ -326,7 +326,7 @@ static struct openssh_key *load_openssh_key(const Filename *filename)
     char base64_bit[4];
     int base64_chars = 0;
 
-    ret = smalloc(sizeof(*ret));
+    ret = snew(struct openssh_key);
     ret->keyblob = NULL;
     ret->keyblob_len = ret->keyblob_size = 0;
     ret->encrypted = 0;
@@ -416,7 +416,8 @@ static struct openssh_key *load_openssh_key(const Filename *filename)
 
                     if (ret->keyblob_len + len > ret->keyblob_size) {
                         ret->keyblob_size = ret->keyblob_len + len + 256;
-                        ret->keyblob = srealloc(ret->keyblob, ret->keyblob_size);
+                        ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
+                                              unsigned char);
                     }
 
                     memcpy(ret->keyblob + ret->keyblob_len, out, len);
@@ -564,7 +565,7 @@ struct ssh2_userkey *openssh_read(const Filename *filename, char *passphrase)
      * Space to create key blob in.
      */
     blobsize = 256+key->keyblob_len;
-    blob = smalloc(blobsize);
+    blob = snewn(blobsize, unsigned char);
     PUT_32BIT(blob, 7);
     if (key->type == OSSH_DSA)
        memcpy(blob+4, "ssh-dss", 7);
@@ -636,7 +637,7 @@ struct ssh2_userkey *openssh_read(const Filename *filename, char *passphrase)
      * the sanity checks for free.
      */
     assert(privptr > 0);              /* should have bombed by now if not */
-    retkey = smalloc(sizeof(struct ssh2_userkey));
+    retkey = snew(struct ssh2_userkey);
     retkey->alg = (key->type == OSSH_RSA ? &ssh_rsa : &ssh_dss);
     retkey->data = retkey->alg->createkey(blob, privptr,
                                          blob+privptr, blobptr-privptr);
@@ -719,7 +720,7 @@ int openssh_write(const Filename *filename, struct ssh2_userkey *key,
         dmp1.bytes = (bignum_bitcount(bdmp1)+8)/8;
         dmq1.bytes = (bignum_bitcount(bdmq1)+8)/8;
         sparelen = dmp1.bytes + dmq1.bytes;
-        spareblob = smalloc(sparelen);
+        spareblob = snewn(sparelen, unsigned char);
         dmp1.start = spareblob;
         dmq1.start = spareblob + dmp1.bytes;
         for (i = 0; i < dmp1.bytes; i++)
@@ -791,7 +792,7 @@ int openssh_write(const Filename *filename, struct ssh2_userkey *key,
     /*
      * Now we know how big outblob needs to be. Allocate it.
      */
-    outblob = smalloc(outlen);
+    outblob = snewn(outlen, unsigned char);
 
     /*
      * And write the data into it.
@@ -996,7 +997,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename)
     char base64_bit[4];
     int base64_chars = 0;
 
-    ret = smalloc(sizeof(*ret));
+    ret = snew(struct sshcom_key);
     ret->comment[0] = '\0';
     ret->keyblob = NULL;
     ret->keyblob_len = ret->keyblob_size = 0;
@@ -1072,7 +1073,8 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename)
 
                     if (ret->keyblob_len + len > ret->keyblob_size) {
                         ret->keyblob_size = ret->keyblob_len + len + 256;
-                        ret->keyblob = srealloc(ret->keyblob, ret->keyblob_size);
+                        ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
+                                              unsigned char);
                     }
 
                     memcpy(ret->keyblob + ret->keyblob_len, out, len);
@@ -1333,7 +1335,7 @@ struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase)
      * end up feeding them to alg->createkey().
      */
     blobsize = cipherlen + 256;
-    blob = smalloc(blobsize);
+    blob = snewn(blobsize, unsigned char);
     privlen = 0;
     if (type == RSA) {
         struct mpint_pos n, e, d, u, p, q;
@@ -1391,7 +1393,7 @@ struct ssh2_userkey *sshcom_read(const Filename *filename, char *passphrase)
 
     assert(privlen > 0);              /* should have bombed by now if not */
 
-    retkey = smalloc(sizeof(struct ssh2_userkey));
+    retkey = snew(struct ssh2_userkey);
     retkey->alg = alg;
     retkey->data = alg->createkey(blob, publen, blob+publen, privlen);
     if (!retkey->data) {
@@ -1502,7 +1504,7 @@ int sshcom_write(const Filename *filename, struct ssh2_userkey *key,
     outlen = 512;
     for (i = 0; i < nnumbers; i++)
        outlen += 4 + numbers[i].bytes;
-    outblob = smalloc(outlen);
+    outblob = snewn(outlen, unsigned char);
 
     /*
      * Create the unencrypted key blob.
diff --git a/ldisc.c b/ldisc.c
index 91af0ac..1febff3 100644 (file)
--- a/ldisc.c
+++ b/ldisc.c
@@ -65,7 +65,7 @@ void *ldisc_create(Config *mycfg, Terminal *term,
                   Backend *back, void *backhandle,
                   void *frontend)
 {
-    Ldisc ldisc = smalloc(sizeof(*ldisc));
+    Ldisc ldisc = snew(struct ldisc_tag);
 
     ldisc->buf = NULL;
     ldisc->buflen = 0;
@@ -261,7 +261,7 @@ void ldisc_send(void *handle, char *buf, int len, int interactive)
                 default_case:
                if (ldisc->buflen >= ldisc->bufsiz) {
                    ldisc->bufsiz = ldisc->buflen + 256;
-                   ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
+                   ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
                }
                ldisc->buf[ldisc->buflen++] = c;
                if (ECHOING)
index 2160266..4ac28d7 100644 (file)
@@ -26,7 +26,7 @@ void lpage_send(void *handle,
     }
 
     widesize = len * 2;
-    widebuffer = smalloc(widesize * sizeof(wchar_t));
+    widebuffer = snewn(widesize, wchar_t);
 
     wclen = mb_to_wc(codepage, 0, buf, len, widebuffer, widesize);
     luni_send(ldisc, widebuffer, wclen, interactive);
@@ -44,7 +44,7 @@ void luni_send(void *handle, wchar_t * widebuf, int len, int interactive)
     char *p;
 
     linesize = len * ratio * 2;
-    linebuffer = smalloc(linesize * sizeof(wchar_t));
+    linebuffer = snewn(linesize, char);
 
     if (in_utf(ldisc->term)) {
        /* UTF is a simple algorithm */
index 499747c..d526474 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -163,7 +163,7 @@ void logfclose(void *handle)
 
 void *log_init(void *frontend, Config *cfg)
 {
-    struct LogContext *ctx = smalloc(sizeof(struct LogContext));
+    struct LogContext *ctx = snew(struct LogContext);
     ctx->lgfp = NULL;
     ctx->frontend = frontend;
     ctx->cfg = *cfg;                  /* STRUCTURE COPY */
diff --git a/misc.c b/misc.c
index 8edb41a..3fea374 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -12,7 +12,7 @@
 char *dupstr(const char *s)
 {
     int len = strlen(s);
-    char *p = smalloc(len + 1);
+    char *p = snewn(len + 1, char);
     strcpy(p, s);
     return p;
 }
@@ -34,7 +34,7 @@ char *dupcat(const char *s1, ...)
     }
     va_end(ap);
 
-    p = smalloc(len + 1);
+    p = snewn(len + 1, char);
     strcpy(p, s1);
     q = p + strlen(p);
 
@@ -76,7 +76,7 @@ char *dupvprintf(const char *fmt, va_list ap)
     char *buf;
     int len, size;
 
-    buf = smalloc(512);
+    buf = snewn(512, char);
     size = 512;
 
     while (1) {
@@ -97,7 +97,7 @@ char *dupvprintf(const char *fmt, va_list ap)
             * buffer wasn't big enough, so we enlarge it a bit and hope. */
            size += 512;
        }
-       buf = srealloc(buf, size);
+       buf = sresize(buf, size, char);
     }
 }
 
@@ -190,7 +190,7 @@ void bufchain_add(bufchain *ch, const void *data, int len)
     while (len > 0) {
        int grainlen = min(len, BUFFER_GRANULE);
        struct bufchain_granule *newbuf;
-       newbuf = smalloc(sizeof(struct bufchain_granule));
+       newbuf = snew(struct bufchain_granule);
        newbuf->bufpos = 0;
        newbuf->buflen = grainlen;
        memcpy(newbuf->buf, buf, grainlen);
index 1b282ab..41ae837 100644 (file)
--- a/pageant.c
+++ b/pageant.c
@@ -419,7 +419,7 @@ static void add_keyfile(Filename filename)
                return;
            }
            /* For our purposes we want the blob prefixed with its length */
-           blob2 = smalloc(bloblen+4);
+           blob2 = snewn(bloblen+4, unsigned char);
            PUT_32BIT(blob2, bloblen);
            memcpy(blob2 + 4, blob, bloblen);
            sfree(blob);
@@ -459,7 +459,7 @@ static void add_keyfile(Filename filename)
        needs_pass = ssh2_userkey_encrypted(&filename, &comment);
     attempts = 0;
     if (type == SSH_KEYTYPE_SSH1)
-       rkey = smalloc(sizeof(*rkey));
+       rkey = snew(struct RSAKey);
     pps.passphrase = passphrase;
     pps.comment = comment;
     original_pass = 0;
@@ -532,7 +532,7 @@ static void add_keyfile(Filename filename)
                ssh1_bignum_length(rkey->q) + 4 + clen  /* comment */
                ;
 
-           request = smalloc(reqlen);
+           request = snewn(reqlen, unsigned char);
 
            request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
            reqlen = 5;
@@ -580,7 +580,7 @@ static void add_keyfile(Filename filename)
                4 + clen               /* comment */
                ;
 
-           request = smalloc(reqlen);
+           request = snewn(reqlen, unsigned char);
 
            request[4] = SSH2_AGENTC_ADD_IDENTITY;
            reqlen = 5;
@@ -639,7 +639,7 @@ static void *make_keylist1(int *length)
     }
 
     /* Allocate the buffer. */
-    p = ret = smalloc(len);
+    p = ret = snewn(len, unsigned char);
     if (length) *length = len;
 
     PUT_32BIT(p, nkeys);
@@ -684,7 +684,7 @@ static void *make_keylist2(int *length)
     }
 
     /* Allocate the buffer. */
-    p = ret = smalloc(len);
+    p = ret = snewn(len, unsigned char);
     if (length) *length = len;
 
     /*
@@ -730,7 +730,7 @@ static void *get_keylist1(void)
        if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER)
            return NULL;
 
-       ret = smalloc(resplen-5);
+       ret = snewn(resplen-5, unsigned char);
        memcpy(ret, response+5, resplen-5);
        sfree(response);
     } else {
@@ -761,7 +761,7 @@ static void *get_keylist2(void)
        if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER)
            return NULL;
 
-       ret = smalloc(resplen-5);
+       ret = snewn(resplen-5, unsigned char);
        memcpy(ret, response+5, resplen-5);
        sfree(response);
     } else {
@@ -913,7 +913,7 @@ static void answer_msg(void *msg)
            struct RSAKey *key;
            char *comment;
             int commentlen;
-           key = smalloc(sizeof(struct RSAKey));
+           key = snew(struct RSAKey);
            memset(key, 0, sizeof(struct RSAKey));
            p += makekey(p, key, NULL, 1);
            p += makeprivate(p, key);
@@ -921,7 +921,7 @@ static void answer_msg(void *msg)
            p += ssh1_read_bignum(p, &key->p);  /* p */
            p += ssh1_read_bignum(p, &key->q);  /* q */
             commentlen = GET_32BIT(p);
-           comment = smalloc(commentlen+1);
+           comment = snewn(commentlen+1, char);
            if (comment) {
                memcpy(comment, p + 4, commentlen);
                 comment[commentlen] = '\0';
@@ -949,7 +949,7 @@ static void answer_msg(void *msg)
            int alglen, commlen;
            int bloblen;
 
-           key = smalloc(sizeof(struct ssh2_userkey));
+           key = snew(struct ssh2_userkey);
 
            alglen = GET_32BIT(p);
            p += 4;
@@ -977,7 +977,7 @@ static void answer_msg(void *msg)
            commlen = GET_32BIT(p);
            p += 4;
 
-           comment = smalloc(commlen + 1);
+           comment = snewn(commlen + 1, char);
            if (comment) {
                memcpy(comment, p, commlen);
                comment[commlen] = '\0';
@@ -1222,7 +1222,7 @@ static void prompt_add_keyfile(void)
 {
     OPENFILENAME of;
     char filename[FILENAME_MAX];
-    char *filelist = smalloc(8192);
+    char *filelist = snewn(8192, char);
     char *filewalker;
     int n, dirlen;
        
@@ -1369,7 +1369,7 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
                }
 
                /* get item indices in an array */
-               selectedArray = smalloc(numSelected * sizeof(int));
+               selectedArray = snewn(numSelected, int);
                SendDlgItemMessage(hwnd, 100, LB_GETSELITEMS,
                                numSelected, (WPARAM)selectedArray);
                
index 384abe2..d65d283 100644 (file)
@@ -64,7 +64,7 @@ void agent_query(void *in, int inlen, void **out, int *outlen)
     if (id > 0) {
        retlen = 4 + GET_32BIT(p);
        debug(("len is %d\n", retlen));
-       ret = smalloc(retlen);
+       ret = snewn(retlen, unsigned char);
        if (ret) {
            memcpy(ret, p, retlen);
            *out = ret;
diff --git a/plink.c b/plink.c
index b1bebf9..3848214 100644 (file)
--- a/plink.c
+++ b/plink.c
@@ -410,13 +410,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;
@@ -651,7 +651,7 @@ int main(int argc, char **argv)
            /* Expand the buffer if necessary. */
            if (i > sksize) {
                sksize = i + 16;
-               sklist = srealloc(sklist, sksize * sizeof(*sklist));
+               sklist = sresize(sklist, sksize, SOCKET);
            }
 
            /* Retrieve the sockets into sklist. */
index 1143714..00c6cfc 100644 (file)
--- a/portfwd.c
+++ b/portfwd.c
@@ -132,7 +132,7 @@ char *pfd_newconnect(Socket *s, char *hostname, int port, void *c,
     /*
      * Open socket.
      */
-    pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+    pr = snew(struct PFwdPrivate);
     pr->fn = &fn_table;
     pr->throttled = pr->throttle_override = 0;
     pr->ready = 1;
@@ -168,7 +168,7 @@ static int pfd_accepting(Plug p, void *sock)
     char *err;
 
     org = (struct PFwdPrivate *)p;
-    pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+    pr = snew(struct PFwdPrivate);
     pr->fn = &fn_table;
 
     pr->c = NULL;
@@ -222,7 +222,7 @@ char *pfd_addforward(char *desthost, int destport, char *srcaddr, int port,
     /*
      * Open socket.
      */
-    pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+    pr = snew(struct PFwdPrivate);
     pr->fn = &fn_table;
     pr->c = NULL;
     strcpy(pr->hostname, desthost);
index 17bf6ff..fdd96bf 100644 (file)
@@ -37,7 +37,7 @@ static char *printer_add_enum(int param, char *buffer,
 {
     DWORD needed, nprinters;
 
-    buffer = srealloc(buffer, offset+512);
+    buffer = sresize(buffer, offset+512, char);
 
     /*
      * Exploratory call to EnumPrinters to determine how much space
@@ -50,7 +50,7 @@ static char *printer_add_enum(int param, char *buffer,
     if (needed < 512)
         needed = 512;
 
-    buffer = srealloc(buffer, offset+needed);
+    buffer = sresize(buffer, offset+needed, char);
 
     if (EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset,
                      needed, &needed, &nprinters) == 0)
@@ -63,11 +63,11 @@ static char *printer_add_enum(int param, char *buffer,
 
 printer_enum *printer_start_enum(int *nprinters_ptr)
 {
-    printer_enum *ret = smalloc(sizeof(printer_enum));
+    printer_enum *ret = snew(printer_enum);
     char *buffer = NULL, *retval;
 
     *nprinters_ptr = 0;                       /* default return value */
-    buffer = smalloc(512);
+    buffer = snewn(512, char);
 
     retval = printer_add_enum(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                              buffer, 0, nprinters_ptr);
@@ -107,7 +107,7 @@ void printer_finish_enum(printer_enum *pe)
 
 printer_job *printer_start_job(char *printer)
 {
-    printer_job *ret = smalloc(sizeof(printer_job));
+    printer_job *ret = snew(printer_job);
     DOC_INFO_1 docinfo;
     int jobstarted = 0, pagestarted = 0;
 
diff --git a/proxy.c b/proxy.c
index ccb1b5e..0065b5c 100644 (file)
--- a/proxy.c
+++ b/proxy.c
@@ -378,7 +378,7 @@ Socket new_connection(SockAddr addr, char *hostname,
        SockAddr proxy_addr;
        char *proxy_canonical_name, *err;
 
-       ret = smalloc(sizeof(struct Socket_proxy_tag));
+       ret = snew(struct Socket_proxy_tag);
        ret->fn = &socket_fn_table;
        ret->cfg = *cfg;               /* STRUCTURE COPY */
        ret->plug = plug;
@@ -413,7 +413,7 @@ Socket new_connection(SockAddr addr, char *hostname,
 
        /* create the proxy plug to map calls from the actual
         * socket into our proxy socket layer */
-       pplug = smalloc(sizeof(struct Plug_proxy_tag));
+       pplug = snew(struct Plug_proxy_tag);
        pplug->fn = &plug_fn_table;
        pplug->proxy_socket = ret;
 
@@ -579,7 +579,7 @@ int proxy_http_negotiate (Proxy_Socket p, int change)
            /* get the status line */
            len = bufchain_size(&p->pending_input_data);
            assert(len > 0);           /* or we wouldn't be here */
-           data = smalloc(len);
+           data = snewn(len, char);
            bufchain_fetch(&p->pending_input_data, data, len);
 
            eol = get_line_end(data, len);
@@ -627,7 +627,7 @@ int proxy_http_negotiate (Proxy_Socket p, int change)
 
            len = bufchain_size(&p->pending_input_data);
            assert(len > 0);           /* or we wouldn't be here */
-           data = smalloc(len);
+           data = snewn(len, char);
            datap = data;
            bufchain_fetch(&p->pending_input_data, data, len);
 
@@ -702,7 +702,7 @@ int proxy_socks4_negotiate (Proxy_Socket p, int change)
        }
 
        length = strlen(p->cfg.proxy_username) + namelen + 9;
-       command = (char*) smalloc(length);
+       command = snewn(length, char);
        strcpy(command + 8, p->cfg.proxy_username);
 
        command[0] = 4; /* version 4 */
diff --git a/psftp.c b/psftp.c
index 3fdfa45..b9f6370 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -245,8 +245,7 @@ int sftp_cmd_ls(struct sftp_command *cmd)
 
            if (nnames + names->nnames >= namesize) {
                namesize += names->nnames + 128;
-               ournames =
-                   srealloc(ournames, namesize * sizeof(*ournames));
+               ournames = sresize(ournames, namesize, struct fxp_name *);
            }
 
            for (i = 0; i < names->nnames; i++)
@@ -934,10 +933,10 @@ static int sftp_cmd_lcd(struct sftp_command *cmd)
        return 0;
     }
 
-    currdir = smalloc(256);
+    currdir = snewn(256, char);
     len = GetCurrentDirectory(256, currdir);
     if (len > 256)
-       currdir = srealloc(currdir, len);
+       currdir = sresize(currdir, len, char);
     GetCurrentDirectory(len, currdir);
     printf("New local directory is %s\n", currdir);
     sfree(currdir);
@@ -950,10 +949,10 @@ static int sftp_cmd_lpwd(struct sftp_command *cmd)
     char *currdir;
     int len;
 
-    currdir = smalloc(256);
+    currdir = snewn(256, char);
     len = GetCurrentDirectory(256, currdir);
     if (len > 256)
-       currdir = srealloc(currdir, len);
+       currdir = sresize(currdir, len, char);
     GetCurrentDirectory(len, currdir);
     printf("Current local directory is %s\n", currdir);
     sfree(currdir);
@@ -1254,7 +1253,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
     }
     fflush(stdout);
 
-    cmd = smalloc(sizeof(struct sftp_command));
+    cmd = snew(struct sftp_command);
     cmd->words = NULL;
     cmd->nwords = 0;
     cmd->wordssize = 0;
@@ -1266,7 +1265,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
        char *ret;
 
        linesize += 512;
-       line = srealloc(line, linesize);
+       line = sresize(line, linesize, char);
        ret = fgets(line + linelen, linesize - linelen, fp);
 
        if (!ret || (linelen == 0 && line[0] == '\0')) {
@@ -1298,7 +1297,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
         * containing everything else on the line.
         */
        cmd->nwords = cmd->wordssize = 2;
-       cmd->words = srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+       cmd->words = sresize(cmd->words, cmd->wordssize, char *);
        cmd->words[0] = "!";
        cmd->words[1] = p+1;
     } else {
@@ -1341,8 +1340,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
            *r = '\0';
            if (cmd->nwords >= cmd->wordssize) {
                cmd->wordssize = cmd->nwords + 16;
-               cmd->words =
-                   srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+               cmd->words = sresize(cmd->words, cmd->wordssize, char *);
            }
            cmd->words[cmd->nwords++] = q;
        }
@@ -1564,10 +1562,7 @@ int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
     if (len > 0) {
        if (pendsize < pendlen + len) {
            pendsize = pendlen + len + 4096;
-           pending = (pending ? srealloc(pending, pendsize) :
-                      smalloc(pendsize));
-           if (!pending)
-               fatalbox("Out of memory");
+           pending = sresize(pending, pendsize, unsigned char);
        }
        memcpy(pending + pendlen, p, len);
        pendlen += len;
index d85f4cc..e16b4f3 100644 (file)
@@ -389,8 +389,8 @@ static void setupbigedit2(HWND hwnd, int id, int idstatic,
     int i;
 
     pub_blob = key->alg->public_blob(key->data, &pub_len);
-    buffer = smalloc(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
-                    strlen(key->comment) + 3);
+    buffer = snewn(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
+                  strlen(key->comment) + 3, char);
     strcpy(buffer, key->alg->name);
     p = buffer + strlen(buffer);
     *p++ = ' ';
@@ -820,7 +820,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
        SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
                    (LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(200)));
 
-       state = smalloc(sizeof(*state));
+       state = snew(struct MainDlgState);
        state->generation_thread_exists = FALSE;
        state->collecting_entropy = FALSE;
        state->entropy = NULL;
@@ -974,7 +974,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                                   MAKELPARAM(0, PROGRESSRANGE));
                SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
 
-               params = smalloc(sizeof(*params));
+               params = snew(struct rsa_key_thread_params);
                params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
                params->dialog = hwnd;
                params->keysize = state->keysize;
@@ -1021,7 +1021,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                    int len = GetWindowTextLength(editctl);
                    if (*state->commentptr)
                        sfree(*state->commentptr);
-                   *state->commentptr = smalloc(len + 1);
+                   *state->commentptr = snewn(len + 1, char);
                    GetWindowText(editctl, *state->commentptr, len + 1);
                    if (state->ssh2) {
                        setupbigedit2(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
@@ -1096,8 +1096,8 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                state->entropy_required = (state->keysize / 2) * 2;
                state->entropy_got = 0;
                state->entropy_size = (state->entropy_required *
-                                      sizeof(*state->entropy));
-               state->entropy = smalloc(state->entropy_size);
+                                      sizeof(unsigned));
+               state->entropy = snewn(state->entropy_required, unsigned);
 
                SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
                                   MAKELPARAM(0, state->entropy_required));
@@ -1270,7 +1270,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
         * the user will immediately want to change it, which is
         * what we want :-)
         */
-       *state->commentptr = smalloc(30);
+       *state->commentptr = snewn(30, char);
        {
            time_t t;
            struct tm *tm;
index 938e340..9fe272f 100644 (file)
@@ -25,13 +25,14 @@ void *safemalloc(size_t);
 void *saferealloc(void *, size_t);
 void safefree(void *);
 
-
-/* smalloc a thing */
-#define smalloca(type) ((type *) smalloc (sizeof (type)))
-/* smalloc a copy of a thing */
-#define smallocc(ptr) memcpy (smalloc (sizeof (*ptr)), ptr, sizeof (*ptr))
-/* smalloc n things */
-#define smallocn(n,type) ((type *) smalloc ((n) * sizeof (type)))
-
+/*
+ * Direct use of smalloc within the code should be avoided where
+ * possible, in favour of these type-casting macros which ensure
+ * you don't mistakenly allocate enough space for one sort of
+ * structure and assign it to a different sort of pointer.
+ */
+#define snew(type) ((type *)smalloc(sizeof(type)))
+#define snewn(n, type) ((type *)smalloc((n)*sizeof(type)))
+#define sresize(ptr, n, type) ((type *)srealloc(ptr, (n)*sizeof(type)))
 
 #endif
diff --git a/raw.c b/raw.c
index a22314f..53a8fc3 100644 (file)
--- a/raw.c
+++ b/raw.c
@@ -80,7 +80,7 @@ static char *raw_init(void *frontend_handle, void **backend_handle,
     char *err;
     Raw raw;
 
-    raw = smalloc(sizeof(*raw));
+    raw = snew(struct raw_backend_data);
     raw->fn = &fn_table;
     raw->s = NULL;
     *backend_handle = raw;
index 7b13a78..c3ef25c 100644 (file)
--- a/rlogin.c
+++ b/rlogin.c
@@ -110,7 +110,7 @@ static char *rlogin_init(void *frontend_handle, void **backend_handle,
     char *err;
     Rlogin rlogin;
 
-    rlogin = smalloc(sizeof(*rlogin));
+    rlogin = snew(struct rlogin_tag);
     rlogin->fn = &fn_table;
     rlogin->s = NULL;
     rlogin->frontend = frontend_handle;
diff --git a/scp.c b/scp.c
index 40d05df..8f21a7f 100644 (file)
--- a/scp.c
+++ b/scp.c
@@ -353,8 +353,7 @@ int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
     if (len > 0) {
        if (pendsize < pendlen + len) {
            pendsize = pendlen + len + 4096;
-           pending = (pending ? srealloc(pending, pendsize) :
-                      smalloc(pendsize));
+           pending = sresize(pending, pendsize, unsigned char);
            if (!pending)
                fatalbox("Out of memory");
        }
@@ -545,7 +544,7 @@ static void do_cmd(char *host, char *user, char *cmd)
        namelen = 0;
        if (GetUserName(user, &namelen) == FALSE)
            bump("Empty user name");
-       user = smalloc(namelen * sizeof(char));
+       user = snewn(namelen, char);
        GetUserName(user, &namelen);
        if (verbose)
            tell_user(stderr, "Guessing user name: %s", user);
@@ -777,8 +776,7 @@ void scp_sftp_listdir(char *dirname)
 
            if (nnames + names->nnames >= namesize) {
                namesize += names->nnames + 128;
-               ournames =
-                   srealloc(ournames, namesize * sizeof(*ournames));
+               ournames = sresize(ournames, namesize, struct fxp_name);
            }
 
            for (i = 0; i < names->nnames; i++)
@@ -1064,7 +1062,7 @@ int scp_sink_setup(char *source, int preserve, int recursive)
         * wildcardness comes before the final slash) and arrange
         * things so that a dirstack entry will be set up.
         */
-       newsource = smalloc(1+strlen(source));
+       newsource = snewn(1+strlen(source), char);
        if (!wc_unescape(newsource, source)) {
            /* Yes, here we go; it's a wildcard. Bah. */
            char *dupsource, *lastpart, *dirpart, *wildcard;
@@ -1097,7 +1095,7 @@ int scp_sink_setup(char *source, int preserve, int recursive)
             * wildcard escapes from the directory part, throwing
             * an error if it contains a real wildcard.
             */
-           dirpart = smalloc(1+strlen(dupsource));
+           dirpart = snewn(1+strlen(dupsource), char);
            if (!wc_unescape(dirpart, dupsource)) {
                tell_user(stderr, "%s: multiple-level wildcards unsupported",
                          source);
@@ -1306,8 +1304,7 @@ int scp_get_sink_action(struct scp_sink_action *act)
                }
                if (nnames + names->nnames >= namesize) {
                    namesize += names->nnames + 128;
-                   ournames =
-                       srealloc(ournames, namesize * sizeof(*ournames));
+                   ournames = sresize(ournames, namesize, struct fxp_name);
                }
                for (i = 0; i < names->nnames; i++)
                    ournames[nnames++] = names->names[i];
@@ -1316,7 +1313,7 @@ int scp_get_sink_action(struct scp_sink_action *act)
            }
            fxp_close(dirhandle);
 
-           newitem = smalloc(sizeof(struct scp_sftp_dirstack));
+           newitem = snew(struct scp_sftp_dirstack);
            newitem->next = scp_sftp_dirstack_head;
            newitem->names = ournames;
            newitem->namepos = 0;
@@ -1404,7 +1401,7 @@ int scp_get_sink_action(struct scp_sink_action *act)
                    bump("Lost connection");
                if (i >= bufsize) {
                    bufsize = i + 128;
-                   act->buf = srealloc(act->buf, bufsize);
+                   act->buf = sresize(act->buf, bufsize, char);
                }
                act->buf[i++] = ch;
            } while (ch != '\n');
@@ -2080,7 +2077,7 @@ static void get_dir_list(int argc, char *argv[])
            user = NULL;
     }
 
-    cmd = smalloc(4 * strlen(src) + 100);
+    cmd = snewn(4 * strlen(src) + 100, char);
     strcpy(cmd, "ls -la '");
     p = cmd + strlen(cmd);
     for (q = src; *q; q++) {
index 2d6a1fb..f633d31 100644 (file)
@@ -672,7 +672,7 @@ void get_sesslist(struct sesslist *list, int allocate)
                    int len = strlen(otherbuf) + 1;
                    if (bufsize < buflen + len) {
                        bufsize = buflen + len + 2048;
-                       list->buffer = srealloc(list->buffer, bufsize);
+                       list->buffer = sresize(list->buffer, bufsize, char);
                    }
                    strcpy(list->buffer + buflen, otherbuf);
                    buflen += strlen(list->buffer + buflen) + 1;
@@ -680,7 +680,7 @@ void get_sesslist(struct sesslist *list, int allocate)
            } while (ret);
            enum_settings_finish(handle);
        }
-       list->buffer = srealloc(list->buffer, buflen + 1);
+       list->buffer = sresize(list->buffer, buflen + 1, char);
        list->buffer[buflen] = '\0';
 
        /*
@@ -699,7 +699,7 @@ void get_sesslist(struct sesslist *list, int allocate)
            p++;
        }
 
-       list->sessions = smalloc((list->nsessions + 1) * sizeof(char *));
+       list->sessions = snewn(list->nsessions + 1, char *);
        list->sessions[0] = "Default Settings";
        p = list->buffer;
        i = 1;
diff --git a/sftp.c b/sftp.c
index 6d78c2c..b49e275 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -40,7 +40,7 @@ static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
 {
     if (pkt->maxlen < length) {
        pkt->maxlen = length + 256;
-       pkt->data = srealloc(pkt->data, pkt->maxlen);
+       pkt->data = sresize(pkt->data, pkt->maxlen, char);
     }
 }
 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
@@ -56,7 +56,7 @@ static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
 static struct sftp_packet *sftp_pkt_init(int pkt_type)
 {
     struct sftp_packet *pkt;
-    pkt = smalloc(sizeof(struct sftp_packet));
+    pkt = snew(struct sftp_packet);
     pkt->data = NULL;
     pkt->savedpos = -1;
     pkt->length = 0;
@@ -228,10 +228,10 @@ struct sftp_packet *sftp_recv(void)
     if (!sftp_recvdata(x, 4))
        return NULL;
 
-    pkt = smalloc(sizeof(struct sftp_packet));
+    pkt = snew(struct sftp_packet);
     pkt->savedpos = 0;
     pkt->length = pkt->maxlen = GET_32BIT(x);
-    pkt->data = smalloc(pkt->length);
+    pkt->data = snewn(pkt->length, char);
 
     if (!sftp_recvdata(pkt->data, pkt->length)) {
        sftp_pkt_free(pkt);
@@ -249,7 +249,7 @@ struct sftp_packet *sftp_recv(void)
 
 static char *mkstr(char *s, int len)
 {
-    char *p = smalloc(len + 1);
+    char *p = snewn(len + 1, char);
     memcpy(p, s, len);
     p[len] = '\0';
     return p;
@@ -444,7 +444,7 @@ struct fxp_handle *fxp_open(char *path, int type)
             sftp_pkt_free(pktin);
            return NULL;
        }
-       handle = smalloc(sizeof(struct fxp_handle));
+       handle = snew(struct fxp_handle);
        handle->hstring = mkstr(hstring, len);
        handle->hlen = len;
        sftp_pkt_free(pktin);
@@ -490,7 +490,7 @@ struct fxp_handle *fxp_opendir(char *path)
             sftp_pkt_free(pktin);
            return NULL;
        }
-       handle = smalloc(sizeof(struct fxp_handle));
+       handle = snew(struct fxp_handle);
        handle->hstring = mkstr(hstring, len);
        handle->hlen = len;
        sftp_pkt_free(pktin);
@@ -855,9 +855,9 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle)
     if (pktin->type == SSH_FXP_NAME) {
        struct fxp_names *ret;
        int i;
-       ret = smalloc(sizeof(struct fxp_names));
+       ret = snew(struct fxp_names);
        ret->nnames = sftp_pkt_getuint32(pktin);
-       ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
+       ret->names = snewn(ret->nnames, struct fxp_name);
        for (i = 0; i < ret->nnames; i++) {
            char *str;
            int len;
@@ -930,7 +930,7 @@ void fxp_free_names(struct fxp_names *names)
 struct fxp_name *fxp_dup_name(struct fxp_name *name)
 {
     struct fxp_name *ret;
-    ret = smalloc(sizeof(struct fxp_name));
+    ret = snew(struct fxp_name);
     ret->filename = dupstr(name->filename);
     ret->longname = dupstr(name->longname);
     ret->attrs = name->attrs;         /* structure copy */
index ab709b5..320fa0a 100644 (file)
--- a/sizetip.c
+++ b/sizetip.c
@@ -42,7 +42,7 @@ static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
            Rectangle(hdc, cr.left, cr.top, cr.right, cr.bottom);
 
            wtlen = GetWindowTextLength(hWnd);
-           wt = (LPTSTR) smalloc((wtlen + 1) * sizeof(TCHAR));
+           wt = (LPTSTR) snewn(wtlen + 1, TCHAR);
            GetWindowText(hWnd, wt, wtlen + 1);
 
            SetTextColor(hdc, tip_text);
diff --git a/ssh.c b/ssh.c
index 662d521..25dee28 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -292,7 +292,7 @@ enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM };
 #define crBegin(v)     { int *crLine = &v; switch(v) { case 0:;
 #define crState(t) \
     struct t *s; \
-    if (!ssh->t) ssh->t = smalloc(sizeof(struct t)); \
+    if (!ssh->t) ssh->t = snew(struct t); \
     s = ssh->t;
 #define crFinish(z)    } *crLine = 0; return (z); }
 #define crFinishV      } *crLine = 0; return; }
@@ -814,7 +814,8 @@ static int ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
 
     if (ssh->pktin.maxlen < st->biglen) {
        ssh->pktin.maxlen = st->biglen;
-       ssh->pktin.data = srealloc(ssh->pktin.data, st->biglen + APIEXTRA);
+       ssh->pktin.data = sresize(ssh->pktin.data, st->biglen + APIEXTRA,
+                                 unsigned char);
     }
 
     st->to_read = st->biglen;
@@ -859,8 +860,9 @@ static int ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
 
        if (ssh->pktin.maxlen < st->pad + decomplen) {
            ssh->pktin.maxlen = st->pad + decomplen;
-           ssh->pktin.data = srealloc(ssh->pktin.data,
-                                      ssh->pktin.maxlen + APIEXTRA);
+           ssh->pktin.data = sresize(ssh->pktin.data,
+                                     ssh->pktin.maxlen + APIEXTRA,
+                                     unsigned char);
            ssh->pktin.body = ssh->pktin.data + st->pad + 1;
        }
 
@@ -942,7 +944,8 @@ static int ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
 
     if (ssh->pktin.maxlen < st->cipherblk) {
        ssh->pktin.maxlen = st->cipherblk;
-       ssh->pktin.data = srealloc(ssh->pktin.data, st->cipherblk + APIEXTRA);
+       ssh->pktin.data = sresize(ssh->pktin.data, st->cipherblk + APIEXTRA,
+                                 unsigned char);
     }
 
     /*
@@ -993,8 +996,9 @@ static int ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
      */
     if (ssh->pktin.maxlen < st->packetlen + st->maclen) {
        ssh->pktin.maxlen = st->packetlen + st->maclen;
-       ssh->pktin.data = srealloc(ssh->pktin.data,
-                                  ssh->pktin.maxlen + APIEXTRA);
+       ssh->pktin.data = sresize(ssh->pktin.data,
+                                 ssh->pktin.maxlen + APIEXTRA,
+                                 unsigned char);
     }
 
     /*
@@ -1036,8 +1040,9 @@ static int ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
                                    &newpayload, &newlen)) {
            if (ssh->pktin.maxlen < newlen + 5) {
                ssh->pktin.maxlen = newlen + 5;
-               ssh->pktin.data = srealloc(ssh->pktin.data,
-                                          ssh->pktin.maxlen + APIEXTRA);
+               ssh->pktin.data = sresize(ssh->pktin.data,
+                                         ssh->pktin.maxlen + APIEXTRA,
+                                         unsigned char);
            }
            ssh->pktin.length = 5 + newlen;
            memcpy(ssh->pktin.data + 5, newpayload, newlen);
@@ -1170,9 +1175,11 @@ static void ssh1_pktout_size(Ssh ssh, int len)
 #ifdef MSCRYPTOAPI
        /* Allocate enough buffer space for extra block
         * for MS CryptEncrypt() */
-       ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 12);
+       ssh->pktout.data = sresize(ssh->pktout.data, biglen + 12,
+                                  unsigned char);
 #else
-       ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 4);
+       ssh->pktout.data = sresize(ssh->pktout.data, biglen + 4,
+                                  unsigned char);
 #endif
     }
     ssh->pktout.body = ssh->pktout.data + 4 + pad + 1;
@@ -1248,8 +1255,9 @@ static void s_wrpkt_defer(Ssh ssh)
     len = s_wrpkt_prepare(ssh);
     if (ssh->deferred_len + len > ssh->deferred_size) {
        ssh->deferred_size = ssh->deferred_len + len + 128;
-       ssh->deferred_send_data = srealloc(ssh->deferred_send_data,
-                                          ssh->deferred_size);
+       ssh->deferred_send_data = sresize(ssh->deferred_send_data,
+                                         ssh->deferred_size,
+                                         unsigned char);
     }
     memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len);
     ssh->deferred_len += len;
@@ -1396,8 +1404,9 @@ static void ssh2_pkt_ensure(Ssh ssh, int length)
 {
     if (ssh->pktout.maxlen < length) {
        ssh->pktout.maxlen = length + 256;
-       ssh->pktout.data = srealloc(ssh->pktout.data,
-                                   ssh->pktout.maxlen + APIEXTRA);
+       ssh->pktout.data = sresize(ssh->pktout.data,
+                                  ssh->pktout.maxlen + APIEXTRA,
+                                  unsigned char);
        if (!ssh->pktout.data)
            fatalbox("Out of memory");
     }
@@ -1453,7 +1462,7 @@ static unsigned char *ssh2_mpint_fmt(Bignum b, int *len)
 {
     unsigned char *p;
     int i, n = (bignum_bitcount(b) + 7) / 8;
-    p = smalloc(n + 1);
+    p = snewn(n + 1, unsigned char);
     if (!p)
        fatalbox("out of memory");
     p[0] = 0;
@@ -1564,8 +1573,9 @@ static void ssh2_pkt_defer(Ssh ssh)
     int len = ssh2_pkt_construct(ssh);
     if (ssh->deferred_len + len > ssh->deferred_size) {
        ssh->deferred_size = ssh->deferred_len + len + 128;
-       ssh->deferred_send_data = srealloc(ssh->deferred_send_data,
-                                          ssh->deferred_size);
+       ssh->deferred_send_data = sresize(ssh->deferred_send_data,
+                                         ssh->deferred_size,
+                                         unsigned char);
     }
     memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len);
     ssh->deferred_len += len;
@@ -1876,7 +1886,7 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
     }
 
     s->vstrsize = 16;
-    s->vstring = smalloc(s->vstrsize);
+    s->vstring = snewn(s->vstrsize, char);
     strcpy(s->vstring, "SSH-");
     s->vslen = 4;
     s->i = 0;
@@ -1884,7 +1894,7 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
        crReturn(1);                   /* get another char */
        if (s->vslen >= s->vstrsize - 1) {
            s->vstrsize += 16;
-           s->vstring = srealloc(s->vstring, s->vstrsize);
+           s->vstring = sresize(s->vstring, s->vstrsize, char);
        }
        s->vstring[s->vslen++] = c;
        if (s->i >= 0) {
@@ -1904,7 +1914,7 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
     s->vstring[strcspn(s->vstring, "\r\n")] = '\0';/* remove EOL chars */
     {
        char *vlog;
-       vlog = smalloc(20 + s->vslen);
+       vlog = snewn(20 + s->vslen, char);
        sprintf(vlog, "Server version: %s", s->vstring);
        logevent(vlog);
        sfree(vlog);
@@ -2083,7 +2093,7 @@ static char *connect_to_host(Ssh ssh, char *host, int port,
     SockAddr addr;
     char *err;
 
-    ssh->savedhost = smalloc(1 + strlen(host));
+    ssh->savedhost = snewn(1 + strlen(host), char);
     if (!ssh->savedhost)
        fatalbox("Out of memory");
     strcpy(ssh->savedhost, host);
@@ -2326,7 +2336,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt)
 
     s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
 
-    s->rsabuf = smalloc(s->len);
+    s->rsabuf = snewn(s->len, unsigned char);
     if (!s->rsabuf)
        fatalbox("Out of memory");
 
@@ -2339,7 +2349,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt)
         */
        int len = rsastr_len(&hostkey);
        char fingerprint[100];
-       char *keystr = smalloc(len);
+       char *keystr = snewn(len, char);
        if (!keystr)
            fatalbox("Out of memory");
        rsastr_fmt(keystr, &hostkey);
@@ -2577,7 +2587,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                        len += ssh1_bignum_length(s->challenge);
                        len += 16;     /* session id */
                        len += 4;      /* response format */
-                       agentreq = smalloc(4 + len);
+                       agentreq = snewn(4 + len, char);
                        PUT_32BIT(agentreq, len);
                        q = agentreq + 4;
                        *q++ = SSH1_AGENTC_RSA_CHALLENGE;
@@ -2892,7 +2902,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt)
 
                    assert(pwlen >= bottom && pwlen <= top);
 
-                   randomstr = smalloc(top + 1);
+                   randomstr = snewn(top + 1, char);
 
                    for (i = bottom; i <= top; i++) {
                        if (i == pwlen)
@@ -3182,7 +3192,7 @@ static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                              dserv, "(", dport, dserv, ")");
                } else {
                    struct ssh_rportfwd *pf;
-                   pf = smalloc(sizeof(*pf));
+                   pf = snew(struct ssh_rportfwd);
                    strcpy(pf->dhost, host);
                    pf->dport = dport;
                    if (saddr) {
@@ -3330,7 +3340,7 @@ static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                                PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END);
                    logevent("Rejected X11 connect request");
                } else {
-                   c = smalloc(sizeof(struct ssh_channel));
+                   c = snew(struct ssh_channel);
                    c->ssh = ssh;
 
                    if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c,
@@ -3365,7 +3375,7 @@ static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                    send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
                                PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END);
                } else {
-                   c = smalloc(sizeof(struct ssh_channel));
+                   c = snew(struct ssh_channel);
                    c->ssh = ssh;
                    c->remoteid = GET_32BIT(ssh->pktin.body);
                    c->localid = alloc_channel_id(ssh);
@@ -3386,7 +3396,7 @@ static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                int hostsize, port;
                char host[256], buf[1024];
                char *p, *h, *e;
-               c = smalloc(sizeof(struct ssh_channel));
+               c = snew(struct ssh_channel);
                c->ssh = ssh;
 
                hostsize = GET_32BIT(ssh->pktin.body+4);
@@ -3542,7 +3552,8 @@ static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                            if (c->u.a.lensofar == 4) {
                                c->u.a.totallen =
                                    4 + GET_32BIT(c->u.a.msglen);
-                               c->u.a.message = smalloc(c->u.a.totallen);
+                               c->u.a.message = snewn(c->u.a.totallen,
+                                                      unsigned char);
                                memcpy(c->u.a.message, c->u.a.msglen, 4);
                            }
                            if (c->u.a.lensofar >= 4 && len > 0) {
@@ -4682,7 +4693,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                        s->len += 4 + s->pklen; /* key blob */
                        s->len += 4 + s->siglen;        /* data to sign */
                        s->len += 4;      /* flags */
-                       s->agentreq = smalloc(4 + s->len);
+                       s->agentreq = snewn(4 + s->len, char);
                        PUT_32BIT(s->agentreq, s->len);
                        s->q = s->agentreq + 4;
                        *s->q++ = SSH2_AGENTC_SIGN_REQUEST;
@@ -4973,7 +4984,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                    sigdata_len = ssh->pktout.length - 5 + 4 + 20;
                     if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)
                         sigdata_len -= 4;
-                   sigdata = smalloc(sigdata_len);
+                   sigdata = snewn(sigdata_len, char);
                     p = 0;
                     if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {
                         PUT_32BIT(sigdata+p, 20);
@@ -5105,7 +5116,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
      * So now create a channel with a session in it.
      */
     ssh->channels = newtree234(ssh_channelcmp);
-    ssh->mainchan = smalloc(sizeof(struct ssh_channel));
+    ssh->mainchan = snew(struct ssh_channel);
     ssh->mainchan->ssh = ssh;
     ssh->mainchan->localid = alloc_channel_id(ssh);
     ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN);
@@ -5262,7 +5273,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                              dserv, "(", dport, dserv, ")");
                } else {
                    struct ssh_rportfwd *pf;
-                   pf = smalloc(sizeof(*pf));
+                   pf = snew(struct ssh_rportfwd);
                    strcpy(pf->dhost, host);
                    pf->dport = dport;
                    pf->sport = sport;
@@ -5530,7 +5541,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                            if (c->u.a.lensofar == 4) {
                                c->u.a.totallen =
                                    4 + GET_32BIT(c->u.a.msglen);
-                               c->u.a.message = smalloc(c->u.a.totallen);
+                               c->u.a.message = snewn(c->u.a.totallen,
+                                                      unsigned char);
                                memcpy(c->u.a.message, c->u.a.msglen, 4);
                            }
                            if (c->u.a.lensofar >= 4 && length > 0) {
@@ -5794,7 +5806,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                struct ssh_channel *c;
                unsigned remid, winsize, pktsize;
                ssh2_pkt_getstring(ssh, &type, &typelen);
-               c = smalloc(sizeof(struct ssh_channel));
+               c = snew(struct ssh_channel);
                c->ssh = ssh;
 
                remid = ssh2_pkt_getuint32(ssh);
@@ -5804,7 +5816,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt)
                port = ssh2_pkt_getuint32(ssh);
 
                if (typelen == 3 && !memcmp(type, "x11", 3)) {
-                   char *addrstr = smalloc(peeraddrlen+1);
+                   char *addrstr = snewn(peeraddrlen+1, char);
                    memcpy(addrstr, peeraddr, peeraddrlen);
                    peeraddr[peeraddrlen] = '\0';
 
@@ -5947,7 +5959,7 @@ static char *ssh_init(void *frontend_handle, void **backend_handle,
     char *p;
     Ssh ssh;
 
-    ssh = smalloc(sizeof(*ssh));
+    ssh = snew(struct ssh_tag);
     ssh->cfg = *cfg;                  /* STRUCTURE COPY */
     ssh->s = NULL;
     ssh->cipher = NULL;
@@ -6241,7 +6253,7 @@ void *new_sock_channel(void *handle, Socket s)
 {
     Ssh ssh = (Ssh) handle;
     struct ssh_channel *c;
-    c = smalloc(sizeof(struct ssh_channel));
+    c = snew(struct ssh_channel);
     c->ssh = ssh;
 
     if (c) {
index 995d2ca..09f5b39 100644 (file)
--- a/sshaes.c
+++ b/sshaes.c
@@ -1085,7 +1085,7 @@ static void aes_decrypt_cbc(unsigned char *blk, int len, AESContext * ctx)
 
 static void *aes_make_context(void)
 {
-    return smalloc(sizeof(AESContext));
+    return snew(AESContext);
 }
 
 static void aes_free_context(void *handle)
index 6bbe7d2..ca04c09 100644 (file)
@@ -478,13 +478,13 @@ static void blowfish_setkey(BlowfishContext * ctx,
 
 static void *blowfish_make_context(void)
 {
-    return smalloc(sizeof(BlowfishContext));
+    return snew(BlowfishContext);
 }
 
 static void *blowfish_ssh1_make_context(void)
 {
     /* In SSH1, need one key for each direction */
-    return smalloc(2*sizeof(BlowfishContext));
+    return snewn(2, BlowfishContext);
 }
 
 static void blowfish_free_context(void *handle)
diff --git a/sshbn.c b/sshbn.c
index 22ed5dc..d404ed0 100644 (file)
--- a/sshbn.c
+++ b/sshbn.c
@@ -34,7 +34,7 @@ Bignum Zero = bnZero, One = bnOne;
 
 static Bignum newbn(int length)
 {
-    Bignum b = smalloc((length + 1) * sizeof(unsigned short));
+    Bignum b = snewn(length + 1, unsigned short);
     if (!b)
        abort();                       /* FIXME */
     memset(b, 0, (length + 1) * sizeof(*b));
@@ -50,7 +50,7 @@ void bn_restore_invariant(Bignum b)
 
 Bignum copybn(Bignum orig)
 {
-    Bignum b = smalloc((orig[0] + 1) * sizeof(unsigned short));
+    Bignum b = snewn(orig[0] + 1, unsigned short);
     if (!b)
        abort();                       /* FIXME */
     memcpy(b, orig, (orig[0] + 1) * sizeof(*b));
@@ -216,7 +216,7 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
     /* Allocate m of size mlen, copy mod to m */
     /* We use big endian internally */
     mlen = mod[0];
-    m = smalloc(mlen * sizeof(unsigned short));
+    m = snewn(mlen, unsigned short);
     for (j = 0; j < mlen; j++)
        m[j] = mod[mod[0] - j];
 
@@ -231,7 +231,7 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
     }
 
     /* Allocate n of size mlen, copy base to n */
-    n = smalloc(mlen * sizeof(unsigned short));
+    n = snewn(mlen, unsigned short);
     i = mlen - base[0];
     for (j = 0; j < i; j++)
        n[j] = 0;
@@ -239,8 +239,8 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
        n[i + j] = base[base[0] - j];
 
     /* Allocate a and b of size 2*mlen. Set a = 1 */
-    a = smalloc(2 * mlen * sizeof(unsigned short));
-    b = smalloc(2 * mlen * sizeof(unsigned short));
+    a = snewn(2 * mlen, unsigned short);
+    b = snewn(2 * mlen, unsigned short);
     for (i = 0; i < 2 * mlen; i++)
        a[i] = 0;
     a[2 * mlen - 1] = 1;
@@ -325,7 +325,7 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
     /* Allocate m of size mlen, copy mod to m */
     /* We use big endian internally */
     mlen = mod[0];
-    m = smalloc(mlen * sizeof(unsigned short));
+    m = snewn(mlen, unsigned short);
     for (j = 0; j < mlen; j++)
        m[j] = mod[mod[0] - j];
 
@@ -342,7 +342,7 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
     pqlen = (p[0] > q[0] ? p[0] : q[0]);
 
     /* Allocate n of size pqlen, copy p to n */
-    n = smalloc(pqlen * sizeof(unsigned short));
+    n = snewn(pqlen, unsigned short);
     i = pqlen - p[0];
     for (j = 0; j < i; j++)
        n[j] = 0;
@@ -350,7 +350,7 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
        n[i + j] = p[p[0] - j];
 
     /* Allocate o of size pqlen, copy q to o */
-    o = smalloc(pqlen * sizeof(unsigned short));
+    o = snewn(pqlen, unsigned short);
     i = pqlen - q[0];
     for (j = 0; j < i; j++)
        o[j] = 0;
@@ -358,7 +358,7 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
        o[i + j] = q[q[0] - j];
 
     /* Allocate a of size 2*pqlen for result */
-    a = smalloc(2 * pqlen * sizeof(unsigned short));
+    a = snewn(2 * pqlen, unsigned short);
 
     /* Main computation */
     internal_mul(n, o, a, pqlen);
@@ -415,7 +415,7 @@ static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
     /* Allocate m of size mlen, copy mod to m */
     /* We use big endian internally */
     mlen = mod[0];
-    m = smalloc(mlen * sizeof(unsigned short));
+    m = snewn(mlen, unsigned short);
     for (j = 0; j < mlen; j++)
        m[j] = mod[mod[0] - j];
 
@@ -435,7 +435,7 @@ static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
        plen = mlen + 1;
 
     /* Allocate n of size plen, copy p to n */
-    n = smalloc(plen * sizeof(unsigned short));
+    n = snewn(plen, unsigned short);
     for (j = 0; j < plen; j++)
        n[j] = 0;
     for (j = 1; j <= p[0]; j++)
@@ -673,7 +673,7 @@ Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
     Bignum ret;
 
     /* mlen space for a, mlen space for b, 2*mlen for result */
-    workspace = smalloc(mlen * 4 * sizeof(unsigned short));
+    workspace = snewn(mlen * 4, unsigned short);
     for (i = 0; i < mlen; i++) {
        workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0);
        workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0);
@@ -949,14 +949,14 @@ char *bignum_decimal(Bignum x)
     i = bignum_bitcount(x);
     ndigits = (28 * i + 92) / 93;      /* multiply by 28/93 and round up */
     ndigits++;                        /* allow for trailing \0 */
-    ret = smalloc(ndigits);
+    ret = snewn(ndigits, char);
 
     /*
      * Now allocate some workspace to hold the binary form as we
      * repeatedly divide it by ten. Initialise this to the
      * big-endian form of the number.
      */
-    workspace = smalloc(sizeof(unsigned short) * x[0]);
+    workspace = snewn(x[0], unsigned short);
     for (i = 0; i < x[0]; i++)
        workspace[i] = x[x[0] - i];
 
index 2821020..7fb5767 100644 (file)
@@ -63,7 +63,7 @@ struct crcda_ctx {
 
 void *crcda_make_context(void)
 {
-    struct crcda_ctx *ret = smalloc(sizeof(struct crcda_ctx));
+    struct crcda_ctx *ret = snew(struct crcda_ctx);
     ret->h = NULL;
     ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
     return ret;
@@ -118,11 +118,11 @@ int detect_attack(void *handle, uchar *buf, uint32 len, uchar *IV)
 
     if (ctx->h == NULL) {
         ctx->n = l;
-        ctx->h = (uint16 *) smalloc(ctx->n * HASH_ENTRYSIZE);
+        ctx->h = snewn(ctx->n, uint16);
     } else {
         if (l > ctx->n) {
             ctx->n = l;
-            ctx->h = (uint16 *) srealloc(ctx->h, ctx->n * HASH_ENTRYSIZE);
+            ctx->h = sresize(ctx->h, ctx->n, uint16);
         }
     }
 
index 4b22c71..6ea32a1 100644 (file)
--- a/sshdes.c
+++ b/sshdes.c
@@ -746,24 +746,24 @@ static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
 
 static void *des3_make_context(void)
 {
-    return smalloc(3*sizeof(DESContext));
+    return snewn(3, DESContext);
 }
 
 static void *des3_ssh1_make_context(void)
 {
     /* Need 3 keys for each direction, in SSH1 */
-    return smalloc(6*sizeof(DESContext));
+    return snewn(6, DESContext);
 }
 
 static void *des_make_context(void)
 {
-    return smalloc(sizeof(DESContext));
+    return snew(DESContext);
 }
 
 static void *des_ssh1_make_context(void)
 {
     /* Need one key for each direction, in SSH1 */
-    return smalloc(2*sizeof(DESContext));
+    return snewn(2, DESContext);
 }
 
 static void des3_free_context(void *handle)   /* used for both 3DES and DES */
diff --git a/sshdh.c b/sshdh.c
index 2e16bbe..27d5aab 100644 (file)
--- a/sshdh.c
+++ b/sshdh.c
@@ -52,7 +52,7 @@ static void dh_init(struct dh_ctx *ctx)
  */
 void *dh_setup_group1(void)
 {
-    struct dh_ctx *ctx = smalloc(sizeof(struct dh_ctx));
+    struct dh_ctx *ctx = snew(struct dh_ctx);
     ctx->p = bignum_from_bytes(P, sizeof(P));
     ctx->g = bignum_from_bytes(G, sizeof(G));
     dh_init(ctx);
@@ -64,7 +64,7 @@ void *dh_setup_group1(void)
  */
 void *dh_setup_group(Bignum pval, Bignum gval)
 {
-    struct dh_ctx *ctx = smalloc(sizeof(struct dh_ctx));
+    struct dh_ctx *ctx = snew(struct dh_ctx);
     ctx->p = copybn(pval);
     ctx->g = copybn(gval);
     dh_init(ctx);
@@ -110,7 +110,7 @@ Bignum dh_create_e(void *handle, int nbits)
     unsigned char *buf;
 
     nbytes = ssh1_bignum_length(ctx->qmask);
-    buf = smalloc(nbytes);
+    buf = snewn(nbytes, unsigned char);
 
     do {
        /*
index 7489467..7350010 100644 (file)
--- a/sshdss.c
+++ b/sshdss.c
@@ -91,7 +91,7 @@ static void *dss_newkey(char *data, int len)
     int slen;
     struct dss_key *dss;
 
-    dss = smalloc(sizeof(struct dss_key));
+    dss = snew(struct dss_key);
     if (!dss)
        return NULL;
     getstring(&data, &len, &p, &slen);
@@ -141,7 +141,7 @@ static char *dss_fmtkey(void *key)
     len += 4 * (bignum_bitcount(dss->q) + 15) / 16;
     len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
     len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
-    p = smalloc(len);
+    p = snewn(len, char);
     if (!p)
        return NULL;
 
@@ -209,7 +209,7 @@ static char *dss_fingerprint(void *key)
     for (i = 0; i < 16; i++)
        sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
                digest[i]);
-    ret = smalloc(strlen(buffer) + 1);
+    ret = snewn(strlen(buffer) + 1, char);
     if (ret)
        strcpy(ret, buffer);
     return ret;
@@ -322,7 +322,7 @@ static unsigned char *dss_public_blob(void *key, int *len)
      * 27 + sum of lengths. (five length fields, 20+7=27).
      */
     bloblen = 27 + plen + qlen + glen + ylen;
-    blob = smalloc(bloblen);
+    blob = snewn(bloblen, unsigned char);
     p = blob;
     PUT_32BIT(p, 7);
     p += 4;
@@ -362,7 +362,7 @@ static unsigned char *dss_private_blob(void *key, int *len)
      * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
      */
     bloblen = 4 + xlen;
-    blob = smalloc(bloblen);
+    blob = snewn(bloblen, unsigned char);
     p = blob;
     PUT_32BIT(p, xlen);
     p += 4;
@@ -422,7 +422,7 @@ static void *dss_openssh_createkey(unsigned char **blob, int *len)
     char **b = (char **) blob;
     struct dss_key *dss;
 
-    dss = smalloc(sizeof(struct dss_key));
+    dss = snew(struct dss_key);
     if (!dss)
        return NULL;
 
@@ -606,7 +606,7 @@ static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
      * i.e. 4+7 + 4+40 bytes.
      */
     nbytes = 4 + 7 + 4 + 40;
-    bytes = smalloc(nbytes);
+    bytes = snewn(nbytes, unsigned char);
     PUT_32BIT(bytes, 7);
     memcpy(bytes + 4, "ssh-dss", 7);
     PUT_32BIT(bytes + 4 + 7, 40);
index b4244fe..7fbd8c7 100644 (file)
--- a/sshmd5.c
+++ b/sshmd5.c
@@ -210,7 +210,7 @@ void MD5Final(unsigned char output[16], struct MD5Context *s)
 
 static void *md5_make_context(void)
 {
-    return smalloc(2*sizeof(struct MD5Context));
+    return snewn(2, struct MD5Context);
 }
 
 static void md5_free_context(void *handle)
index 8bef2da..d0594b7 100644 (file)
--- a/sshpubk.c
+++ b/sshpubk.c
@@ -87,7 +87,7 @@ static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
     i += 4;
     if (len - i < j)
        goto end;
-    comment = smalloc(j + 1);
+    comment = snewn(j + 1, char);
     if (comment) {
        memcpy(comment, buf + i, j);
        comment[j] = '\0';
@@ -457,7 +457,7 @@ static char *read_body(FILE * fp)
     int c;
 
     size = 128;
-    text = smalloc(size);
+    text = snewn(size, char);
     len = 0;
     text[len] = '\0';
 
@@ -475,7 +475,7 @@ static char *read_body(FILE * fp)
        }
        if (len + 1 > size) {
            size += 128;
-           text = srealloc(text, size);
+           text = sresize(text, size, char);
        }
        text[len++] = c;
        text[len] = '\0';
@@ -538,7 +538,7 @@ static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
     int i, j, k;
 
     /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
-    blob = smalloc(48 * nlines);
+    blob = snewn(48 * nlines, unsigned char);
     len = 0;
     for (i = 0; i < nlines; i++) {
        line = read_body(fp);
@@ -727,7 +727,7 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
                      4 + commlen +
                      4 + public_blob_len +
                      4 + private_blob_len);
-           macdata = smalloc(maclen);
+           macdata = snewn(maclen, unsigned char);
            p = macdata;
 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
            DO_STR(alg->name, namelen);
@@ -778,7 +778,7 @@ struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
     /*
      * Create and return the key.
      */
-    ret = smalloc(sizeof(struct ssh2_userkey));
+    ret = snew(struct ssh2_userkey);
     ret->alg = alg;
     ret->comment = comment;
     ret->data = alg->createkey(public_blob, public_blob_len,
@@ -1009,7 +1009,7 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
     }
     priv_encrypted_len = priv_blob_len + cipherblk - 1;
     priv_encrypted_len -= priv_encrypted_len % cipherblk;
-    priv_blob_encrypted = smalloc(priv_encrypted_len);
+    priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
     memset(priv_blob_encrypted, 0, priv_encrypted_len);
     memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
     /* Create padding based on the SHA hash of the unpadded blob. This prevents
@@ -1036,7 +1036,7 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
                  4 + commlen +
                  4 + pub_blob_len +
                  4 + priv_encrypted_len);
-       macdata = smalloc(maclen);
+       macdata = snewn(maclen, unsigned char);
        p = macdata;
 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
        DO_STR(key->alg->name, namelen);
index e238666..524bbd4 100644 (file)
--- a/sshrand.c
+++ b/sshrand.c
@@ -202,7 +202,7 @@ int random_byte(void)
 
 void random_get_savedata(void **data, int *len)
 {
-    void *buf = smalloc(POOLSIZE / 2);
+    void *buf = snewn(POOLSIZE / 2, char);
     random_stir();
     memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
     *len = POOLSIZE / 2;
index 6467666..c97dce0 100644 (file)
--- a/sshrsa.c
+++ b/sshrsa.c
@@ -316,7 +316,7 @@ unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
 
     length = (ssh1_bignum_length(key->modulus) +
              ssh1_bignum_length(key->exponent) + 4);
-    ret = smalloc(length);
+    ret = snewn(length, unsigned char);
 
     PUT_32BIT(ret, bignum_bitcount(key->modulus));
     pos = 4;
@@ -388,7 +388,7 @@ static void *rsa2_newkey(char *data, int len)
     int slen;
     struct RSAKey *rsa;
 
-    rsa = smalloc(sizeof(struct RSAKey));
+    rsa = snew(struct RSAKey);
     if (!rsa)
        return NULL;
     getstring(&data, &len, &p, &slen);
@@ -419,7 +419,7 @@ static char *rsa2_fmtkey(void *key)
     int len;
 
     len = rsastr_len(rsa);
-    p = smalloc(len);
+    p = snewn(len, char);
     rsastr_fmt(p, rsa);
     return p;
 }
@@ -439,7 +439,7 @@ static unsigned char *rsa2_public_blob(void *key, int *len)
      * (three length fields, 12+7=19).
      */
     bloblen = 19 + elen + mlen;
-    blob = smalloc(bloblen);
+    blob = snewn(bloblen, unsigned char);
     p = blob;
     PUT_32BIT(p, 7);
     p += 4;
@@ -475,7 +475,7 @@ static unsigned char *rsa2_private_blob(void *key, int *len)
      * sum of lengths.
      */
     bloblen = 16 + dlen + plen + qlen + ulen;
-    blob = smalloc(bloblen);
+    blob = snewn(bloblen, unsigned char);
     p = blob;
     PUT_32BIT(p, dlen);
     p += 4;
@@ -523,7 +523,7 @@ static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
     char **b = (char **) blob;
     struct RSAKey *rsa;
 
-    rsa = smalloc(sizeof(struct RSAKey));
+    rsa = snew(struct RSAKey);
     if (!rsa)
        return NULL;
     rsa->comment = NULL;
@@ -608,7 +608,7 @@ static char *rsa2_fingerprint(void *key)
     for (i = 0; i < 16; i++)
        sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
                digest[i]);
-    ret = smalloc(strlen(buffer) + 1);
+    ret = snewn(strlen(buffer) + 1, char);
     if (ret)
        strcpy(ret, buffer);
     return ret;
@@ -705,7 +705,7 @@ static unsigned char *rsa2_sign(void *key, char *data, int datalen,
     SHA_Simple(data, datalen, hash);
 
     nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
-    bytes = smalloc(nbytes);
+    bytes = snewn(nbytes, unsigned char);
 
     bytes[0] = 1;
     for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
@@ -722,7 +722,7 @@ static unsigned char *rsa2_sign(void *key, char *data, int datalen,
     freebn(in);
 
     nbytes = (bignum_bitcount(out) + 7) / 8;
-    bytes = smalloc(4 + 7 + 4 + nbytes);
+    bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
     PUT_32BIT(bytes, 7);
     memcpy(bytes + 4, "ssh-rsa", 7);
     PUT_32BIT(bytes + 4 + 7, nbytes);
index d8fc569..d7c1f43 100644 (file)
--- a/sshsha.c
+++ b/sshsha.c
@@ -195,7 +195,7 @@ void SHA_Simple(void *p, int len, unsigned char *output)
 
 static void *sha1_make_context(void)
 {
-    return smalloc(2*sizeof(SHA_State));
+    return snewn(2, SHA_State);
 }
 
 static void sha1_free_context(void *handle)
index d652d5c..a98d96c 100644 (file)
--- a/sshzlib.c
+++ b/sshzlib.c
@@ -126,7 +126,7 @@ static int lz77_init(struct LZ77Context *ctx)
     struct LZ77InternalContext *st;
     int i;
 
-    st = (struct LZ77InternalContext *) smalloc(sizeof(*st));
+    st = snew(struct LZ77InternalContext);
     if (!st)
        return 0;
 
@@ -354,7 +354,7 @@ static void outbits(struct Outbuf *out, unsigned long bits, int nbits)
     while (out->noutbits >= 8) {
        if (out->outlen >= out->outsize) {
            out->outsize = out->outlen + 64;
-           out->outbuf = srealloc(out->outbuf, out->outsize);
+           out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
        }
        out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
        out->outbits >>= 8;
@@ -583,13 +583,13 @@ static void zlib_match(struct LZ77Context *ectx, int distance, int len)
 void *zlib_compress_init(void)
 {
     struct Outbuf *out;
-    struct LZ77Context *ectx = smalloc(sizeof(struct LZ77Context));
+    struct LZ77Context *ectx = snew(struct LZ77Context);
 
     lz77_init(ectx);
     ectx->literal = zlib_literal;
     ectx->match = zlib_match;
 
-    out = smalloc(sizeof(struct Outbuf));
+    out = snew(struct Outbuf);
     out->outbits = out->noutbits = 0;
     out->firstblock = 1;
     out->comp_disabled = FALSE;
@@ -806,11 +806,11 @@ static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
                                        int nsyms,
                                        int pfx, int pfxbits, int bits)
 {
-    struct zlib_table *tab = smalloc(sizeof(struct zlib_table));
+    struct zlib_table *tab = snew(struct zlib_table);
     int pfxmask = (1 << pfxbits) - 1;
     int nbits, i, j, code;
 
-    tab->table = smalloc((1 << bits) * sizeof(struct zlib_tableentry));
+    tab->table = snewn(1 << bits, struct zlib_tableentry);
     tab->mask = (1 << bits) - 1;
 
     for (code = 0; code <= tab->mask; code++) {
@@ -941,8 +941,7 @@ struct zlib_decompress_ctx {
 
 void *zlib_decompress_init(void)
 {
-    struct zlib_decompress_ctx *dctx =
-       smalloc(sizeof(struct zlib_decompress_ctx));
+    struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
     unsigned char lengths[288];
 
     memset(lengths, 8, 144);
@@ -1002,7 +1001,7 @@ static void zlib_emit_char(struct zlib_decompress_ctx *dctx, int c)
     dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
     if (dctx->outlen >= dctx->outsize) {
        dctx->outsize = dctx->outlen + 512;
-       dctx->outblk = srealloc(dctx->outblk, dctx->outsize);
+       dctx->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
     }
     dctx->outblk[dctx->outlen++] = c;
 }
index c0efd71..28d696d 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -611,18 +611,11 @@ static void do_telnet_read(Telnet telnet, char *buf, int len)
            else {
              subneg_addchar:
                if (telnet->sb_len >= telnet->sb_size) {
-                   unsigned char *newbuf;
                    telnet->sb_size += SB_DELTA;
-                   newbuf = (telnet->sb_buf ?
-                             srealloc(telnet->sb_buf, telnet->sb_size) :
-                             smalloc(telnet->sb_size));
-                   if (newbuf)
-                       telnet->sb_buf = newbuf;
-                   else
-                       telnet->sb_size -= SB_DELTA;
+                   telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
+                                            unsigned char);
                }
-               if (telnet->sb_len < telnet->sb_size)
-                   telnet->sb_buf[telnet->sb_len++] = c;
+               telnet->sb_buf[telnet->sb_len++] = c;
                telnet->state = SUBNEGOT;       /* in case we came here by goto */
            }
            break;
@@ -691,7 +684,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle,
     char *err;
     Telnet telnet;
 
-    telnet = smalloc(sizeof(*telnet));
+    telnet = snew(struct telnet_tag);
     telnet->fn = &fn_table;
     telnet->cfg = *cfg;                       /* STRUCTURE COPY */
     telnet->s = NULL;
index a39a379..362d8b5 100644 (file)
@@ -99,7 +99,7 @@ static unsigned long *resizeline(unsigned long *line, int cols)
         */
        oldlen = line[0];
        lineattrs = line[oldlen + 1];
-       line = srealloc(line, TSIZE * (2 + cols));
+       line = sresize(line, 2 + cols, TTYPE);
        line[0] = cols;
        for (i = oldlen; i < cols; i++)
            line[i + 1] = ERASE_CHAR;
@@ -372,7 +372,7 @@ Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
      * Allocate a new Terminal structure and initialise the fields
      * that need it.
      */
-    term = smalloc(sizeof(Terminal));
+    term = snew(Terminal);
     term->frontend = frontend;
     term->ucsdata = ucsdata;
     term->cfg = *mycfg;                       /* STRUCTURE COPY */
@@ -511,7 +511,7 @@ void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
            term->savecurs.y += 1;
        } else {
            /* Add a new blank line at the bottom of the screen. */
-           line = smalloc(TSIZE * (newcols + 2));
+           line = snewn(newcols + 2, TTYPE);
            line[0] = newcols;
            for (j = 0; j < newcols; j++)
                line[j + 1] = ERASE_CHAR;
@@ -551,7 +551,7 @@ void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
     term->disptop = 0;
 
     /* Make a new displayed text buffer. */
-    newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
+    newdisp = snewn(newrows * (newcols + 1), TTYPE);
     for (i = 0; i < newrows * (newcols + 1); i++)
        newdisp[i] = ATTR_INVALID;
     sfree(term->disptext);
@@ -561,7 +561,7 @@ void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
     /* Make a new alternate screen. */
     newalt = newtree234(NULL);
     for (i = 0; i < newrows; i++) {
-       line = smalloc(TSIZE * (newcols + 2));
+       line = snewn(newcols + 2, TTYPE);
        line[0] = newcols;
        for (j = 0; j < newcols; j++)
            line[j + 1] = term->erase_char;
@@ -576,7 +576,7 @@ void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
     term->alt_screen = newalt;
     term->alt_sblines = 0;
 
-    term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
+    term->tabs = sresize(term->tabs, newcols, unsigned char);
     {
        int i;
        for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
@@ -797,7 +797,7 @@ static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
                if (sblen == term->savelines) {
                    sblen--, line2 = delpos234(term->scrollback, 0);
                } else {
-                   line2 = smalloc(TSIZE * (term->cols + 2));
+                   line2 = snewn(term->cols + 2, TTYPE);
                    line2[0] = term->cols;
                    term->tempsblines += 1;
                }
@@ -889,7 +889,7 @@ static void save_scroll(Terminal *term, int topline, int botline, int lines)
        term->scrolltail->botline == botline) {
        term->scrolltail->lines += lines;
     } else {
-       newscroll = smalloc(sizeof(struct scrollregion));
+       newscroll = snew(struct scrollregion);
        newscroll->topline = topline;
        newscroll->botline = botline;
        newscroll->lines = lines;
@@ -1631,7 +1631,7 @@ void term_out(Terminal *term)
                    ticks = GETTICKCOUNT();
 
                    if (!term->beep_overloaded) {
-                       newbeep = smalloc(sizeof(struct beeptime));
+                       newbeep = snew(struct beeptime);
                        newbeep->ticks = ticks;
                        newbeep->next = NULL;
                        if (!term->beephead)
@@ -3563,7 +3563,7 @@ static void clipme(Terminal *term, pos top, pos bottom, int rect)
     int buflen;                               /* amount of memory allocated to workbuf */
 
     buflen = 5120;                    /* Default size */
-    workbuf = smalloc(buflen * sizeof(wchar_t));
+    workbuf = snewn(buflen, wchar_t);
     wbptr = workbuf;                  /* start filling here */
     old_top_x = top.x;                /* needed for rect==1 */
 
@@ -3679,9 +3679,8 @@ static void clipme(Terminal *term, pos top, pos bottom, int rect)
            for (p = cbuf; *p; p++) {
                /* Enough overhead for trailing NL and nul */
                if (wblen >= buflen - 16) {
-                   workbuf =
-                       srealloc(workbuf,
-                                sizeof(wchar_t) * (buflen += 100));
+                   buflen += 100;
+                   workbuf = sresize(workbuf, buflen, wchar_t);
                    wbptr = workbuf + wblen;
                }
                wblen++;
@@ -3950,7 +3949,7 @@ void term_do_paste(Terminal *term)
         if (term->paste_buffer)
             sfree(term->paste_buffer);
         term->paste_pos = term->paste_hold = term->paste_len = 0;
-        term->paste_buffer = smalloc(len * sizeof(wchar_t));
+        term->paste_buffer = snewn(len, wchar_t);
 
         p = q = data;
         while (p < data + len) {
index fe8263c..647b55d 100644 (file)
@@ -53,7 +53,8 @@ struct terminal_tag {
     int beep_overloaded;
     long lastbeep;
 
-#define TSIZE (sizeof(unsigned long))
+#define TTYPE unsigned long
+#define TSIZE (sizeof(TTYPE))
 #define fix_cpos do { \
     term->cpos = lineptr(term->curs.y) + term->curs.x; \
 } while(0)
index 333c316..e24217c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: testback.c,v 1.6 2003/01/15 23:30:21 ben Exp $ */
+/* $Id: testback.c,v 1.7 2003/03/29 16:14:26 simon Exp $ */
 /*
  * Copyright (c) 1999 Simon Tatham
  * Copyright (c) 1999 Ben Harris
@@ -77,7 +77,7 @@ static char *null_init(void *frontend_handle, void **backend_handle,
 static char *loop_init(void *frontend_handle, void **backend_handle,
                       Config *cfg, char *host, int port, char **realhost,
                       int nodelay) {
-    struct loop_state *st = smalloc(sizeof(*st));
+    struct loop_state *st = snew(struct loop_state);
 
     st->term = frontend_handle;
     *backend_handle = st;
index 0837dc6..b5895d0 100644 (file)
--- a/tree234.c
+++ b/tree234.c
 #include <stdlib.h>
 #include <assert.h>
 
+#include "puttymem.h"
 #include "tree234.h"
 
-#define smalloc malloc
-#define sfree free
-
-#define mknew(typ) ( (typ *) smalloc (sizeof (typ)) )
-
 #ifdef TEST
 #define LOG(x) (printf x)
 #else
@@ -61,7 +57,7 @@ struct node234_Tag {
  */
 tree234 *newtree234(cmpfn234 cmp)
 {
-    tree234 *ret = mknew(tree234);
+    tree234 *ret = snew(tree234);
     LOG(("created tree %p\n", ret));
     ret->root = NULL;
     ret->cmp = cmp;
@@ -128,7 +124,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
 
     LOG(("adding node %p to tree %p\n", e, t));
     if (t->root == NULL) {
-       t->root = mknew(node234);
+       t->root = snew(node234);
        t->root->elems[1] = t->root->elems[2] = NULL;
        t->root->kids[0] = t->root->kids[1] = NULL;
        t->root->kids[2] = t->root->kids[3] = NULL;
@@ -300,7 +296,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
            LOG(("  done\n"));
            break;
        } else {
-           node234 *m = mknew(node234);
+           node234 *m = snew(node234);
            m->parent = n->parent;
            LOG(("  splitting a 4-node; created new node %p\n", m));
            /*
@@ -423,7 +419,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
        }
     } else {
        LOG(("  root is overloaded, split into two\n"));
-       t->root = mknew(node234);
+       t->root = snew(node234);
        t->root->kids[0] = left;
        t->root->counts[0] = lcount;
        t->root->elems[0] = e;
@@ -1012,8 +1008,6 @@ void *del234(tree234 * t, void *e)
 
 #include <stdarg.h>
 
-#define srealloc realloc
-
 /*
  * Error reporting function.
  */
@@ -1201,8 +1195,7 @@ void internal_addtest(void *elem, int index, void *realret)
 
     if (arraysize < arraylen + 1) {
        arraysize = arraylen + 1 + 256;
-       array = (array == NULL ? smalloc(arraysize * sizeof(*array)) :
-                srealloc(array, arraysize * sizeof(*array)));
+       array = sresize(array, arraysize, void *);
     }
 
     i = index;
index d1428bb..0242733 100644 (file)
--- a/unicode.c
+++ b/unicode.c
@@ -516,12 +516,12 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
            if (DIRECT_FONT(ucsdata->unitab_line[i]))
                continue;
            if (!ucsdata->uni_tbl) {
-               ucsdata->uni_tbl = smalloc(256 * sizeof(char *));
+               ucsdata->uni_tbl = snewn(256, char *);
                memset(ucsdata->uni_tbl, 0, 256 * sizeof(char *));
            }
            j = ((ucsdata->unitab_line[i] >> 8) & 0xFF);
            if (!ucsdata->uni_tbl[j]) {
-               ucsdata->uni_tbl[j] = smalloc(256 * sizeof(char));
+               ucsdata->uni_tbl[j] = snewn(256, char);
                memset(ucsdata->uni_tbl[j], 0, 256 * sizeof(char));
            }
            ucsdata->uni_tbl[j][ucsdata->unitab_line[i] & 0xFF] = i;
index 8a84395..4afabde 100644 (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",
index aaed73b..6d6a9a5 100644 (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 */
 
index 63fe69e..6d90abb 100644 (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
index 61b4f04..47d0ef5 100644 (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) {
         /*
index af56f1b..e16c92f 100644 (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);
        }
     }
index 08da8f4..9dd4aa6 100644 (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;
index 50189f9..e71e644 100644 (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);
        }
 
        /*
index c835b6f..77bcb7f 100644 (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.
index 5da6ed6..15fcdcb 100644 (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);
index 38e36ab..896c1bc 100644 (file)
--- a/wincfg.c
+++ b/wincfg.c
@@ -128,19 +128,15 @@ void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
                assert(c->generic.handler == dlg_stdradiobutton_handler);
                c->radio.nbuttons++;
                c->radio.buttons =
-                   srealloc(c->radio.buttons,
-                            c->radio.nbuttons * sizeof(*c->radio.buttons));
+                   sresize(c->radio.buttons, c->radio.nbuttons, char *);
                c->radio.buttons[c->radio.nbuttons-1] =
                    dupstr("Play a custom sound file");
                c->radio.buttondata =
-                   srealloc(c->radio.buttondata,
-                            c->radio.nbuttons * sizeof(*c->radio.buttondata));
+                   sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
                c->radio.buttondata[c->radio.nbuttons-1] = I(BELL_WAVEFILE);
                if (c->radio.shortcuts) {
                    c->radio.shortcuts =
-                       srealloc(c->radio.shortcuts,
-                                (c->radio.nbuttons *
-                                 sizeof(*c->radio.shortcuts)));
+                       sresize(c->radio.shortcuts, c->radio.nbuttons, char);
                    c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;
                }
                break;
@@ -201,29 +197,23 @@ void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
                assert(c->generic.handler == dlg_stdradiobutton_handler);
                c->radio.nbuttons += 2;
                c->radio.buttons =
-                   srealloc(c->radio.buttons,
-                            c->radio.nbuttons * sizeof(*c->radio.buttons));
+                   sresize(c->radio.buttons, c->radio.nbuttons, char *);
                c->radio.buttons[c->radio.nbuttons-2] =
                    dupstr("Use font in both ANSI and OEM modes");
                c->radio.buttons[c->radio.nbuttons-1] =
                    dupstr("Use font in OEM mode only");
                c->radio.buttondata =
-                   srealloc(c->radio.buttondata,
-                            c->radio.nbuttons * sizeof(*c->radio.buttondata));
+                   sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
                c->radio.buttondata[c->radio.nbuttons-2] = I(VT_OEMANSI);
                c->radio.buttondata[c->radio.nbuttons-1] = I(VT_OEMONLY);
                if (!c->radio.shortcuts) {
                    int j;
-                   c->radio.shortcuts =
-                       smalloc((c->radio.nbuttons *
-                                sizeof(*c->radio.shortcuts)));
+                   c->radio.shortcuts = snewn(c->radio.nbuttons, char);
                    for (j = 0; j < c->radio.nbuttons; j++)
                        c->radio.shortcuts[j] = NO_SHORTCUT;
                } else {
-                   c->radio.shortcuts =
-                       srealloc(c->radio.shortcuts,
-                                (c->radio.nbuttons *
-                                 sizeof(*c->radio.shortcuts)));
+                   c->radio.shortcuts = sresize(c->radio.shortcuts,
+                                                c->radio.nbuttons, char);
                }
                c->radio.shortcuts[c->radio.nbuttons-2] = 'b';
                c->radio.shortcuts[c->radio.nbuttons-1] = 'e';
index 87957ef..204bc4a 100644 (file)
@@ -289,7 +289,7 @@ void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
        bid = va_arg(ap, int);
     }
     va_end(ap);
-    buttons = smalloc(nbuttons * sizeof(struct radio));
+    buttons = snewn(nbuttons, struct radio);
     va_start(ap, nacross);
     for (i = 0; i < nbuttons; i++) {
        buttons[i].text = va_arg(ap, char *);
@@ -320,7 +320,7 @@ void bareradioline(struct ctlpos *cp, int nacross, ...)
        bid = va_arg(ap, int);
     }
     va_end(ap);
-    buttons = smalloc(nbuttons * sizeof(struct radio));
+    buttons = snewn(nbuttons, struct radio);
     va_start(ap, nacross);
     for (i = 0; i < nbuttons; i++) {
        buttons[i].text = va_arg(ap, char *);
@@ -351,7 +351,7 @@ void radiobig(struct ctlpos *cp, char *text, int id, ...)
        bid = va_arg(ap, int);
     }
     va_end(ap);
-    buttons = smalloc(nbuttons * sizeof(struct radio));
+    buttons = snewn(nbuttons, struct radio);
     va_start(ap, id);
     for (i = 0; i < nbuttons; i++) {
        buttons[i].text = va_arg(ap, char *);
@@ -395,10 +395,10 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
     RECT r;
     HFONT oldfont, newfont;
 
-    ret = smalloc(1+strlen(text));
+    ret = snewn(1+strlen(text), char);
     p = text;
     q = ret;
-    pwidths = smalloc(sizeof(INT)*(1+strlen(text)));
+    pwidths = snewn(1+strlen(text), INT);
 
     /*
      * Work out the width the text will need to fit in, by doing
@@ -973,7 +973,7 @@ static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
     char *txt;
     /* Get the item's data. */
     tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
-    txt = smalloc(tlen+1);
+    txt = snewn(tlen+1, char);
     SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
     val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
     /* Deselect old location. */
@@ -1176,7 +1176,7 @@ static char *shortcut_escape(char *text, char shortcut)
     if (!text)
        return NULL;                   /* sfree won't choke on this */
 
-    ret = smalloc(2*strlen(text)+1);   /* size potentially doubles! */
+    ret = snewn(2*strlen(text)+1, char);   /* size potentially doubles! */
     shortcut = tolower((unsigned char)shortcut);
 
     p = text;
@@ -1338,7 +1338,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
 
     /* Start a containing box, if we have a boxname. */
     if (s->boxname && *s->boxname) {
-       struct winctrl *c = smalloc(sizeof(struct winctrl));
+       struct winctrl *c = snew(struct winctrl);
        c->ctrl = NULL;
        c->base_id = base_id;
        c->num_ids = 1;
@@ -1351,7 +1351,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
 
     /* Draw a title, if we have one. */
     if (!s->boxname && s->boxtitle) {
-       struct winctrl *c = smalloc(sizeof(struct winctrl));
+       struct winctrl *c = snew(struct winctrl);
        c->ctrl = NULL;
        c->base_id = base_id;
        c->num_ids = 1;
@@ -1534,7 +1534,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
                                          ctrl->radio.shortcut);
                shortcuts[nshortcuts++] = ctrl->radio.shortcut;
 
-               buttons = smalloc(ctrl->radio.nbuttons * sizeof(struct radio));
+               buttons = snewn(ctrl->radio.nbuttons, struct radio);
 
                for (i = 0; i < ctrl->radio.nbuttons; i++) {
                    buttons[i].text =
@@ -1584,7 +1584,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
                                      ctrl->listbox.shortcut);
            shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
            if (ctrl->listbox.draglist) {
-               data = smalloc(sizeof(struct prefslist));
+               data = snew(struct prefslist);
                num_ids = 4;
                prefslist(data, &pos, ctrl->listbox.height, escaped,
                          base_id, base_id+1, base_id+2, base_id+3);
@@ -1616,7 +1616,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
                int *tabarray;
                int i, percent;
 
-               tabarray = smalloc((ctrl->listbox.ncols-1) * sizeof(int));
+               tabarray = snewn(ctrl->listbox.ncols-1, int);
                percent = 0;
                for (i = 0; i < ctrl->listbox.ncols-1; i++) {
                    percent += ctrl->listbox.percentages[i];
@@ -1646,7 +1646,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
            statictext(&pos, escaped, 1, base_id);
            staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
            sfree(escaped);
-           data = smalloc(sizeof(FontSpec));
+           data = snew(FontSpec);
            break;
          default:
            assert(!"Can't happen");
@@ -1660,7 +1660,7 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
         * (and isn't tabdelayed).
         */
        if (pos.hwnd) {
-           struct winctrl *c = smalloc(sizeof(struct winctrl));
+           struct winctrl *c = snew(struct winctrl);
 
            c->ctrl = ctrl;
            c->base_id = actual_base_id;
@@ -1814,7 +1814,7 @@ int winctrl_handle_command(struct dlgparam *dp, UINT msg,
                                           CB_GETCURSEL, 0, 0);
                len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
                                         CB_GETLBTEXTLEN, index, 0);
-               text = smalloc(len+1);
+               text = snewn(len+1, char);
                SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
                                   index, (LPARAM)text);
                SetDlgItemText(dp->hwnd, c->base_id+1, text);
@@ -2503,7 +2503,7 @@ void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
     tmp.ctrl = ctrl;
     p = find234(dp->privdata, &tmp, NULL);
     if (!p) {
-       p = smalloc(sizeof(struct perctrl_privdata));
+       p = snew(struct perctrl_privdata);
        p->ctrl = ctrl;
        p->needs_free = FALSE;
        add234(dp->privdata, p);
@@ -2518,13 +2518,17 @@ void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
     tmp.ctrl = ctrl;
     p = find234(dp->privdata, &tmp, NULL);
     if (!p) {
-       p = smalloc(sizeof(struct perctrl_privdata));
+       p = snew(struct perctrl_privdata);
        p->ctrl = ctrl;
        p->needs_free = FALSE;
        add234(dp->privdata, p);
     }
     assert(!p->needs_free);
     p->needs_free = TRUE;
+    /*
+     * This is an internal allocation routine, so it's allowed to
+     * use smalloc directly.
+     */
     p->data = smalloc(size);
     return p->data;
 }
index ef2a2a4..6656c96 100644 (file)
--- a/windlg.c
+++ b/windlg.c
@@ -102,7 +102,7 @@ static int CALLBACK LogProc(HWND hwnd, UINT msg,
                    break;
                }
 
-               selitems = smalloc(selcount * sizeof(int));
+               selitems = snewn(selcount, int);
                if (selitems) {
                    int count = SendDlgItemMessage(hwnd, IDN_LIST,
                                                   LB_GETSELITEMS,
@@ -123,7 +123,7 @@ static int CALLBACK LogProc(HWND hwnd, UINT msg,
                        size +=
                            strlen(events[selitems[i]]) + sizeof(sel_nl);
 
-                   clipdata = smalloc(size);
+                   clipdata = snewn(size, char);
                    if (clipdata) {
                        char *p = clipdata;
                        for (i = 0; i < count; i++) {
@@ -648,14 +648,14 @@ void logevent(void *frontend, char *string)
 
     if (nevents >= negsize) {
        negsize += 64;
-       events = srealloc(events, negsize * sizeof(*events));
+       events = sresize(events, negsize, char *);
     }
 
     time(&t);
     strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t",
             localtime(&t));
 
-    events[nevents] = smalloc(strlen(timebuf) + strlen(string) + 1);
+    events[nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
     strcpy(events[nevents], timebuf);
     strcat(events[nevents], string);
     if (logbox) {
index effa2de..5d6b2cf 100644 (file)
--- a/window.c
+++ b/window.c
@@ -600,7 +600,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
     {
        char *bits;
        int size = (font_width + 15) / 16 * 2 * font_height;
-       bits = smalloc(size);
+       bits = snewn(size, char);
        memset(bits, 0, size);
        caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
        sfree(bits);
@@ -999,6 +999,10 @@ static void init_palette(void)
     HDC hdc = GetDC(hwnd);
     if (hdc) {
        if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
+           /*
+            * This is a genuine case where we must use smalloc
+            * because the snew macros can't cope.
+            */
            logpal = smalloc(sizeof(*logpal)
                             - sizeof(logpal->palPalEntry)
                             + NCOLOURS * sizeof(PALETTEENTRY));
@@ -1723,7 +1727,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                    if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) {
                        char *session =
                            sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16];
-                       cl = smalloc(16 + strlen(session));
+                       cl = snewn(16 + strlen(session), char);
                                       /* 8, but play safe */
                        if (!cl)
                            cl = NULL;    
@@ -2531,7 +2535,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 
            if (n > 0) {
                int i;
-               buff = (char*) smalloc(n);
+               buff = snewn(n, char);
                ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
                /*
                 * Jaeyoun Chung reports that Korean character
@@ -2719,7 +2723,7 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        int i;
        if (len > IpDxLEN) {
            sfree(IpDx);
-           IpDx = smalloc((len + 16) * sizeof(int));
+           IpDx = snewn(len + 16, int);
            IpDxLEN = (len + 16);
        }
        for (i = 0; i < IpDxLEN; i++)
@@ -2850,7 +2854,8 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        int nlen, mptr;
        if (len > uni_len) {
            sfree(uni_buf);
-           uni_buf = smalloc((uni_len = len) * sizeof(wchar_t));
+           uni_len = len;
+           uni_buf = snewn(uni_len, wchar_t);
        }
 
        for(nlen = mptr = 0; mptr<len; mptr++) {
@@ -2910,7 +2915,7 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        if (wlen < len) {
            sfree(wbuf);
            wlen = len;
-           wbuf = smalloc(wlen * sizeof(WCHAR));
+           wbuf = snewn(wlen, WCHAR);
        }
        for (i = 0; i < len; i++)
            wbuf[i] = (WCHAR) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
@@ -3920,7 +3925,7 @@ void request_paste(void *frontend)
 void set_title(void *frontend, char *title)
 {
     sfree(window_name);
-    window_name = smalloc(1 + strlen(title));
+    window_name = snewn(1 + strlen(title), char);
     strcpy(window_name, title);
     if (cfg.win_name_always || !IsIconic(hwnd))
        SetWindowText(hwnd, title);
@@ -3929,7 +3934,7 @@ void set_title(void *frontend, char *title)
 void set_icon(void *frontend, char *title)
 {
     sfree(icon_name);
-    icon_name = smalloc(1 + strlen(title));
+    icon_name = snewn(1 + strlen(title), char);
     strcpy(icon_name, title);
     if (!cfg.win_name_always && IsIconic(hwnd))
        SetWindowText(hwnd, title);
@@ -4100,7 +4105,7 @@ void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
        get_unitab(CP_ACP, unitab, 0);
 
        rtfsize = 100 + strlen(cfg.font.name);
-       rtf = smalloc(rtfsize);
+       rtf = snewn(rtfsize, char);
        sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
                GetACP(), cfg.font.name);
        rtflen = strlen(rtf);
@@ -4165,7 +4170,7 @@ void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
 
            if (rtfsize < rtflen + totallen + 3) {
                rtfsize = rtflen + totallen + 512;
-               rtf = srealloc(rtf, rtfsize);
+               rtf = sresize(rtf, rtfsize, char);
            }
 
            strcpy(rtf + rtflen, before); rtflen += blen;
@@ -4253,7 +4258,7 @@ void get_clip(void *frontend, wchar_t ** p, int *len)
            CloseClipboard();
            s = GlobalLock(clipdata);
            i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
-           *p = converted = smalloc(i * sizeof(wchar_t));
+           *p = converted = snewn(i, wchar_t);
            MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
            *len = i - 1;
            return;
index f617b0f..6bb135c 100644 (file)
--- a/winnet.c
+++ b/winnet.c
@@ -226,7 +226,7 @@ char *winsock_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];
@@ -357,14 +357,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));
@@ -490,7 +490,7 @@ Socket sk_register(void *sock, Plug plug)
     /*
      * Create Socket structure.
      */
-    ret = smalloc(sizeof(struct Socket_tag));
+    ret = snew(struct Socket_tag);
     ret->fn = &fn_table;
     ret->error = NULL;
     ret->plug = plug;
@@ -553,7 +553,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 = &fn_table;
     ret->error = NULL;
     ret->plug = plug;
@@ -732,7 +732,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 = &fn_table;
     ret->error = NULL;
     ret->plug = plug;
index e0d9a78..90d262f 100644 (file)
@@ -70,7 +70,7 @@ void *open_settings_w(const char *sessionname)
     if (!sessionname || !*sessionname)
        sessionname = "Default Settings";
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
 
     ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
@@ -113,7 +113,7 @@ void *open_settings_r(const char *sessionname)
     if (!sessionname || !*sessionname)
        sessionname = "Default Settings";
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
 
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
@@ -231,7 +231,7 @@ void del_settings(const char *sessionname)
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
        return;
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
     RegDeleteKey(subkey1, p);
     sfree(p);
@@ -252,7 +252,7 @@ void *enum_settings_start(void)
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
        return NULL;
 
-    ret = smalloc(sizeof(*ret));
+    ret = snew(struct enumsettings);
     if (ret) {
        ret->key = key;
        ret->i = 0;
@@ -265,7 +265,7 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
 {
     struct enumsettings *e = (struct enumsettings *) handle;
     char *otherbuf;
-    otherbuf = smalloc(3 * buflen);
+    otherbuf = snewn(3 * buflen, char);
     if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
        unmungestr(otherbuf, buffer, buflen);
        sfree(otherbuf);
@@ -310,8 +310,8 @@ int verify_host_key(const char *hostname, int port,
      * Now read a saved key in from the registry and see what it
      * says.
      */
-    otherstr = smalloc(len);
-    regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+    otherstr = snewn(len, char);
+    regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
 
     hostkey_regname(regname, hostname, port, keytype);
 
@@ -330,7 +330,7 @@ int verify_host_key(const char *hostname, int port,
         * under just the hostname and translate that.
         */
        char *justhost = regname + 1 + strcspn(regname, ":");
-       char *oldstyle = smalloc(len + 10);     /* safety margin */
+       char *oldstyle = snewn(len + 10, char); /* safety margin */
        readlen = len;
        ret = RegQueryValueEx(rkey, justhost, NULL, &type,
                              oldstyle, &readlen);
@@ -406,7 +406,7 @@ void store_host_key(const char *hostname, int port,
     char *regname;
     HKEY rkey;
 
-    regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+    regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
 
     hostkey_regname(regname, hostname, port, keytype);
 
index 00d35df..d55e890 100644 (file)
@@ -148,9 +148,9 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
      * This will guaranteeably be big enough; we can realloc it
      * down later.
      */
-    outputline = smalloc(1+strlen(cmdline));
-    outputargv = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
-    outputargstart = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
+    outputline = snewn(1+strlen(cmdline), char);
+    outputargv = snewn(strlen(cmdline)+1 / 2, char *);
+    outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
 
     p = cmdline; q = outputline; outputargc = 0;
 
@@ -217,8 +217,8 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
        *q++ = '\0';
     }
 
-    outputargv = srealloc(outputargv, sizeof(char *) * outputargc);
-    outputargstart = srealloc(outputargstart, sizeof(char *) * outputargc);
+    outputargv = sresize(outputargv, outputargc, char *);
+    outputargstart = sresize(outputargstart, outputargc, char *);
 
     if (argc) *argc = outputargc;
     if (argv) *argv = outputargv; else sfree(outputargv);
index 5467486..914f3f0 100644 (file)
--- a/x11fwd.c
+++ b/x11fwd.c
@@ -81,7 +81,7 @@ struct X11Private {
 void *x11_invent_auth(char *proto, int protomaxlen,
                      char *data, int datamaxlen, int proto_id)
 {
-    struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
+    struct X11Auth *auth = snew(struct X11Auth);
     char ourdata[64];
     int i;
 
@@ -282,7 +282,7 @@ char *x11_init(Socket * s, char *display, void *c, void *auth,
     /*
      * Open socket.
      */
-    pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
+    pr = snew(struct X11Private);
     pr->fn = &fn_table;
     pr->auth_protocol = NULL;
     pr->auth = (struct X11Auth *)auth;
@@ -384,8 +384,8 @@ int x11_send(Socket s, char *data, int len)
        pr->auth_psize = (pr->auth_plen + 3) & ~3;
        pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
        /* Leave room for a terminating zero, to make our lives easier. */
-       pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
-       pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
+       pr->auth_protocol = snewn(pr->auth_psize + 1, char);
+       pr->auth_data = snewn(pr->auth_dsize, char);
     }
 
     /*
@@ -421,7 +421,7 @@ int x11_send(Socket s, char *data, int len)
 
            message = dupprintf("PuTTY X11 proxy: %s", err);
            msglen = strlen(message);
-           reply = smalloc(8 + msglen+1 + 4);   /* include zero byte */
+           reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
            msgsize = (msglen + 3) & ~3;
            reply[0] = 0;              /* failure */
            reply[1] = msglen;         /* length of reason string */