Refactored settings string setter (#7014)

* Common function to clear strings
* Set string memory to '\0' before free
This commit is contained in:
akallabeth 2021-05-18 11:22:27 +02:00 committed by GitHub
parent d418c6ff09
commit 80d7f206af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 276 deletions

View File

@ -7,6 +7,19 @@
#define TAG FREERDP_TAG("common.settings")
static BOOL update_string(char** current, const char* next, size_t next_len, BOOL cleanup)
{
if (cleanup)
{
if (*current)
memset(*current, 0, strlen(*current));
free(*current);
}
*current = (next ? strndup(next, next_len) : NULL);
return !next || (*current != NULL);
}
BOOL freerdp_settings_get_bool(const rdpSettings* settings, size_t id)
{
if (!settings)
@ -2417,412 +2430,208 @@ BOOL freerdp_settings_set_string_(rdpSettings* settings, size_t id, const char*
switch (id)
{
case FreeRDP_AcceptedCert:
if (cleanup)
free(settings->AcceptedCert);
settings->AcceptedCert = (val ? strndup(val, len) : NULL);
return (!val || settings->AcceptedCert != NULL);
return update_string(&settings->AcceptedCert, val, len, cleanup);
case FreeRDP_ActionScript:
if (cleanup)
free(settings->ActionScript);
settings->ActionScript = (val ? strndup(val, len) : NULL);
return (!val || settings->ActionScript != NULL);
return update_string(&settings->ActionScript, val, len, cleanup);
case FreeRDP_AllowedTlsCiphers:
if (cleanup)
free(settings->AllowedTlsCiphers);
settings->AllowedTlsCiphers = (val ? strndup(val, len) : NULL);
return (!val || settings->AllowedTlsCiphers != NULL);
return update_string(&settings->AllowedTlsCiphers, val, len, cleanup);
case FreeRDP_AlternateShell:
if (cleanup)
free(settings->AlternateShell);
settings->AlternateShell = (val ? strndup(val, len) : NULL);
return (!val || settings->AlternateShell != NULL);
return update_string(&settings->AlternateShell, val, len, cleanup);
case FreeRDP_AssistanceFile:
if (cleanup)
free(settings->AssistanceFile);
settings->AssistanceFile = (val ? strndup(val, len) : NULL);
return (!val || settings->AssistanceFile != NULL);
return update_string(&settings->AssistanceFile, val, len, cleanup);
case FreeRDP_AuthenticationServiceClass:
if (cleanup)
free(settings->AuthenticationServiceClass);
settings->AuthenticationServiceClass = (val ? strndup(val, len) : NULL);
return (!val || settings->AuthenticationServiceClass != NULL);
return update_string(&settings->AuthenticationServiceClass, val, len, cleanup);
case FreeRDP_CertificateAcceptedFingerprints:
if (cleanup)
free(settings->CertificateAcceptedFingerprints);
settings->CertificateAcceptedFingerprints = (val ? strndup(val, len) : NULL);
return (!val || settings->CertificateAcceptedFingerprints != NULL);
return update_string(&settings->CertificateAcceptedFingerprints, val, len, cleanup);
case FreeRDP_CertificateContent:
if (cleanup)
free(settings->CertificateContent);
settings->CertificateContent = (val ? strndup(val, len) : NULL);
return (!val || settings->CertificateContent != NULL);
return update_string(&settings->CertificateContent, val, len, cleanup);
case FreeRDP_CertificateFile:
if (cleanup)
free(settings->CertificateFile);
settings->CertificateFile = (val ? strndup(val, len) : NULL);
return (!val || settings->CertificateFile != NULL);
return update_string(&settings->CertificateFile, val, len, cleanup);
case FreeRDP_CertificateName:
if (cleanup)
free(settings->CertificateName);
settings->CertificateName = (val ? strndup(val, len) : NULL);
return (!val || settings->CertificateName != NULL);
return update_string(&settings->CertificateName, val, len, cleanup);
case FreeRDP_ClientAddress:
if (cleanup)
free(settings->ClientAddress);
settings->ClientAddress = (val ? strndup(val, len) : NULL);
return (!val || settings->ClientAddress != NULL);
return update_string(&settings->ClientAddress, val, len, cleanup);
case FreeRDP_ClientDir:
if (cleanup)
free(settings->ClientDir);
settings->ClientDir = (val ? strndup(val, len) : NULL);
return (!val || settings->ClientDir != NULL);
return update_string(&settings->ClientDir, val, len, cleanup);
case FreeRDP_ClientHostname:
if (cleanup)
free(settings->ClientHostname);
settings->ClientHostname = (val ? strndup(val, len) : NULL);
return (!val || settings->ClientHostname != NULL);
return update_string(&settings->ClientHostname, val, len, cleanup);
case FreeRDP_ClientProductId:
if (cleanup)
free(settings->ClientProductId);
settings->ClientProductId = (val ? strndup(val, len) : NULL);
return (!val || settings->ClientProductId != NULL);
return update_string(&settings->ClientProductId, val, len, cleanup);
case FreeRDP_ComputerName:
if (cleanup)
free(settings->ComputerName);
settings->ComputerName = (val ? strndup(val, len) : NULL);
return (!val || settings->ComputerName != NULL);
return update_string(&settings->ComputerName, val, len, cleanup);
case FreeRDP_ConfigPath:
if (cleanup)
free(settings->ConfigPath);
settings->ConfigPath = (val ? strndup(val, len) : NULL);
return (!val || settings->ConfigPath != NULL);
return update_string(&settings->ConfigPath, val, len, cleanup);
case FreeRDP_ConnectionFile:
if (cleanup)
free(settings->ConnectionFile);
settings->ConnectionFile = (val ? strndup(val, len) : NULL);
return (!val || settings->ConnectionFile != NULL);
return update_string(&settings->ConnectionFile, val, len, cleanup);
case FreeRDP_CurrentPath:
if (cleanup)
free(settings->CurrentPath);
settings->CurrentPath = (val ? strndup(val, len) : NULL);
return (!val || settings->CurrentPath != NULL);
return update_string(&settings->CurrentPath, val, len, cleanup);
case FreeRDP_Domain:
if (cleanup)
free(settings->Domain);
settings->Domain = (val ? strndup(val, len) : NULL);
return (!val || settings->Domain != NULL);
return update_string(&settings->Domain, val, len, cleanup);
case FreeRDP_DrivesToRedirect:
if (cleanup)
free(settings->DrivesToRedirect);
settings->DrivesToRedirect = (val ? strndup(val, len) : NULL);
return (!val || settings->DrivesToRedirect != NULL);
return update_string(&settings->DrivesToRedirect, val, len, cleanup);
case FreeRDP_DumpRemoteFxFile:
if (cleanup)
free(settings->DumpRemoteFxFile);
settings->DumpRemoteFxFile = (val ? strndup(val, len) : NULL);
return (!val || settings->DumpRemoteFxFile != NULL);
return update_string(&settings->DumpRemoteFxFile, val, len, cleanup);
case FreeRDP_DynamicDSTTimeZoneKeyName:
if (cleanup)
free(settings->DynamicDSTTimeZoneKeyName);
settings->DynamicDSTTimeZoneKeyName = (val ? strndup(val, len) : NULL);
return (!val || settings->DynamicDSTTimeZoneKeyName != NULL);
return update_string(&settings->DynamicDSTTimeZoneKeyName, val, len, cleanup);
case FreeRDP_GatewayAcceptedCert:
if (cleanup)
free(settings->GatewayAcceptedCert);
settings->GatewayAcceptedCert = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayAcceptedCert != NULL);
return update_string(&settings->GatewayAcceptedCert, val, len, cleanup);
case FreeRDP_GatewayAccessToken:
if (cleanup)
free(settings->GatewayAccessToken);
settings->GatewayAccessToken = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayAccessToken != NULL);
return update_string(&settings->GatewayAccessToken, val, len, cleanup);
case FreeRDP_GatewayDomain:
if (cleanup)
free(settings->GatewayDomain);
settings->GatewayDomain = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayDomain != NULL);
return update_string(&settings->GatewayDomain, val, len, cleanup);
case FreeRDP_GatewayHostname:
if (cleanup)
free(settings->GatewayHostname);
settings->GatewayHostname = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayHostname != NULL);
return update_string(&settings->GatewayHostname, val, len, cleanup);
case FreeRDP_GatewayPassword:
if (cleanup)
free(settings->GatewayPassword);
settings->GatewayPassword = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayPassword != NULL);
return update_string(&settings->GatewayPassword, val, len, cleanup);
case FreeRDP_GatewayUsername:
if (cleanup)
free(settings->GatewayUsername);
settings->GatewayUsername = (val ? strndup(val, len) : NULL);
return (!val || settings->GatewayUsername != NULL);
return update_string(&settings->GatewayUsername, val, len, cleanup);
case FreeRDP_HomePath:
if (cleanup)
free(settings->HomePath);
settings->HomePath = (val ? strndup(val, len) : NULL);
return (!val || settings->HomePath != NULL);
return update_string(&settings->HomePath, val, len, cleanup);
case FreeRDP_ImeFileName:
if (cleanup)
free(settings->ImeFileName);
settings->ImeFileName = (val ? strndup(val, len) : NULL);
return (!val || settings->ImeFileName != NULL);
return update_string(&settings->ImeFileName, val, len, cleanup);
case FreeRDP_KerberosKdc:
if (cleanup)
free(settings->KerberosKdc);
settings->KerberosKdc = (val ? strndup(val, len) : NULL);
return (!val || settings->KerberosKdc != NULL);
return update_string(&settings->KerberosKdc, val, len, cleanup);
case FreeRDP_KerberosRealm:
if (cleanup)
free(settings->KerberosRealm);
settings->KerberosRealm = (val ? strndup(val, len) : NULL);
return (!val || settings->KerberosRealm != NULL);
return update_string(&settings->KerberosRealm, val, len, cleanup);
case FreeRDP_KeyboardRemappingList:
if (cleanup)
free(settings->KeyboardRemappingList);
settings->KeyboardRemappingList = (val ? strndup(val, len) : NULL);
return (!val || settings->KeyboardRemappingList != NULL);
return update_string(&settings->KeyboardRemappingList, val, len, cleanup);
case FreeRDP_NtlmSamFile:
if (cleanup)
free(settings->NtlmSamFile);
settings->NtlmSamFile = (val ? strndup(val, len) : NULL);
return (!val || settings->NtlmSamFile != NULL);
return update_string(&settings->NtlmSamFile, val, len, cleanup);
case FreeRDP_Password:
if (cleanup)
free(settings->Password);
settings->Password = (val ? strndup(val, len) : NULL);
return (!val || settings->Password != NULL);
return update_string(&settings->Password, val, len, cleanup);
case FreeRDP_PasswordHash:
if (cleanup)
free(settings->PasswordHash);
settings->PasswordHash = (val ? strndup(val, len) : NULL);
return (!val || settings->PasswordHash != NULL);
return update_string(&settings->PasswordHash, val, len, cleanup);
case FreeRDP_PlayRemoteFxFile:
if (cleanup)
free(settings->PlayRemoteFxFile);
settings->PlayRemoteFxFile = (val ? strndup(val, len) : NULL);
return (!val || settings->PlayRemoteFxFile != NULL);
return update_string(&settings->PlayRemoteFxFile, val, len, cleanup);
case FreeRDP_PreconnectionBlob:
if (cleanup)
free(settings->PreconnectionBlob);
settings->PreconnectionBlob = (val ? strndup(val, len) : NULL);
return (!val || settings->PreconnectionBlob != NULL);
return update_string(&settings->PreconnectionBlob, val, len, cleanup);
case FreeRDP_PrivateKeyContent:
if (cleanup)
free(settings->PrivateKeyContent);
settings->PrivateKeyContent = (val ? strndup(val, len) : NULL);
return (!val || settings->PrivateKeyContent != NULL);
return update_string(&settings->PrivateKeyContent, val, len, cleanup);
case FreeRDP_PrivateKeyFile:
if (cleanup)
free(settings->PrivateKeyFile);
settings->PrivateKeyFile = (val ? strndup(val, len) : NULL);
return (!val || settings->PrivateKeyFile != NULL);
return update_string(&settings->PrivateKeyFile, val, len, cleanup);
case FreeRDP_ProxyHostname:
if (cleanup)
free(settings->ProxyHostname);
settings->ProxyHostname = (val ? strndup(val, len) : NULL);
return (!val || settings->ProxyHostname != NULL);
return update_string(&settings->ProxyHostname, val, len, cleanup);
case FreeRDP_ProxyPassword:
if (cleanup)
free(settings->ProxyPassword);
settings->ProxyPassword = (val ? strndup(val, len) : NULL);
return (!val || settings->ProxyPassword != NULL);
return update_string(&settings->ProxyPassword, val, len, cleanup);
case FreeRDP_ProxyUsername:
if (cleanup)
free(settings->ProxyUsername);
settings->ProxyUsername = (val ? strndup(val, len) : NULL);
return (!val || settings->ProxyUsername != NULL);
return update_string(&settings->ProxyUsername, val, len, cleanup);
case FreeRDP_RDP2TCPArgs:
if (cleanup)
free(settings->RDP2TCPArgs);
settings->RDP2TCPArgs = (val ? strndup(val, len) : NULL);
return (!val || settings->RDP2TCPArgs != NULL);
return update_string(&settings->RDP2TCPArgs, val, len, cleanup);
case FreeRDP_RdpKeyContent:
if (cleanup)
free(settings->RdpKeyContent);
settings->RdpKeyContent = (val ? strndup(val, len) : NULL);
return (!val || settings->RdpKeyContent != NULL);
return update_string(&settings->RdpKeyContent, val, len, cleanup);
case FreeRDP_RdpKeyFile:
if (cleanup)
free(settings->RdpKeyFile);
settings->RdpKeyFile = (val ? strndup(val, len) : NULL);
return (!val || settings->RdpKeyFile != NULL);
return update_string(&settings->RdpKeyFile, val, len, cleanup);
case FreeRDP_RedirectionAcceptedCert:
if (cleanup)
free(settings->RedirectionAcceptedCert);
settings->RedirectionAcceptedCert = (val ? strndup(val, len) : NULL);
return (!val || settings->RedirectionAcceptedCert != NULL);
return update_string(&settings->RedirectionAcceptedCert, val, len, cleanup);
case FreeRDP_RedirectionDomain:
if (cleanup)
free(settings->RedirectionDomain);
settings->RedirectionDomain = (val ? strndup(val, len) : NULL);
return (!val || settings->RedirectionDomain != NULL);
return update_string(&settings->RedirectionDomain, val, len, cleanup);
case FreeRDP_RedirectionTargetFQDN:
if (cleanup)
free(settings->RedirectionTargetFQDN);
settings->RedirectionTargetFQDN = (val ? strndup(val, len) : NULL);
return (!val || settings->RedirectionTargetFQDN != NULL);
return update_string(&settings->RedirectionTargetFQDN, val, len, cleanup);
case FreeRDP_RedirectionTargetNetBiosName:
if (cleanup)
free(settings->RedirectionTargetNetBiosName);
settings->RedirectionTargetNetBiosName = (val ? strndup(val, len) : NULL);
return (!val || settings->RedirectionTargetNetBiosName != NULL);
return update_string(&settings->RedirectionTargetNetBiosName, val, len, cleanup);
case FreeRDP_RedirectionUsername:
if (cleanup)
free(settings->RedirectionUsername);
settings->RedirectionUsername = (val ? strndup(val, len) : NULL);
return (!val || settings->RedirectionUsername != NULL);
return update_string(&settings->RedirectionUsername, val, len, cleanup);
case FreeRDP_RemoteApplicationCmdLine:
if (cleanup)
free(settings->RemoteApplicationCmdLine);
settings->RemoteApplicationCmdLine = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationCmdLine != NULL);
return update_string(&settings->RemoteApplicationCmdLine, val, len, cleanup);
case FreeRDP_RemoteApplicationFile:
if (cleanup)
free(settings->RemoteApplicationFile);
settings->RemoteApplicationFile = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationFile != NULL);
return update_string(&settings->RemoteApplicationFile, val, len, cleanup);
case FreeRDP_RemoteApplicationGuid:
if (cleanup)
free(settings->RemoteApplicationGuid);
settings->RemoteApplicationGuid = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationGuid != NULL);
return update_string(&settings->RemoteApplicationGuid, val, len, cleanup);
case FreeRDP_RemoteApplicationIcon:
if (cleanup)
free(settings->RemoteApplicationIcon);
settings->RemoteApplicationIcon = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationIcon != NULL);
return update_string(&settings->RemoteApplicationIcon, val, len, cleanup);
case FreeRDP_RemoteApplicationName:
if (cleanup)
free(settings->RemoteApplicationName);
settings->RemoteApplicationName = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationName != NULL);
return update_string(&settings->RemoteApplicationName, val, len, cleanup);
case FreeRDP_RemoteApplicationProgram:
if (cleanup)
free(settings->RemoteApplicationProgram);
settings->RemoteApplicationProgram = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationProgram != NULL);
return update_string(&settings->RemoteApplicationProgram, val, len, cleanup);
case FreeRDP_RemoteApplicationWorkingDir:
if (cleanup)
free(settings->RemoteApplicationWorkingDir);
settings->RemoteApplicationWorkingDir = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteApplicationWorkingDir != NULL);
return update_string(&settings->RemoteApplicationWorkingDir, val, len, cleanup);
case FreeRDP_RemoteAssistancePassStub:
if (cleanup)
free(settings->RemoteAssistancePassStub);
settings->RemoteAssistancePassStub = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteAssistancePassStub != NULL);
return update_string(&settings->RemoteAssistancePassStub, val, len, cleanup);
case FreeRDP_RemoteAssistancePassword:
if (cleanup)
free(settings->RemoteAssistancePassword);
settings->RemoteAssistancePassword = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteAssistancePassword != NULL);
return update_string(&settings->RemoteAssistancePassword, val, len, cleanup);
case FreeRDP_RemoteAssistanceRCTicket:
if (cleanup)
free(settings->RemoteAssistanceRCTicket);
settings->RemoteAssistanceRCTicket = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteAssistanceRCTicket != NULL);
return update_string(&settings->RemoteAssistanceRCTicket, val, len, cleanup);
case FreeRDP_RemoteAssistanceSessionId:
if (cleanup)
free(settings->RemoteAssistanceSessionId);
settings->RemoteAssistanceSessionId = (val ? strndup(val, len) : NULL);
return (!val || settings->RemoteAssistanceSessionId != NULL);
return update_string(&settings->RemoteAssistanceSessionId, val, len, cleanup);
case FreeRDP_ServerHostname:
if (cleanup)
free(settings->ServerHostname);
settings->ServerHostname = (val ? strndup(val, len) : NULL);
return (!val || settings->ServerHostname != NULL);
return update_string(&settings->ServerHostname, val, len, cleanup);
case FreeRDP_ShellWorkingDirectory:
if (cleanup)
free(settings->ShellWorkingDirectory);
settings->ShellWorkingDirectory = (val ? strndup(val, len) : NULL);
return (!val || settings->ShellWorkingDirectory != NULL);
return update_string(&settings->ShellWorkingDirectory, val, len, cleanup);
case FreeRDP_TargetNetAddress:
if (cleanup)
free(settings->TargetNetAddress);
settings->TargetNetAddress = (val ? strndup(val, len) : NULL);
return (!val || settings->TargetNetAddress != NULL);
return update_string(&settings->TargetNetAddress, val, len, cleanup);
case FreeRDP_Username:
if (cleanup)
free(settings->Username);
settings->Username = (val ? strndup(val, len) : NULL);
return (!val || settings->Username != NULL);
return update_string(&settings->Username, val, len, cleanup);
case FreeRDP_WindowTitle:
if (cleanup)
free(settings->WindowTitle);
settings->WindowTitle = (val ? strndup(val, len) : NULL);
return (!val || settings->WindowTitle != NULL);
return update_string(&settings->WindowTitle, val, len, cleanup);
case FreeRDP_WmClass:
if (cleanup)
free(settings->WmClass);
settings->WmClass = (val ? strndup(val, len) : NULL);
return (!val || settings->WmClass != NULL);
return update_string(&settings->WmClass, val, len, cleanup);
default:
WLog_ERR(TAG, "[%s] Invalid key index %" PRIuz, __FUNCTION__, id);

