On Mon, 2006-03-13 at 18:16 -0600, Jonathan Gilbert wrote:
> At 04:12 PM 13/03/2006 -0500, Gonzalo Paniagua Javier wrote:
> >On Mon, 2006-03-13 at 17:32 +0000, Colin JN Breame wrote:
> >> using System.Text;
> >> public class main_t {
> >>   public static void Main() {
> >>     for (int i=0; i<10000; i++) {
> >>       StringBuilder buf = new StringBuilder();
> >>       Mono.Unix.Native.Syscall.readlink("path/to/link/file", buf);
> >>     }
> >>   }
> >> } 
> >
> >Confirmed. I get the same error.
> 
> Don't you need to give the StringBuilder a capacity first? Is the default
> capacity documented, and documented to be large enough for the return value
> of readlink()?

Yes, you are supposed to pass a StringBuilder with the appropiate
capacity, but in any case there should not be a memory corruption.

Here's a patch that fixes the corruption problem. May I commit?

-Gonzalo

Index: marshal.c
===================================================================
--- marshal.c	(revision 57893)
+++ marshal.c	(working copy)
@@ -375,6 +375,7 @@
 	guint16 *ut;
 	glong items_written;
 	int l;
+	gboolean need_nul = FALSE;
 
 	if (!sb || !text)
 		return;
@@ -383,16 +384,21 @@
 
 	ut = g_utf8_to_utf16 (text, l, NULL, &items_written, &error);
 	
-	if (items_written > mono_stringbuilder_capacity (sb))
+	if (items_written > mono_stringbuilder_capacity (sb)) {
 		items_written = mono_stringbuilder_capacity (sb);
+		need_nul = TRUE;
+	}
 	
 	if (!error) {
 		if (! sb->str || sb->str == sb->cached_str) {
-			sb->str = mono_string_new_size (mono_domain_get (), items_written);
+			glong length = items_written + (need_nul) ? 1 : 0;
+			sb->str = mono_string_new_size (mono_domain_get (), length);
 			sb->cached_str = NULL;
 		}
 		
 		memcpy (mono_string_chars (sb->str), ut, items_written * 2);
+		if (need_nul)
+			mono_string_chars (sb->str) [items_written] = 0;
 		sb->length = items_written;
 	} else 
 		g_error_free (error);
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to