mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2026-02-01 10:51:08 -05:00
Use std::string.assign when setting strings in the SDK protocol and description handlers so that string size limit is enforced, then strip out null terminators
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <cstring>
|
||||
#include "NetworkClient.h"
|
||||
#include "RGBController_Network.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
@@ -365,6 +366,7 @@ std::string NetworkClient::ProfileManager_DownloadProfile(std::string profile_na
|
||||
if(response_header.pkt_id == NET_PACKET_ID_PROFILEMANAGER_DOWNLOAD_PROFILE && response_data_ptr != NULL)
|
||||
{
|
||||
response_string.assign(response_data_ptr, response_header.pkt_size);
|
||||
response_string = StringUtils::remove_null_terminating_chars(response_string);
|
||||
delete[] response_data_ptr;
|
||||
response_data_ptr = NULL;
|
||||
}
|
||||
@@ -389,6 +391,7 @@ std::string NetworkClient::ProfileManager_GetActiveProfile()
|
||||
if(response_header.pkt_id == NET_PACKET_ID_PROFILEMANAGER_GET_ACTIVE_PROFILE && response_data_ptr != NULL)
|
||||
{
|
||||
response_string.assign(response_data_ptr, response_header.pkt_size);
|
||||
response_string = StringUtils::remove_null_terminating_chars(response_string);
|
||||
delete[] response_data_ptr;
|
||||
response_data_ptr = NULL;
|
||||
}
|
||||
@@ -417,6 +420,7 @@ std::string NetworkClient::SettingsManager_GetSettings(std::string settings_key)
|
||||
if(response_header.pkt_id == NET_PACKET_ID_SETTINGSMANAGER_GET_SETTINGS && response_data_ptr != NULL)
|
||||
{
|
||||
response_string.assign(response_data_ptr, response_header.pkt_size);
|
||||
response_string = StringUtils::remove_null_terminating_chars(response_string);
|
||||
delete[] response_data_ptr;
|
||||
response_data_ptr = NULL;
|
||||
}
|
||||
@@ -1211,6 +1215,7 @@ void NetworkClient::ProcessRequest_DetectionProgressChanged(unsigned int data_si
|
||||
data += sizeof(string_length);
|
||||
|
||||
detection_string.assign(data, string_length);
|
||||
detection_string = StringUtils::remove_null_terminating_chars(detection_string);
|
||||
|
||||
SignalNetworkClientUpdate(NETWORKCLIENT_UPDATE_REASON_DETECTION_PROGRESS_CHANGED);
|
||||
}
|
||||
@@ -1316,6 +1321,7 @@ void NetworkClient::ProcessRequest_RGBController_SignalUpdate(unsigned int data_
|
||||
void NetworkClient::ProcessRequest_ServerString(unsigned int data_size, char * data)
|
||||
{
|
||||
server_name.assign(data, data_size);
|
||||
server_name = StringUtils::remove_null_terminating_chars(server_name);
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include "NetworkServer.h"
|
||||
#include "LogManager.h"
|
||||
#include "NetworkServer.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/ioctl.h>
|
||||
@@ -1019,6 +1020,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string profile_name;
|
||||
profile_name.assign(data, header.pkt_size);
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
profile_manager->SaveProfile(profile_name);
|
||||
}
|
||||
@@ -1034,6 +1036,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string profile_name;
|
||||
profile_name.assign(data, header.pkt_size);
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
profile_manager->LoadProfile(profile_name);
|
||||
}
|
||||
@@ -1049,6 +1052,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string profile_name;
|
||||
profile_name.assign(data, header.pkt_size);
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
profile_manager->DeleteProfile(profile_name);
|
||||
}
|
||||
@@ -1064,6 +1068,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string profile_json_string;
|
||||
profile_json_string.assign(data, header.pkt_size);
|
||||
profile_json_string = StringUtils::remove_null_terminating_chars(profile_json_string);
|
||||
|
||||
nlohmann::json profile_json = nlohmann::json::parse(profile_json_string);
|
||||
|
||||
@@ -1081,6 +1086,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string profile_name;
|
||||
profile_name.assign(data, header.pkt_size);
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
std::string profile_json_string = profile_manager->ReadProfileJSON(profile_name).dump();
|
||||
|
||||
@@ -1145,6 +1151,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string settings_key;
|
||||
settings_key.assign(data, header.pkt_size);
|
||||
settings_key = StringUtils::remove_null_terminating_chars(settings_key);
|
||||
|
||||
nlohmann::json settings_json = settings_manager->GetSettings(settings_key);
|
||||
std::string settings_json_str = settings_json.dump();
|
||||
@@ -1170,6 +1177,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
{
|
||||
std::string settings_json_str;
|
||||
settings_json_str.assign(data, header.pkt_size);
|
||||
settings_json_str = StringUtils::remove_null_terminating_chars(settings_json_str);
|
||||
|
||||
settings_manager->SetSettingsFromJsonString(settings_json_str);
|
||||
}
|
||||
@@ -1409,6 +1417,7 @@ void NetworkServer::ProcessRequest_ClientString(SOCKET client_sock, unsigned int
|
||||
if(ServerClients[this_idx]->client_sock == client_sock)
|
||||
{
|
||||
ServerClients[this_idx]->client_string.assign(data, data_size);
|
||||
ServerClients[this_idx]->client_string = StringUtils::remove_null_terminating_chars(ServerClients[this_idx]->client_string);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,11 +97,6 @@ ProfileManager::~ProfileManager()
|
||||
|
||||
void ProfileManager::DeleteProfile(std::string profile_name)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Clean up the profile name |
|
||||
\*-----------------------------------------------------*/
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
if(ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClientProtocolVersion() >= 6))
|
||||
{
|
||||
ResourceManager::get()->GetLocalClient()->ProfileManager_DeleteProfile(profile_name);
|
||||
@@ -242,11 +237,6 @@ nlohmann::json ProfileManager::ReadProfileJSON(std::string profile_name)
|
||||
{
|
||||
nlohmann::json profile_json;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Clean up the profile name |
|
||||
\*-----------------------------------------------------*/
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
if(ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClientProtocolVersion() >= 6))
|
||||
{
|
||||
profile_json = nlohmann::json::parse(ResourceManager::get()->GetLocalClient()->ProfileManager_DownloadProfile(profile_name));
|
||||
@@ -271,11 +261,6 @@ nlohmann::json ProfileManager::ReadProfileJSON(std::string profile_name)
|
||||
|
||||
bool ProfileManager::SaveProfile(std::string profile_name)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Clean up the profile name |
|
||||
\*-----------------------------------------------------*/
|
||||
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| If a name was entered, save the profile file |
|
||||
\*-----------------------------------------------------*/
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <cstring>
|
||||
#include "nlohmann/json.hpp"
|
||||
#include "RGBController.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
@@ -2389,7 +2390,8 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&name_len, data_ptr, sizeof(name_len));
|
||||
data_ptr += sizeof(name_len);
|
||||
|
||||
name = (char *)data_ptr;
|
||||
name.assign((char *)data_ptr, name_len);
|
||||
name = StringUtils::remove_null_terminating_chars(name);
|
||||
data_ptr += name_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2412,7 +2414,8 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&description_len, data_ptr, sizeof(description_len));
|
||||
data_ptr += sizeof(description_len);
|
||||
|
||||
description = (char *)data_ptr;
|
||||
description.assign((char *)data_ptr, description_len);
|
||||
description = StringUtils::remove_null_terminating_chars(description);
|
||||
data_ptr += description_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2422,7 +2425,8 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&version_len, data_ptr, sizeof(version_len));
|
||||
data_ptr += sizeof(version_len);
|
||||
|
||||
version = (char *)data_ptr;
|
||||
version.assign((char *)data_ptr, version_len);
|
||||
version = StringUtils::remove_null_terminating_chars(version);
|
||||
data_ptr += version_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2432,7 +2436,8 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&serial_len, data_ptr, sizeof(serial_len));
|
||||
data_ptr += sizeof(serial_len);
|
||||
|
||||
serial = (char *)data_ptr;
|
||||
serial.assign((char *)data_ptr, serial_len);
|
||||
serial = StringUtils::remove_null_terminating_chars(serial);
|
||||
data_ptr += serial_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2442,7 +2447,8 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&location_len, data_ptr, sizeof(location_len));
|
||||
data_ptr += sizeof(location_len);
|
||||
|
||||
location = (char *)data_ptr;
|
||||
location.assign((char *)data_ptr, location_len);
|
||||
location = StringUtils::remove_null_terminating_chars(location);
|
||||
data_ptr += location_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2530,7 +2536,10 @@ unsigned char* RGBController::SetDeviceDescription(unsigned char* data_ptr, unsi
|
||||
memcpy(&string_length, data_ptr, sizeof(string_length));
|
||||
data_ptr += sizeof(string_length);
|
||||
|
||||
led_alt_names.push_back((char *)data_ptr);
|
||||
std::string new_name((char *)data_ptr, string_length);
|
||||
new_name = StringUtils::remove_null_terminating_chars(new_name);
|
||||
|
||||
led_alt_names.push_back(new_name);
|
||||
data_ptr += string_length;
|
||||
}
|
||||
}
|
||||
@@ -2604,7 +2613,8 @@ unsigned char* RGBController::SetLEDDescription(unsigned char* data_ptr, led* le
|
||||
memcpy(&ledname_len, data_ptr, sizeof(ledname_len));
|
||||
data_ptr += sizeof(ledname_len);
|
||||
|
||||
led->name = (char *)data_ptr;
|
||||
led->name.assign((char *)data_ptr, ledname_len);
|
||||
led->name = StringUtils::remove_null_terminating_chars(led->name);
|
||||
data_ptr += ledname_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2625,7 +2635,8 @@ unsigned char* RGBController::SetModeDescription(unsigned char* data_ptr, mode*
|
||||
memcpy(&modename_len, data_ptr, sizeof(unsigned short));
|
||||
data_ptr += sizeof(unsigned short);
|
||||
|
||||
mode->name = (char *)data_ptr;
|
||||
mode->name.assign((char *)data_ptr, modename_len);
|
||||
mode->name = StringUtils::remove_null_terminating_chars(mode->name);
|
||||
data_ptr += modename_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2733,7 +2744,8 @@ unsigned char* RGBController::SetSegmentDescription(unsigned char* data_ptr, seg
|
||||
memcpy(&segmentname_len, data_ptr, sizeof(segmentname_len));
|
||||
data_ptr += sizeof(segmentname_len);
|
||||
|
||||
segment->name = (char *)data_ptr;
|
||||
segment->name.assign((char *)data_ptr, segmentname_len);
|
||||
segment->name = StringUtils::remove_null_terminating_chars(segment->name);
|
||||
data_ptr += segmentname_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2812,7 +2824,8 @@ unsigned char* RGBController::SetZoneDescription(unsigned char* data_ptr, zone*
|
||||
memcpy(&zonename_len, data_ptr, sizeof(zonename_len));
|
||||
data_ptr += sizeof(zonename_len);
|
||||
|
||||
zone->name = (char *)data_ptr;
|
||||
zone->name.assign((char *)data_ptr, zonename_len);
|
||||
zone->name = StringUtils::remove_null_terminating_chars(zone->name);
|
||||
data_ptr += zonename_len;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
|
||||
@@ -45,11 +45,6 @@ json SettingsManager::GetSettings(std::string settings_key)
|
||||
json result;
|
||||
bool ui_settings_key = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Remove any excess null termination from settings key |
|
||||
\*-----------------------------------------------------*/
|
||||
settings_key = StringUtils::remove_null_terminating_chars(settings_key);
|
||||
|
||||
for(std::size_t settings_key_idx = 0; settings_key_idx < 7; settings_key_idx++)
|
||||
{
|
||||
if(settings_key == ui_settings_keys[settings_key_idx])
|
||||
@@ -98,11 +93,6 @@ void SettingsManager::SetSettings(std::string settings_key, json new_settings)
|
||||
{
|
||||
bool ui_settings_key = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Remove any excess null termination from settings key |
|
||||
\*-----------------------------------------------------*/
|
||||
settings_key = StringUtils::remove_null_terminating_chars(settings_key);
|
||||
|
||||
for(std::size_t settings_key_idx = 0; settings_key_idx < 7; settings_key_idx++)
|
||||
{
|
||||
if(settings_key == ui_settings_keys[settings_key_idx])
|
||||
|
||||
Reference in New Issue
Block a user