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

Move the malloc helpers out of misc.c.

Now they live in their own file memory.c. The advantage of this is
that you can link them into a binary without also pulling in the rest
of misc.c with its various dependencies on other parts of the code,
such as conf.c.
This commit is contained in:
Simon Tatham 2018-11-27 19:20:32 +00:00
parent 1586a41656
commit abec9e1c7e
4 changed files with 123 additions and 110 deletions

16
Recipe
View File

@ -271,7 +271,7 @@ SFTP = sftp sftpcommon logging cmdline
# Miscellaneous objects appearing in all the utilities, or all the
# network ones, or the Unix or Windows subsets of those in turn.
MISC = misc marshal
MISC = misc marshal memory
MISCNETCOMMON = timing callback MISC version tree234 CONF
MISCNET = MISCNETCOMMON be_misc settings proxy
WINMISC = MISCNET winstore winnet winhandl cmdline windefs winmisc winproxy
@ -337,13 +337,13 @@ puttygen : [G] winpgen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version
pterm : [X] GTKTERM uxmisc misc ldisc settings uxpty uxsel BE_NONE uxstore
+ uxsignal CHARSET cmdline uxpterm version time xpmpterm xpmptcfg
+ nogss GTKMAIN
+ nogss memory GTKMAIN
putty : [X] GTKTERM uxmisc misc ldisc settings uxsel U_BE_ALL uxstore
+ uxsignal CHARSET uxputty NONSSH UXSSH UXMISC ux_x11 xpmputty
+ xpmpucfg GTKMAIN
+ xpmpucfg memory GTKMAIN
puttytel : [X] GTKTERM uxmisc misc ldisc settings uxsel U_BE_NOSSH
+ uxstore uxsignal CHARSET uxputty NONSSH UXMISC xpmputty xpmpucfg
+ nogss GTKMAIN
+ nogss memory GTKMAIN
plink : [U] uxplink uxcons NONSSH UXSSH U_BE_ALL logging UXMISC uxsignal
+ ux_x11 noterm uxnogtk sessprep cmdline
@ -361,18 +361,18 @@ psftp : [U] psftp uxsftp uxcons UXSSH BE_SSH SFTP wildcard UXMISC uxnogtk
pageant : [X] uxpgnt uxagentc aqsync pageant sshrsa sshpubk sshdes sshbn
+ sshmd5 version tree234 misc sshaes sshsha sshdss sshsh256 sshsh512
+ sshecc CONF uxsignal nocproxy nogss be_none x11fwd ux_x11 uxcons
+ gtkask gtkmisc nullplug logging UXMISC uxagentsock
+ gtkask gtkmisc nullplug logging UXMISC uxagentsock memory
ptermapp : [XT] GTKTERM uxmisc misc ldisc settings uxpty uxsel BE_NONE uxstore
+ uxsignal CHARSET uxpterm version time xpmpterm xpmptcfg
+ nogss gtkapp nocmdline
+ nogss gtkapp nocmdline memory
puttyapp : [XT] GTKTERM uxmisc misc ldisc settings uxsel U_BE_ALL uxstore
+ uxsignal CHARSET uxputty NONSSH UXSSH UXMISC ux_x11 xpmputty
+ xpmpucfg gtkapp nocmdline
+ xpmpucfg gtkapp nocmdline memory
osxlaunch : [UT] osxlaunch
fuzzterm : [UT] UXTERM CHARSET misc version uxmisc uxucs fuzzterm time settings
+ uxstore be_none uxnogtk
+ uxstore be_none uxnogtk memory
testbn : [UT] testbn sshbn MISC version CONF tree234 uxmisc uxnogtk
testbn : [C] testbn sshbn MISC version CONF tree234 winmisc LIBS

104
memory.c Normal file
View File

@ -0,0 +1,104 @@
/*
* PuTTY's memory allocation wrappers.
*/
#include <stdlib.h>
#include "putty.h"
void *safemalloc(size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (size == 0) size = 1;
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "malloc(%d) returns %p\n", size, p);
#endif
return p;
}
void *saferealloc(void *ptr, size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (!ptr) {
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
} else {
#ifdef MINEFIELD
p = minefield_c_realloc(ptr, size);
#else
p = realloc(ptr, size);
#endif
}
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
#endif
return p;
}
void safefree(void *ptr)
{
if (ptr) {
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "free(%p)\n", ptr);
#endif
#ifdef MINEFIELD
minefield_c_free(ptr);
#else
free(ptr);
#endif
}
#ifdef MALLOC_LOG
else if (fp)
fprintf(fp, "freeing null pointer - no action taken\n");
#endif
}

102
misc.c
View File

@ -850,12 +850,6 @@ void sanitise_term_data(bufchain *out, const void *vdata, int len)
* one.
*/
#ifdef MINEFIELD
void *minefield_c_malloc(size_t size);
void minefield_c_free(void *p);
void *minefield_c_realloc(void *p, size_t size);
#endif
#ifdef MALLOC_LOG
static FILE *fp = NULL;
@ -875,102 +869,6 @@ void mlog(char *file, int line)
}
#endif
void *safemalloc(size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (size == 0) size = 1;
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "malloc(%d) returns %p\n", size, p);
#endif
return p;
}
void *saferealloc(void *ptr, size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (!ptr) {
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
} else {
#ifdef MINEFIELD
p = minefield_c_realloc(ptr, size);
#else
p = realloc(ptr, size);
#endif
}
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
#endif
return p;
}
void safefree(void *ptr)
{
if (ptr) {
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "free(%p)\n", ptr);
#endif
#ifdef MINEFIELD
minefield_c_free(ptr);
#else
free(ptr);
#endif
}
#ifdef MALLOC_LOG
else if (fp)
fprintf(fp, "freeing null pointer - no action taken\n");
#endif
}
/* ----------------------------------------------------------------------
* Debugging routines.
*/

View File

@ -693,4 +693,15 @@ char *get_jumplist_registry_entries(void);
#define CLIPUI_DEFAULT_MOUSE CLIPUI_EXPLICIT
#define CLIPUI_DEFAULT_INS CLIPUI_EXPLICIT
#ifdef MINEFIELD
/*
* Definitions for Minefield, PuTTY's own Windows-specific malloc
* debugger in the style of Electric Fence. Implemented in winmisc.c,
* and referred to by the main malloc wrappers in memory.c.
*/
void *minefield_c_malloc(size_t size);
void minefield_c_free(void *p);
void *minefield_c_realloc(void *p, size_t size);
#endif
#endif