diff mbox series

[PATCH-for-5.2?,2/2] tests/qtest: fix memleak in npcm7xx_watchdog_timer-test

Message ID 20201118115646.2461726-3-kuhn.chenqun@huawei.com (mailing list archive)
State New, archived
Headers show
Series two qtest bugfix | expand

Commit Message

Chen Qun Nov. 18, 2020, 11:56 a.m. UTC
Properly free resp for get_watchdog_action() to avoid memory leak.
ASAN shows memory leak stack:

Indirect leak of 12360 byte(s) in 3 object(s) allocated from:
    #0 0x7f41ab6cbd4e in __interceptor_calloc (/lib64/libasan.so.5+0x112d4e)
    #1 0x7f41ab4eaa50 in g_malloc0 (/lib64/libglib-2.0.so.0+0x55a50)
    #2 0x556487d5374b in qdict_new ../qobject/qdict.c:29
    #3 0x556487d65e1a in parse_object ../qobject/json-parser.c:318
    #4 0x556487d65cb6 in parse_pair ../qobject/json-parser.c:287
    #5 0x556487d65ebd in parse_object ../qobject/json-parser.c:343
    #6 0x556487d661d5 in json_parser_parse ../qobject/json-parser.c:580
    #7 0x556487d513df in json_message_process_token ../qobject/json-streamer.c:92
    #8 0x556487d63919 in json_lexer_feed_char ../qobject/json-lexer.c:313
    #9 0x556487d63d75 in json_lexer_feed ../qobject/json-lexer.c:350
    #10 0x556487d28b2a in qmp_fd_receive ../tests/qtest/libqtest.c:613
    #11 0x556487d2a16f in qtest_qmp_eventwait_ref ../tests/qtest/libqtest.c:827
    #12 0x556487d248e2 in get_watchdog_action ../tests/qtest/npcm7xx_watchdog_timer-test.c:94
    #13 0x556487d25765 in test_enabling_flags ../tests/qtest/npcm7xx_watchdog_timer-test.c:243

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Chen Qun <kuhn.chenqun@huawei.com>
---
 tests/qtest/npcm7xx_watchdog_timer-test.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Havard Skinnemoen Nov. 18, 2020, 5:14 p.m. UTC | #1
On Wed, Nov 18, 2020 at 3:57 AM Chen Qun <kuhn.chenqun@huawei.com> wrote:
>
> Properly free resp for get_watchdog_action() to avoid memory leak.
> ASAN shows memory leak stack:
>
> Indirect leak of 12360 byte(s) in 3 object(s) allocated from:
>     #0 0x7f41ab6cbd4e in __interceptor_calloc (/lib64/libasan.so.5+0x112d4e)
>     #1 0x7f41ab4eaa50 in g_malloc0 (/lib64/libglib-2.0.so.0+0x55a50)
>     #2 0x556487d5374b in qdict_new ../qobject/qdict.c:29
>     #3 0x556487d65e1a in parse_object ../qobject/json-parser.c:318
>     #4 0x556487d65cb6 in parse_pair ../qobject/json-parser.c:287
>     #5 0x556487d65ebd in parse_object ../qobject/json-parser.c:343
>     #6 0x556487d661d5 in json_parser_parse ../qobject/json-parser.c:580
>     #7 0x556487d513df in json_message_process_token ../qobject/json-streamer.c:92
>     #8 0x556487d63919 in json_lexer_feed_char ../qobject/json-lexer.c:313
>     #9 0x556487d63d75 in json_lexer_feed ../qobject/json-lexer.c:350
>     #10 0x556487d28b2a in qmp_fd_receive ../tests/qtest/libqtest.c:613
>     #11 0x556487d2a16f in qtest_qmp_eventwait_ref ../tests/qtest/libqtest.c:827
>     #12 0x556487d248e2 in get_watchdog_action ../tests/qtest/npcm7xx_watchdog_timer-test.c:94
>     #13 0x556487d25765 in test_enabling_flags ../tests/qtest/npcm7xx_watchdog_timer-test.c:243
>
> Reported-by: Euler Robot <euler.robot@huawei.com>
> Signed-off-by: Chen Qun <kuhn.chenqun@huawei.com>

Reviewed-by: Havard Skinnemoen <hskinnemoen@google.com>

