Skip to content

Commit 208b89d

Browse files
committed
Fix memory leak
Debug statement in inventory_conn caused memory leak because I did not read documentation detailed enough, and forgot to free memory. Also, fix some compile warnings and valgrind warnings about unitialized variables.
1 parent 6eae174 commit 208b89d

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 2.6)
33
project(meta_exporter)
44

5-
set(CMAKE_C_FLAGS "-O1 -Wall -std=gnu99 -g -s")
5+
set(CMAKE_C_FLAGS "-O1 -Wall -std=gnu99 -g")
66

77
set(LIBS pthread mnl)
88
set(SOURCE

metadata_input_zeromq.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,26 @@ static void md_input_zeromq_handle_event(void *ptr, int32_t fd, uint32_t events)
238238
int zmq_events = 0;
239239
size_t events_len = sizeof(zmq_events);
240240
json_object *json_event = NULL, *zmqh_obj = NULL;
241-
const char *json_msg;
241+
const char *json_msg = NULL, *json_str;
242242
uint8_t event_type = 0;
243243

244244
zmq_getsockopt(miz->zmq_socket, ZMQ_EVENTS, &zmq_events, &events_len);
245245

246246
while (zmq_events & ZMQ_POLLIN)
247247
{
248248
char buf[2048] = {0};
249-
zmq_recv(miz->zmq_socket, buf, 2048, 0);
249+
int nbytes = zmq_recv(miz->zmq_socket, buf, 2047, 0);
250+
251+
if (nbytes == -1) {
252+
zmq_getsockopt(miz->zmq_socket, ZMQ_EVENTS, &zmq_events, &events_len);
253+
continue;
254+
}
255+
256+
//Ensure zero teminated string, in case zmq does something funky with buffer
257+
buf[nbytes] = '\0';
250258

251259
json_msg = strchr(buf, '{');
252-
if (json_msg == NULL)
253-
{
260+
if (json_msg == NULL) {
254261
zmq_getsockopt(miz->zmq_socket, ZMQ_EVENTS, &zmq_events, &events_len);
255262
continue;
256263
}
@@ -289,7 +296,8 @@ static void md_input_zeromq_handle_event(void *ptr, int32_t fd, uint32_t events)
289296
return;
290297
}
291298

292-
META_PRINT(miz->parent->logfile, "Got JSON %s\n", json_object_to_json_string(zmqh_obj));
299+
json_str = json_object_to_json_string(zmqh_obj);
300+
META_PRINT(miz->parent->logfile, "Got JSON %s\n", json_str);
293301

294302
switch (event_type) {
295303
case META_TYPE_INTERFACE:

metadata_writer_file.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ static int md_file_add_json_string(struct json_object *obj,
5151
{
5252
struct json_object *value_obj = NULL;
5353

54-
if (value != NULL)
55-
{
54+
if (value != NULL) {
5655
value_obj = json_object_new_string(value);
5756
if (!value_obj)
5857
return RETVAL_FAILURE;
58+
59+
json_object_object_add(obj, key, value_obj);
5960
}
6061

61-
json_object_object_add(obj, key, value_obj);
6262
return RETVAL_SUCCESS;
6363
}
6464

@@ -79,7 +79,7 @@ static int md_file_add_json_obj(struct json_object *obj,
7979
static int md_file_save(struct md_writer_file *mwf, int event_type, const char *content)
8080
{
8181
int output_fd;
82-
char dst_filename[128] = {0};
82+
char dst_filename[133] = {0};
8383
char *prefix;
8484
FILE *output;
8585

metadata_writer_inventory_conn.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ static int32_t md_inventory_execute_update_usage(struct md_writer_sqlite *mws,
284284
uint64_t date_start)
285285
{
286286
const char *no_iccid_str = "0";
287+
char *sql_stmt_str;
288+
287289
sqlite3_stmt *stmt = mws->update_usage;
288290

289291
sqlite3_clear_bindings(stmt);
@@ -319,7 +321,12 @@ static int32_t md_inventory_execute_update_usage(struct md_writer_sqlite *mws,
319321
}
320322
}
321323

322-
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Update query: %s\n", sqlite3_expanded_sql(stmt));
324+
sql_stmt_str = sqlite3_expanded_sql(stmt);
325+
326+
if (sql_stmt_str) {
327+
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Update query: %s\n", sql_stmt_str);
328+
sqlite3_free(sql_stmt_str);
329+
}
323330

324331
return sqlite3_step(stmt);
325332
}

system_helpers.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@
4545
#ifdef OPENWRT
4646
uint32_t system_helpers_get_nodeid()
4747
{
48-
char *opt;
49-
struct uci_ptr uciptr;
48+
char *opt = NULL;
49+
struct uci_ptr uciptr = {0};
5050
int retval;
5151
long node_id;
5252
struct uci_context *ctx = uci_alloc_context();
5353

54-
if(ctx == NULL)
54+
if (ctx == NULL)
5555
return 0;
5656

57-
opt = malloc(strlen(NODEIDPATH)+1);
57+
opt = calloc(strlen(NODEIDPATH)+1, 1);
5858
strcpy(opt, NODEIDPATH);
59-
memset(&uciptr, 0, sizeof(uciptr));
6059

6160
retval = uci_lookup_ptr(ctx, &uciptr, opt, true);
6261

0 commit comments

Comments
 (0)