1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

Make memory management uniform: _everything_ now goes through the

smalloc() macros and thence to the safemalloc() functions in misc.c.
This should allow me to plug in a debugging allocator and track
memory leaks and segfaults and things.

[originally from svn r818]
This commit is contained in:
Simon Tatham 2000-12-12 10:33:13 +00:00
parent 8eca227b92
commit d5240d4157
20 changed files with 201 additions and 183 deletions

View File

@ -72,7 +72,7 @@ OBJS2 = sshsha.$(OBJ) sshblowf.$(OBJ) noise.$(OBJ) sshdh.$(OBJ) sshdss.$(OBJ)
OBJS3 = sshbn.$(OBJ) sshpubk.$(OBJ) ssh.$(OBJ) pageantc.$(OBJ) sshzlib.$(OBJ)
##-- objects pageant
PAGE1 = pageant.$(OBJ) sshrsa.$(OBJ) sshpubk.$(OBJ) sshdes.$(OBJ) sshbn.$(OBJ)
PAGE2 = sshmd5.$(OBJ) version.$(OBJ) tree234.$(OBJ)
PAGE2 = sshmd5.$(OBJ) version.$(OBJ) tree234.$(OBJ) misc.$(OBJ)
##-- objects puttygen
GEN1 = puttygen.$(OBJ) sshrsag.$(OBJ) sshprime.$(OBJ) sshdes.$(OBJ)
GEN2 = sshbn.$(OBJ) sshmd5.$(OBJ) version.$(OBJ) sshrand.$(OBJ) noise.$(OBJ)
@ -204,40 +204,41 @@ plink.rsp: makefile
echo $(SOCK2) >> plink.rsp
##-- dependencies
window.$(OBJ): window.c putty.h network.h win_res.h storage.h winstuff.h
windlg.$(OBJ): windlg.c putty.h network.h ssh.h win_res.h winstuff.h
window.$(OBJ): window.c putty.h puttymem.h network.h win_res.h storage.h winstuff.h
windlg.$(OBJ): windlg.c putty.h puttymem.h network.h ssh.h win_res.h winstuff.h
winctrls.$(OBJ): winctrls.c winstuff.h winstuff.h
settings.$(OBJ): settings.c putty.h network.h storage.h
winstore.$(OBJ): winstore.c putty.h network.h storage.h
terminal.$(OBJ): terminal.c putty.h network.h
sizetip.$(OBJ): sizetip.c putty.h network.h winstuff.h
telnet.$(OBJ): telnet.c putty.h network.h
raw.$(OBJ): raw.c putty.h network.h
xlat.$(OBJ): xlat.c putty.h network.h
ldisc.$(OBJ): ldisc.c putty.h network.h
misc.$(OBJ): misc.c putty.h network.h
noise.$(OBJ): noise.c putty.h network.h ssh.h storage.h
ssh.$(OBJ): ssh.c ssh.h putty.h network.h tree234.h
sshcrc.$(OBJ): sshcrc.c ssh.h
sshdes.$(OBJ): sshdes.c ssh.h
sshmd5.$(OBJ): sshmd5.c ssh.h
sshrsa.$(OBJ): sshrsa.c ssh.h
sshsha.$(OBJ): sshsha.c ssh.h
sshrand.$(OBJ): sshrand.c ssh.h
sshblowf.$(OBJ): sshblowf.c ssh.h
sshdh.$(OBJ): sshdh.c ssh.h
sshdss.$(OBJ): sshdss.c ssh.h
sshbn.$(OBJ): sshbn.c ssh.h
sshpubk.$(OBJ): sshpubk.c ssh.h
sshzlib.$(OBJ): sshzlib.c ssh.h
scp.$(OBJ): scp.c putty.h network.h winstuff.h
settings.$(OBJ): settings.c putty.h puttymem.h network.h storage.h
winstore.$(OBJ): winstore.c putty.h puttymem.h network.h storage.h
terminal.$(OBJ): terminal.c putty.h puttymem.h network.h
sizetip.$(OBJ): sizetip.c putty.h puttymem.h network.h winstuff.h
telnet.$(OBJ): telnet.c putty.h puttymem.h network.h
raw.$(OBJ): raw.c putty.h puttymem.h network.h
xlat.$(OBJ): xlat.c putty.h puttymem.h network.h
ldisc.$(OBJ): ldisc.c putty.h puttymem.h network.h
misc.$(OBJ): misc.c putty.h puttymem.h network.h
noise.$(OBJ): noise.c putty.h puttymem.h network.h ssh.h storage.h
ssh.$(OBJ): ssh.c ssh.h putty.h puttymem.h network.h tree234.h
sshcrc.$(OBJ): sshcrc.c ssh.h puttymem.h
sshdes.$(OBJ): sshdes.c ssh.h puttymem.h
sshmd5.$(OBJ): sshmd5.c ssh.h puttymem.h
sshrsa.$(OBJ): sshrsa.c ssh.h puttymem.h
sshsha.$(OBJ): sshsha.c ssh.h puttymem.h
sshrand.$(OBJ): sshrand.c ssh.h puttymem.h
sshblowf.$(OBJ): sshblowf.c ssh.h puttymem.h
sshdh.$(OBJ): sshdh.c ssh.h puttymem.h
sshdss.$(OBJ): sshdss.c ssh.h puttymem.h
sshbn.$(OBJ): sshbn.c ssh.h puttymem.h
sshpubk.$(OBJ): sshpubk.c ssh.h puttymem.h
sshzlib.$(OBJ): sshzlib.c ssh.h puttymem.h
scp.$(OBJ): scp.c putty.h puttymem.h network.h winstuff.h
version.$(OBJ): version.c
be_all.$(OBJ): be_all.c
be_nossh.$(OBJ): be_nossh.c
be_none.$(OBJ): be_none.c
plink.$(OBJ): plink.c putty.h network.h winstuff.h
pageant.$(OBJ): pageant.c ssh.h tree234.h
tree234.$(OBJ): tree234.c tree234.h
plink.$(OBJ): plink.c putty.h puttymem.h network.h winstuff.h
pageant.$(OBJ): pageant.c ssh.h puttymem.h tree234.h
pageantc.$(OBJ): pageantc.c puttymem.h
tree234.$(OBJ): tree234.c tree234.h puttymem.h
##--
# Hack to force version.obj to be rebuilt always

