Fix some code bugs

This commit is contained in:
enWILLYado 2018-03-12 22:58:55 +01:00
parent 82da84d849
commit 9b68f187f9
4 changed files with 26 additions and 29 deletions

View file

@ -32,25 +32,25 @@
#include "version.h" #include "version.h"
#ifdef XMRIG_NVIDIA_PROJECT #ifdef XMRIG_NVIDIA_PROJECT
# include "nvidia/cryptonight.h" #include "nvidia/cryptonight.h"
#endif #endif
static inline char* createUserAgent() static inline std::string createUserAgent()
{ {
const size_t max = 160; const size_t max = 160;
char* buf = new char[max]; char buf[max];
# ifdef XMRIG_NVIDIA_PROJECT #ifdef XMRIG_NVIDIA_PROJECT
const int cudaVersion = cuda_get_runtime_version(); const int cudaVersion = cuda_get_runtime_version();
snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s CUDA/%d.%d clang/%d.%d.%d", APP_NAME, snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s CUDA/%d.%d clang/%d.%d.%d", APP_NAME,
APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, __clang_major__, __clang_minor__, APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, __clang_major__, __clang_minor__,
__clang_patchlevel__); __clang_patchlevel__);
# else #else
snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s clang/%d.%d.%d", APP_NAME, APP_VERSION, snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s clang/%d.%d.%d", APP_NAME, APP_VERSION,
uv_version_string(), __clang_major__, __clang_minor__, __clang_patchlevel__); uv_version_string(), __clang_major__, __clang_minor__, __clang_patchlevel__);
# endif #endif
return buf; return buf;
} }
@ -58,13 +58,13 @@ static inline char* createUserAgent()
void Platform::init(const std::string & userAgent) void Platform::init(const std::string & userAgent)
{ {
m_userAgent = (userAgent.size() != "") ? userAgent : createUserAgent(); m_userAgent = (0 < userAgent.size()) ? userAgent : createUserAgent();
} }
void Platform::release() void Platform::release()
{ {
delete [] m_userAgent; m_userAgent.clear();
} }

View file

@ -36,7 +36,7 @@
#include "version.h" #include "version.h"
#ifdef XMRIG_NVIDIA_PROJECT #ifdef XMRIG_NVIDIA_PROJECT
# include "nvidia/cryptonight.h" #include "nvidia/cryptonight.h"
#endif #endif
@ -44,23 +44,23 @@ static inline std::string createUserAgent()
{ {
const size_t max = 160; const size_t max = 160;
char* buf = new char[max]; char buf[max];
int length = snprintf(buf, max, "%s/%s (Linux ", APP_NAME, APP_VERSION); int length = snprintf(buf, max, "%s/%s (Linux ", APP_NAME, APP_VERSION);
# if defined(__x86_64__) #if defined(__x86_64__)
length += snprintf(buf + length, max - length, "x86_64) libuv/%s", uv_version_string()); length += snprintf(buf + length, max - length, "x86_64) libuv/%s", uv_version_string());
# else #else
length += snprintf(buf + length, max - length, "i686) libuv/%s", uv_version_string()); length += snprintf(buf + length, max - length, "i686) libuv/%s", uv_version_string());
# endif #endif
# ifdef XMRIG_NVIDIA_PROJECT #ifdef XMRIG_NVIDIA_PROJECT
const int cudaVersion = cuda_get_runtime_version(); const int cudaVersion = cuda_get_runtime_version();
length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100);
# endif #endif
# ifdef __GNUC__ #ifdef __GNUC__
length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
# endif #endif
return buf; return buf;
} }
@ -71,19 +71,15 @@ void Platform::init(const std::string & userAgent)
m_userAgent = (0 < userAgent.size()) ? userAgent : createUserAgent(); m_userAgent = (0 < userAgent.size()) ? userAgent : createUserAgent();
} }
void Platform::release() void Platform::release()
{ {
m_userAgent.clear(); m_userAgent.clear();
} }
void Platform::setProcessPriority(int priority) void Platform::setProcessPriority(int priority)
{ {
} }
void Platform::setThreadPriority(int priority) void Platform::setThreadPriority(int priority)
{ {
if(priority == -1) if(priority == -1)
@ -120,7 +116,7 @@ void Platform::setThreadPriority(int priority)
setpriority(PRIO_PROCESS, 0, prio); setpriority(PRIO_PROCESS, 0, prio);
# ifdef SCHED_IDLE #ifdef SCHED_IDLE
if(priority == 0) if(priority == 0)
{ {
sched_param param; sched_param param;

View file

@ -485,7 +485,7 @@ void Client::parse(char* line, size_t len)
} }
void Client::parseNotification(const char* method, const rapidjson::Value & params, void Client::parseNotification(const std::string & method, const rapidjson::Value & params,
const rapidjson::Value & error) const rapidjson::Value & error)
{ {
if(error.IsObject()) if(error.IsObject())
@ -498,12 +498,12 @@ void Client::parseNotification(const char* method, const rapidjson::Value & para
return; return;
} }
if(!method) if(0 == method.size())
{ {
return; return;
} }
if(strcmp(method, "job") == 0) if(method == "job")
{ {
int code = -1; int code = -1;
if(parseJob(params, &code)) if(parseJob(params, &code))
@ -522,7 +522,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value & result, const ra
{ {
if(error.IsObject()) if(error.IsObject())
{ {
const char* message = error["message"].GetString(); const std::string message = error["message"].GetString();
auto it = m_results.find(id); auto it = m_results.find(id);
if(it != m_results.end()) if(it != m_results.end())
@ -582,8 +582,8 @@ void Client::parseResponse(int64_t id, const rapidjson::Value & result, const ra
void Client::ping() void Client::ping()
{ {
send(snprintf(m_sendBuf, sizeof(m_sendBuf), send(snprintf(m_sendBuf, sizeof(m_sendBuf),
"{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n",
m_rpcId)); m_sequence, m_rpcId));
} }

View file

@ -120,7 +120,8 @@ private:
void prelogin(); void prelogin();
void login(); void login();
void parse(char* line, size_t len); void parse(char* line, size_t len);
void parseNotification(const char* method, const rapidjson::Value & params, const rapidjson::Value & error); void parseNotification(const std::string & method, const rapidjson::Value & params,
const rapidjson::Value & error);
void parseResponse(int64_t id, const rapidjson::Value & result, const rapidjson::Value & error); void parseResponse(int64_t id, const rapidjson::Value & result, const rapidjson::Value & error);
void ping(); void ping();
void reconnect(); void reconnect();