From 790626ec82134ca91132fc60fe8eabe5e38a0260 Mon Sep 17 00:00:00 2001
From: Diederik de Groot <dkgroot@talon.nl>
Date: Sat, 5 Jan 2019 18:14:26 +0100
Subject: [PATCH] RAII: Change order or variables in clang version

This prevents use-after-scope issues when unwinding the stack,
which happens in reverse order. The varname variable needs to
remain alive for the destruction to be able to access it.
Issue was found using clang + address-sanitizer.

ASTERISK-28232 #close

Change-Id: I00811c34ae910836a5fb6d22304528aef92624db
---
 include/asterisk/utils.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 288bff1812..bcd73aace5 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -1068,10 +1068,10 @@ char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
 typedef void (^_raii_cleanup_block_t)(void);
 static inline void _raii_cleanup_block(_raii_cleanup_block_t *b) { (*b)(); }
 
-#define RAII_VAR(vartype, varname, initval, dtor)                                                                \
-    _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = NULL; \
-    __block vartype varname = initval;                                                                           \
-    _raii_cleanup_ ## varname = ^{ {(void)dtor(varname);} }
+#define RAII_VAR(vartype, varname, initval, dtor)                                                              \
+    __block vartype varname = initval;                                                                         \
+    _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) =     \
+        ^{ {(void)dtor(varname);} };
 
 #elif defined(__GNUC__)
 
-- 
GitLab