Skip to content

Commit c6c2c26

Browse files
Merge pull request zabbix-tools#41 from Viste/memleak
unref msg and free all mem we can closes zabbix-tools#39
2 parents 92ecb08 + 7e69664 commit c6c2c26

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

src/modules/systemd/cgroups.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int cgroup_init()
5858
zabbix_log(LOG_LEVEL_DEBUG, LOG_PREFIX "cpu_cgroup is cpuacct");
5959
}
6060

61+
free(ddir);
6162
pclose(fp);
6263
return SYSINFO_RET_OK;
6364
}

src/modules/systemd/dbus.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,20 @@ DBusMessage *dbus_exchange_message(DBusMessage *msg) {
7979

8080
if (NULL == msg) {
8181
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "message is null");
82+
dbus_message_unref(msg);
8283
return NULL;
8384
}
8485

8586
// send message
8687
if (!dbus_connection_send_with_reply (conn, msg, &pending, timeout)) {
8788
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "oom sending message");
89+
dbus_message_unref(msg);
8890
return NULL;
8991
}
9092

9193
if (NULL == pending) {
9294
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "pending message is null");
95+
dbus_message_unref(msg);
9396
return NULL;
9497
}
9598

@@ -101,13 +104,16 @@ DBusMessage *dbus_exchange_message(DBusMessage *msg) {
101104
msg = dbus_pending_call_steal_reply(pending);
102105
if (NULL == msg) {
103106
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "returned message is null");
107+
dbus_message_unref(msg);
104108
return NULL;
105109
}
106110
dbus_pending_call_unref(pending);
107111

108112
// check for errors
109-
if (dbus_check_error(msg))
110-
return NULL;
113+
if (dbus_check_error(msg)){
114+
dbus_message_unref(msg);
115+
return NULL;
116+
}
111117

112118
return msg;
113119
}
@@ -150,14 +156,20 @@ DBusMessageIter* dbus_get_property(
150156
}
151157

152158
dbus_message_iter_init_append(msg, &args);
153-
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface))
159+
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface)){
160+
dbus_message_unref(msg);
154161
return NULL;
162+
}
155163

156-
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property))
164+
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property)){
165+
dbus_message_unref(msg);
157166
return NULL;
167+
}
158168

159-
if (NULL == (msg = dbus_exchange_message(msg)))
169+
if (NULL == (msg = dbus_exchange_message(msg))){
170+
dbus_message_unref(msg);
160171
return NULL;
172+
}
161173

162174
// check type
163175
if (!dbus_message_iter_init(msg, &args)) {
@@ -207,22 +219,28 @@ int dbus_get_property_string(
207219
interface,
208220
property);
209221

210-
if (NULL == iter)
222+
if (NULL == iter){
223+
zbx_free(iter);
211224
return FAIL;
225+
}
212226

213227
type = dbus_message_iter_get_arg_type(iter);
214228
switch (type) {
215229
case DBUS_TYPE_STRING:
216230
dbus_message_iter_get_basic(iter, &value);
217231
zbx_strlcpy(s, value, n);
232+
zbx_free(iter);
218233
return SUCCEED;
219234

220235
case DBUS_TYPE_BOOLEAN:
221236
dbus_message_iter_get_basic(iter, &value);
222237
zbx_strlcpy(s, yes_no(value), n);
238+
zbx_free(iter);
223239
return SUCCEED;
224240
}
225241

242+
zbx_free(iter);
243+
226244
return FAIL;
227245
}
228246

@@ -282,6 +300,7 @@ int dbus_marshall_property(
282300

283301
if (NULL == iter) {
284302
SET_MSG_RESULT(result, strdup("failed to get property"));
303+
zbx_free(iter);
285304
return SYSINFO_RET_FAIL;
286305
}
287306

@@ -332,6 +351,7 @@ int dbus_marshall_property(
332351
return SYSINFO_RET_OK;
333352
}
334353

354+
zbx_free(iter);
335355
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "unsupported value type: %c", type));
336356
return SYSINFO_RET_FAIL;
337357
}

src/modules/systemd/libzbxsystemd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ static int SYSTEMD_UNIT_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
178178

179179
if (NULL == (msg = dbus_exchange_message(msg))) {
180180
SET_MSG_RESULT(result, strdup("failed to list units"));
181+
dbus_message_unref(msg);
181182
return res;
182183
}
183184

184185
// check result message
185186
if (!dbus_message_iter_init(msg, &args)) {
186187
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "no value returned");
188+
dbus_message_unref(msg);
187189
return res;
188190
}
189191

190192
if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&args)) {
191193
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "returned value is not an array");
194+
dbus_message_unref(msg);
192195
return res;
193196
}
194197

@@ -420,17 +423,20 @@ static int SYSTEMD_SERVICE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *resul
420423

421424
if (NULL == (msg = dbus_exchange_message(msg))) {
422425
SET_MSG_RESULT(result, strdup("failed to list units"));
426+
dbus_message_unref(msg);
423427
return res;
424428
}
425429

426430
// check result message
427431
if (!dbus_message_iter_init(msg, &args)) {
428432
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "no value returned");
433+
dbus_message_unref(msg);
429434
return res;
430435
}
431436

432437
if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&args)) {
433438
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "returned value is not an array");
439+
dbus_message_unref(msg);
434440
return res;
435441
}
436442

@@ -446,6 +452,7 @@ static int SYSTEMD_SERVICE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *resul
446452
dbus_message_iter_next_n(&unit, 6);
447453
if (DBUS_TYPE_INVALID == (type = dbus_message_iter_get_arg_type(&unit))) {
448454
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "unexpected value type");
455+
dbus_message_unref(msg);
449456
goto next_unit;
450457
}
451458

src/modules/systemd/systemd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,26 @@ int systemd_get_unit(char *s, size_t n, const char* unit)
4343
"GetUnit");
4444

4545
dbus_message_iter_init_append(msg, &args);
46-
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &c))
46+
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &c)){
47+
dbus_message_unref(msg);
4748
return FAIL;
49+
}
4850

49-
if (NULL == (msg = dbus_exchange_message(msg)))
51+
if (NULL == (msg = dbus_exchange_message(msg))){
52+
dbus_message_unref(msg);
5053
return FAIL;
54+
}
5155

5256
// read value
5357
if (!dbus_message_iter_init(msg, &args)) {
5458
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "message has no arguments");
59+
dbus_message_unref(msg);
5560
return FAIL;
5661
}
5762

5863
if (DBUS_TYPE_OBJECT_PATH != (type = dbus_message_iter_get_arg_type(&args))) {
5964
zabbix_log(LOG_LEVEL_ERR, LOG_PREFIX "argument is not an object path: %c", type);
65+
dbus_message_unref(msg);
6066
return FAIL;
6167
}
6268

0 commit comments

Comments
 (0)