From 48730b744197b10b1c498de1b37c67e6f7b23d51 Mon Sep 17 00:00:00 2001 From: Robert Jordan Date: Fri, 28 May 2010 22:02:42 +0000 Subject: [PATCH] 2010-05-28 Robert Jordan * libtest.c: Implement and use marshal_strdup () as a g_strdup () replacement under Windows. Fixes memory allocation mismatches. svn path=/trunk/mono/; revision=158125 --- mono/tests/ChangeLog | 5 +++++ mono/tests/libtest.c | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/mono/tests/ChangeLog b/mono/tests/ChangeLog index e92fc5cf97f..e408f8dee86 100644 --- a/mono/tests/ChangeLog +++ b/mono/tests/ChangeLog @@ -1,3 +1,8 @@ +2010-05-28 Robert Jordan + + * libtest.c: Implement and use marshal_strdup () as a g_strdup () + replacement under Windows. Fixes memory allocation mismatches. + 2010-05-27 Zoltan Varga * interlocked.cs: Add a test for Interlocked.Exchange on a local variable. diff --git a/mono/tests/libtest.c b/mono/tests/libtest.c index 57e8f700f6c..64a9152bfb8 100644 --- a/mono/tests/libtest.c +++ b/mono/tests/libtest.c @@ -53,6 +53,22 @@ static void* marshal_alloc (gsize size) #endif } +static char* marshal_strdup (char *str) +{ +#ifdef WIN32 + int len; + char *buf; + + if (!str) + return NULL; + + len = strlen (str); + buf = (char *) CoTaskMemAlloc (len + 1); + return strcpy (buf, str); +#else + return g_strdup (str); +#endif +} static gunichar2* marshal_bstr_alloc(const gchar* str) { @@ -474,7 +490,7 @@ mono_test_return_string (ReturnStringDelegate func) marshal_free (res); // printf ("got string: %s\n", res); - return g_strdup ("12345"); + return marshal_strdup ("12345"); } typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b); @@ -549,7 +565,7 @@ mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d) ss->a = !ss->a; ss->b = !ss->b; ss->c = !ss->c; - ss->d = g_strdup ("DEF"); + ss->d = marshal_strdup ("DEF"); return res ? 0 : 1; } @@ -654,7 +670,7 @@ mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l) res = g_new0 (simplestruct2, 1); memcpy (res, ss, sizeof (simplestruct2)); - res->d = g_strdup ("TEST"); + res->d = marshal_strdup ("TEST"); return res; } @@ -671,7 +687,7 @@ mono_test_marshal_byref_class (simplestruct2 **ssp) res = g_new0 (simplestruct2, 1); memcpy (res, ss, sizeof (simplestruct2)); - res->d = g_strdup ("TEST-RES"); + res->d = marshal_strdup ("TEST-RES"); *ssp = res; return 0; @@ -1002,7 +1018,7 @@ mono_test_marshal_stringbuilder_out (char **s) const char m[] = "This is my message. Isn't it nice?"; char *str; - str = g_malloc (strlen (m) + 1); + str = marshal_alloc (strlen (m) + 1); memcpy (str, m, strlen (m) + 1); *s = str; @@ -1018,7 +1034,7 @@ mono_test_marshal_stringbuilder_out_unicode (gunichar2 **s) s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL); len = (len * 2) + 2; - *s = g_malloc (len); + *s = marshal_alloc (len); memcpy (*s, s2, len); g_free (s2); @@ -1154,7 +1170,8 @@ mono_test_byvalstr_check (ByValStrStruct* data, char* correctString) // printf ("T1: %s\n", data->a); // printf ("T2: %s\n", correctString); - marshal_free (data); + /* we need g_free because the allocation was performed by mono_test_byvalstr_gen */ + g_free (data); return (ret != 0); } @@ -1295,7 +1312,7 @@ class_marshal_test1 (SimpleObj **obj1) { SimpleObj *res = malloc (sizeof (SimpleObj)); - res->str = g_strdup ("ABC"); + res->str = marshal_strdup ("ABC"); res->i = 5; *obj1 = res; @@ -1326,7 +1343,7 @@ string_marshal_test0 (char *str) LIBTEST_API void STDCALL string_marshal_test1 (const char **str) { - *str = g_strdup ("TEST1"); + *str = marshal_strdup ("TEST1"); } LIBTEST_API int STDCALL @@ -1535,6 +1552,10 @@ mono_test_asany (void *ptr, int what) char *s; s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error); + + if (!s) + return 1; + if (!strcmp (s, "ABC")) { g_free (s); return 0; -- 2.25.1