[winpr,collections] add generic string clone functions

The ArrayList, HashTable, ... New functions require
void* (*fkt)(const void* ptr) type functions. Provide a generic wrapper
for CHAR and WCHAR strdup to eliminate warnings. Also export a
corresponding free function to avoid issues with runtime
differences.
This commit is contained in:
akallabeth 2024-02-12 10:39:37 +01:00 committed by akallabeth
parent d0dc657412
commit b894199f72
11 changed files with 67 additions and 45 deletions

View File

@ -331,8 +331,8 @@ static IWTSVirtualChannelManager* dvcman_new(drdynvcPlugin* plugin)
if (!dvcman->plugin_names)
goto fail;
obj = ArrayList_Object(dvcman->plugin_names);
obj->fnObjectNew = _strdup;
obj->fnObjectFree = free;
obj->fnObjectNew = winpr_ObjectStringClone;
obj->fnObjectFree = winpr_ObjectStringFree;
dvcman->plugins = ArrayList_New(TRUE);
if (!dvcman->plugins)

View File

@ -187,8 +187,8 @@ BOOL xf_event_action_script_init(xfContext* xfc)
obj = ArrayList_Object(xfc->xevents);
WINPR_ASSERT(obj);
obj->fnObjectNew = _strdup;
obj->fnObjectFree = free;
obj->fnObjectNew = winpr_ObjectStringClone;
obj->fnObjectFree = winpr_ObjectStringFree;
ActionScript = freerdp_settings_get_string(settings, FreeRDP_ActionScript);
sprintf_s(command, sizeof(command), "%s xevent", ActionScript);
actionScript = popen(command, "r");

View File

@ -104,8 +104,8 @@ static BOOL xf_keyboard_action_script_init(xfContext* xfc)
obj = ArrayList_Object(xfc->keyCombinations);
WINPR_ASSERT(obj);
obj->fnObjectNew = _strdup;
obj->fnObjectFree = free;
obj->fnObjectNew = winpr_ObjectStringClone;
obj->fnObjectFree = winpr_ObjectStringFree;
sprintf_s(command, sizeof(command), "%s key", ActionScript);
keyScript = popen(command, "r");

View File

@ -1373,7 +1373,7 @@ static BOOL setup_string(wArrayList* list)
if (!obj)
return FALSE;
obj->fnObjectFree = free;
// obj->fnObjectNew = _strdup;
// obj->fnObjectNew = wwinpr_ObjectStringClone;
return TRUE;
}

View File

@ -134,14 +134,6 @@ static BOOL strings_equals_nocase(const void* obj1, const void* obj2)
return _stricmp(obj1, obj2) == 0;
}
static void* copy_string(const void* ptr)
{
const char* str = ptr;
if (!str)
return NULL;
return _strdup(ptr);
}
HttpContext* http_context_new(void)
{
HttpContext* context = (HttpContext*)calloc(1, sizeof(HttpContext));
@ -157,10 +149,10 @@ HttpContext* http_context_new(void)
if (!key || !value)
goto fail;
key->fnObjectFree = free;
key->fnObjectNew = copy_string;
value->fnObjectFree = free;
value->fnObjectNew = copy_string;
key->fnObjectFree = winpr_ObjectStringFree;
key->fnObjectNew = winpr_ObjectStringClone;
value->fnObjectFree = winpr_ObjectStringFree;
value->fnObjectNew = winpr_ObjectStringClone;
return context;

View File

@ -200,18 +200,6 @@ static BOOL wchar_compare(const void* a, const void* b)
return _wcscmp(wa, wb) == 0;
}
static void* str_copy(const void* ptr)
{
const char* src = ptr;
return _strdup(src);
}
static void* wstr_copy(const void* ptr)
{
const WCHAR* src = ptr;
return _wcsdup(src);
}
static SCardContext* scard_context_new(void)
{
SCardContext* ctx = calloc(1, sizeof(SCardContext));
@ -239,8 +227,8 @@ static SCardContext* scard_context_new(void)
WINPR_ASSERT(val);
key->fnObjectEquals = char_compare;
key->fnObjectNew = str_copy;
key->fnObjectFree = free;
key->fnObjectNew = winpr_ObjectStringClone;
key->fnObjectFree = winpr_ObjectStringFree;
val->fnObjectFree = free;
}
@ -256,8 +244,8 @@ static SCardContext* scard_context_new(void)
WINPR_ASSERT(val);
key->fnObjectEquals = wchar_compare;
key->fnObjectNew = wstr_copy;
key->fnObjectFree = free;
key->fnObjectNew = winpr_ObjectWStringClone;
key->fnObjectFree = winpr_ObjectStringFree;
val->fnObjectFree = free;
}

View File

@ -62,6 +62,11 @@ extern "C"
OBJECT_EQUALS_FN fnObjectEquals;
} wObject;
/* utility function with compatible arguments for string data */
WINPR_API void* winpr_ObjectStringClone(const void* pvstr);
WINPR_API void* winpr_ObjectWStringClone(const void* pvstr);
WINPR_API void winpr_ObjectStringFree(void* pvstr);
/* System.Collections.Queue */
typedef struct s_wQueue wQueue;

View File

@ -189,12 +189,6 @@ static BOOL find_first_file_fail(const char* FilePath)
return FALSE;
}
static void* string_dup(const void* val)
{
const char* str = (const char*)val;
return _strdup(str);
}
static int TestFileFindFirstFileA(const char* str)
{
int rc = -1;
@ -216,8 +210,8 @@ static int TestFileFindFirstFileA(const char* str)
if (!files)
return -3;
wObject* obj = ArrayList_Object(files);
obj->fnObjectFree = free;
obj->fnObjectNew = string_dup;
obj->fnObjectFree = winpr_ObjectStringFree;
obj->fnObjectNew = winpr_ObjectStringClone;
if (!create_layout(BasePath, files))
return -1;

View File

@ -59,6 +59,7 @@ endif()
set(COLLECTIONS_SRCS
collections/Object.c
collections/Queue.c
collections/Stack.c
collections/PubSub.c

View File

@ -96,12 +96,12 @@ UINT32 HashTable_StringHash(const void* key)
void* HashTable_StringClone(const void* str)
{
return _strdup((const char*)str);
return winpr_ObjectStringClone(str);
}
void HashTable_StringFree(void* str)
{
free(str);
winpr_ObjectStringFree(str);
}
static INLINE BOOL HashTable_IsProbablePrime(size_t oddNumber)

View File

@ -0,0 +1,42 @@
/**
* WinPR: Windows Portable Runtime
* Collections
*
* Copyright 2024 Armin Novak <anovak@thincast.com>
* Copyright 2024 Thincast Technologies GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/string.h>
#include <winpr/collections.h>
void* winpr_ObjectStringClone(const void* pvstr)
{
const char* str = pvstr;
if (!str)
return NULL;
return _strdup(str);
}
void* winpr_ObjectWStringClone(const void* pvstr)
{
const WCHAR* str = pvstr;
if (!str)
return NULL;
return _wcsdup(str);
}
void winpr_ObjectStringFree(void* pvstr)
{
free(pvstr);
}