vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / glibc / libio / iofopen.c
blob8cd68a3690f05216fd877521f32518748ebe995f
1 /* Copyright (C) 1993,1997,1998,1999,2000,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
28 #include "libioP.h"
29 #ifdef __STDC__
30 #include <stdlib.h>
31 #include <stddef.h>
32 #endif
33 #ifdef _LIBC
34 # include <shlib-compat.h>
35 #else
36 # define _IO_new_fopen fopen
37 #endif
39 _IO_FILE *
40 __fopen_maybe_mmap (fp)
41 _IO_FILE *fp;
43 #ifdef _G_HAVE_MMAP
44 if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES))
46 /* Since this is read-only, we might be able to mmap the contents
47 directly. We delay the decision until the first read attempt by
48 giving it a jump table containing functions that choose mmap or
49 vanilla file operations and reset the jump table accordingly. */
51 if (fp->_mode <= 0)
52 _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps_maybe_mmap;
53 else
54 _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_wfile_jumps_maybe_mmap;
55 fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap;
57 #endif
58 return fp;
62 _IO_FILE *
63 __fopen_internal (filename, mode, is32)
64 const char *filename;
65 const char *mode;
66 int is32;
68 struct locked_FILE
70 struct _IO_FILE_plus fp;
71 #ifdef _IO_MTSAFE_IO
72 _IO_lock_t lock;
73 #endif
74 struct _IO_wide_data wd;
75 } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
77 if (new_f == NULL)
78 return NULL;
79 #ifdef _IO_MTSAFE_IO
80 new_f->fp.file._lock = &new_f->lock;
81 #endif
82 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
83 _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &INTUSE(_IO_wfile_jumps));
84 #else
85 _IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL);
86 #endif
87 _IO_JUMPS (&new_f->fp) = &INTUSE(_IO_file_jumps);
88 INTUSE(_IO_file_init) (&new_f->fp);
89 #if !_IO_UNIFIED_JUMPTABLES
90 new_f->fp.vtable = NULL;
91 #endif
92 if (INTUSE(_IO_file_fopen) ((_IO_FILE *) new_f, filename, mode, is32)
93 != NULL)
94 return __fopen_maybe_mmap (&new_f->fp.file);
96 INTUSE(_IO_un_link) (&new_f->fp);
97 free (new_f);
98 return NULL;
101 _IO_FILE *
102 _IO_new_fopen (filename, mode)
103 const char *filename;
104 const char *mode;
106 return __fopen_internal (filename, mode, 1);
109 #ifdef _LIBC
110 strong_alias (_IO_new_fopen, __new_fopen)
111 versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
112 versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
113 #endif