> ---
>  tests/qtest/npcm7xx_watchdog_timer-test.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qtest/npcm7xx_watchdog_timer-test.c b/tests/qtest/npcm7xx_watchdog_timer-test.c
> index 54d5d6d8f2..3aae5a0438 100644
> --- a/tests/qtest/npcm7xx_watchdog_timer-test.c
> +++ b/tests/qtest/npcm7xx_watchdog_timer-test.c
> @@ -204,6 +204,7 @@ static void test_enabling_flags(gconstpointer watchdog)
>  {
>      const Watchdog *wd = watchdog;
>      QTestState *qts;
> +    QDict *rsp;
>
>      /* Neither WTIE or WTRE is set, no interrupt or reset should happen */
>      qts = qtest_init("-machine quanta-gsj");
> @@ -240,8 +241,9 @@ static void test_enabling_flags(gconstpointer watchdog)
>      g_assert_false(qtest_get_irq(qts, wd->irq));
>      qtest_clock_step(qts, watchdog_calculate_steps(RESET_CYCLES,
>                  watchdog_prescaler(qts, wd)));
> -    g_assert_false(strcmp(qdict_get_str(get_watchdog_action(qts), "action"),
> -                "reset"));
> +    rsp = get_watchdog_action(qts);
> +    g_assert_false(strcmp(qdict_get_str(rsp, "action"), "reset"));
> +    qobject_unref(rsp);
>      qtest_qmp_eventwait(qts, "RESET");
>      qtest_quit(qts);
>
> --
> 2.23.0
>
Hao Wu Nov. 18, 2020, 5:22 p.m. UTC | #2
On Wed, Nov 18, 2020 at 9:14 AM Havard Skinnemoen <hskinnemoen@google.com>
wrote:

> On Wed, Nov 18, 2020 at 3:57 AM Chen Qun <kuhn.chenqun@huawei.com> wrote:
> >
> > Properly free resp for get_watchdog_action() to avoid memory leak.
> > ASAN shows memory leak stack:
> >
> > Indirect leak of 12360 byte(s) in 3 object(s) allocated from:
> >     #0 0x7f41ab6cbd4e in __interceptor_calloc
> (/lib64/libasan.so.5+0x112d4e)
> >     #1 0x7f41ab4eaa50 in g_malloc0 (/lib64/libglib-2.0.so.0+0x55a50)
> >     #2 0x556487d5374b in qdict_new ../qobject/qdict.c:29
> >     #3 0x556487d65e1a in parse_object ../qobject/json-parser.c:318
> >     #4 0x556487d65cb6 in parse_pair ../qobject/json-parser.c:287
> >     #5 0x556487d65ebd in parse_object ../qobject/json-parser.c:343
> >     #6 0x556487d661d5 in json_parser_parse ../qobject/json-parser.c:580
> >     #7 0x556487d513df in json_message_process_token
> ../qobject/json-streamer.c:92
> >     #8 0x556487d63919 in json_lexer_feed_char ../qobject/json-lexer.c:313
> >     #9 0x556487d63d75 in json_lexer_feed ../qobject/json-lexer.c:350
> >     #10 0x556487d28b2a in qmp_fd_receive ../tests/qtest/libqtest.c:613
> >     #11 0x556487d2a16f in qtest_qmp_eventwait_ref
> ../tests/qtest/libqtest.c:827
> >     #12 0x556487d248e2 in get_watchdog_action
> ../tests/qtest/npcm7xx_watchdog_timer-test.c:94
> >     #13 0x556487d25765 in test_enabling_flags
> ../tests/qtest/npcm7xx_watchdog_timer-test.c:243
> >
> > Reported-by: Euler Robot <euler.robot@huawei.com>
> > Signed-off-by: Chen Qun <kuhn.chenqun@huawei.com>
>
> Reviewed-by: Havard Skinnemoen <hskinnemoen@google.com>
>
> Reviewed-by: Hao Wu <wuhaotsh@google.com>

> > ---
> >  tests/qtest/npcm7xx_watchdog_timer-test.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/tests/qtest/npcm7xx_watchdog_timer-test.c
> b/tests/qtest/npcm7xx_watchdog_timer-test.c
> > index 54d5d6d8f2..3aae5a0438 100644
> > --- a/tests/qtest/npcm7xx_watchdog_timer-test.c
> > +++ b/tests/qtest/npcm7xx_watchdog_timer-test.c
> > @@ -204,6 +204,7 @@ static void test_enabling_flags(gconstpointer
> watchdog)
> >  {
> >      const Watchdog *wd = watchdog;
> >      QTestState *qts;
> > +    QDict *rsp;
> >
> >      /* Neither WTIE or WTRE is set, no interrupt or reset should happen
> */
> >      qts = qtest_init("-machine quanta-gsj");
> > @@ -240,8 +241,9 @@ static void test_enabling_flags(gconstpointer
> watchdog)
> >      g_assert_false(qtest_get_irq(qts, wd->irq));
> >      qtest_clock_step(qts, watchdog_calculate_steps(RESET_CYCLES,
> >                  watchdog_prescaler(qts, wd)));
> > -    g_assert_false(strcmp(qdict_get_str(get_watchdog_action(qts),
> "action"),
> > -                "reset"));
> > +    rsp = get_watchdog_action(qts);
> > +    g_assert_false(strcmp(qdict_get_str(rsp, "action"), "reset"));
> > +    qobject_unref(rsp);
> >      qtest_qmp_eventwait(qts, "RESET");
> >      qtest_quit(qts);
> >
> > --
> > 2.23.0
> >
>
diff mbox series

Patch

diff --git a/tests/qtest/npcm7xx_watchdog_timer-test.c b/tests/qtest/npcm7xx_watchdog_timer-test.c
index 54d5d6d8f2..3aae5a0438 100644
--- a/tests/qtest/npcm7xx_watchdog_timer-test.c
+++ b/tests/qtest/npcm7xx_watchdog_timer-test.c
@@ -204,6 +204,7 @@  static void test_enabling_flags(gconstpointer watchdog)
 {
     const Watchdog *wd = watchdog;
     QTestState *qts;
+    QDict *rsp;
 
     /* Neither WTIE or WTRE is set, no interrupt or reset should happen */
     qts = qtest_init("-machine quanta-gsj");
@@ -240,8 +241,9 @@  static void test_enabling_flags(gconstpointer watchdog)
     g_assert_false(qtest_get_irq(qts, wd->irq));
     qtest_clock_step(qts, watchdog_calculate_steps(RESET_CYCLES,
                 watchdog_prescaler(qts, wd)));
-    g_assert_false(strcmp(qdict_get_str(get_watchdog_action(qts), "action"),
-                "reset"));
+    rsp = get_watchdog_action(qts);
+    g_assert_false(strcmp(qdict_get_str(rsp, "action"), "reset"));
+    qobject_unref(rsp);
     qtest_qmp_eventwait(qts, "RESET");
     qtest_quit(qts);