View File

@ -238,7 +238,7 @@ static void add_keyfile(char *filename) {
needs_pass = rsakey_encrypted(filename, &comment);
attempts = 0;
key = malloc(sizeof(*key));
key = smalloc(sizeof(*key));
pps.passphrase = passphrase;
pps.comment = comment;
do {
@ -248,8 +248,8 @@ static void add_keyfile(char *filename) {
NULL, PassphraseProc,
(LPARAM)&pps);
if (!dlgret) {
if (comment) free(comment);
free(key);
if (comment) sfree(comment);
sfree(key);
return; /* operation cancelled */
}
} else
@ -257,15 +257,15 @@ static void add_keyfile(char *filename) {
ret = loadrsakey(filename, key, NULL, passphrase);
attempts++;
} while (ret == -1);
if (comment) free(comment);
if (comment) sfree(comment);
if (ret == 0) {
MessageBox(NULL, "Couldn't load private key.", APPNAME,
MB_OK | MB_ICONERROR);
free(key);
sfree(key);
return;
}
if (add234(rsakeys, key) != key)
free(key); /* already present, don't waste RAM */
sfree(key); /* already present, don't waste RAM */
}
/*
@ -382,14 +382,14 @@ static void answer_msg(void *msg) {
{
struct RSAKey *key;
char *comment;
key = malloc(sizeof(struct RSAKey));
key = smalloc(sizeof(struct RSAKey));
memset(key, 0, sizeof(key));
p += makekey(p, key, NULL, 1);
p += makeprivate(p, key);
p += ssh1_read_bignum(p, NULL); /* p^-1 mod q */
p += ssh1_read_bignum(p, NULL); /* p */
p += ssh1_read_bignum(p, NULL); /* q */
comment = malloc(GET_32BIT(p));
comment = smalloc(GET_32BIT(p));
if (comment) {
memcpy(comment, p+4, GET_32BIT(p));
key->comment = comment;
@ -401,7 +401,7 @@ static void answer_msg(void *msg) {
ret[4] = SSH_AGENT_SUCCESS;
} else {
freersakey(key);
free(key);
sfree(key);
}
}
break;

View File

