Use tor_event_free instead of event_del+tor_free

Using tor_free is wrong; event_free must be called for objects obtained from
event_new. Additionally, this slightly simplifies the code.

Also, add a static_assert to prevent further instances.
This commit is contained in:
Alex Xu (Hello71) 2022-04-19 22:50:31 -04:00
parent 616c06c0b2
commit 15e95c3bda
3 changed files with 11 additions and 26 deletions

View File

@ -274,16 +274,8 @@ connection_add_impl(connection_t *conn, int is_connecting)
void void
connection_unregister_events(connection_t *conn) connection_unregister_events(connection_t *conn)
{ {
if (conn->read_event) { tor_event_free(conn->read_event);
if (event_del(conn->read_event)) tor_event_free(conn->write_event);
log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
tor_free(conn->read_event);
}
if (conn->write_event) {
if (event_del(conn->write_event))
log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
tor_free(conn->write_event);
}
if (conn->type == CONN_TYPE_AP_DNS_LISTENER) { if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
dnsserv_close_listener(conn); dnsserv_close_listener(conn);
} }

View File

@ -11,6 +11,7 @@
#ifndef TOR_UTIL_MALLOC_H #ifndef TOR_UTIL_MALLOC_H
#define TOR_UTIL_MALLOC_H #define TOR_UTIL_MALLOC_H
#include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include "lib/cc/compat_compiler.h" #include "lib/cc/compat_compiler.h"
@ -45,6 +46,9 @@ void tor_free_(void *mem);
#ifdef __GNUC__ #ifdef __GNUC__
#define tor_free(p) STMT_BEGIN \ #define tor_free(p) STMT_BEGIN \
typeof(&(p)) tor_free__tmpvar = &(p); \ typeof(&(p)) tor_free__tmpvar = &(p); \
_Static_assert(!__builtin_types_compatible_p(typeof(*tor_free__tmpvar), \
struct event *), \
"use tor_event_free for struct event *"); \
raw_free(*tor_free__tmpvar); \ raw_free(*tor_free__tmpvar); \
*tor_free__tmpvar=NULL; \ *tor_free__tmpvar=NULL; \
STMT_END STMT_END

View File

@ -22,6 +22,7 @@
#include "feature/dircommon/directory.h" #include "feature/dircommon/directory.h"
#include "core/or/connection_or.h" #include "core/or/connection_or.h"
#include "lib/net/resolve.h" #include "lib/net/resolve.h"
#include "lib/evloop/compat_libevent.h"
#include "test/test_connection.h" #include "test/test_connection.h"
#include "test/test_helpers.h" #include "test/test_helpers.h"
@ -113,14 +114,8 @@ test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
/* We didn't call tor_libevent_initialize(), so event_base was NULL, /* We didn't call tor_libevent_initialize(), so event_base was NULL,
* so we can't rely on connection_unregister_events() use of event_del(). * so we can't rely on connection_unregister_events() use of event_del().
*/ */
if (conn->linked_conn->read_event) { tor_event_free(conn->linked_conn->read_event);
tor_free(conn->linked_conn->read_event); tor_event_free(conn->linked_conn->write_event);
conn->linked_conn->read_event = NULL;
}
if (conn->linked_conn->write_event) {
tor_free(conn->linked_conn->write_event);
conn->linked_conn->write_event = NULL;
}
if (!conn->linked_conn->marked_for_close) { if (!conn->linked_conn->marked_for_close) {
connection_close_immediate(conn->linked_conn); connection_close_immediate(conn->linked_conn);
@ -142,14 +137,8 @@ test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
/* We didn't set the events up properly, so we can't use event_del() in /* We didn't set the events up properly, so we can't use event_del() in
* close_closeable_connections() > connection_free() * close_closeable_connections() > connection_free()
* > connection_unregister_events() */ * > connection_unregister_events() */
if (conn->read_event) { tor_event_free(conn->read_event);
tor_free(conn->read_event); tor_event_free(conn->write_event);
conn->read_event = NULL;
}
if (conn->write_event) {
tor_free(conn->write_event);
conn->write_event = NULL;
}
if (!conn->marked_for_close) { if (!conn->marked_for_close) {
connection_close_immediate(conn); connection_close_immediate(conn);