diff --git a/configure.ac b/configure.ac index 5cf46f7d..10bf3bbf 100644 --- a/configure.ac +++ b/configure.ac @@ -371,11 +371,7 @@ fi AC_MSG_NOTICE([checking for libraries]) AC_FUNC_FSEEKO - -AC_REPLACE_FUNCS(vasprintf) -if test "$ac_cv_func_vasprintf" != yes; then - GNUPG_CHECK_VA_COPY -fi +GNUPG_CHECK_VA_COPY # Try to find a thread-safe version of ttyname(). gnupg_REPLACE_TTYNAME_R diff --git a/doc/gpgme.texi b/doc/gpgme.texi index b23e7b11..1df669b1 100644 --- a/doc/gpgme.texi +++ b/doc/gpgme.texi @@ -129,6 +129,7 @@ Preparation * Largefile Support (LFS):: How to use @acronym{GPGME} with LFS. * Using Automake:: Compiler options to be used the easy way. * Using Libtool:: Avoiding compiler options entirely. +* Custom Memory Functions:: Using your own memory allocators. * Library Version Check:: Getting and verifying the library version. * Signal Handling:: How @acronym{GPGME} affects signal handling. * Multi Threading:: How @acronym{GPGME} can be used in an MT environment. @@ -365,6 +366,7 @@ of the library are verified. * Largefile Support (LFS):: How to use @acronym{GPGME} with LFS. * Using Automake:: Compiler options to be used the easy way. * Using Libtool:: Avoiding compiler options entirely. +* Custom Memory Functions:: Using your own memory allocators. * Library Version Check:: Getting and verifying the library version. * Signal Handling:: How @acronym{GPGME} affects signal handling. * Multi Threading:: How @acronym{GPGME} can be used in an MT environment. @@ -618,6 +620,50 @@ link to @code{libgpgme.la}, @code{libgpgme-pth.la} or automatically by Libtool. +@node Custom Memory Functions +@section Custom Memory Functions +@cindex Using your own memory allocators + +If your application uses its own memory allocation functions or wrappers +it is good idea to tell @code{libgpgme} about it so it can make use of the +same functions or wrappers: + +@deftp {Data type} {struct gpgme_malloc_hooks} +This structure is used to store the memory allocation callback +interface functions. It has the following members, whose semantics +are identical to the corresponding system functions: + +@table @code +@item void *(*malloc) (size_t cnt) +This is the function called by @acronym{GPGME} to allocate memory for a +context. + +@item void *(*calloc) (size_t nmemb, size_t cnt) +This is the function called by @acronym{GPGME} to allocate memory for a +context and zero its contents. + +@item void *(*realloc) (void *ptr, size_t cnt) +This is the function called by @acronym{GPGME} to reallocate memory for a +context. + +@item void (*free) (void *ptr) +This is the function called by @acronym{GPGME} to release memory for a +context. +@end table +@end deftp + +@deftp {Data type} {gpgme_malloc_hooks_t} +This is a pointer to a @code{struct gpgme_malloc_hooks}. +@end deftp + +@deftypefun void gpgme_set_global_malloc_hooks (@w{gpgme_malloc_hooks_t @var{malloc_hooks}}) +This function sets the default allocation hooks for all memory operations. +You need to provide all four functions. Those functions need to behave +exactly as their standard counterparts @code{malloc}, @code{calloc}, +@code{realloc} and @code{free}. If you write your own functions, please take +care to set @code{errno} whenever an error has occurred. +@end deftypefun + @node Library Version Check @section Library Version Check @cindex version check, of the library diff --git a/src/Makefile.am b/src/Makefile.am index b7ddbc13..c5d76933 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -99,7 +99,8 @@ main_sources = \ engine-spawn.c \ gpgconf.c \ sema.h priv-io.h $(system_components) sys-util.h dirinfo.c \ - debug.c debug.h gpgme.c version.c error.c + debug.c debug.h gpgme.c version.c error.c mem.c mem.h \ + vasprintf.c libgpgme_la_SOURCES = $(main_sources) \ ath.h ath.c $(system_components_not_extra) diff --git a/src/assuan-support.c b/src/assuan-support.c index 745d2aa5..99a73a7a 100644 --- a/src/assuan-support.c +++ b/src/assuan-support.c @@ -12,13 +12,32 @@ #include "ath.h" #include "priv-io.h" #include "debug.h" +#include "mem.h" +static void * +_gpgme_assuan_malloc (size_t size) +{ + return _gpgme_malloc (size); +} + +static void * +_gpgme_assuan_realloc (void *p, size_t size) +{ + return _gpgme_realloc (p, size); +} + +static void +_gpgme_assuan_free (void *p) +{ + _gpgme_free (p); +} + struct assuan_malloc_hooks _gpgme_assuan_malloc_hooks = { - malloc, - realloc, - free + _gpgme_assuan_malloc, + _gpgme_assuan_realloc, + _gpgme_assuan_free }; @@ -140,7 +159,7 @@ my_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name, } /* fd_in, fd_out, terminator */ i += 3; - fd_items = calloc (i, sizeof (struct spawn_fd_item_s)); + fd_items = _gpgme_calloc (i, sizeof (struct spawn_fd_item_s)); if (! fd_items) return -1; i = 0; @@ -184,7 +203,7 @@ my_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name, } } } - free (fd_items); + _gpgme_free (fd_items); return err; } diff --git a/src/ath-pthread.c b/src/ath-pthread.c index 47b38ee0..6206b169 100644 --- a/src/ath-pthread.c +++ b/src/ath-pthread.c @@ -42,6 +42,7 @@ #include "gpgme.h" #include "ath.h" +#include "mem.h" /* The lock we take while checking for lazy lock initialization. */ @@ -58,14 +59,14 @@ mutex_pthread_init (ath_mutex_t *priv, int just_check) pthread_mutex_lock (&check_init_lock); if (!*priv || !just_check) { - pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); + pthread_mutex_t *lock = _gpgme_malloc (sizeof (pthread_mutex_t)); if (!lock) err = ENOMEM; if (!err) { err = pthread_mutex_init (lock, NULL); if (err) - free (lock); + _gpgme_free (lock); else *priv = (ath_mutex_t) lock; } @@ -104,7 +105,7 @@ ath_mutex_destroy (ath_mutex_t *lock) if (!err) { err = pthread_mutex_destroy ((pthread_mutex_t *) *lock); - free (*lock); + _gpgme_free (*lock); } return err; } diff --git a/src/conversion.c b/src/conversion.c index d04a6bef..cf35ce8f 100644 --- a/src/conversion.c +++ b/src/conversion.c @@ -35,6 +35,7 @@ #include "gpgme.h" #include "util.h" #include "debug.h" +#include "mem.h" #define atoi_1(p) (*(p) - '0' ) #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1)) @@ -93,7 +94,7 @@ _gpgme_decode_c_string (const char *src, char **destp, size_t len) { /* The converted string will never be larger than the original string. */ - dest = malloc (strlen (src) + 1); + dest = _gpgme_malloc (strlen (src) + 1); if (!dest) return gpg_error_from_syserror (); @@ -198,7 +199,7 @@ _gpgme_decode_percent_string (const char *src, char **destp, size_t len, { /* The converted string will never be larger than the original string. */ - dest = malloc (strlen (src) + 1); + dest = _gpgme_malloc (strlen (src) + 1); if (!dest) return gpg_error_from_syserror (); @@ -291,7 +292,7 @@ _gpgme_encode_percent_string (const char *src, char **destp, size_t len) { /* The converted string will never be larger than the original string. */ - dest = malloc (destlen); + dest = _gpgme_malloc (destlen); if (!dest) return gpg_error_from_syserror (); diff --git a/src/data-compat.c b/src/data-compat.c index 99827f16..57b2a228 100644 --- a/src/data-compat.c +++ b/src/data-compat.c @@ -34,6 +34,7 @@ #include "data.h" #include "util.h" #include "debug.h" +#include "mem.h" /* Create a new data buffer filled with LENGTH bytes starting from @@ -77,7 +78,7 @@ gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, return TRACE_ERR (saved_err); } - buf = malloc (length); + buf = _gpgme_malloc (length); if (!buf) { int saved_err = gpg_error_from_syserror (); @@ -92,7 +93,7 @@ gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, { int saved_err = gpg_error_from_syserror (); if (buf) - free (buf); + _gpgme_free (buf); if (fname) fclose (stream); return TRACE_ERR (saved_err); @@ -105,7 +106,7 @@ gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, if (err) { if (buf) - free (buf); + _gpgme_free (buf); return err; } diff --git a/src/data-identify.c b/src/data-identify.c index 96006335..46d96505 100644 --- a/src/data-identify.c +++ b/src/data-identify.c @@ -28,6 +28,7 @@ #include "data.h" #include "util.h" #include "parsetlv.h" +#include "mem.h" /* The size of the sample data we take for detection. */ #define SAMPLE_SIZE 2048 @@ -228,19 +229,19 @@ gpgme_data_identify (gpgme_data_t dh, int reserved) return GPGME_DATA_TYPE_INVALID; /* Allocate a buffer and read the data. */ - sample = malloc (SAMPLE_SIZE); + sample = _gpgme_malloc (SAMPLE_SIZE); if (!sample) return GPGME_DATA_TYPE_INVALID; /* Ooops. */ n = gpgme_data_read (dh, sample, SAMPLE_SIZE - 1); if (n < 0) { - free (sample); + _gpgme_free (sample); return GPGME_DATA_TYPE_INVALID; /* Ooops. */ } sample[n] = 0; /* (Required for our string functions.) */ result = basic_detection (sample, n); - free (sample); + _gpgme_free (sample); gpgme_data_seek (dh, off, SEEK_SET); return result; diff --git a/src/data-mem.c b/src/data-mem.c index e06a920c..b00e494e 100644 --- a/src/data-mem.c +++ b/src/data-mem.c @@ -33,6 +33,7 @@ #include "data.h" #include "util.h" #include "debug.h" +#include "mem.h" static gpgme_ssize_t @@ -67,7 +68,7 @@ mem_write (gpgme_data_t dh, const void *buffer, size_t size) if (new_size < dh->data.mem.offset + size) new_size = dh->data.mem.offset + size; - new_buffer = malloc (new_size); + new_buffer = _gpgme_malloc (new_size); if (!new_buffer) return -1; memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length); @@ -88,12 +89,12 @@ mem_write (gpgme_data_t dh, const void *buffer, size_t size) if (new_size < dh->data.mem.offset + size) new_size = dh->data.mem.offset + size; - new_buffer = realloc (dh->data.mem.buffer, new_size); + new_buffer = _gpgme_realloc (dh->data.mem.buffer, new_size); if (!new_buffer && new_size > dh->data.mem.offset + size) { /* Maybe we were too greedy, try again. */ new_size = dh->data.mem.offset + size; - new_buffer = realloc (dh->data.mem.buffer, new_size); + new_buffer = _gpgme_realloc (dh->data.mem.buffer, new_size); } if (!new_buffer) return -1; @@ -151,7 +152,7 @@ static void mem_release (gpgme_data_t dh) { if (dh->data.mem.buffer) - free (dh->data.mem.buffer); + _gpgme_free (dh->data.mem.buffer); } @@ -199,7 +200,7 @@ gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer, if (copy) { - char *bufcpy = malloc (size); + char *bufcpy = _gpgme_malloc (size); if (!bufcpy) { int saved_err = gpg_error_from_syserror (); @@ -239,7 +240,7 @@ gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len) str = dh->data.mem.buffer; if (!str && dh->data.mem.orig_buffer) { - str = malloc (dh->data.mem.length); + str = _gpgme_malloc (dh->data.mem.length); if (!str) { int saved_err = gpg_error_from_syserror (); @@ -278,5 +279,5 @@ gpgme_free (void *buffer) TRACE (DEBUG_DATA, "gpgme_free", buffer); if (buffer) - free (buffer); + _gpgme_free (buffer); } diff --git a/src/data.c b/src/data.c index 7123a82b..144c1667 100644 --- a/src/data.c +++ b/src/data.c @@ -35,6 +35,7 @@ #include "ops.h" #include "priv-io.h" #include "debug.h" +#include "mem.h" gpgme_error_t @@ -46,7 +47,7 @@ _gpgme_data_new (gpgme_data_t *r_dh, struct _gpgme_data_cbs *cbs) return gpg_error (GPG_ERR_INV_VALUE); *r_dh = NULL; - dh = calloc (1, sizeof (*dh)); + dh = _gpgme_calloc (1, sizeof (*dh)); if (!dh) return gpg_error_from_syserror (); @@ -64,8 +65,8 @@ _gpgme_data_release (gpgme_data_t dh) return; if (dh->file_name) - free (dh->file_name); - free (dh); + _gpgme_free (dh->file_name); + _gpgme_free (dh); } @@ -212,11 +213,11 @@ gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (dh->file_name) - free (dh->file_name); + _gpgme_free (dh->file_name); if (file_name) { - dh->file_name = strdup (file_name); + dh->file_name = _gpgme_strdup (file_name); if (!dh->file_name) return TRACE_ERR (gpg_error_from_syserror ()); } diff --git a/src/debug.c b/src/debug.c index ca0bb21a..a50b4487 100644 --- a/src/debug.c +++ b/src/debug.c @@ -47,6 +47,7 @@ #include "ath.h" #include "sema.h" #include "debug.h" +#include "mem.h" /* Lock to serialize initialization of the debug output subsystem and @@ -122,8 +123,8 @@ trim_spaces (char *str) int _gpgme_debug_set_debug_envvar (const char *value) { - free (envvar_override); - envvar_override = strdup (value); + _gpgme_free (envvar_override); + envvar_override = _gpgme_strdup (value); return !envvar_override; } @@ -142,8 +143,8 @@ debug_init (void) if (envvar_override) { - e = strdup (envvar_override); - free (envvar_override); + e = _gpgme_strdup (envvar_override); + _gpgme_free (envvar_override); envvar_override = NULL; } else @@ -182,7 +183,7 @@ debug_init (void) s1++; if (!(s2 = strchr (s1, PATHSEP_C))) s2 = s1 + strlen (s1); - p = malloc (s2 - s1 + 1); + p = _gpgme_malloc (s2 - s1 + 1); if (p) { memcpy (p, s1, s2 - s1); @@ -194,13 +195,13 @@ debug_init (void) setvbuf (fp, NULL, _IOLBF, 0); errfp = fp; } - free (p); + _gpgme_free (p); } #ifndef HAVE_DOSISH_SYSTEM } #endif } - free (e); + _gpgme_free (e); } } UNLOCK (debug_lock); @@ -292,7 +293,7 @@ _gpgme_debug_begin (void **line, int level, const char *format, ...) } va_start (arg_ptr, format); - res = vasprintf ((char **) line, format, arg_ptr); + res = _gpgme_vasprintf ((char **) line, format, arg_ptr); va_end (arg_ptr); if (res < 0) *line = NULL; @@ -312,16 +313,16 @@ _gpgme_debug_add (void **line, const char *format, ...) return; va_start (arg_ptr, format); - res = vasprintf (&toadd, format, arg_ptr); + res = _gpgme_vasprintf (&toadd, format, arg_ptr); va_end (arg_ptr); if (res < 0) { - free (*line); + _gpgme_free (*line); *line = NULL; } - res = asprintf (&result, "%s%s", *(char **) line, toadd); - free (toadd); - free (*line); + res = _gpgme_asprintf (&result, "%s%s", *(char **) line, toadd); + _gpgme_free (toadd); + _gpgme_free (*line); if (res < 0) *line = NULL; else @@ -340,7 +341,7 @@ _gpgme_debug_end (void **line) /* The smallest possible level is 1, so force logging here by using that. */ _gpgme_debug (1, "%s", *line); - free (*line); + _gpgme_free (*line); *line = NULL; } diff --git a/src/decrypt.c b/src/decrypt.c index 47420601..40b66e6d 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -31,6 +31,7 @@ #include "util.h" #include "context.h" #include "ops.h" +#include "mem.h" @@ -55,15 +56,15 @@ release_op_data (void *hook) gpgme_recipient_t recipient = opd->result.recipients; if (opd->result.unsupported_algorithm) - free (opd->result.unsupported_algorithm); + _gpgme_free (opd->result.unsupported_algorithm); if (opd->result.file_name) - free (opd->result.file_name); + _gpgme_free (opd->result.file_name); while (recipient) { gpgme_recipient_t next = recipient->next; - free (recipient); + _gpgme_free (recipient); recipient = next; } } @@ -125,7 +126,7 @@ parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) char *tail; int i; - rec = malloc (sizeof (*rec)); + rec = _gpgme_malloc (sizeof (*rec)); if (!rec) return gpg_error_from_syserror (); @@ -145,7 +146,7 @@ parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) args = &args[i]; if (*args != '\0' && *args != ' ') { - free (rec); + _gpgme_free (rec); return trace_gpg_error (GPG_ERR_INV_ENGINE); } @@ -159,7 +160,7 @@ parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (rec); + _gpgme_free (rec); return trace_gpg_error (GPG_ERR_INV_ENGINE); } } @@ -242,7 +243,7 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code, if (!(*args == '?' && *(args + 1) == '\0')) { - opd->result.unsupported_algorithm = strdup (args); + opd->result.unsupported_algorithm = _gpgme_strdup (args); if (!opd->result.unsupported_algorithm) return gpg_error_from_syserror (); } diff --git a/src/dirinfo.c b/src/dirinfo.c index 8824c9a4..4e138561 100644 --- a/src/dirinfo.c +++ b/src/dirinfo.c @@ -30,6 +30,7 @@ #include "debug.h" #include "sema.h" #include "sys-util.h" +#include "mem.h" DEFINE_STATIC_LOCK (dirinfo_lock); @@ -101,11 +102,11 @@ parse_output (char *line, int components) if (components) { if (!strcmp (line, "gpg") && !dirinfo.gpg_name) - dirinfo.gpg_name = strdup (value); + dirinfo.gpg_name = _gpgme_strdup (value); else if (!strcmp (line, "gpgsm") && !dirinfo.gpgsm_name) - dirinfo.gpgsm_name = strdup (value); + dirinfo.gpgsm_name = _gpgme_strdup (value); else if (!strcmp (line, "g13") && !dirinfo.g13_name) - dirinfo.g13_name = strdup (value); + dirinfo.g13_name = _gpgme_strdup (value); } else { @@ -113,10 +114,10 @@ parse_output (char *line, int components) { const char name[] = "S.uiserver"; - dirinfo.homedir = strdup (value); + dirinfo.homedir = _gpgme_strdup (value); if (dirinfo.homedir) { - dirinfo.uisrv_socket = malloc (strlen (dirinfo + dirinfo.uisrv_socket = _gpgme_malloc (strlen (dirinfo .homedir) + 1 + strlen (name) + 1); if (dirinfo.uisrv_socket) @@ -125,7 +126,7 @@ parse_output (char *line, int components) } } else if (!strcmp (line, "agent-socket") && !dirinfo.agent_socket) - dirinfo.agent_socket = strdup (value); + dirinfo.agent_socket = _gpgme_strdup (value); } } @@ -216,7 +217,7 @@ get_gpgconf_item (int what) { _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: gpgconf='%s' [not installed]\n", pgmname); - free (pgmname); + _gpgme_free (pgmname); pgmname = NULL; /* Not available. */ } else diff --git a/src/encrypt.c b/src/encrypt.c index 792c25c5..f0894592 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -30,6 +30,7 @@ #include "debug.h" #include "context.h" #include "ops.h" +#include "mem.h" typedef struct @@ -53,8 +54,8 @@ release_op_data (void *hook) { gpgme_invalid_key_t next = invalid_recipient->next; if (invalid_recipient->fpr) - free (invalid_recipient->fpr); - free (invalid_recipient); + _gpgme_free (invalid_recipient->fpr); + _gpgme_free (invalid_recipient); invalid_recipient = next; } } diff --git a/src/engine-assuan.c b/src/engine-assuan.c index 663b2eab..559d6cdc 100644 --- a/src/engine-assuan.c +++ b/src/engine-assuan.c @@ -53,6 +53,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" typedef struct @@ -131,7 +132,7 @@ llass_get_home_dir (void) static char * llass_get_version (const char *file_name) { - return strdup ("1.0"); + return _gpgme_strdup ("1.0"); } @@ -205,7 +206,7 @@ llass_release (void *engine) llass_cancel (engine); - free (llass); + _gpgme_free (llass); } @@ -218,7 +219,7 @@ llass_new (void **engine, const char *file_name, const char *home_dir) engine_llass_t llass; char *optstr; - llass = calloc (1, sizeof *llass); + llass = _gpgme_calloc (1, sizeof *llass); if (!llass) return gpg_error_from_syserror (); @@ -259,17 +260,17 @@ llass_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_display) { - if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0) + if (_gpgme_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { err = gpg_error_from_syserror (); - free (dft_display); + _gpgme_free (dft_display); goto leave; } - free (dft_display); + _gpgme_free (dft_display); err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -289,14 +290,14 @@ llass_new (void **engine, const char *file_name, const char *home_dir) } else { - if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; @@ -305,17 +306,17 @@ llass_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_ttytype) { - if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) { err = gpg_error_from_syserror (); - free (dft_ttytype); + _gpgme_free (dft_ttytype); goto leave; } - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -393,13 +394,13 @@ llass_set_locale (void *engine, int category, const char *value) if (!value) return 0; - if (asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) + if (_gpgme_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); } return err; } diff --git a/src/engine-g13.c b/src/engine-g13.c index a9717eec..76f0cd3f 100644 --- a/src/engine-g13.c +++ b/src/engine-g13.c @@ -50,6 +50,8 @@ #include "engine-backend.h" +#include "mem.h" + typedef struct { @@ -207,7 +209,7 @@ g13_release (void *engine) g13_cancel (engine); - free (g13); + _gpgme_free (g13); } @@ -224,7 +226,7 @@ g13_new (void **engine, const char *file_name, const char *home_dir) char *dft_ttytype = NULL; char *optstr; - g13 = calloc (1, sizeof *g13); + g13 = _gpgme_calloc (1, sizeof *g13); if (!g13) return gpg_error_from_syserror (); @@ -266,17 +268,17 @@ g13_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_display) { - if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0) + if (_gpgme_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { - free (dft_display); + _gpgme_free (dft_display); err = gpg_error_from_syserror (); goto leave; } - free (dft_display); + _gpgme_free (dft_display); err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -293,14 +295,14 @@ g13_new (void **engine, const char *file_name, const char *home_dir) } else { - if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; @@ -309,17 +311,17 @@ g13_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_ttytype) { - if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) { - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = gpg_error_from_syserror (); goto leave; } - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -389,13 +391,13 @@ g13_set_locale (void *engine, int category, const char *value) if (!value) return 0; - if (asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) + if (_gpgme_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); } return err; diff --git a/src/engine-gpg.c b/src/engine-gpg.c index 30c3bfbe..b54e769d 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -44,6 +44,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" /* This type is used to build a list of gpg arguments and data @@ -209,7 +210,7 @@ _add_arg (engine_gpg_t gpg, const char *arg, int front, int *arg_locp) assert (gpg); assert (arg); - a = malloc (sizeof *a + strlen (arg)); + a = _gpgme_malloc (sizeof *a + strlen (arg)); if (!a) return gpg_error_from_syserror (); @@ -268,7 +269,7 @@ add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) assert (gpg); assert (data); - a = malloc (sizeof *a - 1); + a = _gpgme_malloc (sizeof *a - 1); if (!a) return gpg_error_from_syserror (); a->next = NULL; @@ -313,8 +314,8 @@ free_argv (char **argv) int i; for (i = 0; argv[i]; i++) - free (argv[i]); - free (argv); + _gpgme_free (argv[i]); + _gpgme_free (argv); } @@ -334,7 +335,7 @@ free_fd_data_map (struct fd_data_map_s *fd_data_map) _gpgme_io_close (fd_data_map[i].peer_fd); /* Don't release data because this is only a reference. */ } - free (fd_data_map); + _gpgme_free (fd_data_map); } @@ -385,32 +386,32 @@ gpg_release (void *engine) gpg_cancel (engine); if (gpg->file_name) - free (gpg->file_name); + _gpgme_free (gpg->file_name); if (gpg->lc_messages) - free (gpg->lc_messages); + _gpgme_free (gpg->lc_messages); if (gpg->lc_ctype) - free (gpg->lc_ctype); + _gpgme_free (gpg->lc_ctype); while (gpg->arglist) { struct arg_and_data_s *next = gpg->arglist->next; if (gpg->arglist) - free (gpg->arglist); + _gpgme_free (gpg->arglist); gpg->arglist = next; } if (gpg->status.buffer) - free (gpg->status.buffer); + _gpgme_free (gpg->status.buffer); if (gpg->colon.buffer) - free (gpg->colon.buffer); + _gpgme_free (gpg->colon.buffer); if (gpg->argv) free_argv (gpg->argv); if (gpg->cmd.keyword) - free (gpg->cmd.keyword); + _gpgme_free (gpg->cmd.keyword); - free (gpg); + _gpgme_free (gpg); } @@ -423,13 +424,13 @@ gpg_new (void **engine, const char *file_name, const char *home_dir) char dft_ttyname[64]; char *dft_ttytype = NULL; - gpg = calloc (1, sizeof *gpg); + gpg = _gpgme_calloc (1, sizeof *gpg); if (!gpg) return gpg_error_from_syserror (); if (file_name) { - gpg->file_name = strdup (file_name); + gpg->file_name = _gpgme_strdup (file_name); if (!gpg->file_name) { rc = gpg_error_from_syserror (); @@ -450,7 +451,7 @@ gpg_new (void **engine, const char *file_name, const char *home_dir) /* Allocate the read buffer for the status pipe. */ gpg->status.bufsize = 1024; gpg->status.readpos = 0; - gpg->status.buffer = malloc (gpg->status.bufsize); + gpg->status.buffer = _gpgme_malloc (gpg->status.bufsize); if (!gpg->status.buffer) { rc = gpg_error_from_syserror (); @@ -513,7 +514,7 @@ gpg_new (void **engine, const char *file_name, const char *home_dir) if (!rc) rc = add_arg (gpg, dft_display); - free (dft_display); + _gpgme_free (dft_display); } if (isatty (1)) @@ -546,7 +547,7 @@ gpg_new (void **engine, const char *file_name, const char *home_dir) rc = add_arg (gpg, dft_ttytype); } - free (dft_ttytype); + _gpgme_free (dft_ttytype); } } if (rc) @@ -574,12 +575,12 @@ gpg_set_locale (void *engine, int category, const char *value) { if (gpg->lc_ctype) { - free (gpg->lc_ctype); + _gpgme_free (gpg->lc_ctype); gpg->lc_ctype = NULL; } if (value) { - gpg->lc_ctype = strdup (value); + gpg->lc_ctype = _gpgme_strdup (value); if (!gpg->lc_ctype) return gpg_error_from_syserror (); } @@ -590,12 +591,12 @@ gpg_set_locale (void *engine, int category, const char *value) { if (gpg->lc_messages) { - free (gpg->lc_messages); + _gpgme_free (gpg->lc_messages); gpg->lc_messages = NULL; } if (value) { - gpg->lc_messages = strdup (value); + gpg->lc_messages = _gpgme_strdup (value); if (!gpg->lc_messages) return gpg_error_from_syserror (); } @@ -629,14 +630,14 @@ gpg_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, gpg->colon.bufsize = 1024; gpg->colon.readpos = 0; - gpg->colon.buffer = malloc (gpg->colon.bufsize); + gpg->colon.buffer = _gpgme_malloc (gpg->colon.bufsize); if (!gpg->colon.buffer) return gpg_error_from_syserror (); if (_gpgme_io_pipe (gpg->colon.fd, 1) == -1) { int saved_err = gpg_error_from_syserror (); - free (gpg->colon.buffer); + _gpgme_free (gpg->colon.buffer); gpg->colon.buffer = NULL; return saved_err; } @@ -741,7 +742,7 @@ build_argv (engine_gpg_t gpg, const char *pgmname) return err; use_agent = (p && strchr (p, ':')); if (p) - free (p); + _gpgme_free (p); } if (gpg->argv) @@ -781,10 +782,10 @@ build_argv (engine_gpg_t gpg, const char *pgmname) argc++; /* --batch */ argc += 1; /* --no-sk-comments */ - argv = calloc (argc + 1, sizeof *argv); + argv = _gpgme_calloc (argc + 1, sizeof *argv); if (!argv) return gpg_error_from_syserror (); - fd_data_map = calloc (datac + 1, sizeof *fd_data_map); + fd_data_map = _gpgme_calloc (datac + 1, sizeof *fd_data_map); if (!fd_data_map) { int saved_err = gpg_error_from_syserror (); @@ -793,22 +794,22 @@ build_argv (engine_gpg_t gpg, const char *pgmname) } argc = datac = 0; - argv[argc] = strdup (_gpgme_get_basename (pgmname)); /* argv[0] */ + argv[argc] = _gpgme_strdup (_gpgme_get_basename (pgmname)); /* argv[0] */ if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } argc++; if (need_special) { - argv[argc] = strdup ("--enable-special-filenames"); + argv[argc] = _gpgme_strdup ("--enable-special-filenames"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -816,11 +817,11 @@ build_argv (engine_gpg_t gpg, const char *pgmname) } if (use_agent) { - argv[argc] = strdup ("--use-agent"); + argv[argc] = _gpgme_strdup ("--use-agent"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -840,11 +841,11 @@ build_argv (engine_gpg_t gpg, const char *pgmname) } if (s) { - argv[argc] = strdup (s); + argv[argc] = _gpgme_strdup (s); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -854,21 +855,21 @@ build_argv (engine_gpg_t gpg, const char *pgmname) if (!gpg->cmd.used) { - argv[argc] = strdup ("--batch"); + argv[argc] = _gpgme_strdup ("--batch"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } argc++; } - argv[argc] = strdup ("--no-sk-comments"); + argv[argc] = _gpgme_strdup ("--no-sk-comments"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -891,7 +892,7 @@ build_argv (engine_gpg_t gpg, const char *pgmname) == -1) { int saved_errno = errno; - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return gpg_error (saved_errno); } @@ -943,11 +944,11 @@ build_argv (engine_gpg_t gpg, const char *pgmname) char *ptr; int buflen = 25; - argv[argc] = malloc (buflen); + argv[argc] = _gpgme_malloc (buflen); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -968,11 +969,11 @@ build_argv (engine_gpg_t gpg, const char *pgmname) } else { - argv[argc] = strdup (a->arg); + argv[argc] = _gpgme_strdup (a->arg); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); - free (fd_data_map); + _gpgme_free (fd_data_map); free_argv (argv); return saved_err; } @@ -1023,7 +1024,7 @@ read_status (engine_gpg_t gpg) { /* Need more room for the read. */ bufsize += 1024; - buffer = realloc (buffer, bufsize); + buffer = _gpgme_realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } @@ -1078,8 +1079,8 @@ read_status (engine_gpg_t gpg) { gpg->cmd.code = r; if (gpg->cmd.keyword) - free (gpg->cmd.keyword); - gpg->cmd.keyword = strdup (rest); + _gpgme_free (gpg->cmd.keyword); + gpg->cmd.keyword = _gpgme_strdup (rest); if (!gpg->cmd.keyword) return gpg_error_from_syserror (); /* This should be the last thing we have @@ -1199,7 +1200,7 @@ read_colon_line (engine_gpg_t gpg) { /* Need more room for the read. */ bufsize += 1024; - buffer = realloc (buffer, bufsize); + buffer = _gpgme_realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } @@ -1257,7 +1258,7 @@ read_colon_line (engine_gpg_t gpg) } while (linep && *linep); - free (line); + _gpgme_free (line); } else gpg->colon.fnc (gpg->colon.fnc_value, buffer); @@ -1352,7 +1353,7 @@ start (engine_gpg_t gpg) n = 3; for (i = 0; gpg->fd_data_map[i].data; i++) n++; - fd_list = calloc (n, sizeof *fd_list); + fd_list = _gpgme_calloc (n, sizeof *fd_list); if (! fd_list) return gpg_error_from_syserror (); @@ -1383,7 +1384,7 @@ start (engine_gpg_t gpg) fd_list, NULL, NULL, &pid); { int saved_err = gpg_error_from_syserror (); - free (fd_list); + _gpgme_free (fd_list); if (status == -1) return saved_err; } @@ -1548,7 +1549,7 @@ append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) the name, one byte for '=', the value, and a terminating '\0'. */ - arg = malloc (1 + notation->name_len + 1 + notation->value_len + 1); + arg = _gpgme_malloc (1 + notation->name_len + 1 + notation->value_len + 1); if (!arg) err = gpg_error_from_syserror (); @@ -1574,7 +1575,7 @@ append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) err = add_arg (gpg, arg); if (arg) - free (arg); + _gpgme_free (arg); } else { @@ -1584,7 +1585,7 @@ append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) if (notation->critical) { - value = malloc (1 + notation->value_len + 1); + value = _gpgme_malloc (1 + notation->value_len + 1); if (!value) err = gpg_error_from_syserror (); else @@ -1603,7 +1604,7 @@ append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) err = add_arg (gpg, value); if (value != notation->value) - free (value); + _gpgme_free (value); } notation = notation->next; @@ -1921,7 +1922,7 @@ string_from_data (gpgme_data_t data, int delim, { if (*helpptr) { - free (*helpptr); + _gpgme_free (*helpptr); *helpptr = NULL; } return NULL; @@ -1931,7 +1932,7 @@ string_from_data (gpgme_data_t data, int delim, self = *helpptr; else { - self = malloc (sizeof *self); + self = _gpgme_malloc (sizeof *self); if (!self) { *r_err = gpg_error_from_syserror (); @@ -2135,7 +2136,7 @@ gpg_keylist_preprocess (char *line, char **r_line) n = strlen (field[1]); if (n > 16) { - if (asprintf (r_line, + if (_gpgme_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::\n" "fpr:::::::::%s:", field[6], field[3], field[2], field[1] + n - 16, @@ -2144,7 +2145,7 @@ gpg_keylist_preprocess (char *line, char **r_line) } else { - if (asprintf (r_line, + if (_gpgme_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::", field[6], field[3], field[2], field[1], field[4], field[5]) < 0) @@ -2170,7 +2171,7 @@ gpg_keylist_preprocess (char *line, char **r_line) Because we have to replace each '%HL' by '\xHL', we need at most 4/3 th the number of bytes. But because we also need to escape the backslashes we allocate twice as much. */ - char *uid = malloc (2 * strlen (field[1]) + 1); + char *uid = _gpgme_malloc (2 * strlen (field[1]) + 1); char *src; char *dst; @@ -2201,7 +2202,7 @@ gpg_keylist_preprocess (char *line, char **r_line) } *dst = '\0'; - if (asprintf (r_line, "uid:o%s::::%s:%s:::%s:", + if (_gpgme_asprintf (r_line, "uid:o%s::::%s:%s:::%s:", field[4], field[2], field[3], uid) < 0) return gpg_error_from_syserror (); } diff --git a/src/engine-gpgconf.c b/src/engine-gpgconf.c index a2407ac7..4369f272 100644 --- a/src/engine-gpgconf.c +++ b/src/engine-gpgconf.c @@ -46,6 +46,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" struct engine_gpgconf @@ -81,11 +82,11 @@ gpgconf_release (void *engine) return; if (gpgconf->file_name) - free (gpgconf->file_name); + _gpgme_free (gpgconf->file_name); if (gpgconf->home_dir) - free (gpgconf->home_dir); + _gpgme_free (gpgconf->home_dir); - free (gpgconf); + _gpgme_free (gpgconf); } @@ -95,18 +96,18 @@ gpgconf_new (void **engine, const char *file_name, const char *home_dir) gpgme_error_t err = 0; engine_gpgconf_t gpgconf; - gpgconf = calloc (1, sizeof *gpgconf); + gpgconf = _gpgme_calloc (1, sizeof *gpgconf); if (!gpgconf) return gpg_error_from_syserror (); - gpgconf->file_name = strdup (file_name ? file_name + gpgconf->file_name = _gpgme_strdup (file_name ? file_name : _gpgme_get_default_gpgconf_name ()); if (!gpgconf->file_name) err = gpg_error_from_syserror (); if (!err && home_dir) { - gpgconf->home_dir = strdup (home_dir); + gpgconf->home_dir = _gpgme_strdup (home_dir); if (!gpgconf->home_dir) err = gpg_error_from_syserror (); } @@ -128,8 +129,8 @@ release_arg (gpgme_conf_arg_t arg, gpgme_conf_type_t alt_type) gpgme_conf_arg_t next = arg->next; if (alt_type == GPGME_CONF_STRING) - free (arg->value.string); - free (arg); + _gpgme_free (arg->value.string); + _gpgme_free (arg); arg = next; } } @@ -139,21 +140,21 @@ static void release_opt (gpgme_conf_opt_t opt) { if (opt->name) - free (opt->name); + _gpgme_free (opt->name); if (opt->description) - free (opt->description); + _gpgme_free (opt->description); if (opt->argname) - free (opt->argname); + _gpgme_free (opt->argname); release_arg (opt->default_value, opt->alt_type); if (opt->default_description) - free (opt->default_description); + _gpgme_free (opt->default_description); release_arg (opt->no_arg_value, opt->alt_type); release_arg (opt->value, opt->alt_type); release_arg (opt->new_value, opt->alt_type); - free (opt); + _gpgme_free (opt); } @@ -163,11 +164,11 @@ release_comp (gpgme_conf_comp_t comp) gpgme_conf_opt_t opt; if (comp->name) - free (comp->name); + _gpgme_free (comp->name); if (comp->description) - free (comp->description); + _gpgme_free (comp->description); if (comp->program_name) - free (comp->program_name); + _gpgme_free (comp->program_name); opt = comp->options; while (opt) @@ -177,7 +178,7 @@ release_comp (gpgme_conf_comp_t comp) opt = next; } - free (comp); + _gpgme_free (comp); } @@ -238,7 +239,7 @@ gpgconf_read (void *engine, char *arg1, char *arg2, } linebufsize = 1024; /* Usually enough for conf lines. */ - linebuf = malloc (linebufsize); + linebuf = _gpgme_malloc (linebufsize); if (!linebuf) { err = gpg_error_from_syserror (); @@ -298,7 +299,7 @@ gpgconf_read (void *engine, char *arg1, char *arg2, goto leave; } - newlinebuf = realloc (linebuf, linebufsize); + newlinebuf = _gpgme_realloc (linebuf, linebufsize); if (!newlinebuf) { err = gpg_error_from_syserror (); @@ -309,7 +310,7 @@ gpgconf_read (void *engine, char *arg1, char *arg2, } leave: - free (linebuf); + _gpgme_free (linebuf); _gpgme_io_close (rp[0]); return err; } @@ -342,24 +343,24 @@ gpgconf_config_load_cb (void *hook, char *line) if (comp) comp_p = &comp->next; - comp = calloc (1, sizeof (*comp)); + comp = _gpgme_calloc (1, sizeof (*comp)); if (!comp) return gpg_error_from_syserror (); /* Prepare return value. */ comp->_last_opt_p = &comp->options; *comp_p = comp; - comp->name = strdup (field[0]); + comp->name = _gpgme_strdup (field[0]); if (!comp->name) return gpg_error_from_syserror (); - comp->description = strdup (field[1]); + comp->description = _gpgme_strdup (field[1]); if (!comp->description) return gpg_error_from_syserror (); if (fields >= 3) { - comp->program_name = strdup (field[2]); + comp->program_name = _gpgme_strdup (field[2]); if (!comp->program_name) return gpg_error_from_syserror (); } @@ -386,7 +387,7 @@ gpgconf_parse_option (gpgme_conf_opt_t opt, if (mark) *mark = '\0'; - arg = calloc (1, sizeof (*arg)); + arg = _gpgme_calloc (1, sizeof (*arg)); if (!arg) return gpg_error_from_syserror (); *arg_p = arg; @@ -462,7 +463,7 @@ gpgconf_config_load_cb2 (void *hook, char *line) if (fields < 10) return trace_gpg_error (GPG_ERR_INV_ENGINE); - opt = calloc (1, sizeof (*opt)); + opt = _gpgme_calloc (1, sizeof (*opt)); if (!opt) return gpg_error_from_syserror (); @@ -471,7 +472,7 @@ gpgconf_config_load_cb2 (void *hook, char *line) if (field[0][0]) { - opt->name = strdup (field[0]); + opt->name = _gpgme_strdup (field[0]); if (!opt->name) return gpg_error_from_syserror (); } @@ -482,7 +483,7 @@ gpgconf_config_load_cb2 (void *hook, char *line) if (field[3][0]) { - opt->description = strdup (field[3]); + opt->description = _gpgme_strdup (field[3]); if (!opt->description) return gpg_error_from_syserror (); } @@ -493,7 +494,7 @@ gpgconf_config_load_cb2 (void *hook, char *line) if (field[6][0]) { - opt->argname = strdup (field[6]); + opt->argname = _gpgme_strdup (field[6]); if (!opt->argname) return gpg_error_from_syserror (); } @@ -506,14 +507,14 @@ gpgconf_config_load_cb2 (void *hook, char *line) } else if ((opt->flags & GPGME_CONF_DEFAULT_DESC) && field[7][0]) { - opt->default_description = strdup (field[7]); + opt->default_description = _gpgme_strdup (field[7]); if (!opt->default_description) return gpg_error_from_syserror (); } if (opt->flags & GPGME_CONF_NO_ARG_DESC) { - opt->no_arg_description = strdup (field[8]); + opt->no_arg_description = _gpgme_strdup (field[8]); if (!opt->no_arg_description) return gpg_error_from_syserror (); } @@ -575,7 +576,7 @@ _gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, { gpgme_conf_arg_t arg; - arg = calloc (1, sizeof (*arg)); + arg = _gpgme_calloc (1, sizeof (*arg)); if (!arg) return gpg_error_from_syserror (); @@ -603,16 +604,16 @@ _gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: - arg->value.string = strdup (value); + arg->value.string = _gpgme_strdup (value); if (!arg->value.string) { - free (arg); + _gpgme_free (arg); return gpg_error_from_syserror (); } break; default: - free (arg); + _gpgme_free (arg); return gpg_error (GPG_ERR_INV_VALUE); } } diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index 3a837577..ab979e78 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -51,6 +51,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" typedef struct @@ -157,7 +158,7 @@ close_notify_handler (int fd, void *opaque) } if (gpgsm->input_helper_memory) { - free (gpgsm->input_helper_memory); + _gpgme_free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; } } @@ -229,8 +230,8 @@ gpgsm_release (void *engine) gpgsm_cancel (engine); - free (gpgsm->colon.attic.line); - free (gpgsm); + _gpgme_free (gpgsm->colon.attic.line); + _gpgme_free (gpgsm); } @@ -251,7 +252,7 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir) char *dft_ttytype = NULL; char *optstr; - gpgsm = calloc (1, sizeof *gpgsm); + gpgsm = _gpgme_calloc (1, sizeof *gpgsm); if (!gpgsm) return gpg_error_from_syserror (); @@ -388,17 +389,17 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_display) { - if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0) + if (_gpgme_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { - free (dft_display); + _gpgme_free (dft_display); err = gpg_error_from_syserror (); goto leave; } - free (dft_display); + _gpgme_free (dft_display); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -415,14 +416,14 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir) } else { - if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; @@ -431,17 +432,17 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_ttytype) { - if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) { - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = gpg_error_from_syserror (); goto leave; } - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -546,13 +547,13 @@ gpgsm_set_locale (void *engine, int category, const char *value) if (!value) return 0; - if (asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) + if (_gpgme_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); } return err; @@ -830,7 +831,7 @@ status_handler (void *opaque, int fd) if (gpgsm->colon.attic.linesize < *alinelen + linelen + 1) { - char *newline = realloc (*aline, *alinelen + linelen + 1); + char *newline = _gpgme_realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else @@ -1118,7 +1119,7 @@ gpgsm_delete (void *engine, gpgme_key_t key, int allow_secret) } length++; - line = malloc (length); + line = _gpgme_malloc (length); if (!line) return gpg_error_from_syserror (); @@ -1158,7 +1159,7 @@ gpgsm_delete (void *engine, gpgme_key_t key, int allow_secret) gpgsm->inline_data = NULL; err = start (gpgsm, line); - free (line); + _gpgme_free (line); return err; } @@ -1175,7 +1176,7 @@ set_recipients (engine_gpgsm_t gpgsm, gpgme_key_t recp[]) int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ - line = malloc (10 + 40 + 1); + line = _gpgme_malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); @@ -1194,11 +1195,11 @@ set_recipients (engine_gpgsm_t gpgsm, gpgme_key_t recp[]) newlen = 11 + strlen (fpr); if (linelen < newlen) { - char *newline = realloc (line, newlen); + char *newline = _gpgme_realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); - free (line); + _gpgme_free (line); return saved_err; } line = newline; @@ -1213,11 +1214,11 @@ set_recipients (engine_gpgsm_t gpgsm, gpgme_key_t recp[]) invalid_recipients++; else if (err) { - free (line); + _gpgme_free (line); return err; } } - free (line); + _gpgme_free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } @@ -1281,7 +1282,7 @@ gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode, if (!pattern) pattern = ""; - cmd = malloc (7 + strlen (pattern) + 1); + cmd = _gpgme_malloc (7 + strlen (pattern) + 1); if (!cmd) return gpg_error_from_syserror (); strcpy (cmd, "EXPORT "); @@ -1297,7 +1298,7 @@ gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgsm->inline_data = NULL; err = start (gpgsm, cmd); - free (cmd); + _gpgme_free (cmd); return err; } @@ -1338,7 +1339,7 @@ gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, length++; } } - line = malloc (length); + line = _gpgme_malloc (length); if (!line) return gpg_error_from_syserror (); @@ -1393,7 +1394,7 @@ gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgsm->inline_data = NULL; err = start (gpgsm, line); - free (line); + _gpgme_free (line); return err; } @@ -1468,7 +1469,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) } /* Allocate a bufer with extra space for the trailing Nul introduced by the use of stpcpy. */ - buffer = malloc (buflen+1); + buffer = _gpgme_malloc (buflen+1); if (!buffer) return gpg_error_from_syserror (); for (idx=0, p = buffer; keyarray[idx]; idx++) @@ -1484,7 +1485,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) buffer, buflen, 0); if (err) { - free (buffer); + _gpgme_free (buffer); return err; } gpgsm->input_helper_memory = buffer; @@ -1495,7 +1496,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { gpgme_data_release (gpgsm->input_helper_data); gpgsm->input_helper_data = NULL; - free (gpgsm->input_helper_memory); + _gpgme_free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; return err; } @@ -1556,10 +1557,10 @@ gpgsm_keylist (void *engine, const char *pattern, int secret_only, NULL, NULL); /* Always send list-mode option because RESET does not reset it. */ - if (asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) + if (_gpgme_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, line, NULL, NULL); - free (line); + _gpgme_free (line); if (err) return err; @@ -1588,7 +1589,7 @@ gpgsm_keylist (void *engine, const char *pattern, int secret_only, /* Length is "LISTSECRETKEYS " + p + '\0'. */ - line = malloc (15 + strlen (pattern) + 1); + line = _gpgme_malloc (15 + strlen (pattern) + 1); if (!line) return gpg_error_from_syserror (); if (secret_only) @@ -1608,7 +1609,7 @@ gpgsm_keylist (void *engine, const char *pattern, int secret_only, gpgsm->inline_data = NULL; err = start (gpgsm, line); - free (line); + _gpgme_free (line); return err; } @@ -1635,10 +1636,10 @@ gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only, list_mode |= 2; /* Always send list-mode option because RESET does not reset it. */ - if (asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) + if (_gpgme_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, line, NULL, NULL); - free (line); + _gpgme_free (line); if (err) return err; @@ -1676,7 +1677,7 @@ gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only, length++; } } - line = malloc (length); + line = _gpgme_malloc (length); if (!line) return gpg_error_from_syserror (); if (secret_only) @@ -1736,7 +1737,7 @@ gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only, gpgsm->inline_data = NULL; err = start (gpgsm, line); - free (line); + _gpgme_free (line); return err; } @@ -1763,11 +1764,11 @@ gpgsm_sign (void *engine, gpgme_data_t in, gpgme_data_t out, can reset any previously set value in case the default is requested. */ - if (asprintf (&assuan_cmd, "OPTION include-certs %i", include_certs) < 0) + if (_gpgme_asprintf (&assuan_cmd, "OPTION include-certs %i", include_certs) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, assuan_cmd, NULL, NULL); - free (assuan_cmd); + _gpgme_free (assuan_cmd); if (err) return err; } @@ -1937,7 +1938,7 @@ gpgsm_passwd (void *engine, gpgme_key_t key, unsigned int flags) if (!key || !key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_CERT_OBJ); - if (asprintf (&line, "PASSWD -- %s", key->subkeys->fpr) < 0) + if (_gpgme_asprintf (&line, "PASSWD -- %s", key->subkeys->fpr) < 0) return gpg_error_from_syserror (); gpgsm_clear_fd (gpgsm, OUTPUT_FD); @@ -1946,7 +1947,7 @@ gpgsm_passwd (void *engine, gpgme_key_t key, unsigned int flags) gpgsm->inline_data = NULL; err = start (gpgsm, line); - free (line); + _gpgme_free (line); return err; } diff --git a/src/engine-spawn.c b/src/engine-spawn.c index 8ffc6280..c2d0846d 100644 --- a/src/engine-spawn.c +++ b/src/engine-spawn.c @@ -42,6 +42,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" /* This type is used to build a list of data sources/sinks. */ @@ -120,7 +121,7 @@ add_data (engine_spawn_t esp, gpgme_data_t data, int dup_to, int inbound) assert (esp); assert (data); - a = malloc (sizeof *a - 1); + a = _gpgme_malloc (sizeof *a - 1); if (!a) return gpg_error_from_syserror (); a->next = NULL; @@ -149,7 +150,7 @@ free_fd_data_map (struct fd_data_map_s *fd_data_map) _gpgme_io_close (fd_data_map[i].peer_fd); /* Don't release data because this is only a reference. */ } - free (fd_data_map); + _gpgme_free (fd_data_map); } @@ -165,7 +166,7 @@ build_fd_data_map (engine_spawn_t esp) datac++; free_fd_data_map (esp->fd_data_map); - esp->fd_data_map = calloc (datac + 1, sizeof *esp->fd_data_map); + esp->fd_data_map = _gpgme_calloc (datac + 1, sizeof *esp->fd_data_map); if (!esp->fd_data_map) return gpg_error_from_syserror (); @@ -175,7 +176,7 @@ build_fd_data_map (engine_spawn_t esp) if (_gpgme_io_pipe (fds, a->inbound ? 1 : 0) == -1) { - free (esp->fd_data_map); + _gpgme_free (esp->fd_data_map); esp->fd_data_map = NULL; return gpg_error_from_syserror (); } @@ -250,7 +251,7 @@ engspawn_start (engine_spawn_t esp, const char *file, const char *argv[], n = 0; for (i = 0; esp->fd_data_map[i].data; i++) n++; - fd_list = calloc (n+1, sizeof *fd_list); + fd_list = _gpgme_calloc (n+1, sizeof *fd_list); if (!fd_list) return gpg_error_from_syserror (); @@ -274,7 +275,7 @@ engspawn_start (engine_spawn_t esp, const char *file, const char *argv[], fd_list, NULL, NULL, &pid); if (save_argv0) argv[0] = save_argv0; - free (fd_list); + _gpgme_free (fd_list); if (status == -1) return gpg_error_from_syserror (); @@ -312,7 +313,7 @@ static char * engspawn_get_version (const char *file_name) { (void)file_name; - return strdup ("1.0"); + return _gpgme_strdup ("1.0"); } @@ -331,7 +332,7 @@ engspawn_new (void **engine, const char *file_name, const char *home_dir) (void)file_name; (void)home_dir; - esp = calloc (1, sizeof *esp); + esp = _gpgme_calloc (1, sizeof *esp); if (!esp) return gpg_error_from_syserror (); @@ -356,11 +357,11 @@ engspawn_release (void *engine) struct datalist_s *next = esp->arglist->next; if (esp->arglist) - free (esp->arglist); + _gpgme_free (esp->arglist); esp->arglist = next; } - free (esp); + _gpgme_free (esp); } diff --git a/src/engine-uiserver.c b/src/engine-uiserver.c index a7184b7a..7049311c 100644 --- a/src/engine-uiserver.c +++ b/src/engine-uiserver.c @@ -52,6 +52,7 @@ #include "debug.h" #include "engine-backend.h" +#include "mem.h" typedef struct @@ -121,7 +122,7 @@ static void uiserver_io_event (void *engine, static char * uiserver_get_version (const char *file_name) { - return strdup ("1.0"); + return _gpgme_strdup ("1.0"); } @@ -158,7 +159,7 @@ close_notify_handler (int fd, void *opaque) } if (uiserver->input_helper_memory) { - free (uiserver->input_helper_memory); + _gpgme_free (uiserver->input_helper_memory); uiserver->input_helper_memory = NULL; } } @@ -230,8 +231,8 @@ uiserver_release (void *engine) uiserver_cancel (engine); - free (uiserver->colon.attic.line); - free (uiserver); + _gpgme_free (uiserver->colon.attic.line); + _gpgme_free (uiserver); } @@ -245,7 +246,7 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir) char *dft_ttytype = NULL; char *optstr; - uiserver = calloc (1, sizeof *uiserver); + uiserver = _gpgme_calloc (1, sizeof *uiserver); if (!uiserver) return gpg_error_from_syserror (); @@ -306,17 +307,17 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_display) { - if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0) + if (_gpgme_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { err = gpg_error_from_syserror (); - free (dft_display); + _gpgme_free (dft_display); goto leave; } - free (dft_display); + _gpgme_free (dft_display); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -333,14 +334,14 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir) } else { - if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; @@ -349,17 +350,17 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir) goto leave; if (dft_ttytype) { - if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) + if (_gpgme_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0) { err = gpg_error_from_syserror (); - free (dft_ttytype); + _gpgme_free (dft_ttytype); goto leave; } - free (dft_ttytype); + _gpgme_free (dft_ttytype); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); if (err) goto leave; } @@ -424,13 +425,13 @@ uiserver_set_locale (void *engine, int category, const char *value) if (!value) return 0; - if (asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) + if (_gpgme_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); - free (optstr); + _gpgme_free (optstr); } return err; @@ -692,7 +693,7 @@ status_handler (void *opaque, int fd) if (uiserver->colon.attic.linesize < *alinelen + linelen + 1) { - char *newline = realloc (*aline, *alinelen + linelen + 1); + char *newline = _gpgme_realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else @@ -941,7 +942,7 @@ _uiserver_decrypt (void *engine, int verify, else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); - if (asprintf (&cmd, "DECRYPT%s%s", protocol, + if (_gpgme_asprintf (&cmd, "DECRYPT%s%s", protocol, verify ? "" : " --no-verify") < 0) return gpg_error_from_syserror (); @@ -950,20 +951,20 @@ _uiserver_decrypt (void *engine, int verify, map_data_enc (uiserver->input_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->output_cb.data = plain; err = uiserver_set_fd (uiserver, OUTPUT_FD, 0); if (err) { - free (cmd); + _gpgme_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->inline_data = NULL; err = start (engine, cmd); - free (cmd); + _gpgme_free (cmd); return err; } @@ -993,7 +994,7 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[]) int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ - line = malloc (10 + 40 + 1); + line = _gpgme_malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); @@ -1012,11 +1013,11 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[]) newlen = 11 + strlen (uid); if (linelen < newlen) { - char *newline = realloc (line, newlen); + char *newline = _gpgme_realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); - free (line); + _gpgme_free (line); return saved_err; } line = newline; @@ -1032,11 +1033,11 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[]) invalid_recipients++; else if (err) { - free (line); + _gpgme_free (line); return err; } } - free (line); + _gpgme_free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } @@ -1067,7 +1068,7 @@ uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, if (!recp || plain || ciph) return gpg_error (GPG_ERR_INV_VALUE); - if (asprintf (&cmd, "PREP_ENCRYPT%s%s", protocol, + if (_gpgme_asprintf (&cmd, "PREP_ENCRYPT%s%s", protocol, (flags & GPGME_ENCRYPT_EXPECT_SIGN) ? " --expect-sign" : "") < 0) return gpg_error_from_syserror (); @@ -1077,7 +1078,7 @@ uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, if (!plain || !ciph) return gpg_error (GPG_ERR_INV_VALUE); - if (asprintf (&cmd, "ENCRYPT%s", protocol) < 0) + if (_gpgme_asprintf (&cmd, "ENCRYPT%s", protocol) < 0) return gpg_error_from_syserror (); } @@ -1088,7 +1089,7 @@ uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, map_data_enc (uiserver->input_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } } @@ -1100,7 +1101,7 @@ uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, : map_data_enc (uiserver->output_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } } @@ -1112,13 +1113,13 @@ uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, err = set_recipients (uiserver, recp); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } } err = start (uiserver, cmd); - free (cmd); + _gpgme_free (cmd); return err; } @@ -1145,7 +1146,7 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); - if (asprintf (&cmd, "SIGN%s%s", protocol, + if (_gpgme_asprintf (&cmd, "SIGN%s%s", protocol, (mode == GPGME_SIG_MODE_DETACH) ? " --detached" : "") < 0) return gpg_error_from_syserror (); @@ -1171,7 +1172,7 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_key_unref (key); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } } @@ -1181,7 +1182,7 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, map_data_enc (uiserver->input_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } uiserver->output_cb.data = out; @@ -1189,13 +1190,13 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, : map_data_enc (uiserver->output_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } uiserver->inline_data = NULL; err = start (uiserver, cmd); - free (cmd); + _gpgme_free (cmd); return err; } @@ -1221,7 +1222,7 @@ uiserver_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); - if (asprintf (&cmd, "VERIFY%s", protocol) < 0) + if (_gpgme_asprintf (&cmd, "VERIFY%s", protocol) < 0) return gpg_error_from_syserror (); uiserver->input_cb.data = sig; @@ -1229,7 +1230,7 @@ uiserver_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, map_data_enc (uiserver->input_cb.data)); if (err) { - free (cmd); + _gpgme_free (cmd); return err; } if (plaintext) @@ -1249,7 +1250,7 @@ uiserver_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, if (!err) err = start (uiserver, cmd); - free (cmd); + _gpgme_free (cmd); return err; } diff --git a/src/engine.c b/src/engine.c index ff015c00..8d95732a 100644 --- a/src/engine.c +++ b/src/engine.c @@ -34,6 +34,7 @@ #include "engine.h" #include "engine-backend.h" +#include "mem.h" struct engine @@ -165,12 +166,12 @@ _gpgme_engine_info_release (gpgme_engine_info_t info) gpgme_engine_info_t next_info = info->next; assert (info->file_name); - free (info->file_name); + _gpgme_free (info->file_name); if (info->home_dir) - free (info->home_dir); + _gpgme_free (info->home_dir); if (info->version) - free (info->version); - free (info); + _gpgme_free (info->version); + _gpgme_free (info); info = next_info; } } @@ -209,20 +210,20 @@ gpgme_get_engine_info (gpgme_engine_info_t *info) if (!ofile_name) continue; - file_name = strdup (ofile_name); + file_name = _gpgme_strdup (ofile_name); if (!file_name) err = gpg_error_from_syserror (); if (ohome_dir) { - home_dir = strdup (ohome_dir); + home_dir = _gpgme_strdup (ohome_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } else home_dir = NULL; - *lastp = malloc (sizeof (*engine_info)); + *lastp = _gpgme_malloc (sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); @@ -232,9 +233,9 @@ gpgme_get_engine_info (gpgme_engine_info_t *info) engine_info = NULL; if (file_name) - free (file_name); + _gpgme_free (file_name); if (home_dir) - free (home_dir); + _gpgme_free (home_dir); UNLOCK (engine_info_lock); return err; @@ -288,13 +289,13 @@ _gpgme_engine_info_copy (gpgme_engine_info_t *r_info) char *version; assert (info->file_name); - file_name = strdup (info->file_name); + file_name = _gpgme_strdup (info->file_name); if (!file_name) err = gpg_error_from_syserror (); if (info->home_dir) { - home_dir = strdup (info->home_dir); + home_dir = _gpgme_strdup (info->home_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } @@ -303,14 +304,14 @@ _gpgme_engine_info_copy (gpgme_engine_info_t *r_info) if (info->version) { - version = strdup (info->version); + version = _gpgme_strdup (info->version); if (!version && !err) err = gpg_error_from_syserror (); } else version = NULL; - *lastp = malloc (sizeof (*engine_info)); + *lastp = _gpgme_malloc (sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); @@ -318,11 +319,11 @@ _gpgme_engine_info_copy (gpgme_engine_info_t *r_info) { _gpgme_engine_info_release (new_info); if (file_name) - free (file_name); + _gpgme_free (file_name); if (home_dir) - free (home_dir); + _gpgme_free (home_dir); if (version) - free (version); + _gpgme_free (version); UNLOCK (engine_info_lock); return err; @@ -366,22 +367,22 @@ _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto, /* Prepare new members. */ if (file_name) - new_file_name = strdup (file_name); + new_file_name = _gpgme_strdup (file_name); else { const char *ofile_name = engine_get_file_name (proto); assert (ofile_name); - new_file_name = strdup (ofile_name); + new_file_name = _gpgme_strdup (ofile_name); } if (!new_file_name) return gpg_error_from_syserror (); if (home_dir) { - new_home_dir = strdup (home_dir); + new_home_dir = _gpgme_strdup (home_dir); if (!new_home_dir) { - free (new_file_name); + _gpgme_free (new_file_name); return gpg_error_from_syserror (); } } @@ -390,10 +391,10 @@ _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto, const char *ohome_dir = engine_get_home_dir (proto); if (ohome_dir) { - new_home_dir = strdup (ohome_dir); + new_home_dir = _gpgme_strdup (ohome_dir); if (!new_home_dir) { - free (new_file_name); + _gpgme_free (new_file_name); return gpg_error_from_syserror (); } } @@ -403,11 +404,11 @@ _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto, /* Remove the old members. */ assert (info->file_name); - free (info->file_name); + _gpgme_free (info->file_name); if (info->home_dir) - free (info->home_dir); + _gpgme_free (info->home_dir); if (info->version) - free (info->version); + _gpgme_free (info->version); /* Install the new members. */ info->file_name = new_file_name; @@ -454,7 +455,7 @@ _gpgme_engine_new (gpgme_engine_info_t info, engine_t *r_engine) if (!info->file_name || !info->version) return trace_gpg_error (GPG_ERR_INV_ENGINE); - engine = calloc (1, sizeof *engine); + engine = _gpgme_calloc (1, sizeof *engine); if (!engine) return gpg_error_from_syserror (); @@ -466,7 +467,7 @@ _gpgme_engine_new (gpgme_engine_info_t info, engine_t *r_engine) info->file_name, info->home_dir); if (err) { - free (engine); + _gpgme_free (engine); return err; } } @@ -499,7 +500,7 @@ _gpgme_engine_release (engine_t engine) if (engine->ops->release) (*engine->ops->release) (engine->engine); - free (engine); + _gpgme_free (engine); } diff --git a/src/export.c b/src/export.c index 81a23b04..9e7d179e 100644 --- a/src/export.c +++ b/src/export.c @@ -29,6 +29,7 @@ #include "debug.h" #include "context.h" #include "ops.h" +#include "mem.h" static gpgme_error_t @@ -222,7 +223,7 @@ export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[], if (!nkeys) return gpg_error (GPG_ERR_NO_DATA); - pattern = calloc (nkeys+1, sizeof *pattern); + pattern = _gpgme_calloc (nkeys+1, sizeof *pattern); if (!pattern) return gpg_error_from_syserror (); @@ -232,7 +233,7 @@ export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[], && keys[idx]->subkeys->fpr && *keys[idx]->subkeys->fpr) { - pattern[nkeys] = strdup (keys[idx]->subkeys->fpr); + pattern[nkeys] = _gpgme_strdup (keys[idx]->subkeys->fpr); if (!pattern[nkeys]) { err = gpg_error_from_syserror (); @@ -248,8 +249,8 @@ export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[], leave: for (idx=0; pattern[idx]; idx++) - free (pattern[idx]); - free (pattern); + _gpgme_free (pattern[idx]); + _gpgme_free (pattern); return err; } diff --git a/src/genkey.c b/src/genkey.c index fd6685ef..eb653e90 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -31,6 +31,7 @@ #include "context.h" #include "ops.h" #include "util.h" +#include "mem.h" typedef struct @@ -48,7 +49,7 @@ release_op_data (void *hook) op_data_t opd = (op_data_t) hook; if (opd->result.fpr) - free (opd->result.fpr); + _gpgme_free (opd->result.fpr); if (opd->key_parameter) gpgme_data_release (opd->key_parameter); } @@ -110,8 +111,8 @@ genkey_status_handler (void *priv, gpgme_status_code_t code, char *args) if (args[1] == ' ') { if (opd->result.fpr) - free (opd->result.fpr); - opd->result.fpr = strdup (&args[2]); + _gpgme_free (opd->result.fpr); + opd->result.fpr = _gpgme_strdup (&args[2]); if (!opd->result.fpr) return gpg_error_from_syserror (); } diff --git a/src/get-env.c b/src/get-env.c index 57fd4196..d611d7e3 100644 --- a/src/get-env.c +++ b/src/get-env.c @@ -26,13 +26,14 @@ #include #include "util.h" +#include "mem.h" #if defined(HAVE_THREAD_SAFE_GETENV) || !defined (HAVE_GETENV_R) /* We prefer using getenv() if it is thread-safe. */ /* Retrieve the environment variable NAME and return a copy of it in a - malloc()'ed buffer in *VALUE. If the environment variable is not + _gpgme_malloc()'ed buffer in *VALUE. If the environment variable is not set, return NULL in *VALUE. */ gpgme_error_t _gpgme_getenv (const char *name, char **value) @@ -44,7 +45,7 @@ _gpgme_getenv (const char *name, char **value) *value = NULL; else { - *value = strdup (env_value); + *value = _gpgme_strdup (env_value); if (!*value) return gpg_error_from_syserror (); } diff --git a/src/gpgme-w32spawn.c b/src/gpgme-w32spawn.c index b510ba3a..20b62136 100644 --- a/src/gpgme-w32spawn.c +++ b/src/gpgme-w32spawn.c @@ -77,7 +77,7 @@ build_commandline (char **argv) /* And a trailing zero. */ n++; - buf = p = malloc (n); + buf = p = _gpgme_malloc (n); if (!buf) return NULL; for (i = 0; argv[i]; i++) @@ -188,7 +188,7 @@ my_spawn (char **argv, struct spawn_fd_item_s *fd_list, unsigned int flags) NULL); if (hnul == INVALID_HANDLE_VALUE) { - free (arg_string); + _gpgme_free (arg_string); /* FIXME: Should translate the error code. */ errno = EIO; return -1; @@ -217,13 +217,13 @@ my_spawn (char **argv, struct spawn_fd_item_s *fd_list, unsigned int flags) &si, /* startup information */ &pi)) /* returns process information */ { - free (arg_string); + _gpgme_free (arg_string); /* FIXME: Should translate the error code. */ errno = EIO; return -1; } - free (arg_string); + _gpgme_free (arg_string); /* Close the /dev/nul handle if used. */ if (hnul != INVALID_HANDLE_VALUE) @@ -399,10 +399,10 @@ translate_handles (const char *trans_file, const char * const *argv, for (idx = 0; argv[idx]; idx++) ; - args = malloc (sizeof (*args) * (idx + 1)); + args = _gpgme_malloc (sizeof (*args) * (idx + 1)); for (idx = 0; argv[idx]; idx++) { - args[idx] = strdup (argv[idx]); + args[idx] = _gpgme_strdup (argv[idx]); if (!args[idx]) return NULL; } @@ -424,7 +424,7 @@ translate_handles (const char *trans_file, const char * const *argv, return NULL; } - args[aidx] = malloc (sizeof (buf)); + args[aidx] = _gpgme_malloc (sizeof (buf)); /* We currently disable translation for stdin/stdout/stderr. We assume that the spawned program handles 0/1/2 specially already. FIXME: Check if this is true. */ diff --git a/src/gpgme.c b/src/gpgme.c index 628cdaee..c76602fe 100644 --- a/src/gpgme.c +++ b/src/gpgme.c @@ -38,6 +38,7 @@ #include "debug.h" #include "priv-io.h" #include "sys-util.h" +#include "mem.h" /* The default locale. */ @@ -52,6 +53,14 @@ gpgme_error_t _gpgme_selftest = GPG_ERR_NOT_OPERATIONAL; accesses to a result structure are read only. */ DEFINE_STATIC_LOCK (result_ref_lock); + +/* Set the default malloc hooks. */ +void +gpgme_set_global_malloc_hooks (gpgme_malloc_hooks_t malloc_hooks) +{ + _gpgme_set_global_malloc_hooks (malloc_hooks); +} + /* Set the global flag NAME to VALUE. Return 0 on success. Note that this function does not use gpgme_error and thus a non-zero return @@ -96,7 +105,7 @@ gpgme_new (gpgme_ctx_t *r_ctx) if (!r_ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); - ctx = calloc (1, sizeof *ctx); + ctx = _gpgme_calloc (1, sizeof *ctx); if (!ctx) return TRACE_ERR (gpg_error_from_syserror ()); @@ -107,7 +116,7 @@ gpgme_new (gpgme_ctx_t *r_ctx) err = gpg_error (GPG_ERR_NO_ENGINE); if (err) { - free (ctx); + _gpgme_free (ctx); return TRACE_ERR (err); } @@ -120,13 +129,13 @@ gpgme_new (gpgme_ctx_t *r_ctx) LOCK (def_lc_lock); if (def_lc_ctype) { - ctx->lc_ctype = strdup (def_lc_ctype); + ctx->lc_ctype = _gpgme_strdup (def_lc_ctype); if (!ctx->lc_ctype) { int saved_err = gpg_error_from_syserror (); UNLOCK (def_lc_lock); _gpgme_engine_info_release (ctx->engine_info); - free (ctx); + _gpgme_free (ctx); return TRACE_ERR (saved_err); } } @@ -135,15 +144,15 @@ gpgme_new (gpgme_ctx_t *r_ctx) if (def_lc_messages) { - ctx->lc_messages = strdup (def_lc_messages); + ctx->lc_messages = _gpgme_strdup (def_lc_messages); if (!ctx->lc_messages) { int saved_err = gpg_error_from_syserror (); UNLOCK (def_lc_lock); if (ctx->lc_ctype) - free (ctx->lc_ctype); + _gpgme_free (ctx->lc_ctype); _gpgme_engine_info_release (ctx->engine_info); - free (ctx); + _gpgme_free (ctx); return TRACE_ERR (saved_err); } } @@ -239,15 +248,15 @@ gpgme_release (gpgme_ctx_t ctx) _gpgme_signers_clear (ctx); _gpgme_sig_notation_clear (ctx); if (ctx->signers) - free (ctx->signers); + _gpgme_free (ctx->signers); if (ctx->lc_ctype) - free (ctx->lc_ctype); + _gpgme_free (ctx->lc_ctype); if (ctx->lc_messages) - free (ctx->lc_messages); + _gpgme_free (ctx->lc_messages); _gpgme_engine_info_release (ctx->engine_info); ctx->engine_info = NULL; DESTROY_LOCK (ctx->lock); - free (ctx); + _gpgme_free (ctx); } @@ -291,7 +300,7 @@ gpgme_result_unref (void *result) if (data->cleanup) (*data->cleanup) (data->hook); - free (data); + _gpgme_free (data); } @@ -745,7 +754,7 @@ gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value) if (!failed && value \ && (category == LC_ALL || category == LC_ ## ucat)) \ { \ - new_lc_ ## lcat = strdup (value); \ + new_lc_ ## lcat = _gpgme_strdup (value); \ if (!new_lc_ ## lcat) \ failed = 1; \ } @@ -762,9 +771,9 @@ gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value) int saved_err = gpg_error_from_syserror (); if (new_lc_ctype) - free (new_lc_ctype); + _gpgme_free (new_lc_ctype); if (new_lc_messages) - free (new_lc_messages); + _gpgme_free (new_lc_messages); return TRACE_ERR (saved_err); } @@ -775,13 +784,13 @@ gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value) if (ctx) \ { \ if (ctx->lc_ ## lcat) \ - free (ctx->lc_ ## lcat); \ + _gpgme_free (ctx->lc_ ## lcat); \ ctx->lc_ ## lcat = new_lc_ ## lcat; \ } \ else \ { \ if (def_lc_ ## lcat) \ - free (def_lc_ ## lcat); \ + _gpgme_free (def_lc_ ## lcat); \ def_lc_ ## lcat = new_lc_ ## lcat; \ } \ } diff --git a/src/gpgme.def b/src/gpgme.def index dc189484..15cced21 100644 --- a/src/gpgme.def +++ b/src/gpgme.def @@ -217,5 +217,6 @@ EXPORTS gpgme_op_spawn_start @163 gpgme_op_spawn @164 + gpgme_set_global_malloc_hooks @165 ; END diff --git a/src/gpgme.h.in b/src/gpgme.h.in index 15ed8037..3a6a69a9 100644 --- a/src/gpgme.h.in +++ b/src/gpgme.h.in @@ -92,6 +92,14 @@ extern "C" { #endif #endif +struct gpgme_malloc_hooks +{ + void *(*malloc) (size_t cnt); + void *(*calloc) (size_t nmemb, size_t cnt); + void *(*realloc) (void *ptr, size_t cnt); + void (*free) (void *ptr); +}; +typedef struct gpgme_malloc_hooks *gpgme_malloc_hooks_t; /* Some opaque data types used by GPGME. */ @@ -196,6 +204,9 @@ gpgme_error_from_syserror (void) return gpgme_error (gpgme_err_code_from_syserror ()); } +/* Set the default malloc hooks. */ +void gpgme_set_global_malloc_hooks (gpgme_malloc_hooks_t malloc_hooks); + /* The possible encoding mode of gpgme_data_t objects. */ typedef enum diff --git a/src/import.c b/src/import.c index 6233a155..c782a3ff 100644 --- a/src/import.c +++ b/src/import.c @@ -31,6 +31,7 @@ #include "context.h" #include "ops.h" #include "util.h" +#include "mem.h" typedef struct @@ -53,8 +54,8 @@ release_op_data (void *hook) while (import) { gpgme_import_status_t next = import->next; - free (import->fpr); - free (import); + _gpgme_free (import->fpr); + _gpgme_free (import); import = next; } } @@ -120,7 +121,7 @@ parse_import (char *args, gpgme_import_status_t *import_status, int problem) char *tail; long int nr; - import = malloc (sizeof (*import)); + import = _gpgme_malloc (sizeof (*import)); if (!import) return gpg_error_from_syserror (); import->next = NULL; @@ -130,7 +131,7 @@ parse_import (char *args, gpgme_import_status_t *import_status, int problem) if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (import); + _gpgme_free (import); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; @@ -171,10 +172,10 @@ parse_import (char *args, gpgme_import_status_t *import_status, int problem) if (tail) *tail = '\0'; - import->fpr = strdup (args); + import->fpr = _gpgme_strdup (args); if (!import->fpr) { - free (import); + _gpgme_free (import); return gpg_error_from_syserror (); } diff --git a/src/key.c b/src/key.c index 1a68966d..1ccecd89 100644 --- a/src/key.c +++ b/src/key.c @@ -31,6 +31,7 @@ #include "ops.h" #include "sema.h" #include "debug.h" +#include "mem.h" /* Protects all reference counters in keys. All other accesses to a @@ -44,7 +45,7 @@ _gpgme_key_new (gpgme_key_t *r_key) { gpgme_key_t key; - key = calloc (1, sizeof *key); + key = _gpgme_calloc (1, sizeof *key); if (!key) return gpg_error_from_syserror (); key->_refs = 1; @@ -59,7 +60,7 @@ _gpgme_key_add_subkey (gpgme_key_t key, gpgme_subkey_t *r_subkey) { gpgme_subkey_t subkey; - subkey = calloc (1, sizeof *subkey); + subkey = _gpgme_calloc (1, sizeof *subkey); if (!subkey) return gpg_error_from_syserror (); subkey->keyid = subkey->_keyid; @@ -213,7 +214,7 @@ _gpgme_key_append_name (gpgme_key_t key, const char *src, int convert) /* We can malloc a buffer of the same length, because the converted string will never be larger. Actually we allocate it twice the size, so that we are able to store the parsed stuff there too. */ - uid = malloc (sizeof (*uid) + 2 * src_len + 3); + uid = _gpgme_malloc (sizeof (*uid) + 2 * src_len + 3); if (!uid) return gpg_error_from_syserror (); memset (uid, 0, sizeof *uid); @@ -258,7 +259,7 @@ _gpgme_key_add_sig (gpgme_key_t key, char *src) /* We can malloc a buffer of the same length, because the converted string will never be larger. Actually we allocate it twice the size, so that we are able to store the parsed stuff there too. */ - sig = malloc (sizeof (*sig) + 2 * src_len + 3); + sig = _gpgme_malloc (sizeof (*sig) + 2 * src_len + 3); if (!sig) return NULL; memset (sig, 0, sizeof *sig); @@ -330,12 +331,12 @@ gpgme_key_unref (gpgme_key_t key) { gpgme_subkey_t next = subkey->next; if (subkey->fpr) - free (subkey->fpr); + _gpgme_free (subkey->fpr); if (subkey->curve) - free (subkey->curve); + _gpgme_free (subkey->curve); if (subkey->card_number) - free (subkey->card_number); - free (subkey); + _gpgme_free (subkey->card_number); + _gpgme_free (subkey); subkey = next; } @@ -358,22 +359,22 @@ gpgme_key_unref (gpgme_key_t key) notation = next_notation; } - free (keysig); + _gpgme_free (keysig); keysig = next_keysig; } - free (uid); + _gpgme_free (uid); uid = next_uid; } if (key->issuer_serial) - free (key->issuer_serial); + _gpgme_free (key->issuer_serial); if (key->issuer_name) - free (key->issuer_name); + _gpgme_free (key->issuer_name); if (key->chain_id) - free (key->chain_id); + _gpgme_free (key->chain_id); - free (key); + _gpgme_free (key); } diff --git a/src/keylist.c b/src/keylist.c index 36ee3eaa..ac363365 100644 --- a/src/keylist.c +++ b/src/keylist.c @@ -41,6 +41,7 @@ #include "context.h" #include "ops.h" #include "debug.h" +#include "mem.h" struct key_queue_item_s @@ -385,7 +386,7 @@ parse_sec_field15 (gpgme_key_t key, gpgme_subkey_t subkey, char *field) /* Fields starts with a hex digit; thus it is a serial number. */ key->secret = 1; subkey->is_cardkey = 1; - subkey->card_number = strdup (field); + subkey->card_number = _gpgme_strdup (field); if (!subkey->card_number) return gpg_error_from_syserror (); } @@ -564,7 +565,7 @@ keylist_colon_handler (void *priv, char *line) /* Field 8 has the X.509 serial number. */ if (fields >= 8 && (rectype == RT_CRT || rectype == RT_CRS)) { - key->issuer_serial = strdup (field[7]); + key->issuer_serial = _gpgme_strdup (field[7]); if (!key->issuer_serial) return gpg_error_from_syserror (); } @@ -598,7 +599,7 @@ keylist_colon_handler (void *priv, char *line) /* Field 17 has the curve name for ECC. */ if (fields >= 17 && *field[16]) { - subkey->curve = strdup (field[16]); + subkey->curve = _gpgme_strdup (field[16]); if (!subkey->curve) return gpg_error_from_syserror (); } @@ -671,7 +672,7 @@ keylist_colon_handler (void *priv, char *line) /* Field 17 has the curve name for ECC. */ if (fields >= 17 && *field[16]) { - subkey->curve = strdup (field[16]); + subkey->curve = _gpgme_strdup (field[16]); if (!subkey->curve) return gpg_error_from_syserror (); } @@ -702,7 +703,7 @@ keylist_colon_handler (void *priv, char *line) subkey = key->_last_subkey; if (!subkey->fpr) { - subkey->fpr = strdup (field[9]); + subkey->fpr = _gpgme_strdup (field[9]); if (!subkey->fpr) return gpg_error_from_syserror (); } @@ -711,7 +712,7 @@ keylist_colon_handler (void *priv, char *line) /* Field 13 has the gpgsm chain ID (take only the first one). */ if (fields >= 13 && !key->chain_id && *field[12]) { - key->chain_id = strdup (field[12]); + key->chain_id = _gpgme_strdup (field[12]); if (!key->chain_id) return gpg_error_from_syserror (); } @@ -858,7 +859,7 @@ _gpgme_op_keylist_event_cb (void *data, gpgme_event_io_t type, void *type_data) if (err) return; - q = malloc (sizeof *q); + q = _gpgme_malloc (sizeof *q); if (!q) { gpgme_key_unref (key); @@ -1000,7 +1001,7 @@ gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key) opd->key_cond = 0; *r_key = queue_item->key; - free (queue_item); + _gpgme_free (queue_item); return TRACE_SUC2 ("key=%p (%s)", *r_key, ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? diff --git a/src/libgpgme.vers b/src/libgpgme.vers index 39663c1c..2284c698 100644 --- a/src/libgpgme.vers +++ b/src/libgpgme.vers @@ -81,6 +81,7 @@ GPGME_1.1 { gpgme_op_passwd_start; gpgme_op_passwd; + gpgme_set_global_malloc_hooks; gpgme_set_global_flag; gpgme_io_writen; diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 00000000..99ae41a5 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,69 @@ +/* gpgme.c - GnuPG Made Easy. + Copyright (C) 2014 g10 Code GmbH + + This file is part of GPGME. + + GPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + GPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this program; if not, see . + */ +#if HAVE_CONFIG_H +#include +#endif +#include +#include +#include "gpgme.h" +#include "util.h" +#include "mem.h" + +/* The default memory management functions. */ +static struct gpgme_malloc_hooks _gpgme_default_malloc_hooks = + { malloc, calloc, realloc, free }; + +void * +_gpgme_malloc (size_t size) +{ + return _gpgme_default_malloc_hooks.malloc (size); +} + +void * +_gpgme_calloc (size_t nmemb, size_t size) +{ + return _gpgme_default_malloc_hooks.calloc (nmemb, size); +} + +void * +_gpgme_realloc (void *p, size_t size) +{ + return _gpgme_default_malloc_hooks.realloc (p, size); +} + +void +_gpgme_free (void *p) +{ + _gpgme_default_malloc_hooks.free (p); +} + +char * +_gpgme_strdup (const char *s) +{ + char *r = _gpgme_malloc (strlen(s)+1); + + stpcpy (r, s); + return r; +} + +void +_gpgme_set_global_malloc_hooks (gpgme_malloc_hooks_t malloc_hooks) +{ + _gpgme_default_malloc_hooks = *malloc_hooks; +} diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 00000000..8ee11bad --- /dev/null +++ b/src/mem.h @@ -0,0 +1,31 @@ +/* gpgme.c - GnuPG Made Easy. + Copyright (C) 2014 g10 Code GmbH + + This file is part of GPGME. + + GPGME is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + GPGME is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this program; if not, see . + */ +#ifndef MEM_H +#define MEM_H + +#include "gpgme.h" + +void *_gpgme_malloc (size_t); +void *_gpgme_calloc (size_t, size_t); +void *_gpgme_realloc (void *, size_t); +void _gpgme_free (void *); +char *_gpgme_strdup (const char *); +void _gpgme_set_global_malloc_hooks (gpgme_malloc_hooks_t); + +#endif diff --git a/src/op-support.c b/src/op-support.c index 2bcb3a35..885048f2 100644 --- a/src/op-support.c +++ b/src/op-support.c @@ -32,6 +32,7 @@ #include "ops.h" #include "util.h" #include "debug.h" +#include "mem.h" gpgme_error_t @@ -54,7 +55,7 @@ _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook, return 0; } - data = calloc (1, sizeof (struct ctx_op_data) + size); + data = _gpgme_calloc (1, sizeof (struct ctx_op_data) + size); if (!data) return gpg_error_from_syserror (); data->magic = CTX_OP_DATA_MAGIC; @@ -199,7 +200,7 @@ _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key) char *tail; long int reason; - inv_key = malloc (sizeof (*inv_key)); + inv_key = _gpgme_malloc (sizeof (*inv_key)); if (!inv_key) return gpg_error_from_syserror (); inv_key->next = NULL; @@ -208,7 +209,7 @@ _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key) if (errno || args == tail || (*tail && *tail != ' ')) { /* The crypto backend does not behave. */ - free (inv_key); + _gpgme_free (inv_key); return trace_gpg_error (GPG_ERR_INV_ENGINE); } @@ -280,10 +281,10 @@ _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key) tail++; if (*tail) { - inv_key->fpr = strdup (tail); + inv_key->fpr = _gpgme_strdup (tail); if (!inv_key->fpr) { - free (inv_key); + _gpgme_free (inv_key); return gpg_error_from_syserror (); } } @@ -329,7 +330,7 @@ _gpgme_parse_plaintext (char *args, char **filenamep) *tail = '\0'; if (filenamep && *args != '\0') { - char *filename = strdup (args); + char *filename = _gpgme_strdup (args); if (!filename) return gpg_error_from_syserror (); diff --git a/src/passphrase.c b/src/passphrase.c index 00e9d999..67b4ce69 100644 --- a/src/passphrase.c +++ b/src/passphrase.c @@ -33,6 +33,7 @@ #include "ops.h" #include "util.h" #include "debug.h" +#include "mem.h" typedef struct @@ -50,9 +51,9 @@ release_op_data (void *hook) op_data_t opd = (op_data_t) hook; if (opd->passphrase_info) - free (opd->passphrase_info); + _gpgme_free (opd->passphrase_info); if (opd->uid_hint) - free (opd->uid_hint); + _gpgme_free (opd->uid_hint); } @@ -75,8 +76,8 @@ _gpgme_passphrase_status_handler (void *priv, gpgme_status_code_t code, { case GPGME_STATUS_USERID_HINT: if (opd->uid_hint) - free (opd->uid_hint); - if (!(opd->uid_hint = strdup (args))) + _gpgme_free (opd->uid_hint); + if (!(opd->uid_hint = _gpgme_strdup (args))) return gpg_error_from_syserror (); break; @@ -94,8 +95,8 @@ _gpgme_passphrase_status_handler (void *priv, gpgme_status_code_t code, case GPGME_STATUS_NEED_PASSPHRASE_SYM: case GPGME_STATUS_NEED_PASSPHRASE_PIN: if (opd->passphrase_info) - free (opd->passphrase_info); - opd->passphrase_info = strdup (args); + _gpgme_free (opd->passphrase_info); + opd->passphrase_info = _gpgme_strdup (args); if (!opd->passphrase_info) return gpg_error_from_syserror (); break; diff --git a/src/posix-io.c b/src/posix-io.c index ac823fc8..6e19015c 100644 --- a/src/posix-io.c +++ b/src/posix-io.c @@ -49,6 +49,7 @@ #include "sema.h" #include "ath.h" #include "debug.h" +#include "mem.h" void @@ -224,7 +225,7 @@ _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler, notify_table_item_t newtbl; size_t newsize = notify_table_size + 64; - newtbl = calloc (newsize, sizeof *newtbl); + newtbl = _gpgme_calloc (newsize, sizeof *newtbl); if (!newtbl) { res = -1; @@ -238,7 +239,7 @@ _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler, newtbl[idx].handler = NULL; newtbl[idx].value = NULL; } - free (notify_table); + _gpgme_free (notify_table); notify_table = newtbl; idx = notify_table_size; notify_table_size = newsize; diff --git a/src/posix-util.c b/src/posix-util.c index f7e0a171..55a076e9 100644 --- a/src/posix-util.c +++ b/src/posix-util.c @@ -30,6 +30,7 @@ #include "util.h" #include "sys-util.h" #include "debug.h" +#include "mem.h" /* These variables store the malloced name of alternative default binaries. The are set only once by gpgme_set_global_flag. */ @@ -49,7 +50,7 @@ _gpgme_set_default_gpg_name (const char *name) name = s + 1; if (!default_gpg_name) - default_gpg_name = strdup (name); + default_gpg_name = _gpgme_strdup (name); return !default_gpg_name; } @@ -66,7 +67,7 @@ _gpgme_set_default_gpgconf_name (const char *name) name = s + 1; if (!default_gpgconf_name) - default_gpgconf_name = strdup (name); + default_gpgconf_name = _gpgme_strdup (name); return !default_gpgconf_name; } @@ -86,7 +87,7 @@ walk_path (const char *pgm) orig_path = "/bin:/usr/bin"; #endif - fname = malloc (strlen (orig_path) + 1 + strlen (pgm) + 1); + fname = _gpgme_malloc (strlen (orig_path) + 1 + strlen (pgm) + 1); if (!fname) return NULL; @@ -108,7 +109,7 @@ walk_path (const char *pgm) _gpgme_debug (DEBUG_ENGINE, "gpgme-walk_path: '%s' not found in '%s'", pgm, orig_path); - free (fname); + _gpgme_free (fname); return NULL; } diff --git a/src/progress.c b/src/progress.c index c10ccaa8..7a2545f5 100644 --- a/src/progress.c +++ b/src/progress.c @@ -29,6 +29,7 @@ #include "util.h" #include "context.h" #include "debug.h" +#include "mem.h" gpgme_error_t @@ -45,7 +46,7 @@ _gpgme_progress_status_handler (void *priv, gpgme_status_code_t code, if (code != GPGME_STATUS_PROGRESS || !*args || !ctx->progress_cb) return 0; - args_cpy = strdup (args); + args_cpy = _gpgme_strdup (args); if (!args_cpy) return gpg_error_from_syserror (); @@ -77,6 +78,6 @@ _gpgme_progress_status_handler (void *priv, gpgme_status_code_t code, if (type != 'X') ctx->progress_cb (ctx->progress_cb_value, args_cpy, type, current, total); - free (args_cpy); + _gpgme_free (args_cpy); return 0; } diff --git a/src/setenv.c b/src/setenv.c index d85bec9c..99d762cf 100644 --- a/src/setenv.c +++ b/src/setenv.c @@ -142,7 +142,7 @@ __add_to_environ (const char *name, const char *value, const char *combined, char **new_environ; /* We allocated this space; we can extend it. */ - new_environ = (char **) realloc (last_environ, + new_environ = (char **) _gpgme_realloc (last_environ, (size + 2) * sizeof (char *)); if (new_environ == NULL) { @@ -177,7 +177,7 @@ __add_to_environ (const char *name, const char *value, const char *combined, if (__builtin_expect (new_environ[size] == NULL, 1)) #endif { - new_environ[size] = (char *) malloc (namelen + 1 + vallen); + new_environ[size] = (char *) _gpgme_malloc (namelen + 1 + vallen); if (__builtin_expect (new_environ[size] == NULL, 0)) { __set_errno (ENOMEM); @@ -235,7 +235,7 @@ __add_to_environ (const char *name, const char *value, const char *combined, if (__builtin_expect (np == NULL, 1)) #endif { - np = malloc (namelen + 1 + vallen); + np = _gpgme_malloc (namelen + 1 + vallen); if (__builtin_expect (np == NULL, 0)) { UNLOCK; @@ -321,7 +321,7 @@ clearenv (void) if (__environ == last_environ && __environ != NULL) { /* We allocated this environment so we can free it. */ - free (__environ); + _gpgme_free (__environ); last_environ = NULL; } diff --git a/src/sig-notation.c b/src/sig-notation.c index c747ad62..e18f54d8 100644 --- a/src/sig-notation.c +++ b/src/sig-notation.c @@ -31,6 +31,7 @@ #include "context.h" #include "ops.h" #include "debug.h" +#include "mem.h" /* Free the signature notation object and all associated resources. @@ -40,12 +41,12 @@ void _gpgme_sig_notation_free (gpgme_sig_notation_t notation) { if (notation->name) - free (notation->name); + _gpgme_free (notation->name); if (notation->value) - free (notation->value); + _gpgme_free (notation->value); - free (notation); + _gpgme_free (notation); } @@ -77,7 +78,7 @@ _gpgme_sig_notation_create (gpgme_sig_notation_t *notationp, if (name && !(flags & GPGME_SIG_NOTATION_HUMAN_READABLE)) return gpg_error (GPG_ERR_INV_VALUE); - notation = calloc (1, sizeof (*notation)); + notation = _gpgme_calloc (1, sizeof (*notation)); if (!notation) return gpg_error_from_syserror (); @@ -88,7 +89,7 @@ _gpgme_sig_notation_create (gpgme_sig_notation_t *notationp, { /* We add a trailing '\0' for stringification in the good case. */ - notation->name = malloc (name_len + 1); + notation->name = _gpgme_malloc (name_len + 1); if (!notation->name) { err = gpg_error_from_syserror (); @@ -104,7 +105,7 @@ _gpgme_sig_notation_create (gpgme_sig_notation_t *notationp, { /* We add a trailing '\0' for stringification in the good case. */ - notation->value = malloc (value_len + 1); + notation->value = _gpgme_malloc (value_len + 1); if (!notation->value) { err = gpg_error_from_syserror (); @@ -234,7 +235,7 @@ _gpgme_parse_notation (gpgme_sig_notation_t *notationp, /* Small sanity check. */ if (4 + 2 + 2 + name_len + value_len > len) { - free (decoded_data); + _gpgme_free (decoded_data); return trace_gpg_error (GPG_ERR_INV_ENGINE); } @@ -256,6 +257,6 @@ _gpgme_parse_notation (gpgme_sig_notation_t *notationp, err = _gpgme_sig_notation_create (notationp, name, name_len, value, value_len, flags); - free (decoded_data); + _gpgme_free (decoded_data); return err; } diff --git a/src/sign.c b/src/sign.c index c55441df..377ce66d 100644 --- a/src/sign.c +++ b/src/sign.c @@ -33,6 +33,7 @@ #include "ops.h" #include "util.h" #include "debug.h" +#include "mem.h" typedef struct @@ -65,16 +66,16 @@ release_op_data (void *hook) { gpgme_invalid_key_t next = invalid_signer->next; if (invalid_signer->fpr) - free (invalid_signer->fpr); - free (invalid_signer); + _gpgme_free (invalid_signer->fpr); + _gpgme_free (invalid_signer); invalid_signer = next; } while (sig) { gpgme_new_signature_t next = sig->next; - free (sig->fpr); - free (sig); + _gpgme_free (sig->fpr); + _gpgme_free (sig); sig = next; } } @@ -148,7 +149,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, gpgme_new_signature_t sig; char *tail; - sig = malloc (sizeof (*sig)); + sig = _gpgme_malloc (sizeof (*sig)); if (!sig) return gpg_error_from_syserror (); @@ -169,14 +170,14 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, default: /* The backend engine is not behaving. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args++; if (*args != ' ') { - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } @@ -185,7 +186,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; @@ -194,7 +195,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; @@ -205,7 +206,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; @@ -214,7 +215,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (sig->timestamp == -1 || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; @@ -224,7 +225,7 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (!*args) { /* The crypto backend does not behave. */ - free (sig); + _gpgme_free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } @@ -232,10 +233,10 @@ parse_sig_created (char *args, gpgme_new_signature_t *sigp, if (tail) *tail = '\0'; - sig->fpr = strdup (args); + sig->fpr = _gpgme_strdup (args); if (!sig->fpr) { - free (sig); + _gpgme_free (sig); return gpg_error_from_syserror (); } *sigp = sig; diff --git a/src/signers.c b/src/signers.c index f43fafc7..e2535b66 100644 --- a/src/signers.c +++ b/src/signers.c @@ -32,6 +32,7 @@ #include "util.h" #include "context.h" #include "debug.h" +#include "mem.h" /* Delete all signers from CTX. */ @@ -78,7 +79,7 @@ gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key) int n = ctx->signers_size + 5; int j; - newarr = realloc (ctx->signers, n * sizeof (*newarr)); + newarr = _gpgme_realloc (ctx->signers, n * sizeof (*newarr)); if (!newarr) return TRACE_ERR (gpg_error_from_syserror ()); for (j = ctx->signers_size; j < n; j++) diff --git a/src/trust-item.c b/src/trust-item.c index f9378c68..9c1a57ea 100644 --- a/src/trust-item.c +++ b/src/trust-item.c @@ -31,6 +31,7 @@ #include "ops.h" #include "sema.h" #include "debug.h" +#include "mem.h" /* Protects all reference counters in trust items. All other accesses @@ -45,7 +46,7 @@ _gpgme_trust_item_new (gpgme_trust_item_t *r_item) { gpgme_trust_item_t item; - item = calloc (1, sizeof *item); + item = _gpgme_calloc (1, sizeof *item); if (!item) return gpg_error_from_syserror (); item->_refs = 1; @@ -88,8 +89,8 @@ gpgme_trust_item_unref (gpgme_trust_item_t item) UNLOCK (trust_item_ref_lock); if (item->name) - free (item->name); - free (item); + _gpgme_free (item->name); + _gpgme_free (item); } diff --git a/src/trustlist.c b/src/trustlist.c index d4567805..a935b3fe 100644 --- a/src/trustlist.c +++ b/src/trustlist.c @@ -32,6 +32,7 @@ #include "util.h" #include "context.h" #include "ops.h" +#include "mem.h" struct trust_queue_item_s @@ -108,7 +109,7 @@ trustlist_colon_handler (void *priv, char *line) item->_validity[0] = *p; break; case 9: /* user ID */ - item->name = strdup (p); + item->name = _gpgme_strdup (p); if (!item->name) { int saved_err = gpg_error_from_syserror (); @@ -143,7 +144,7 @@ _gpgme_op_trustlist_event_cb (void *data, gpgme_event_io_t type, if (err) return; - q = malloc (sizeof *q); + q = _gpgme_malloc (sizeof *q); if (!q) { gpgme_trust_item_unref (item); @@ -239,7 +240,7 @@ gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item) opd->trust_queue = q->next; *r_item = q->item; - free (q); + _gpgme_free (q); if ((*r_item)->type == 1) { TRACE_SUC5 ("trust_item=%p: %s: owner trust %s with level %i " diff --git a/src/util.h b/src/util.h index 365f1d83..cc844366 100644 --- a/src/util.h +++ b/src/util.h @@ -81,11 +81,9 @@ _gpgme_stpcpy (char *a, const char *b) #define stpcpy(a,b) _gpgme_stpcpy ((a), (b)) #endif /*!HAVE_STPCPY*/ -#if !HAVE_VASPRINTF #include -int vasprintf (char **result, const char *format, va_list args); -int asprintf (char **result, const char *format, ...); -#endif +int _gpgme_vasprintf (char **result, const char *format, va_list args); +int _gpgme_asprintf (char **result, const char *format, ...); #if REPLACE_TTYNAME_R int _gpgme_ttyname_r (int fd, char *buf, size_t buflen); diff --git a/src/vasprintf.c b/src/vasprintf.c index 4c40131d..326a2c35 100644 --- a/src/vasprintf.c +++ b/src/vasprintf.c @@ -26,6 +26,8 @@ Boston, MA 02111-1307, USA. */ #include #include +#include "mem.h" + #ifndef va_copy /* According to POSIX, va_copy is a macro. */ #if defined (__GNUC__) && defined (__PPC__) \ @@ -54,7 +56,7 @@ int_vasprintf (result, format, args) #ifdef HAVE_W32CE_SYSTEM /* No va_copy and the replacement above doesn't work. */ #define MAX_STRLEN 256 - *result = malloc (MAX_STRLEN); + *result = _gpgme_malloc (MAX_STRLEN); if (*result != NULL) { int res = _vsnprintf (*result, MAX_STRLEN, format, *args); @@ -141,7 +143,7 @@ int_vasprintf (result, format, args) #ifdef TEST global_total_width = total_width; #endif - *result = malloc (total_width); + *result = _gpgme_malloc (total_width); if (*result != NULL) return vsprintf (*result, format, *args); else @@ -150,7 +152,7 @@ int_vasprintf (result, format, args) } int -vasprintf (result, format, args) +_gpgme_vasprintf (result, format, args) char **result; const char *format; #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__) @@ -164,13 +166,13 @@ vasprintf (result, format, args) int -asprintf (char **buf, const char *fmt, ...) +_gpgme_asprintf (char **buf, const char *fmt, ...) { int status; va_list ap; va_start (ap, fmt); - status = vasprintf (buf, fmt, ap); + status = _gpgme_vasprintf (buf, fmt, ap); va_end (ap); return status; } @@ -184,7 +186,7 @@ checkit (const char* format, ...) char *result; va_start (args, format); - vasprintf (&result, format, args); + _gpgme_vasprintf (&result, format, args); if (strlen (result) < global_total_width) printf ("PASS: "); else diff --git a/src/verify.c b/src/verify.c index 37b2bd46..04299c78 100644 --- a/src/verify.c +++ b/src/verify.c @@ -32,6 +32,7 @@ #include "util.h" #include "context.h" #include "ops.h" +#include "mem.h" typedef struct @@ -65,15 +66,15 @@ release_op_data (void *hook) } if (sig->fpr) - free (sig->fpr); + _gpgme_free (sig->fpr); if (sig->pka_address) - free (sig->pka_address); - free (sig); + _gpgme_free (sig->pka_address); + _gpgme_free (sig); sig = next; } if (opd->result.file_name) - free (opd->result.file_name); + _gpgme_free (opd->result.file_name); } @@ -249,7 +250,7 @@ prepare_new_sig (op_data_t opd) } else { - sig = calloc (1, sizeof (*sig)); + sig = _gpgme_calloc (1, sizeof (*sig)); if (!sig) return gpg_error_from_syserror (); if (!opd->result.signatures) @@ -385,7 +386,7 @@ parse_new_sig (op_data_t opd, gpgme_status_code_t code, char *args, if (*args) { - sig->fpr = strdup (args); + sig->fpr = _gpgme_strdup (args); if (!sig->fpr) return gpg_error_from_syserror (); } @@ -408,8 +409,8 @@ parse_valid_sig (gpgme_signature_t sig, char *args, gpgme_protocol_t protocol) return gpg_error (GPG_ERR_GENERAL); if (sig->fpr) - free (sig->fpr); - sig->fpr = strdup (args); + _gpgme_free (sig->fpr); + sig->fpr = _gpgme_strdup (args); if (!sig->fpr) return gpg_error_from_syserror (); @@ -550,14 +551,14 @@ parse_notation (gpgme_signature_t sig, gpgme_status_code_t code, char *args) if (!notation->value) { - dest = notation->value = malloc (len); + dest = notation->value = _gpgme_malloc (len); if (!dest) return gpg_error_from_syserror (); } else { int cur_len = strlen (notation->value); - dest = realloc (notation->value, len + strlen (notation->value)); + dest = _gpgme_realloc (notation->value, len + strlen (notation->value)); if (!dest) return gpg_error_from_syserror (); notation->value = dest; @@ -753,7 +754,7 @@ _gpgme_verify_status_handler (void *priv, gpgme_status_code_t code, char *args) end = strchr (args, ' '); if (end) *end = 0; - sig->pka_address = strdup (args); + sig->pka_address = _gpgme_strdup (args); break; case GPGME_STATUS_ERROR: @@ -784,7 +785,7 @@ _gpgme_verify_status_handler (void *priv, gpgme_status_code_t code, char *args) } /* Note that there is no need to release the members of SIG because we won't be here if they have been set. */ - free (sig); + _gpgme_free (sig); opd->current_sig = NULL; } opd->only_newsig_seen = 0; diff --git a/src/version.c b/src/version.c index 15e5aeec..75b7bcda 100644 --- a/src/version.c +++ b/src/version.c @@ -34,6 +34,7 @@ #include "priv-io.h" #include "debug.h" #include "context.h" +#include "mem.h" /* For _gpgme_sema_subsystem_init and _gpgme_status_init. */ #include "sema.h" @@ -359,7 +360,7 @@ _gpgme_get_program_version (const char *const file_name) s = extract_version_string (line, &len); if (!len) return NULL; - mark = malloc (len + 1); + mark = _gpgme_malloc (len + 1); if (!mark) return NULL; memcpy (mark, s, len); diff --git a/src/vfs-create.c b/src/vfs-create.c index 48931b9a..0ff88f43 100644 --- a/src/vfs-create.c +++ b/src/vfs-create.c @@ -30,6 +30,7 @@ #include "context.h" #include "ops.h" #include "util.h" +#include "mem.h" static gpgme_error_t vfs_start (gpgme_ctx_t ctx, int synchronous, @@ -132,39 +133,39 @@ _gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], { if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) { - free (container_file_esc); + _gpgme_free (container_file_esc); return gpg_error (GPG_ERR_UNUSABLE_PUBKEY); } - if (asprintf (&cmd, "RECIPIENT %s", recp[i]->subkeys->fpr) < 0) + if (_gpgme_asprintf (&cmd, "RECIPIENT %s", recp[i]->subkeys->fpr) < 0) { err = gpg_error_from_syserror (); - free (container_file_esc); + _gpgme_free (container_file_esc); return err; } err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); - free (cmd); + _gpgme_free (cmd); if (err || *op_err) { - free (container_file_esc); + _gpgme_free (container_file_esc); return err; } recp++; } - if (asprintf (&cmd, "CREATE -- %s", container_file_esc) < 0) + if (_gpgme_asprintf (&cmd, "CREATE -- %s", container_file_esc) < 0) { err = gpg_error_from_syserror (); - free (container_file_esc); + _gpgme_free (container_file_esc); return err; } - free (container_file_esc); + _gpgme_free (container_file_esc); err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); - free (cmd); + _gpgme_free (cmd); return err; } diff --git a/src/vfs-mount.c b/src/vfs-mount.c index b9b1b4d2..5f0a196e 100644 --- a/src/vfs-mount.c +++ b/src/vfs-mount.c @@ -29,6 +29,7 @@ #include "context.h" #include "ops.h" #include "util.h" +#include "mem.h" typedef struct { @@ -71,8 +72,8 @@ _gpgme_vfs_mount_status_handler (void *priv, const char *code, const char *args) if (! strcasecmp ("MOUNTPOINT", code)) { if (opd->result.mount_dir) - free (opd->result.mount_dir); - opd->result.mount_dir = strdup (args); + _gpgme_free (opd->result.mount_dir); + opd->result.mount_dir = _gpgme_strdup (args); } return 0; @@ -182,17 +183,17 @@ _gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, if (err) return err; - if (asprintf (&cmd, "OPEN -- %s", container_file_esc) < 0) + if (_gpgme_asprintf (&cmd, "OPEN -- %s", container_file_esc) < 0) { err = gpg_error_from_syserror (); - free (container_file_esc); + _gpgme_free (container_file_esc); return err; } - free (container_file_esc); + _gpgme_free (container_file_esc); err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); - free (cmd); + _gpgme_free (cmd); if (err || *op_err) return err; @@ -204,23 +205,23 @@ _gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, if (err) return err; - if (asprintf (&cmd, "MOUNT -- %s", mount_dir_esc) < 0) + if (_gpgme_asprintf (&cmd, "MOUNT -- %s", mount_dir_esc) < 0) { err = gpg_error_from_syserror (); - free (mount_dir_esc); + _gpgme_free (mount_dir_esc); return err; } - free (mount_dir_esc); + _gpgme_free (mount_dir_esc); } else { - if (asprintf (&cmd, "MOUNT") < 0) + if (_gpgme_asprintf (&cmd, "MOUNT") < 0) return gpg_error_from_syserror (); } err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, _gpgme_vfs_mount_status_handler, ctx, op_err); - free (cmd); + _gpgme_free (cmd); return err; } diff --git a/src/w32-ce.c b/src/w32-ce.c index e42f053c..067f5d6f 100644 --- a/src/w32-ce.c +++ b/src/w32-ce.c @@ -58,14 +58,14 @@ wchar_to_utf8 (const wchar_t *string) return NULL; } - result = malloc (n+1); + result = _gpgme_malloc (n+1); if (!result) return NULL; n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL); if (n < 0) { - free (result); + _gpgme_free (result); gpg_err_set_errno (EINVAL); result = NULL; } @@ -97,14 +97,14 @@ utf8_to_wchar (const char *string) gpg_err_set_errno (ENOMEM); return NULL; } - result = malloc (nbytes); + result = _gpgme_malloc (nbytes); if (!result) return NULL; n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n); if (n < 0) { - free (result); + _gpgme_free (result); gpg_err_set_errno (EINVAL); result = NULL; } @@ -124,7 +124,7 @@ getenv (const char *name) if (past_result) { - free (past_result); + _gpgme_free (past_result); past_result = NULL; } @@ -179,7 +179,7 @@ DeleteFileA (LPCSTR lpFileName) result = DeleteFileW (filename); err = GetLastError (); - free (filename); + _gpgme_free (filename); SetLastError (err); return result; } @@ -204,7 +204,7 @@ CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwSharedMode, dwFlagsAndAttributes, hTemplateFile); err = GetLastError (); - free (filename); + _gpgme_free (filename); SetLastError (err); return result; } @@ -242,7 +242,7 @@ CreateProcessA (LPCSTR pszImageName, LPSTR pszCmdLine, if (!cmd_line) { if (image_name) - free (image_name); + _gpgme_free (image_name); return 0; } } @@ -251,8 +251,8 @@ CreateProcessA (LPCSTR pszImageName, LPSTR pszCmdLine, fdwCreate, NULL, NULL, NULL, pProcInfo); err = GetLastError (); - free (image_name); - free (cmd_line); + _gpgme_free (image_name); + _gpgme_free (cmd_line); SetLastError (err); return result; } @@ -278,7 +278,7 @@ RegOpenKeyExA (HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, result = RegOpenKeyEx (hKey, subkey, ulOptions, samDesired, phkResult); err = GetLastError (); - free (subkey); + _gpgme_free (subkey); SetLastError (err); return result; } @@ -307,21 +307,21 @@ RegQueryValueExA (HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, err = RegQueryValueExW (hKey, name, lpReserved, lpType, NULL, &data_len); if (err || !lpcbData) { - free (name); + _gpgme_free (name); return err; } - data = malloc (data_len + sizeof (wchar_t)); + data = _gpgme_malloc (data_len + sizeof (wchar_t)); if (!data) { - free (name); + _gpgme_free (name); return ERROR_NOT_ENOUGH_MEMORY; } err = RegQueryValueExW (hKey, name, lpReserved, &type, data, &data_len); if (lpType) *lpType = type; - free (name); + _gpgme_free (name); /* If err is ERROR_MORE_DATA, there probably was a race condition. We can punt this to the caller just as well. */ if (err) @@ -346,7 +346,7 @@ RegQueryValueExA (HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, assert (data_c_len <= data_len + sizeof (wchar_t)); memcpy (data, data_c, data_c_len); data_len = data_c_len; - free (data_c); + _gpgme_free (data_c); } /* DATA and DATA_LEN now contain the result. */ @@ -384,7 +384,7 @@ GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer) char *buffer_c; DWORD len_c; - buffer_w = malloc (sizeof (wchar_t) * len); + buffer_w = _gpgme_malloc (sizeof (wchar_t) * len); if (! buffer_w) return 0; @@ -392,7 +392,7 @@ GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer) /* Give up if we still can't get at it. */ if (len_w == 0 || len_w >= len) { - free (buffer_w); + _gpgme_free (buffer_w); return 0; } @@ -400,7 +400,7 @@ GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer) buffer_w[len_w] = '\0'; buffer_c = wchar_to_utf8 (buffer_w); - free (buffer_w); + _gpgme_free (buffer_w); if (! buffer_c) return 0; @@ -411,7 +411,7 @@ GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer) return len_c; strcpy (lpBuffer, buffer_c); - free (buffer_c); + _gpgme_free (buffer_c); return len_c - 1; } } @@ -441,7 +441,7 @@ SHGetSpecialFolderPathA (HWND hwndOwner, LPSTR lpszPath, int nFolder, return 0; strncpy (lpszPath, path_c, MAX_PATH); - free (path_c); + _gpgme_free (path_c); lpszPath[MAX_PATH - 1] = '\0'; return result; } @@ -465,7 +465,7 @@ _gpgme_wince_access (const char *fname, int mode) return -1; attr = GetFileAttributes (wfname); - free (wfname); + _gpgme_free (wfname); if (attr == (DWORD)(-1)) { gpg_err_set_errno (ENOENT); diff --git a/src/w32-ce.h b/src/w32-ce.h index c52ea992..cc4f619c 100644 --- a/src/w32-ce.h +++ b/src/w32-ce.h @@ -23,10 +23,11 @@ #include #include +#include "mem.h" #ifdef _MSC_VER typedef int pid_t; -#define strdup _strdup +#define strdup _gpgme_strdup #define strcasecmp _stricmp #endif diff --git a/src/w32-glib-io.c b/src/w32-glib-io.c index a5af4e68..ddeb64b0 100644 --- a/src/w32-glib-io.c +++ b/src/w32-glib-io.c @@ -565,7 +565,7 @@ build_commandline (char **argv) /* And a trailing zero. */ n++; - buf = p = malloc (n); + buf = p = _gpgme_malloc (n); if (!buf) return NULL; for (i = 0; argv[i]; i++) @@ -634,7 +634,7 @@ _gpgme_io_spawn (const char *path, char * const argv[], unsigned int flags, } TRACE_LOG1 ("tmp_name = %s", tmp_name); - args = calloc (2 + i + 1, sizeof (*args)); + args = _gpgme_calloc (2 + i + 1, sizeof (*args)); args[0] = (char *) _gpgme_get_w32spawn_path (); args[1] = tmp_name; args[2] = path; @@ -645,7 +645,7 @@ _gpgme_io_spawn (const char *path, char * const argv[], unsigned int flags, sec_attr.bInheritHandle = FALSE; arg_string = build_commandline (args); - free (args); + _gpgme_free (args); if (!arg_string) { close (tmp_fd); @@ -676,7 +676,7 @@ _gpgme_io_spawn (const char *path, char * const argv[], unsigned int flags, &pi)) /* returns process information */ { TRACE_LOG1 ("CreateProcess failed: ec=%d", (int) GetLastError ()); - free (arg_string); + _gpgme_free (arg_string); close (tmp_fd); DeleteFile (tmp_name); @@ -685,7 +685,7 @@ _gpgme_io_spawn (const char *path, char * const argv[], unsigned int flags, return TRACE_SYSRES (-1); } - free (arg_string); + _gpgme_free (arg_string); if (flags & IOSPAWN_FLAG_ALLOW_SET_FG) _gpgme_allow_set_foreground_window ((pid_t)pi.dwProcessId); @@ -826,13 +826,13 @@ _gpgme_io_select (struct io_select_fd_s *fds, size_t nfds, int nonblock) if (nonblock) timeout = 0; - pollfds = calloc (nfds, sizeof *pollfds); + pollfds = _gpgme_calloc (nfds, sizeof *pollfds); if (!pollfds) return -1; - pollfds_map = calloc (nfds, sizeof *pollfds_map); + pollfds_map = _gpgme_calloc (nfds, sizeof *pollfds_map); if (!pollfds_map) { - free (pollfds); + _gpgme_free (pollfds); return -1; } npollfds = 0; @@ -928,8 +928,8 @@ _gpgme_io_select (struct io_select_fd_s *fds, size_t nfds, int nonblock) } leave: - free (pollfds); - free (pollfds_map); + _gpgme_free (pollfds); + _gpgme_free (pollfds_map); return TRACE_SYSRES (count); } diff --git a/src/w32-io.c b/src/w32-io.c index 42961e31..6cea2c0d 100644 --- a/src/w32-io.c +++ b/src/w32-io.c @@ -396,7 +396,7 @@ reader (void *arg) CloseHandle (ctx->have_space_ev); CloseHandle (ctx->thread_hd); DESTROY_LOCK (ctx->mutex); - free (ctx); + _gpgme_free (ctx); return TRACE_SUC (); } @@ -415,7 +415,7 @@ create_reader (int fd) sec_attr.nLength = sizeof sec_attr; sec_attr.bInheritHandle = FALSE; - ctx = calloc (1, sizeof *ctx); + ctx = _gpgme_calloc (1, sizeof *ctx); if (!ctx) { TRACE_SYSERR (errno); @@ -425,7 +425,7 @@ create_reader (int fd) if (fd < 0 || fd >= MAX_SLAFD || !fd_table[fd].used) { TRACE_SYSERR (EIO); - free (ctx); + _gpgme_free (ctx); return NULL; } TRACE_LOG4 ("fd=%d -> handle=%p socket=%d dupfrom=%d", @@ -449,7 +449,7 @@ create_reader (int fd) CloseHandle (ctx->have_space_ev); if (ctx->close_ev) CloseHandle (ctx->close_ev); - free (ctx); + _gpgme_free (ctx); /* FIXME: Translate the error code. */ TRACE_SYSERR (EIO); return NULL; @@ -475,7 +475,7 @@ create_reader (int fd) CloseHandle (ctx->have_space_ev); if (ctx->close_ev) CloseHandle (ctx->close_ev); - free (ctx); + _gpgme_free (ctx); TRACE_SYSERR (EIO); return NULL; } @@ -766,7 +766,7 @@ writer (void *arg) CloseHandle (ctx->is_empty); CloseHandle (ctx->thread_hd); DESTROY_LOCK (ctx->mutex); - free (ctx); + _gpgme_free (ctx); return TRACE_SUC (); } @@ -785,7 +785,7 @@ create_writer (int fd) sec_attr.nLength = sizeof sec_attr; sec_attr.bInheritHandle = FALSE; - ctx = calloc (1, sizeof *ctx); + ctx = _gpgme_calloc (1, sizeof *ctx); if (!ctx) { TRACE_SYSERR (errno); @@ -795,7 +795,7 @@ create_writer (int fd) if (fd < 0 || fd >= MAX_SLAFD || !fd_table[fd].used) { TRACE_SYSERR (EIO); - free (ctx); + _gpgme_free (ctx); return NULL; } TRACE_LOG4 ("fd=%d -> handle=%p socket=%d dupfrom=%d", @@ -819,7 +819,7 @@ create_writer (int fd) CloseHandle (ctx->is_empty); if (ctx->close_ev) CloseHandle (ctx->close_ev); - free (ctx); + _gpgme_free (ctx); /* FIXME: Translate the error code. */ TRACE_SYSERR (EIO); return NULL; @@ -845,7 +845,7 @@ create_writer (int fd) CloseHandle (ctx->is_empty); if (ctx->close_ev) CloseHandle (ctx->close_ev); - free (ctx); + _gpgme_free (ctx); TRACE_SYSERR (EIO); return NULL; } @@ -1323,7 +1323,7 @@ build_commandline (char **argv, int fd0, int fd0_isnull, n++; /* Need to double inner quotes. */ } n++; - buf = p = malloc (n); + buf = p = _gpgme_malloc (n); if (! buf) return NULL; @@ -1388,7 +1388,7 @@ build_commandline (char **argv) /* And a trailing zero. */ n++; - buf = p = malloc (n); + buf = p = _gpgme_malloc (n); if (!buf) return NULL; for (i = 0; argv[i]; i++) @@ -1502,7 +1502,7 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, )) { TRACE_LOG1 ("CreateProcess failed: ec=%d", (int) GetLastError ()); - free (cmdline); + _gpgme_free (cmdline); gpg_err_set_errno (EIO); return TRACE_SYSRES (-1); } @@ -1572,7 +1572,7 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, } TRACE_LOG1 ("tmp_name = %s", tmp_name); - args = calloc (2 + i + 1, sizeof (*args)); + args = _gpgme_calloc (2 + i + 1, sizeof (*args)); args[0] = (char *) _gpgme_get_w32spawn_path (); args[1] = tmp_name; args[2] = (char *)path; @@ -1583,7 +1583,7 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, sec_attr.bInheritHandle = FALSE; arg_string = build_commandline (args); - free (args); + _gpgme_free (args); if (!arg_string) { close (tmp_fd); @@ -1615,7 +1615,7 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, &pi)) /* returns process information */ { TRACE_LOG1 ("CreateProcess failed: ec=%d", (int) GetLastError ()); - free (arg_string); + _gpgme_free (arg_string); close (tmp_fd); DeleteFileA (tmp_name); @@ -1624,7 +1624,7 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, return TRACE_SYSRES (-1); } - free (arg_string); + _gpgme_free (arg_string); if (flags & IOSPAWN_FLAG_ALLOW_SET_FG) _gpgme_allow_set_foreground_window ((pid_t)pi.dwProcessId); diff --git a/src/w32-sema.c b/src/w32-sema.c index 648a6bbd..3bf26b25 100644 --- a/src/w32-sema.c +++ b/src/w32-sema.c @@ -72,7 +72,7 @@ critsect_init (struct critsect_s *s) return; } /* now init it */ - mp = malloc ( sizeof *mp ); + mp = _gpgme_malloc ( sizeof *mp ); if (!mp) { LeaveCriticalSection (&init_lock); sema_fatal ("out of core while creating critical section lock"); @@ -111,7 +111,7 @@ _gpgme_sema_cs_destroy ( struct critsect_s *s ) { if (s && s->priv) { DeleteCriticalSection ((CRITICAL_SECTION*)s->priv); - free (s->priv); + _gpgme_free (s->priv); s->priv = NULL; } } diff --git a/src/w32-util.c b/src/w32-util.c index daf3bd2d..6f510b30 100644 --- a/src/w32-util.c +++ b/src/w32-util.c @@ -141,14 +141,14 @@ wchar_to_utf8 (const wchar_t *string) return NULL; } - result = malloc (n+1); + result = _gpgme_malloc (n+1); if (!result) return NULL; n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL); if (n < 0) { - free (result); + _gpgme_free (result); gpg_err_set_errno (EINVAL); result = NULL; } @@ -276,12 +276,12 @@ read_w32_registry_string (const char *root, const char *dir, const char *name) goto leave; } n1 = nbytes + 1; - result = malloc (n1); + result = _gpgme_malloc (n1); if (!result) goto leave; if (RegQueryValueExA (key_handle, name, 0, &type, (LPBYTE) result, &n1)) { - free (result); + _gpgme_free (result); result = NULL; goto leave; } @@ -294,42 +294,42 @@ read_w32_registry_string (const char *root, const char *dir, const char *name) char *tmp; n1 += 1000; - tmp = malloc (n1 + 1); + tmp = _gpgme_malloc (n1 + 1); if (!tmp) goto leave; nbytes = ExpandEnvironmentStrings (result, tmp, n1); if (nbytes && nbytes > n1) { - free (tmp); + _gpgme_free (tmp); n1 = nbytes; - tmp = malloc (n1 + 1); + tmp = _gpgme_malloc (n1 + 1); if (!tmp) goto leave; nbytes = ExpandEnvironmentStrings (result, tmp, n1); if (nbytes && nbytes > n1) { - free (tmp); /* Oops - truncated, better don't expand at all. */ + _gpgme_free (tmp); /* Oops - truncated, better don't expand at all. */ goto leave; } tmp[nbytes] = 0; - free (result); + _gpgme_free (result); result = tmp; } else if (nbytes) /* Okay, reduce the length. */ { tmp[nbytes] = 0; - free (result); - result = malloc (strlen (tmp)+1); + _gpgme_free (result); + result = _gpgme_malloc (strlen (tmp)+1); if (!result) result = tmp; else { strcpy (result, tmp); - free (tmp); + _gpgme_free (tmp); } } else /* Error - don't expand. */ { - free (tmp); + _gpgme_free (tmp); } } #endif @@ -352,7 +352,7 @@ _gpgme_get_inst_dir (void) { wchar_t *moddir; - moddir = malloc ((MAX_PATH+5) * sizeof *moddir); + moddir = _gpgme_malloc ((MAX_PATH+5) * sizeof *moddir); if (moddir) { if (!GetModuleFileNameW (my_hmodule, moddir, MAX_PATH)) @@ -369,7 +369,7 @@ _gpgme_get_inst_dir (void) *p = 0; } } - free (moddir); + _gpgme_free (moddir); } } UNLOCK (get_path_lock); @@ -382,14 +382,14 @@ find_program_in_dir (const char *dir, const char *name) { char *result; - result = malloc (strlen (dir) + 1 + strlen (name) + 1); + result = _gpgme_malloc (strlen (dir) + 1 + strlen (name) + 1); if (!result) return NULL; strcpy (stpcpy (stpcpy (result, dir), "\\"), name); if (access (result, F_OK)) { - free (result); + _gpgme_free (result); return NULL; } @@ -424,7 +424,7 @@ find_program_in_inst_dir (const char *inst_dir, const char *name) if (dir) { result = find_program_in_dir (dir, name); - free (dir); + _gpgme_free (dir); return result; } return NULL; @@ -440,13 +440,13 @@ find_program_at_standard_place (const char *name) /* See http://wiki.tcl.tk/17492 for details on compatibility. */ if (SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILES, 0)) { - result = malloc (strlen (path) + 1 + strlen (name) + 1); + result = _gpgme_malloc (strlen (path) + 1 + strlen (name) + 1); if (result) { strcpy (stpcpy (stpcpy (result, path), "\\"), name); if (access (result, F_OK)) { - free (result); + _gpgme_free (result); result = NULL; } } @@ -462,7 +462,7 @@ _gpgme_set_default_gpg_name (const char *name) { if (!default_gpg_name) { - default_gpg_name = malloc (strlen (name) + 5); + default_gpg_name = _gpgme_malloc (strlen (name) + 5); if (default_gpg_name) { strcpy (stpcpy (default_gpg_name, name), ".exe"); @@ -479,7 +479,7 @@ _gpgme_set_default_gpgconf_name (const char *name) { if (!default_gpgconf_name) { - default_gpgconf_name = malloc (strlen (name) + 5); + default_gpgconf_name = _gpgme_malloc (strlen (name) + 5); if (default_gpgconf_name) { strcpy (stpcpy (default_gpgconf_name, name), ".exe"); @@ -571,7 +571,7 @@ _gpgme_get_conf_int (const char *key, int *value) if (!tmp) return 0; *value = atoi (tmp); - free (tmp); + _gpgme_free (tmp); return 1; } @@ -704,14 +704,14 @@ _gpgme_mkstemp (int *fd, char **name) } } - tmpname = malloc (strlen (tmp) + 13 + 1); + tmpname = _gpgme_malloc (strlen (tmp) + 13 + 1); if (!tmpname) return -1; strcpy (stpcpy (tmpname, tmp), "\\gpgme-XXXXXX"); *fd = mkstemp (tmpname); if (fd < 0) { - free (tmpname); + _gpgme_free (tmpname); return -1; } @@ -734,7 +734,7 @@ _gpgme_w32ce_get_debug_envvar (void) tmp = read_w32_registry_string (NULL, "\\Software\\GNU\\gpgme", "debug"); if (tmp && !*tmp) { - free (tmp); + _gpgme_free (tmp); tmp = NULL; } return tmp; diff --git a/src/wait-global.c b/src/wait-global.c index f03775e2..d9eeaa04 100644 --- a/src/wait-global.c +++ b/src/wait-global.c @@ -35,6 +35,7 @@ #include "priv-io.h" #include "ops.h" #include "debug.h" +#include "mem.h" /* The global event loop is used for all asynchronous operations (except key listing) for which no user I/O callbacks are specified. @@ -95,7 +96,7 @@ static struct ctx_list_item *ctx_done_list; static gpgme_error_t ctx_active (gpgme_ctx_t ctx) { - struct ctx_list_item *li = malloc (sizeof (struct ctx_list_item)); + struct ctx_list_item *li = _gpgme_malloc (sizeof (struct ctx_list_item)); if (!li) return gpg_error_from_syserror (); li->ctx = ctx; @@ -177,7 +178,7 @@ ctx_wait (gpgme_ctx_t ctx, gpgme_error_t *status, gpgme_error_t *op_err) li->prev->next = li->next; else ctx_done_list = li->next; - free (li); + _gpgme_free (li); } else ctx = NULL; @@ -266,7 +267,7 @@ gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, LOCK (ctx_list_lock); for (li = ctx_active_list; li; li = li->next) i += li->ctx->fdt.size; - fdt.fds = malloc (i * sizeof (struct io_select_fd_s)); + fdt.fds = _gpgme_malloc (i * sizeof (struct io_select_fd_s)); if (!fdt.fds) { int saved_err = gpg_error_from_syserror (); @@ -291,7 +292,7 @@ gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, if (nr < 0) { int saved_err = gpg_error_from_syserror (); - free (fdt.fds); + _gpgme_free (fdt.fds); if (status) *status = saved_err; if (op_err) @@ -336,7 +337,7 @@ gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, } } } - free (fdt.fds); + _gpgme_free (fdt.fds); /* Now some contexts might have finished successfully. */ LOCK (ctx_list_lock); diff --git a/src/wait.c b/src/wait.c index 20b23032..808bebff 100644 --- a/src/wait.c +++ b/src/wait.c @@ -38,6 +38,7 @@ #include "priv-io.h" #include "engine.h" #include "debug.h" +#include "mem.h" void @@ -51,7 +52,7 @@ void _gpgme_fd_table_deinit (fd_table_t fdt) { if (fdt->fds) - free (fdt->fds); + _gpgme_free (fdt->fds); } @@ -70,7 +71,7 @@ fd_table_put (fd_table_t fdt, int fd, int dir, void *opaque, int *idx) if (i == fdt->size) { #define FDT_ALLOCSIZE 10 - new_fds = realloc (fdt->fds, (fdt->size + FDT_ALLOCSIZE) + new_fds = _gpgme_realloc (fdt->fds, (fdt->size + FDT_ALLOCSIZE) * sizeof (*new_fds)); if (!new_fds) return gpg_error_from_syserror (); @@ -111,16 +112,16 @@ _gpgme_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, fdt = &ctx->fdt; assert (fdt); - tag = malloc (sizeof *tag); + tag = _gpgme_malloc (sizeof *tag); if (!tag) return gpg_error_from_syserror (); tag->ctx = ctx; /* Allocate a structure to hold information about the handler. */ - item = calloc (1, sizeof *item); + item = _gpgme_calloc (1, sizeof *item); if (!item) { - free (tag); + _gpgme_free (tag); return gpg_error_from_syserror (); } item->ctx = ctx; @@ -131,8 +132,8 @@ _gpgme_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, err = fd_table_put (fdt, fd, dir, item, &tag->idx); if (err) { - free (tag); - free (item); + _gpgme_free (tag); + _gpgme_free (item); return err; } @@ -163,8 +164,8 @@ _gpgme_remove_io_cb (void *data) "setting fd 0x%x (item=%p) done", fdt->fds[idx].fd, fdt->fds[idx].opaque); - free (fdt->fds[idx].opaque); - free (tag); + _gpgme_free (fdt->fds[idx].opaque); + _gpgme_free (tag); /* Free the table entry. */ fdt->fds[idx].fd = -1;