@ -6,6 +6,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "puttymem.h"
#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
#define AGENT_MAX_MSGLEN 8192
@ -60,7 +62,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 = malloc(retlen);
ret = smalloc(retlen);
if (ret) {
memcpy(ret, p, retlen);
*out = ret;

16
putty.h
View File

@ -354,21 +354,7 @@ void random_get_savedata(void **data, int *len);
* Exports from misc.c.
*/
/* #define MALLOC_LOG do this if you suspect putty of leaking memory */
#ifdef MALLOC_LOG
#define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
#define srealloc(y,z) (mlog(__FILE__,__LINE__), saferealloc(y,z))
#define sfree(z) (mlog(__FILE__,__LINE__), safefree(z))
void mlog(char *, int);
#else
#define smalloc safemalloc
#define srealloc saferealloc
#define sfree safefree
#endif
void *safemalloc(size_t);
void *saferealloc(void *, size_t);
void safefree(void *);
#include "puttymem.h"
/*
* Exports from version.c.

View File

@ -241,7 +241,7 @@ static DWORD WINAPI generate_rsa_key_thread(void *param) {
PostMessage(params->dialog, WM_DONEKEY, 0, 0);
free(params);
sfree(params);
return 0;
}
@ -268,15 +268,15 @@ static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) {
dec1 = bignum_decimal(key->exponent);
dec2 = bignum_decimal(key->modulus);
buffer = malloc(strlen(dec1)+strlen(dec2)+
strlen(key->comment)+30);
buffer = smalloc(strlen(dec1)+strlen(dec2)+
strlen(key->comment)+30);
sprintf(buffer, "%d %s %s %s",
ssh1_bignum_bitcount(key->modulus),
dec1, dec2, key->comment);
SetDlgItemText(hwnd, id, buffer);
free(dec1);
free(dec2);
free(buffer);
sfree(dec1);
sfree(dec2);
sfree(buffer);
}
/*
@ -320,7 +320,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
switch (msg) {
case WM_INITDIALOG:
state = malloc(sizeof(*state));
state = smalloc(sizeof(*state));
state->generation_thread_exists = FALSE;
state->collecting_entropy = FALSE;
state->entropy = NULL;
@ -403,7 +403,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
*/
random_add_heavynoise(state->entropy, state->entropy_size);
memset(state->entropy, 0, state->entropy_size);
free(state->entropy);
sfree(state->entropy);
state->collecting_entropy = FALSE;
SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
@ -411,7 +411,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
MAKELPARAM(0, PROGRESSRANGE));
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
params = malloc(sizeof(*params));
params = smalloc(sizeof(*params));
params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
params->dialog = hwnd;
params->keysize = state->keysize;
@ -423,7 +423,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
MessageBox(hwnd, "Out of thread resources",
"Key generation error",
MB_OK | MB_ICONERROR);
free(params);
sfree(params);
} else {
state->generation_thread_exists = TRUE;
}
@ -440,8 +440,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
int len = GetWindowTextLength(editctl);
if (state->key.comment)
free(state->key.comment);
state->key.comment = malloc(len+1);
sfree(state->key.comment);
state->key.comment = smalloc(len+1);
GetWindowText(editctl, state->key.comment, len+1);
}
}
@ -498,7 +498,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
state->entropy_got = 0;
state->entropy_size = (state->entropy_required *
sizeof(*state->entropy));
state->entropy = malloc(state->entropy_size);
state->entropy = smalloc(state->entropy_size);
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
MAKELPARAM(0, state->entropy_required));
@ -589,7 +589,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
ret = loadrsakey(filename, &newkey, &newaux,
passphrase);
} while (ret == -1);
if (comment) free(comment);
if (comment) sfree(comment);
if (ret == 0) {
MessageBox(NULL, "Couldn't load private key.",
"PuTTYgen Error", MB_OK | MB_ICONERROR);
@ -657,7 +657,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
* the user will immediately want to change it, which is
* what we want :-)
*/
state->key.comment = malloc(30);
state->key.comment = smalloc(30);
{
time_t t;
struct tm *tm;
@ -707,7 +707,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
break;
case WM_CLOSE:
state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
free(state);
sfree(state);
EndDialog(hwnd, 1);
return 0;
}

24
puttymem.h Normal file
View File

@ -0,0 +1,24 @@
/*
* PuTTY memory-handling header.
*/
#ifndef PUTTY_PUTTYMEM_H
#define PUTTY_PUTTYMEM_H
/* #define MALLOC_LOG do this if you suspect putty of leaking memory */
#ifdef MALLOC_LOG
#define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
#define srealloc(y,z) (mlog(__FILE__,__LINE__), saferealloc(y,z))
#define sfree(z) (mlog(__FILE__,__LINE__), safefree(z))
void mlog(char *, int);
#else
#define smalloc safemalloc
#define srealloc saferealloc
#define sfree safefree
#endif
void *safemalloc(size_t);
void *saferealloc(void *, size_t);
void safefree(void *);
#endif

6
scp.c
View File

