Skip to content

Commit 0b2346f

Browse files
committed
Android: Fixed httpsObject leaked on every request.
1 parent e9d9353 commit 0b2346f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/android/AndroidClient.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include <dlfcn.h>
99

10+
// We want std::string that contains null byte, hence length of 1.
11+
// NOLINTNEXTLINE
12+
static std::string null("", 1);
13+
1014
static std::string replace(const std::string &str, const std::string &from, const std::string &to)
1115
{
1216
std::stringstream ss;
@@ -31,19 +35,13 @@ static std::string replace(const std::string &str, const std::string &from, cons
3135

3236
static jstring newStringUTF(JNIEnv *env, const std::string &str)
3337
{
34-
// We want std::string that contains null byte, hence length of 1.
35-
static std::string null("", 1);
36-
3738
std::string newStr = replace(str, null, "\xC0\x80");
3839
jstring jstr = env->NewStringUTF(newStr.c_str());
3940
return jstr;
4041
}
4142

4243
static std::string getStringUTF(JNIEnv *env, jstring str)
4344
{
44-
// We want std::string that contains null byte, hence length of 1.
45-
static std::string null("", 1);
46-
4745
const char *c = env->GetStringUTFChars(str, nullptr);
4846
std::string result = replace(c, "\xC0\x80", null);
4947

@@ -53,7 +51,6 @@ static std::string getStringUTF(JNIEnv *env, jstring str)
5351

5452
AndroidClient::AndroidClient()
5553
: HTTPSClient()
56-
, SDL_AndroidGetJNIEnv(nullptr)
5754
{
5855
// Look for SDL_AndroidGetJNIEnv
5956
SDL_AndroidGetJNIEnv = (decltype(SDL_AndroidGetJNIEnv)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetJNIEnv");
@@ -116,12 +113,14 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
116113
env->DeleteLocalRef(method);
117114

118115
// Set post data
119-
if (req.postdata.size() > 0)
116+
if (!req.postdata.empty())
120117
{
121118
jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
122119
jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());
123120
jbyte *byteArrayData = env->GetByteArrayElements(byteArray, nullptr);
124121

122+
// The usage of memcpy is intentional.
123+
// NOLINTNEXTLINE
125124
memcpy(byteArrayData, req.postdata.data(), req.postdata.length());
126125
env->ReleaseByteArrayElements(byteArray, byteArrayData, 0);
127126

@@ -156,9 +155,9 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
156155
{
157156
// Get headers
158157
jobjectArray interleavedHeaders = (jobjectArray) env->CallObjectMethod(httpsObject, getInterleavedHeaders);
159-
int len = env->GetArrayLength(interleavedHeaders);
158+
int headerLen = env->GetArrayLength(interleavedHeaders);
160159

161-
for (int i = 0; i < len; i += 2)
160+
for (int i = 0; i < headerLen; i += 2)
162161
{
163162
jstring key = (jstring) env->GetObjectArrayElement(interleavedHeaders, i);
164163
jstring value = (jstring) env->GetObjectArrayElement(interleavedHeaders, i + 1);
@@ -176,15 +175,17 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
176175

177176
if (responseData)
178177
{
179-
int len = env->GetArrayLength(responseData);
178+
int responseLen = env->GetArrayLength(responseData);
180179
jbyte *responseByte = env->GetByteArrayElements(responseData, nullptr);
181180

182-
response.body = std::string((char *) responseByte, len);
181+
response.body = std::string((char *) responseByte, responseLen);
183182

184183
env->DeleteLocalRef(responseData);
185184
}
186185
}
187186

187+
env->DeleteLocalRef(httpsObject);
188+
188189
return response;
189190
}
190191

0 commit comments

Comments
 (0)