From 97f8d2167437e573531e0575741e279fd7e32ffd Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 20 Nov 2017 11:33:20 +0100 Subject: [PATCH] shared: add nm_auto_free_secret macro --- shared/nm-utils/nm-macros-internal.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index f4dca4b37a..ce13d3a6fa 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -69,6 +69,30 @@ static inline int nm_close (int fd); #define nm_auto_free nm_auto(_nm_auto_free_impl) GS_DEFINE_CLEANUP_FUNCTION(void*, _nm_auto_free_impl, free) +static inline void +nm_free_secret (char *secret) +{ + if (secret) { + memset (secret, 0, strlen (secret)); + g_free (secret); + } +} + +static inline void +_nm_auto_free_secret_impl (char **v) +{ + nm_free_secret (*v); +} + +/** + * nm_auto_free_secret: + * + * Call g_free() on a variable location when it goes out of scope. + * Also, previously, calls memset(loc, 0, strlen(loc)) to clear out + * the secret. + */ +#define nm_auto_free_secret nm_auto(_nm_auto_free_secret_impl) + static inline void _nm_auto_unset_gvalue_impl (GValue *v) {