From 410bb7c09d481d5ec9413566a2f64b28b8ef7aff Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 20 Jun 2024 19:49:21 -0400 Subject: [PATCH] Fix a ref count mistake and add a long-overdue tf_util_print_backtrace() that helped me find it. --- src/http.c | 2 +- src/httpd.js.c | 6 +++--- src/util.js.c | 7 +++++++ src/util.js.h | 5 +++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/http.c b/src/http.c index 10afb215..4abeeabf 100644 --- a/src/http.c +++ b/src/http.c @@ -1019,7 +1019,7 @@ void tf_http_request_unref(tf_http_request_t* request) tf_free(request); } - if (--connection->ref_count == 0) + if (connection && --connection->ref_count == 0) { if (connection->http->is_shutting_down) { diff --git a/src/httpd.js.c b/src/httpd.js.c index 8ce608b1..db277b6a 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -1541,7 +1541,6 @@ static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data) } else { - tf_http_request_ref(request); login->name = account_name_copy; login->error = login_error; @@ -1566,6 +1565,7 @@ static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data) } login->pending++; + tf_http_request_ref(request); tf_file_read(login->request->user_data, "core/auth.html", _httpd_endpoint_login_file_read_callback, login); account_name_copy = NULL; @@ -1584,9 +1584,9 @@ done: static void _httpd_endpoint_login_after_work(tf_ssb_t* ssb, int status, void* user_data) { login_request_t* login = user_data; + tf_http_request_t* request = login->request; if (login->pending == 1) { - tf_http_request_t* request = login->request; if (*login->location_header) { const char* headers[] = { @@ -1597,8 +1597,8 @@ static void _httpd_endpoint_login_after_work(tf_ssb_t* ssb, int status, void* us }; tf_http_respond(request, 303, headers, tf_countof(headers) / 2, NULL, 0); } - tf_http_request_unref(request); } + tf_http_request_unref(request); _login_release(login); } diff --git a/src/util.js.c b/src/util.js.c index ca1a8370..1df06cc0 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -435,6 +435,13 @@ const char* tf_util_backtrace_string() return tf_util_backtrace_to_string(buffer, count); } +void tf_util_print_backtrace() +{ + const char* bt = tf_util_backtrace_string(); + tf_printf("%s\n", bt); + tf_free((void*)bt); +} + #if defined(__ANDROID__) typedef struct _android_backtrace_t { diff --git a/src/util.js.h b/src/util.js.h index 9c71b505..73827f9e 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -126,6 +126,11 @@ const char* tf_util_backtrace_to_string(void* const* buffer, int count); */ const char* tf_util_backtrace_string(); +/** +** Print a stack backtrace of the calling thread. +*/ +void tf_util_print_backtrace(); + /** ** Convert a function pointer to its name, if possible. ** @return The function name or null.