From abec9e1c7eaefa877404b0786e7497fd3bbf71dc Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 27 Nov 2018 19:20:32 +0000 Subject: [PATCH] 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. --- Recipe | 16 +++---- memory.c | 104 +++++++++++++++++++++++++++++++++++++++++++++ misc.c | 102 -------------------------------------------- windows/winstuff.h | 11 +++++ 4 files changed, 123 insertions(+), 110 deletions(-) create mode 100644 memory.c diff --git a/Recipe b/Recipe index 0c4fd582..43c6582e 100644 --- a/Recipe +++ b/Recipe @@ -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 diff --git a/memory.c b/memory.c new file mode 100644 index 00000000..ac77e454 --- /dev/null +++ b/memory.c @@ -0,0 +1,104 @@ +/* + * PuTTY's memory allocation wrappers. + */ + +#include + +#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 +} + diff --git a/misc.c b/misc.c index 7e3acd71..2aa53126 100644 --- a/misc.c +++ b/misc.c @@ -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. */ diff --git a/windows/winstuff.h b/windows/winstuff.h index cd128156..f6b6e6a6 100644 --- a/windows/winstuff.h +++ b/windows/winstuff.h @@ -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