Skip to content

Commit 96d5379

Browse files
authored
Merge pull request #880 from Unity-Technologies/unity-master-invalid-string-allocations
case 1015822 - Avoid assert and crash on invalid text content
2 parents 9c63172 + f650db8 commit 96d5379

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

mono/metadata/object.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6285,6 +6285,24 @@ mono_string_new_len_checked (MonoDomain *domain, const char *text, guint length,
62856285
return o;
62866286
}
62876287

6288+
static MonoString*
6289+
mono_string_new_internal (MonoDomain *domain, const char *text)
6290+
{
6291+
MonoError error;
6292+
MonoString *res = NULL;
6293+
res = mono_string_new_checked (domain, text, &error);
6294+
if (!is_ok (&error)) {
6295+
/* Mono API compatability: assert on Out of Memory errors,
6296+
* return NULL otherwise (most likely an invalid UTF-8 byte
6297+
* sequence). */
6298+
if (mono_error_get_error_code (&error) == MONO_ERROR_OUT_OF_MEMORY)
6299+
mono_error_assert_ok (&error);
6300+
else
6301+
mono_error_cleanup (&error);
6302+
}
6303+
return res;
6304+
}
6305+
62886306
/**
62896307
* mono_string_new:
62906308
* \param text a pointer to a UTF-8 string
@@ -6295,11 +6313,7 @@ mono_string_new_len_checked (MonoDomain *domain, const char *text, guint length,
62956313
MonoString*
62966314
mono_string_new (MonoDomain *domain, const char *text)
62976315
{
6298-
MonoError error;
6299-
MonoString *res = NULL;
6300-
res = mono_string_new_checked (domain, text, &error);
6301-
mono_error_assert_ok (&error);
6302-
return res;
6316+
return mono_string_new_internal (domain, text);
63036317
}
63046318

63056319
/**
@@ -6372,16 +6386,7 @@ mono_string_new_wrapper (const char *text)
63726386
{
63736387
MONO_REQ_GC_UNSAFE_MODE;
63746388

6375-
MonoDomain *domain = mono_domain_get ();
6376-
6377-
if (text) {
6378-
MonoError error;
6379-
MonoString *result = mono_string_new_checked (domain, text, &error);
6380-
mono_error_assert_ok (&error);
6381-
return result;
6382-
}
6383-
6384-
return NULL;
6389+
return mono_string_new_internal (mono_domain_get (), text);
63856390
}
63866391

63876392
/**

mono/utils/mono-error-internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct _MonoErrorBoxed {
6363
void
6464
mono_error_assert_ok_pos (MonoError *error, const char* filename, int lineno) MONO_LLVM_INTERNAL;
6565

66-
#define mono_error_assert_ok(e) mono_error_assert_ok_pos (e, __FILE__, __LINE__);
66+
#define mono_error_assert_ok(e) mono_error_assert_ok_pos (e, __FILE__, __LINE__)
6767

6868
void
6969
mono_error_dup_strings (MonoError *error, gboolean dup_strings);

0 commit comments

Comments
 (0)