From 0000ac6b0f6e8710d84a562a86141fef637d27ff Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Wed, 18 Jan 2017 17:52:36 +0530 Subject: [PATCH] Fix memory leak Change-Id: I2fb5fb4d12fcac486a4b6994ee9fe14a37de1513 Signed-off-by: Saurav Babu --- lib/route/pktloc.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/route/pktloc.c b/lib/route/pktloc.c index f0d0155..99e51d3 100644 --- a/lib/route/pktloc.c +++ b/lib/route/pktloc.c @@ -90,12 +90,16 @@ static int read_pktlocs(void) /* if stat fails, just try to read the file */ if (stat(path, &st) == 0) { /* Don't re-read file if file is unchanged */ - if (last_read == st.st_mtime) - return 0; + if (last_read == st.st_mtime) { + err = 0; + goto errout; + } } - if (!(fd = fopen(path, "r"))) - return -NLE_PKTLOC_FILE; + if (!(fd = fopen(path, "r"))) { + err = -NLE_PKTLOC_FILE; + goto errout; + } for (i = 0; i < PKTLOC_NAME_HT_SIZ; i++) { struct rtnl_pktloc *loc, *n; @@ -106,22 +110,30 @@ static int read_pktlocs(void) nl_init_list_head(&pktloc_name_ht[i]); } - if ((err = pktloc_lex_init(&scanner)) < 0) - return -NLE_FAILURE; + if ((err = pktloc_lex_init(&scanner)) < 0) { + err = -NLE_FAILURE; + goto errout_close; + } buf = pktloc__create_buffer(fd, YY_BUF_SIZE, scanner); pktloc__switch_to_buffer(buf, scanner); - if ((err = pktloc_parse(scanner)) < 0) - return -NLE_FAILURE; + if ((err = pktloc_parse(scanner)) < 0) { + err = -NLE_FAILURE; + goto errout_scanner; + } + last_read = st.st_mtime; + +errout_scanner: if (scanner) pktloc_lex_destroy(scanner); - +errout_close: + fclose(fd); +errout: free(path); - last_read = st.st_mtime; - return 0; + return err; } /** @@ -163,6 +175,6 @@ static int __init pktloc_init(void) for (i = 0; i < PKTLOC_NAME_HT_SIZ; i++) nl_init_list_head(&pktloc_name_ht[i]); - + return 0; } -- 2.7.4