X Tutup
Skip to content

Commit fe0b828

Browse files
committed
Avoid leaks resuling from ASAN tests
1 parent 099354c commit fe0b828

File tree

13 files changed

+138
-142
lines changed

13 files changed

+138
-142
lines changed

src/details/http_endpoint.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,13 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
147147
this->family_url = h.family_url;
148148
this->reg_compiled = h.reg_compiled;
149149
if(this->reg_compiled)
150+
{
151+
regfree(&(this->re_url_normalized));
152+
150153
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
151154
REG_EXTENDED|REG_ICASE|REG_NOSUB
152155
);
156+
}
153157
this->url_pars = h.url_pars;
154158
this->url_pieces = h.url_pieces;
155159
this->chunk_positions = h.chunk_positions;

src/http_utils.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,20 +539,18 @@ bool ip_representation::operator <(const ip_representation& b) const
539539
return this_score < b_score;
540540
}
541541

542-
char* load_file (const char *filename)
542+
const std::string load_file (const std::string& filename)
543543
{
544544
ifstream fp(filename, ios::in | ios::binary | ios::ate);
545545
if(fp.is_open())
546546
{
547-
fp.seekg(0, fp.end);
548-
int size = fp.tellg();
549-
fp.seekg(0, fp.beg);
547+
std::string content;
550548

551-
char* content = new char[size + 1];
552-
fp.read(content, size);
553-
fp.close();
549+
fp.seekg(0, std::ios::end);
550+
content.reserve(fp.tellg());
551+
fp.seekg(0, std::ios::beg);
554552

555-
content[size] = '\0';
553+
content.assign((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
556554
return content;
557555
}
558556
else

src/httpserver/create_webserver.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,17 @@ class create_webserver
197197
create_webserver& no_pedantic() { _pedantic = false; return *this; }
198198
create_webserver& https_mem_key(const std::string& https_mem_key)
199199
{
200-
char* _https_mem_key_pt = http::load_file(https_mem_key.c_str());
201-
_https_mem_key = _https_mem_key_pt;
202-
delete _https_mem_key_pt;
200+
_https_mem_key = http::load_file(https_mem_key);
203201
return *this;
204202
}
205203
create_webserver& https_mem_cert(const std::string& https_mem_cert)
206204
{
207-
char* _https_mem_cert_pt = http::load_file(https_mem_cert.c_str());
208-
_https_mem_cert = _https_mem_cert_pt;
209-
delete _https_mem_cert_pt;
205+
_https_mem_cert = http::load_file(https_mem_cert);
210206
return *this;
211207
}
212208
create_webserver& https_mem_trust(const std::string& https_mem_trust)
213209
{
214-
char* _https_mem_trust_pt = http::load_file(https_mem_trust.c_str());
215-
_https_mem_trust = _https_mem_trust_pt;
216-
delete _https_mem_trust_pt;
210+
_https_mem_trust = http::load_file(https_mem_trust);
217211
return *this;
218212
}
219213
create_webserver& raw_https_mem_key(const std::string& https_mem_key)

src/httpserver/http_resource.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class http_resource
6060
**/
6161
virtual ~http_resource()
6262
{
63+
allowed_methods.clear();
6364
}
6465

6566
/**
@@ -204,6 +205,7 @@ class http_resource
204205
{
205206
resource_init(allowed_methods);
206207
}
208+
207209
/**
208210
* Copy constructor
209211
**/

src/httpserver/http_utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void dump_arg_map(std::ostream &os, const std::string &prefix,
354354
*/
355355
size_t http_unescape (char *val);
356356

357-
char* load_file (const char *filename);
357+
const std::string load_file (const std::string& filename);
358358

359359
};
360360
};

test/integ/authentication.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ LT_END_SUITE(authentication_suite)
9191
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth)
9292
webserver ws = create_webserver(8080);
9393

94-
user_pass_resource* user_pass = new user_pass_resource();
95-
ws.register_resource("base", user_pass);
94+
user_pass_resource user_pass;
95+
ws.register_resource("base", &user_pass);
9696
ws.start(false);
9797

9898
curl_global_init(CURL_GLOBAL_ALL);
@@ -116,8 +116,8 @@ LT_END_AUTO_TEST(base_auth)
116116
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail)
117117
webserver ws = create_webserver(8080);
118118

