Merge pull request #1529 from alistair/xmlreader_read_to_next_sibling_bug
[mono.git] / mono / metadata / file-mmap-posix.c
index 4b7fa7a8bfe50626cd1ad6a63b8fd7f76314085e..27fe27be4ec02c5a8e62c71c9a74af5e668611b2 100644 (file)
@@ -81,8 +81,10 @@ enum {
        MMAP_FILE_ACCESS_READ_WRITE_EXECUTE = 5,
 };
 
-#ifndef DEFFILEMODE
-#define DEFFILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
+#ifdef DEFFILEMODE
+#define DEFAULT_FILEMODE DEFFILEMODE
+#else
+#define DEFAULT_FILEMODE 0666
 #endif
 
 static int mmap_init_state;
@@ -269,7 +271,7 @@ open_file_map (MonoString *path, int input_fd, int mode, gint64 *capacity, int a
        }
 
        if (path) //FIXME use io portability?
-               fd = open (c_path, file_mode_to_unix (mode) | access_mode_to_unix (access), DEFFILEMODE);
+               fd = open (c_path, file_mode_to_unix (mode) | access_mode_to_unix (access), DEFAULT_FILEMODE);
        else
                fd = dup (input_fd);
 
@@ -294,7 +296,7 @@ done:
        return (void*)handle;
 }
 
-#define MONO_ANON_FILE_TEMPLATE "/tmp/mono.anonmap.XXXXXXXXX"
+#define MONO_ANON_FILE_TEMPLATE "/mono.anonmap.XXXXXXXXX"
 static void*
 open_memory_map (MonoString *mapName, int mode, gint64 *capacity, int access, int options, int *error)
 {
@@ -324,7 +326,9 @@ open_memory_map (MonoString *mapName, int mode, gint64 *capacity, int access, in
                //XXX should we ftruncate if the file is smaller than capacity?
        } else {
                int fd;
-               char file_name [sizeof (MONO_ANON_FILE_TEMPLATE) + 1];
+               char *file_name;
+               const char *tmp_dir;
+               int unused G_GNUC_UNUSED, alloc_size;
 
                if (mode == FILE_MODE_OPEN) {
                        *error = FILE_NOT_FOUND;
@@ -332,7 +336,16 @@ open_memory_map (MonoString *mapName, int mode, gint64 *capacity, int access, in
                }
                *capacity = align_up_to_page_size (*capacity);
 
-               strcpy (file_name, MONO_ANON_FILE_TEMPLATE);
+               tmp_dir = g_get_tmp_dir ();
+               alloc_size = strlen (tmp_dir) + strlen (MONO_ANON_FILE_TEMPLATE) + 1;
+               if (alloc_size > 1024) {//rather fail that stack overflow
+                       *error = COULD_NOT_MAP_MEMORY;
+                       goto done;
+               }
+               file_name = alloca (alloc_size);
+               strcpy (file_name, tmp_dir);
+               strcat (file_name, MONO_ANON_FILE_TEMPLATE);
+
                fd = mkstemp (file_name);
                if (fd == -1) {
                        *error = COULD_NOT_MAP_MEMORY;
@@ -340,7 +353,7 @@ open_memory_map (MonoString *mapName, int mode, gint64 *capacity, int access, in
                }
 
                unlink (file_name);
-               ftruncate (fd, (off_t)*capacity);
+               unused = ftruncate (fd, (off_t)*capacity);
 
                handle = g_new0 (MmapHandle, 1);
                handle->ref_count = 1;