[Mono-list] Mono.Unix.Native.Syscall.readlink memory corruption

Colin JN Breame colin at breame.net
Tue Mar 14 06:33:03 EST 2006


On Tuesday 14 March 2006 00:16, 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()?
>

Look like the default capacity is the capacity of the StringBuilder passed, so 
setting the Length of the buffer seems to solve the problem.  A test for zero 
length would probably be useful:

support/unistd.c:
gint32
Mono_Posix_Syscall_readlink (const char *path, char *buf, mph_size_t len)
{
  int r;
  mph_return_if_size_t_overflow (len);
> if (len <= 0) return -1;
  r = readlink (path, buf, (size_t) len);
  if (r >= 0 && r < len)
    buf [r] = '\0';
  return r;
}

As in the C world we known MAX_PATH, might it not be better to change the 
semantics of the call so that it returns the path?

On a related note:  if a C function mallocs some memory and returns a pointer 
to that memory, does p/invoke free the memory once marshalling is complete?

> Jonathan Gilbert
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list


More information about the Mono-list mailing list