119-
user_pass_resource* user_pass = new user_pass_resource();
120-
ws.register_resource("base", user_pass);
119+
user_pass_resource user_pass;
120+
ws.register_resource("base", &user_pass);
121121
ws.start(false);
122122

123123
curl_global_init(CURL_GLOBAL_ALL);
@@ -143,8 +143,8 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth)
143143
.digest_auth_random("myrandom")
144144
.nonce_nc_size(300);
145145

146-
digest_resource* digest = new digest_resource();
147-
ws.register_resource("base", digest);
146+
digest_resource digest;
147+
ws.register_resource("base", &digest);
148148
ws.start(false);
149149

150150
curl_global_init(CURL_GLOBAL_ALL);
@@ -174,8 +174,8 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass)
174174
.digest_auth_random("myrandom")
175175
.nonce_nc_size(300);
176176

177-
digest_resource* digest = new digest_resource();
178-
ws.register_resource("base", digest);
177+
digest_resource digest;
178+
ws.register_resource("base", &digest);
179179
ws.start(false);
180180

181181
curl_global_init(CURL_GLOBAL_ALL);

test/integ/ban_system.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
5858
webserver ws = create_webserver(8080).default_policy(http_utils::ACCEPT);
5959
ws.start(false);
6060

61-
ok_resource* resource = new ok_resource();
62-
ws.register_resource("base", resource);
61+
ok_resource resource;
62+
ws.register_resource("base", &resource);
6363

64-
{
6564
curl_global_init(CURL_GLOBAL_ALL);
65+
66+
{
6667
std::string s;
6768
CURL *curl = curl_easy_init();
6869
CURLcode res;
@@ -79,14 +80,10 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
7980
{
8081
ws.ban_ip("127.0.0.1");
8182

82-
curl_global_init(CURL_GLOBAL_ALL);
83-
std::string s;
8483
CURL *curl = curl_easy_init();
8584
CURLcode res;
8685
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
8786
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
88-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
89-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
9087
res = curl_easy_perform(curl);
9188
LT_ASSERT_NEQ(res, 0);
9289
curl_easy_cleanup(curl);
@@ -95,7 +92,6 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
9592
{
9693
ws.unban_ip("127.0.0.1");
9794

98-
curl_global_init(CURL_GLOBAL_ALL);
9995
std::string s;
10096
CURL *curl = curl_easy_init();
10197
CURLcode res;
@@ -109,25 +105,24 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
109105
curl_easy_cleanup(curl);
110106
}
111107

108+
curl_global_cleanup();
112109
ws.stop();
113110
LT_END_AUTO_TEST(accept_default_ban_blocks)
114111

115112
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
116113
webserver ws = create_webserver(8080).default_policy(http_utils::REJECT);
117114
ws.start(false);
118115

119-
ok_resource* resource = new ok_resource();
120-
ws.register_resource("base", resource);
116+
ok_resource resource;
117+
ws.register_resource("base", &resource);
121118

122-
{
123119
curl_global_init(CURL_GLOBAL_ALL);
124-
std::string s;
120+
121+
{
125122
CURL *curl = curl_easy_init();
126123
CURLcode res;
127124
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
128125
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
129-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
130-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
131126
res = curl_easy_perform(curl);
132127
LT_ASSERT_NEQ(res, 0);
133128
curl_easy_cleanup(curl);
@@ -136,7 +131,6 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
136131
{
137132
ws.allow_ip("127.0.0.1");
138133

139-
curl_global_init(CURL_GLOBAL_ALL);
140134
std::string s;
141135
CURL *curl = curl_easy_init();
142136
CURLcode res;
@@ -147,24 +141,22 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
147141
res = curl_easy_perform(curl);
148142
LT_ASSERT_EQ(res, 0);
149143
LT_CHECK_EQ(s, "OK");
144+
curl_easy_cleanup(curl);
150145
}
151146

152147
{
153148
ws.disallow_ip("127.0.0.1");
154149

155-
curl_global_init(CURL_GLOBAL_ALL);
156-
std::string s;
157150
CURL *curl = curl_easy_init();
158151
CURLcode res;
159152
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
160153
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
161-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
162-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
163154
res = curl_easy_perform(curl);
164155
LT_ASSERT_NEQ(res, 0);
165156
curl_easy_cleanup(curl);
166157
}
167158

159+
curl_global_cleanup();
168160
ws.stop();
169161
LT_END_AUTO_TEST(reject_default_allow_passes)
170162

0 commit comments

Comments
 (0)
X Tutup