@ -299,8 +299,8 @@ void from_backend(int is_stderr, char *data, int datalen) {
if (len > 0) {
if (pendsize < pendlen + len) {
pendsize = pendlen + len + 4096;
pending = (pending ? realloc(pending, pendsize) :
malloc(pendsize));
pending = (pending ? srealloc(pending, pendsize) :
smalloc(pendsize));
if (!pending)
fatalbox("Out of memory");
}
@ -327,7 +327,7 @@ static int ssh_scp_recv(unsigned char *buf, int len) {
pendlen -= pendused;
if (pendlen == 0) {
pendsize = 0;
free(pending);
sfree(pending);
pending = NULL;
}
if (outlen == 0)

View File

@ -43,7 +43,7 @@ static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
Rectangle(hdc, cr.left, cr.top, cr.right, cr.bottom);
wtlen = GetWindowTextLength(hWnd);
wt = (LPTSTR)malloc((wtlen+1)*sizeof(TCHAR));
wt = (LPTSTR)smalloc((wtlen+1)*sizeof(TCHAR));
GetWindowText(hWnd, wt, wtlen+1);
SetTextColor(hdc, tip_text);
@ -51,7 +51,7 @@ static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
TextOut(hdc, cr.left+3, cr.top+3, wt, wtlen);
free(wt);
sfree(wt);
SelectObject(hdc, holdbr);
DeleteObject(hbr);

82
ssh.c
View File

@ -355,8 +355,8 @@ next_packet:
if (pktin.maxlen < st->biglen) {
pktin.maxlen = st->biglen;
pktin.data = (pktin.data == NULL ? malloc(st->biglen+APIEXTRA) :
realloc(pktin.data, st->biglen+APIEXTRA));
pktin.data = (pktin.data == NULL ? smalloc(st->biglen+APIEXTRA) :
srealloc(pktin.data, st->biglen+APIEXTRA));
if (!pktin.data)
fatalbox("Out of memory");
}
@ -409,13 +409,13 @@ next_packet:
if (pktin.maxlen < st->pad + decomplen) {
pktin.maxlen = st->pad + decomplen;
pktin.data = realloc(pktin.data, pktin.maxlen+APIEXTRA);
pktin.data = srealloc(pktin.data, pktin.maxlen+APIEXTRA);
if (!pktin.data)
fatalbox("Out of memory");
}
memcpy(pktin.body-1, decompblk, decomplen);
free(decompblk);
sfree(decompblk);
pktin.length = decomplen-1;
#if 0
debug(("Packet payload post-decompression:\n"));
@ -475,8 +475,8 @@ next_packet:
if (pktin.maxlen < st->cipherblk) {
pktin.maxlen = st->cipherblk;
pktin.data = (pktin.data == NULL ? malloc(st->cipherblk+APIEXTRA) :
realloc(pktin.data, st->cipherblk+APIEXTRA));
pktin.data = (pktin.data == NULL ? smalloc(st->cipherblk+APIEXTRA) :
srealloc(pktin.data, st->cipherblk+APIEXTRA));
if (!pktin.data)
fatalbox("Out of memory");
}
@ -523,8 +523,8 @@ next_packet:
*/
if (pktin.maxlen < st->packetlen+st->maclen) {
pktin.maxlen = st->packetlen+st->maclen;
pktin.data = (pktin.data == NULL ? malloc(pktin.maxlen+APIEXTRA) :
realloc(pktin.data, pktin.maxlen+APIEXTRA));
pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) :
srealloc(pktin.data, pktin.maxlen+APIEXTRA));
if (!pktin.data)
fatalbox("Out of memory");
}
@ -569,8 +569,8 @@ next_packet:
&newpayload, &newlen)) {
if (pktin.maxlen < newlen+5) {
pktin.maxlen = newlen+5;
pktin.data = (pktin.data == NULL ? malloc(pktin.maxlen+APIEXTRA) :
realloc(pktin.data, pktin.maxlen+APIEXTRA));
pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) :
srealloc(pktin.data, pktin.maxlen+APIEXTRA));
if (!pktin.data)
fatalbox("Out of memory");
}
@ -583,7 +583,7 @@ next_packet:
debug(("\r\n"));
#endif
free(newpayload);
sfree(newpayload);
}
}
@ -609,11 +609,11 @@ static void ssh1_pktout_size(int len) {
#ifdef MSCRYPTOAPI
/* Allocate enough buffer space for extra block
* for MS CryptEncrypt() */
pktout.data = (pktout.data == NULL ? malloc(biglen+12) :
realloc(pktout.data, biglen+12));
pktout.data = (pktout.data == NULL ? smalloc(biglen+12) :
srealloc(pktout.data, biglen+12));
#else
pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
realloc(pktout.data, biglen+4));
pktout.data = (pktout.data == NULL ? smalloc(biglen+4) :
srealloc(pktout.data, biglen+4));
#endif
if (!pktout.data)
fatalbox("Out of memory");
@ -645,7 +645,7 @@ static void s_wrpkt(void) {
&compblk, &complen);
ssh1_pktout_size(complen-1);
memcpy(pktout.body-1, compblk, complen);
free(compblk);
sfree(compblk);
#if 0
debug(("Packet payload post-compression:\n"));
for (i = -1; i < pktout.length; i++)
@ -795,8 +795,8 @@ static void ssh2_pkt_adddata(void *data, int len) {
pktout.length += len;
if (pktout.maxlen < pktout.length) {
pktout.maxlen = pktout.length + 256;
pktout.data = (pktout.data == NULL ? malloc(pktout.maxlen+APIEXTRA) :
realloc(pktout.data, pktout.maxlen+APIEXTRA));
pktout.data = (pktout.data == NULL ? smalloc(pktout.maxlen+APIEXTRA) :
srealloc(pktout.data, pktout.maxlen+APIEXTRA));
if (!pktout.data)
fatalbox("Out of memory");
}
@ -838,7 +838,7 @@ static void ssh2_pkt_addstring(char *data) {
static char *ssh2_mpint_fmt(Bignum b, int *len) {
unsigned char *p;
int i, n = b[0];
p = malloc(n * 2 + 1);
p = smalloc(n * 2 + 1);
if (!p)
fatalbox("out of memory");
p[0] = 0;
@ -859,7 +859,7 @@ static void ssh2_pkt_addmp(Bignum b) {
p = ssh2_mpint_fmt(b, &len);
ssh2_pkt_addstring_start();
ssh2_pkt_addstring_data(p, len);
free(p);
sfree(p);
}
static void ssh2_pkt_send(void) {
int cipherblk, maclen, padding, i;
@ -881,7 +881,7 @@ static void ssh2_pkt_send(void) {
&newpayload, &newlen)) {
pktout.length = 5;
ssh2_pkt_adddata(newpayload, newlen);
free(newpayload);
sfree(newpayload);
}
}
@ -925,7 +925,7 @@ void bndebug(char *string, Bignum b) {
for (i = 0; i < len; i++)
debug((" %02x", p[i]));
debug(("\r\n"));
free(p);
sfree(p);
}
#endif
@ -934,7 +934,7 @@ static void sha_mpint(SHA_State *s, Bignum b) {
int len;
p = ssh2_mpint_fmt(b, &len);
sha_string(s, p, len);
free(p);
sfree(p);
}
/*
@ -1149,7 +1149,7 @@ static char *connect_to_host(char *host, int port, char **realhost)
int FWport;
#endif
savedhost = malloc(1+strlen(host));
savedhost = smalloc(1+strlen(host));
if (!savedhost)
fatalbox("Out of memory");
strcpy(savedhost, host);
@ -1255,7 +1255,7 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
rsabuf = malloc(len);
rsabuf = smalloc(len);
if (!rsabuf)
fatalbox("Out of memory");
@ -1268,13 +1268,13 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
*/
int len = rsastr_len(&hostkey);
char fingerprint[100];
char *keystr = malloc(len);
char *keystr = smalloc(len);
if (!keystr)
fatalbox("Out of memory");
rsastr_fmt(keystr, &hostkey);
rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey);
verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint);
free(keystr);
sfree(keystr);
}
for (i=0; i<32; i++) {
@ -1316,7 +1316,7 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
logevent("Trying to enable encryption...");
free(rsabuf);
sfree(rsabuf);
cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :
cipher_type == SSH_CIPHER_DES ? &ssh_des :
@ -1460,7 +1460,7 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
len += ssh1_bignum_length(challenge);
len += 16; /* session id */
len += 4; /* response format */
agentreq = malloc(4 + len);
agentreq = smalloc(4 + len);
PUT_32BIT(agentreq, len);
q = agentreq + 4;
*q++ = SSH_AGENTC_RSA_CHALLENGE;
@ -1472,13 +1472,13 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
memcpy(q, session_id, 16); q += 16;
PUT_32BIT(q, 1); /* response format */
agent_query(agentreq, len+4, &ret, &retlen);
free(agentreq);
sfree(agentreq);
if (ret) {
if (ret[4] == SSH_AGENT_RSA_RESPONSE) {
logevent("Sending Pageant's response");
send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE,
PKT_DATA, ret+5, 16, PKT_END);
free(ret);
sfree(ret);
crWaitUntil(ispkt);
if (pktin.type == SSH1_SMSG_SUCCESS) {
logevent("Pageant's response accepted");
@ -1493,7 +1493,7 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
logevent("Pageant's response not accepted");
} else {
logevent("Pageant failed to answer challenge");
free(ret);
sfree(ret);
}
} else {
logevent("No reply received from Pageant");
@ -1573,7 +1573,7 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
goto tryauth;
}
sprintf(prompt, "Passphrase for key \"%.100s\": ", comment);
free(comment);
sfree(comment);
}
if (ssh_get_password) {
@ -1817,7 +1817,7 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
break; /* found a free number */
i = c->localid + 1;
}
c = malloc(sizeof(struct ssh_channel));
c = smalloc(sizeof(struct ssh_channel));
c->remoteid = GET_32BIT(pktin.body);
c->localid = i;
c->closes = 0;
@ -1841,7 +1841,7 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
c->closes |= closetype;
if (c->closes == 3) {
del234(ssh_channels, c);
free(c);
sfree(c);
}
}
} else if (pktin.type == SSH1_MSG_CHANNEL_DATA) {
@ -1863,7 +1863,7 @@ static void ssh1_protocol(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 = malloc(c->u.a.totallen);
c->u.a.message = smalloc(c->u.a.totallen);
memcpy(c->u.a.message, c->u.a.msglen, 4);
}
if (c->u.a.lensofar >= 4 && len > 0) {
@ -1889,8 +1889,8 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
PKT_DATA, sentreply, replylen,
PKT_END);
if (reply)
free(reply);
free(c->u.a.message);
sfree(reply);
sfree(c->u.a.message);
c->u.a.lensofar = 0;
}
}
@ -2242,8 +2242,8 @@ static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt)
keystr, fingerprint);
logevent("Host key fingerprint is:");
logevent(fingerprint);
free(fingerprint);
free(keystr);
sfree(fingerprint);
sfree(keystr);
hostkey->freekey(hkey);
/*
@ -2442,7 +2442,7 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
/*
* So now create a channel with a session in it.
*/
mainchan = malloc(sizeof(struct ssh_channel));
mainchan = smalloc(sizeof(struct ssh_channel));
mainchan->localid = 100; /* as good as any */
ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
ssh2_pkt_addstring("session");

