Fix double-free on unmatched logger error pattern
authorZachary Snow <zach@zachjs.com>
Wed, 24 Feb 2021 01:39:13 +0000 (20:39 -0500)
committerZachary Snow <zach@zachjs.com>
Wed, 24 Feb 2021 01:49:21 +0000 (20:49 -0500)
When an expected logger error pattern is unmatched, the logger raises
another (hidden) error. Because of the previous ordering of actions,
`logv_error_with_prefix()` would inadvertently invoke `yosys_atexit()`
twice, causing a double-free.

kernel/log.cc

index c7ae873bc82e09ef7e16144f05bf74de781d8837..41e91119e97f4c1acf4295b436f3ab47b25d1eaf 100644 (file)
@@ -345,9 +345,6 @@ static void logv_error_with_prefix(const char *prefix,
 
        log_make_debug = bak_log_make_debug;
 
-       if (log_error_atexit)
-               log_error_atexit();
-
        for (auto &item : log_expect_error)
                if (YS_REGEX_NS::regex_search(log_last_error, item.second.pattern))
                        item.second.current_count++;
@@ -355,6 +352,9 @@ static void logv_error_with_prefix(const char *prefix,
        if (check_expected_logs)
                log_check_expected();
 
+       if (log_error_atexit)
+               log_error_atexit();
+
        YS_DEBUGTRAP_IF_DEBUGGING;
 
 #ifdef EMSCRIPTEN