From d53a4f27214f7144e091b9f6ca12aeca449f834d Mon Sep 17 00:00:00 2001 From: Ben Maurer Date: Mon, 24 Jan 2005 16:42:07 +0000 Subject: [PATCH] 2005-01-24 Ben Maurer * marshal.c (mono_string_utf8_to_builder) (mono_string_builder_to_utf16): We might not have ownership of the string. In thise case, we need to create a new buffer. * object-internals.h (mono_stringbuilder_capacity): sb->str might be null, in which case, use the default capacity. svn path=/trunk/mono/; revision=39424 --- mono/metadata/ChangeLog | 8 ++++++++ mono/metadata/marshal.c | 21 +++++++++++++++++++++ mono/metadata/object-internals.h | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mono/metadata/ChangeLog b/mono/metadata/ChangeLog index 7fcf5240a26..313fe276caa 100644 --- a/mono/metadata/ChangeLog +++ b/mono/metadata/ChangeLog @@ -1,3 +1,11 @@ +2005-01-24 Ben Maurer + + * marshal.c (mono_string_utf8_to_builder) + (mono_string_builder_to_utf16): We might not have ownership of the + string. In thise case, we need to create a new buffer. + + * object-internals.h (mono_stringbuilder_capacity): sb->str might + be null, in which case, use the default capacity. Mon Jan 24 16:42:29 CET 2005 Paolo Molaro diff --git a/mono/metadata/marshal.c b/mono/metadata/marshal.c index ad4998ba309..fbf3f68c148 100644 --- a/mono/metadata/marshal.c +++ b/mono/metadata/marshal.c @@ -340,6 +340,11 @@ mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text) items_written = mono_stringbuilder_capacity (sb); if (!error) { + if (! sb->str || sb->str == sb->cached_str) { + sb->str = mono_string_new_size (mono_domain_get (), items_written); + sb->cached_str = NULL; + } + memcpy (mono_string_chars (sb->str), ut, items_written * 2); sb->length = items_written; } else @@ -395,6 +400,22 @@ mono_string_builder_to_utf16 (MonoStringBuilder *sb) if (!sb) return NULL; + /* + * The sb could have been allocated with the default capacity and be empty. + * we need to alloc a buffer of the default capacity in this case. + */ + if (! sb->str) + sb->str = mono_string_new_size (mono_domain_get (), mono_stringbuilder_capacity (sb)); + /* + * The stringbuilder might not have ownership of this string. If this is + * the case, we must duplicate the string, so that we don't munge immutable + * strings + */ + else if (sb->str == sb->cached_str) { + sb->str = mono_string_new_utf16 (mono_domain_get (), mono_string_chars (sb->str), mono_stringbuilder_capacity (sb)); + sb->cached_str = NULL; + } + return mono_string_chars (sb->str); } diff --git a/mono/metadata/object-internals.h b/mono/metadata/object-internals.h index e721f892006..2761cfddb84 100644 --- a/mono/metadata/object-internals.h +++ b/mono/metadata/object-internals.h @@ -76,7 +76,8 @@ }; }G_STMT_END -#define mono_stringbuilder_capacity(sb) ((sb)->str->length) +/* 16 == default capacity */ +#define mono_stringbuilder_capacity(sb) ((sb)->str ? ((sb)->str->length) : 16) typedef struct { MonoObject obj; -- 2.11.4.GIT