From c7efe4e6681529e4ccd6970735475eb2fc16e719 Mon Sep 17 00:00:00 2001 From: timlyeee <43355299+timlyeee@users.noreply.github.com> Date: Wed, 17 Nov 2021 10:27:12 +0800 Subject: [PATCH] Using self defined function to replace strdup (#4046) --- native/cocos/network/HttpClient-java.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/native/cocos/network/HttpClient-java.cpp b/native/cocos/network/HttpClient-java.cpp index 57c549c517..a5e83c7cd4 100644 --- a/native/cocos/network/HttpClient-java.cpp +++ b/native/cocos/network/HttpClient-java.cpp @@ -86,9 +86,6 @@ public: explicit HttpURLConnection(HttpClient *httpClient) : _client(httpClient), _httpURLConnection(nullptr), - _requestmethod(""), - _responseCookies(""), - _cookieFileName(""), _contentLength(0) { } @@ -370,7 +367,7 @@ public: _cookieFileName = filename; } - int getContentLength() { + int getContentLength() const { return _contentLength; } @@ -549,7 +546,13 @@ private: return nullptr; } std::string strValue = cc::StringUtils::getStringUTFCharsJNI(env, jstr); - return strdup(strValue.c_str()); + size_t size = strValue.size() + 1; + char* retVal = static_cast(malloc(size)); + if (retVal == nullptr) { + return nullptr; + } + memcpy(retVal, strValue.c_str(), size); + return retVal; } int getCStrFromJByteArray(jbyteArray jba, JNIEnv *env, char **ppData) { //NOLINT(readability-convert-member-functions-to-static) @@ -570,7 +573,6 @@ private: return _responseCookies; } -private: HttpClient *_client; jobject _httpURLConnection; std::string _requestmethod; @@ -582,7 +584,7 @@ private: // Process Response void HttpClient::processResponse(HttpResponse *response, char *responseMessage) { - auto request = response->getHttpRequest(); + auto* request = response->getHttpRequest(); HttpRequest::Type requestType = request->getRequestType(); if (HttpRequest::Type::GET != requestType && @@ -728,7 +730,7 @@ void HttpClient::networkThread() { _schedulerMutex.lock(); if (auto sche = _scheduler.lock()) { - sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); + sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); //NOLINT } _schedulerMutex.unlock(); } @@ -787,7 +789,7 @@ void HttpClient::destroyInstance() { CC_LOG_DEBUG("HttpClient::destroyInstance ..."); - auto thiz = gHttpClient; + auto *thiz = gHttpClient; gHttpClient = nullptr; if (auto sche = thiz->_scheduler.lock()) { @@ -844,7 +846,7 @@ bool HttpClient::lazyInitThreadSemaphore() { return true; } - auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this)); + auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this)); //NOLINT t.detach(); _isInited = true;