mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-02 16:03:45 +00:00
Refine memory management (#579)
- Added one macro to avoid dangling pointers - Added two functions to perform NULL pointer check since the allocation is not guaranteed by C library, although it is a rare case, just for sanity - Add NULL pointer check to brealloc() and bfree() and for sanity as well Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
+12
-13
@@ -46,6 +46,7 @@
|
||||
|
||||
#include "resolv.h"
|
||||
#include "utils.h"
|
||||
#include "mm-wrapper.h"
|
||||
|
||||
/*
|
||||
* Implement DNS resolution interface using libudns
|
||||
@@ -146,7 +147,7 @@ struct ResolvQuery *resolv_query(const char *hostname, void (*client_cb)(struct
|
||||
/*
|
||||
* Wrap udns's call back in our own
|
||||
*/
|
||||
struct ResolvQuery *cb_data = malloc(sizeof(struct ResolvQuery));
|
||||
struct ResolvQuery *cb_data = SS_SAFEMALLOC(sizeof(struct ResolvQuery));
|
||||
if (cb_data == NULL) {
|
||||
LOGE("Failed to allocate memory for DNS query callback data.");
|
||||
return NULL;
|
||||
@@ -184,8 +185,7 @@ struct ResolvQuery *resolv_query(const char *hostname, void (*client_cb)(struct
|
||||
if (cb_data->client_free_cb != NULL) {
|
||||
cb_data->client_free_cb(cb_data->client_cb_data);
|
||||
}
|
||||
free(cb_data);
|
||||
cb_data = NULL;
|
||||
SS_SAFEFREE(cb_data);
|
||||
}
|
||||
|
||||
return cb_data;
|
||||
@@ -200,15 +200,14 @@ void resolv_cancel(struct ResolvQuery *query_handle)
|
||||
i++)
|
||||
if (cb_data->queries[i] != NULL) {
|
||||
dns_cancel(ctx, cb_data->queries[i]);
|
||||
free(cb_data->queries[i]);
|
||||
cb_data->queries[i] = NULL;
|
||||
SS_SAFEFREE(cb_data->queries[i]);
|
||||
}
|
||||
|
||||
if (cb_data->client_free_cb != NULL) {
|
||||
cb_data->client_free_cb(cb_data->client_cb_data);
|
||||
}
|
||||
|
||||
free(cb_data);
|
||||
SS_SAFEFREE(cb_data);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -235,7 +234,7 @@ static void dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void
|
||||
LOGI("IPv4 resolv: %s", dns_strerror(dns_status(ctx)));
|
||||
}
|
||||
} else if (result->dnsa4_nrr > 0) {
|
||||
struct sockaddr **new_responses = realloc(cb_data->responses,
|
||||
struct sockaddr **new_responses = SS_SAFEREALLOC(cb_data->responses,
|
||||
(cb_data->response_count +
|
||||
result->dnsa4_nrr) *
|
||||
sizeof(struct sockaddr *));
|
||||
@@ -263,7 +262,7 @@ static void dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void
|
||||
}
|
||||
}
|
||||
|
||||
free(result);
|
||||
SS_SAFEFREE(result);
|
||||
cb_data->queries[0] = NULL; /* mark A query as being completed */
|
||||
|
||||
/* Once all queries have completed, call client callback */
|
||||
@@ -281,7 +280,7 @@ static void dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void
|
||||
LOGI("IPv6 resolv: %s", dns_strerror(dns_status(ctx)));
|
||||
}
|
||||
} else if (result->dnsa6_nrr > 0) {
|
||||
struct sockaddr **new_responses = realloc(cb_data->responses,
|
||||
struct sockaddr **new_responses = SS_SAFEREALLOC(cb_data->responses,
|
||||
(cb_data->response_count +
|
||||
result->dnsa6_nrr) *
|
||||
sizeof(struct sockaddr *));
|
||||
@@ -309,7 +308,7 @@ static void dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void
|
||||
}
|
||||
}
|
||||
|
||||
free(result);
|
||||
SS_SAFEFREE(result);
|
||||
cb_data->queries[1] = NULL; /* mark AAAA query as being completed */
|
||||
|
||||
/* Once all queries have completed, call client callback */
|
||||
@@ -336,13 +335,13 @@ static void process_client_callback(struct ResolvQuery *cb_data)
|
||||
cb_data->client_cb(best_address, cb_data->client_cb_data);
|
||||
|
||||
for (int i = 0; i < cb_data->response_count; i++)
|
||||
free(cb_data->responses[i]);
|
||||
SS_SAFEFREE(cb_data->responses[i]);
|
||||
|
||||
free(cb_data->responses);
|
||||
SS_SAFEFREE(cb_data->responses);
|
||||
if (cb_data->client_free_cb != NULL) {
|
||||
cb_data->client_free_cb(cb_data->client_cb_data);
|
||||
}
|
||||
free(cb_data);
|
||||
SS_SAFEFREE(cb_data);
|
||||
}
|
||||
|
||||
static struct sockaddr *choose_ipv4_first(struct ResolvQuery *cb_data)
|
||||
|
||||
Reference in New Issue
Block a user