2
ssh.h
View File

@ -1,5 +1,7 @@
#include <string.h>
#include "puttymem.h"
/*
* Useful thing.
*/

52
sshbn.c
View File

@ -28,7 +28,7 @@ unsigned short bnOne[2] = { 1, 1 };
Bignum Zero = bnZero, One = bnOne;
Bignum newbn(int length) {
Bignum b = malloc((length+1)*sizeof(unsigned short));
Bignum b = smalloc((length+1)*sizeof(unsigned short));
if (!b)
abort(); /* FIXME */
memset(b, 0, (length+1)*sizeof(*b));
@ -37,7 +37,7 @@ Bignum newbn(int length) {
}
Bignum copybn(Bignum orig) {
Bignum b = malloc((orig[0]+1)*sizeof(unsigned short));
Bignum b = smalloc((orig[0]+1)*sizeof(unsigned short));
if (!b)
abort(); /* FIXME */
memcpy(b, orig, (orig[0]+1)*sizeof(*b));
@ -49,7 +49,7 @@ void freebn(Bignum b) {
* Burn the evidence, just in case.
*/
memset(b, 0, sizeof(b[0]) * (b[0] + 1));
free(b);
sfree(b);
}
/*
@ -194,7 +194,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 = malloc(mlen * sizeof(unsigned short));
m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */
@ -207,14 +207,14 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
}
/* Allocate n of size mlen, copy base to n */
n = malloc(mlen * sizeof(unsigned short));
n = smalloc(mlen * sizeof(unsigned short));
i = mlen - base[0];
for (j = 0; j < i; j++) n[j] = 0;
for (j = 0; j < base[0]; j++) n[i+j] = base[base[0] - j];
/* Allocate a and b of size 2*mlen. Set a = 1 */
a = malloc(2 * mlen * sizeof(unsigned short));
b = malloc(2 * mlen * sizeof(unsigned short));
a = smalloc(2 * mlen * sizeof(unsigned short));
b = smalloc(2 * mlen * sizeof(unsigned short));
for (i = 0; i < 2*mlen; i++) a[i] = 0;
a[2*mlen-1] = 1;
@ -259,10 +259,10 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
while (result[0] > 1 && result[result[0]] == 0) result[0]--;
/* Free temporary arrays */
for (i = 0; i < 2*mlen; i++) a[i] = 0; free(a);
for (i = 0; i < 2*mlen; i++) b[i] = 0; free(b);
for (i = 0; i < mlen; i++) m[i] = 0; free(m);
for (i = 0; i < mlen; i++) n[i] = 0; free(n);
for (i = 0; i < 2*mlen; i++) a[i] = 0; sfree(a);
for (i = 0; i < 2*mlen; i++) b[i] = 0; sfree(b);
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
for (i = 0; i < mlen; i++) n[i] = 0; sfree(n);
return result;
}
@ -282,7 +282,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 = malloc(mlen * sizeof(unsigned short));
m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */
@ -297,19 +297,19 @@ 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 = malloc(pqlen * sizeof(unsigned short));
n = smalloc(pqlen * sizeof(unsigned short));
i = pqlen - p[0];
for (j = 0; j < i; j++) n[j] = 0;
for (j = 0; j < p[0]; j++) n[i+j] = p[p[0] - j];
/* Allocate o of size pqlen, copy q to o */
o = malloc(pqlen * sizeof(unsigned short));
o = smalloc(pqlen * sizeof(unsigned short));
i = pqlen - q[0];
for (j = 0; j < i; j++) o[j] = 0;
for (j = 0; j < q[0]; j++) o[i+j] = q[q[0] - j];
/* Allocate a of size 2*pqlen for result */
a = malloc(2 * pqlen * sizeof(unsigned short));
a = smalloc(2 * pqlen * sizeof(unsigned short));
/* Main computation */
internal_mul(n, o, a, pqlen);
@ -332,10 +332,10 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
while (result[0] > 1 && result[result[0]] == 0) result[0]--;
/* Free temporary arrays */
for (i = 0; i < 2*pqlen; i++) a[i] = 0; free(a);
for (i = 0; i < mlen; i++) m[i] = 0; free(m);
for (i = 0; i < pqlen; i++) n[i] = 0; free(n);
for (i = 0; i < pqlen; i++) o[i] = 0; free(o);
for (i = 0; i < 2*pqlen; i++) a[i] = 0; sfree(a);
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
for (i = 0; i < pqlen; i++) n[i] = 0; sfree(n);
for (i = 0; i < pqlen; i++) o[i] = 0; sfree(o);
return result;
}
@ -355,7 +355,7 @@ void bigmod(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 = malloc(mlen * sizeof(unsigned short));
m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */
@ -372,7 +372,7 @@ void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
if (plen <= mlen) plen = mlen+1;
/* Allocate n of size plen, copy p to n */
n = malloc(plen * sizeof(unsigned short));
n = smalloc(plen * sizeof(unsigned short));
for (j = 0; j < plen; j++) n[j] = 0;
for (j = 1; j <= p[0]; j++) n[plen-j] = p[j];
@ -396,8 +396,8 @@ void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
}
/* Free temporary arrays */
for (i = 0; i < mlen; i++) m[i] = 0; free(m);
for (i = 0; i < plen; i++) n[i] = 0; free(n);
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
for (i = 0; i < plen; i++) n[i] = 0; sfree(n);
}
/*
@ -574,7 +574,7 @@ Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
Bignum ret;
/* mlen space for a, mlen space for b, 2*mlen for result */
workspace = malloc(mlen * 4 * sizeof(unsigned short));
workspace = smalloc(mlen * 4 * sizeof(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);
@ -787,14 +787,14 @@ char *bignum_decimal(Bignum x) {
i = ssh1_bignum_bitcount(x);
ndigits = (28*i + 92)/93; /* multiply by 28/93 and round up */
ndigits++; /* allow for trailing \0 */
ret = malloc(ndigits);
ret = smalloc(ndigits);
/*
* 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 = malloc(sizeof(unsigned short) * x[0]);
workspace = smalloc(sizeof(unsigned short) * x[0]);
for (i = 0; i < x[0]; i++)
workspace[i] = x[x[0] - i];

View File

@ -102,7 +102,7 @@ static void *dss_newkey(char *data, int len) {
int slen;
struct dss_key *dss;
dss = malloc(sizeof(struct dss_key));
dss = smalloc(sizeof(struct dss_key));
if (!dss) return NULL;
getstring(&data, &len, &p, &slen);
@ -117,7 +117,7 @@ static void *dss_newkey(char *data, int len) {
#endif
if (!p || memcmp(p, "ssh-dss", 7)) {
free(dss);
sfree(dss);
return NULL;
}
dss->p = getmp(&data, &len);
@ -134,7 +134,7 @@ static void dss_freekey(void *key) {
freebn(dss->q);
freebn(dss->g);
freebn(dss->y);
free(dss);
sfree(dss);
}
static char *dss_fmtkey(void *key) {
@ -146,7 +146,7 @@ static char *dss_fmtkey(void *key) {
return NULL;
len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
len += 4 * (dss->p[0] + dss->q[0] + dss->g[0] + dss->y[0]); /* digits */
p = malloc(len);
p = smalloc(len);
if (!p) return NULL;
pos = 0;
@ -199,7 +199,7 @@ static char *dss_fingerprint(void *key) {
sprintf(buffer, "%d ", ssh1_bignum_bitcount(dss->p));
for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
ret = malloc(strlen(buffer)+1);
ret = smalloc(strlen(buffer)+1);
if (ret)
strcpy(ret, buffer);
return ret;

View File

@ -77,7 +77,7 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key, struct RSAAux *aux,
j = GET_32BIT(buf+i);
i += 4;
if (len-i < j) goto end;
comment = malloc(j+1);
comment = smalloc(j+1);
if (comment) {
memcpy(comment, buf+i, j);
comment[j] = '\0';

View File

@ -169,5 +169,5 @@ void freersakey(struct RSAKey *key) {
if (key->modulus) freebn(key->modulus);
if (key->exponent) freebn(key->exponent);
if (key->private_exponent) freebn(key->private_exponent);
if (key->comment) free(key->comment);
if (key->comment) sfree(key->comment);
}

View File

@ -122,7 +122,7 @@ static int lz77_init(struct LZ77Context *ctx) {
struct LZ77InternalContext *st;
int i;
st = (struct LZ77InternalContext *)malloc(sizeof(*st));
st = (struct LZ77InternalContext *)smalloc(sizeof(*st));
if (!st)
return 0;
@ -345,7 +345,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 = realloc(out->outbuf, out->outsize);
out->outbuf = srealloc(out->outbuf, out->outsize);
}
out->outbuf[out->outlen++] = (unsigned char)(out->outbits & 0xFF);
out->outbits >>= 8;
@ -560,7 +560,7 @@ void zlib_compress_init(void) {
ectx.literal = zlib_literal;
ectx.match = zlib_match;
out = malloc(sizeof(struct Outbuf));
out = smalloc(sizeof(struct Outbuf));
out->outbits = out->noutbits = 0;
out->firstblock = 1;
ectx.userdata = out;
@ -665,11 +665,11 @@ struct zlib_table {
static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
int nsyms,
int pfx, int pfxbits, int bits) {
struct zlib_table *tab = malloc(sizeof(struct zlib_table));
struct zlib_table *tab = smalloc(sizeof(struct zlib_table));
int pfxmask = (1 << pfxbits) - 1;
int nbits, i, j, code;
tab->table = malloc((1 << bits) * sizeof(struct zlib_tableentry));
tab->table = smalloc((1 << bits) * sizeof(struct zlib_tableentry));
tab->mask = (1 << bits) - 1;
for (code = 0; code <= tab->mask; code++) {
@ -778,7 +778,7 @@ void zlib_decompress_init(void) {
memset(lengths, 5, 32);
dctx.staticdisttable = zlib_mktable(lengths, 32);
dctx.state = START; /* even before header */
dctx.currlentable = dctx.currdisttable = NULL;
dctx.currlentable = dctx.currdisttable = dctx.lenlentable = NULL;
dctx.bits = 0;
dctx.nbits = 0;
logevent("Initialised zlib (RFC1950) decompression");
@ -809,7 +809,7 @@ static void zlib_emit_char(int c) {
dctx.winpos = (dctx.winpos + 1) & (WINSIZE-1);
if (dctx.outlen >= dctx.outsize) {
dctx.outsize = dctx.outlen + 512;
dctx.outblk = realloc(dctx.outblk, dctx.outsize);
dctx.outblk = srealloc(dctx.outblk, dctx.outsize);
}
dctx.outblk[dctx.outlen++] = c;
}

View File

@ -6,10 +6,12 @@
#include <stdio.h>
#include <stdlib.h>
#include "puttymem.h"
#include "tree234.h"
#define mknew(typ) ( (typ *) malloc (sizeof (typ)) )
#define sfree free
#define mknew(typ) ( (typ *) smalloc (sizeof (typ)) )
/* #define sfree free */
#ifdef TEST
#define LOG(x) (printf x)
@ -819,8 +821,8 @@ void addtest(void *elem) {
if (arraysize < arraylen+1) {
arraysize = arraylen+1+256;
array = (array == NULL ? malloc(arraysize*sizeof(*array)) :
realloc(array, arraysize*sizeof(*array)));
array = (array == NULL ? smalloc(arraysize*sizeof(*array)) :
srealloc(array, arraysize*sizeof(*array)));
}
i = 0;

View File

@ -66,7 +66,7 @@ static int CALLBACK LogProc (HWND hwnd, UINT msg,
int *selitems;
selcount = SendDlgItemMessage(hwnd, IDN_LIST,
LB_GETSELCOUNT, 0, 0);
selitems = malloc(selcount * sizeof(int));
selitems = smalloc(selcount * sizeof(int));
if (selitems) {
int count = SendDlgItemMessage(hwnd, IDN_LIST,
LB_GETSELITEMS,
@ -85,7 +85,7 @@ static int CALLBACK LogProc (HWND hwnd, UINT msg,
for (i = 0; i < count; i++)
size += strlen(events[selitems[i]]) + sizeof(sel_nl);
clipdata = malloc(size);
clipdata = smalloc(size);
if (clipdata) {
char *p = clipdata;
for (i = 0; i < count; i++) {
@ -97,9 +97,9 @@ static int CALLBACK LogProc (HWND hwnd, UINT msg,
p += sizeof(sel_nl);
}
write_clip(clipdata, size, TRUE);
free(clipdata);
sfree(clipdata);
}
free(selitems);
sfree(selitems);
for (i = 0; i < nevents; i++)
SendDlgItemMessage(hwnd, IDN_LIST, LB_SETSEL,

View File

@ -413,9 +413,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
{
char *bits;
int size = (font_width+15)/16 * 2 * font_height;
bits = calloc(size, 1);
bits = smalloc(size);
memset(bits, 0, size);
caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
free(bits);
sfree(bits);
}
/*
@ -1130,7 +1131,7 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
cl = c;
} else if (wParam == IDM_SAVEDSESS) {
char *session = sessions[(lParam - IDM_SAVED_MIN) / 16];
cl = malloc(16 + strlen(session)); /* 8, but play safe */
cl = smalloc(16 + strlen(session)); /* 8, but play safe */
if (!cl)
cl = NULL; /* not a very important failure mode */
else {
@ -1154,7 +1155,7 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
if (filemap)
CloseHandle(filemap);
if (freecl)
free(cl);
sfree(cl);
}
break;
case IDM_RECONF:

View File

@ -163,7 +163,7 @@ void sk_close(Socket s) {
del234(sktree, s);
do_select(s->s, 0);
closesocket(s->s);
free(s);
sfree(s);
}
char *winsock_error_string(int error) {
@ -258,7 +258,7 @@ void try_send(Socket s) {
if (s->head->bufpos >= s->head->buflen) {
struct buffer *tmp = s->head;
s->head = tmp->next;
free(tmp);
sfree(tmp);
if (!s->head)
s->tail = NULL;
}
@ -312,7 +312,7 @@ void sk_write_oob(Socket s, char *buf, int len) {
while (walk) {
struct buffer *tmp = walk;
walk = tmp->next;
free(tmp);
sfree(tmp);
}
}
s->head->next = NULL;

View File

@ -58,16 +58,16 @@ void *open_settings_w(char *sessionname) {
int ret;
char *p;
p = malloc(3*strlen(sessionname)+1);
p = smalloc(3*strlen(sessionname)+1);
mungestr(sessionname, p);
ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
if (ret != ERROR_SUCCESS) {
free(p);
sfree(p);
return NULL;
}
ret = RegCreateKey(subkey1, p, &sesskey);
free(p);
sfree(p);
RegCloseKey(subkey1);
if (ret != ERROR_SUCCESS)
return NULL;
@ -93,7 +93,7 @@ void *open_settings_r(char *sessionname) {
HKEY subkey1, sesskey;
char *p;
p = malloc(3*strlen(sessionname)+1);
p = smalloc(3*strlen(sessionname)+1);
mungestr(sessionname, p);
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
@ -105,7 +105,7 @@ void *open_settings_r(char *sessionname) {
RegCloseKey(subkey1);
}
free(p);
sfree(p);
return (void *)sesskey;
}
@ -147,10 +147,10 @@ void del_settings (char *sessionname) {
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
return;
p = malloc(3*strlen(sessionname)+1);
p = smalloc(3*strlen(sessionname)+1);
mungestr(sessionname, p);
RegDeleteKey(subkey1, p);
free(p);
sfree(p);
RegCloseKey(subkey1);
}
@ -167,7 +167,7 @@ void *enum_settings_start(void) {
if (RegCreateKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
return NULL;
ret = malloc(sizeof(*ret));
ret = smalloc(sizeof(*ret));
if (ret) {
ret->key = key;
ret->i = 0;
@ -179,11 +179,11 @@ void *enum_settings_start(void) {
char *enum_settings_next(void *handle, char *buffer, int buflen) {
struct enumsettings *e = (struct enumsettings *)handle;
char *otherbuf;
otherbuf = malloc(3*buflen);
otherbuf = smalloc(3*buflen);
if (otherbuf && RegEnumKey(e->key, e->i++, otherbuf,
3*buflen) == ERROR_SUCCESS) {
unmungestr(otherbuf, buffer, buflen);
free(otherbuf);
sfree(otherbuf);
return buffer;
} else
return NULL;
@ -193,7 +193,7 @@ char *enum_settings_next(void *handle, char *buffer, int buflen) {
void enum_settings_finish(void *handle) {
struct enumsettings *e = (struct enumsettings *)handle;
RegCloseKey(e->key);
free(e);
sfree(e);
}
static void hostkey_regname(char *buffer, char *hostname,