@@ -46,6 +46,7 @@ static dd_result ATTR_WARN_UNUSED _imsg_recv(
46
46
47
47
static inline ATTR_WARN_UNUSED mpack_error_t _imsg_destroy (
48
48
dd_imsg * nonnull imsg );
49
+ static void _imsg_cleanup (dd_imsg * nullable * imsg );
49
50
50
51
static dd_result _dd_command_exec (dd_conn * nonnull conn ,
51
52
const dd_command_spec * nonnull spec , void * unspecnull ctx )
@@ -98,20 +99,21 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn,
98
99
return res ;
99
100
}
100
101
102
+ // automatic cleanup of imsg on error branches
103
+ // set to NULL before calling _imsg_destroy
104
+ __attribute__((cleanup (_imsg_cleanup ))) dd_imsg * nullable destroy_imsg =
105
+ & imsg ;
106
+
101
107
mpack_node_t first_response = mpack_node_array_at (imsg .root , 0 );
102
108
mpack_error_t err = mpack_node_error (first_response );
103
109
if (err != mpack_ok ) {
104
110
mlog (dd_log_error , "Array of responses could not be retrieved - %s" ,
105
111
mpack_error_to_string (err ));
106
- // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
107
- err = _imsg_destroy (& imsg );
108
112
return dd_error ;
109
113
}
110
114
if (mpack_node_type (first_response ) != mpack_type_array ) {
111
115
mlog (dd_log_error , "Invalid response. Expected array but got %s" ,
112
116
mpack_type_to_string (mpack_node_type (first_response )));
113
- // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
114
- err = _imsg_destroy (& imsg );
115
117
return dd_error ;
116
118
}
117
119
mpack_node_t first_message = mpack_node_array_at (first_response , 1 );
@@ -127,16 +129,12 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn,
127
129
if (err != mpack_ok ) {
128
130
mlog (dd_log_error , "Response type could not be retrieved - %s" ,
129
131
mpack_error_to_string (err ));
130
- // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
131
- err = _imsg_destroy (& imsg );
132
132
return dd_error ;
133
133
}
134
134
if (mpack_node_type (type ) != mpack_type_str ) {
135
135
mlog (dd_log_error ,
136
136
"Unexpected type field. Expected string but got %s" ,
137
137
mpack_type_to_string (mpack_node_type (type )));
138
- // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
139
- err = _imsg_destroy (& imsg );
140
138
return dd_error ;
141
139
}
142
140
if (dd_mpack_node_lstr_eq (type , "config_features" )) {
@@ -147,8 +145,6 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn,
147
145
mlog (dd_log_debug ,
148
146
"Received message for command %.*s unexpected: %.*s\n" , NAME_L ,
149
147
(int )mpack_node_strlen (type ), mpack_node_str (type ));
150
- // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
151
- err = _imsg_destroy (& imsg );
152
148
return dd_error ;
153
149
}
154
150
@@ -157,6 +153,7 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn,
157
153
err = imsg .root .tree -> error ;
158
154
_dump_in_msg (err == mpack_ok ? dd_log_trace : dd_log_debug , imsg ._data ,
159
155
imsg ._size );
156
+ destroy_imsg = NULL ;
160
157
err = _imsg_destroy (& imsg );
161
158
if (err != mpack_ok ) {
162
159
mlog (dd_log_warning ,
@@ -281,6 +278,14 @@ static inline ATTR_WARN_UNUSED mpack_error_t _imsg_destroy(
281
278
return mpack_tree_destroy (& imsg -> _tree );
282
279
}
283
280
281
+ static void _imsg_cleanup (dd_imsg * nullable * imsg )
282
+ {
283
+ dd_imsg * * imsg_c = (dd_imsg * nullable * nonnull ) imsg ;
284
+ if (* imsg_c ) {
285
+ UNUSED (_imsg_destroy (* imsg_c ));
286
+ }
287
+ }
288
+
284
289
/* Baked response */
285
290
286
291
static void _add_appsec_span_data_frag (mpack_node_t node );
0 commit comments