View File

@ -307,10 +307,7 @@ def write_setter_case(f, val, isString, isPointer):
f.write('\t\t\tsettings->' + val + ' = val;\n')
f.write('\t\t\tbreak;\n\n')
else:
f.write('\t\t\tif (cleanup)\n')
f.write('\t\t\t\tfree(settings->' + val + ');\n')
f.write('\t\t\tsettings->' + val + ' = (val ? strndup(val, len) : NULL);\n')
f.write('\t\t\treturn (!val || settings->' + val + ' != NULL);\n\n')
f.write('\t\t\treturn update_string(&settings->' + val + ', val, len, cleanup);\n\n')
def write_setter(f, entry_dict, entry_type, entry_name):
isString = 'string' in entry_name
@ -408,6 +405,19 @@ try:
f.write('#include "../core/settings.h"\n\n')
f.write('#define TAG FREERDP_TAG("common.settings")\n\n')
f.write('static BOOL update_string(char** current, const char* next, size_t next_len, BOOL cleanup)\n')
f.write('{\n')
f.write('\tif (cleanup)\n')
f.write('\t{\n')
f.write('\t\tif (*current)\n')
f.write('\t\t\tmemset(*current, 0, strlen(*current));\n')
f.write('\t\tfree(*current);\n')
f.write('\t}\n')
f.write('\n')
f.write('\t*current = (next ? strndup(next, next_len) : NULL);\n')
f.write('\treturn !next || (*current != NULL);\n')
f.write('}\n\n')
getter_list = dict(type_list)
setter_list = dict(type_list)
write_getter(f, getter_list, 'BOOL', 'bool')