re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / libio / fmemopen.c
blobd3750fc7de9ca358d8d8f75eb13e75ee5b55727f
1 /* Fmemopen implementation.
2 Copyright (C) 2000, 2002, 2005, 2006, 2008, 2009
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
23 * fmemopen() - "my" version of a string stream
24 * Hanno Mueller, kontakt@hanno.de
27 * I needed fmemopen() for an application that I currently work on,
28 * but couldn't find it in libio. The following snippet of code is an
29 * attempt to implement what glibc's documentation describes.
33 * I already see some potential problems:
35 * - I never used the "original" fmemopen(). I am sure that "my"
36 * fmemopen() behaves differently than the original version.
38 * - The documentation doesn't say wether a string stream allows
39 * seeks. I checked the old fmemopen implementation in glibc's stdio
40 * directory, wasn't quite able to see what is going on in that
41 * source, but as far as I understand there was no seek there. For
42 * my application, I needed fseek() and ftell(), so it's here.
44 * - "append" mode and fseek(p, SEEK_END) have two different ideas
45 * about the "end" of the stream.
47 * As described in the documentation, when opening the file in
48 * "append" mode, the position pointer will be set to the first null
49 * character of the string buffer (yet the buffer may already
50 * contain more data). For fseek(), the last byte of the buffer is
51 * used as the end of the stream.
53 * - It is unclear to me what the documentation tries to say when it
54 * explains what happens when you use fmemopen with a NULL
55 * buffer.
57 * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
58 * is really only useful if you are going to write things to the
59 * buffer and then read them back in again."
61 * What does that mean if the original fmemopen() did not allow
62 * seeking? How do you read what you just wrote without seeking back
63 * to the beginning of the stream?
65 * - I think there should be a second version of fmemopen() that does
66 * not add null characters for each write. (At least in my
67 * application, I am not actually using strings but binary data and
68 * so I don't need the stream to add null characters on its own.)
71 #include <errno.h>
72 #include <libio.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <stdint.h>
76 #include <string.h>
77 #include <sys/types.h>
78 #include "libioP.h"
81 typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
82 struct fmemopen_cookie_struct
84 char *buffer;
85 int mybuffer;
86 int binmode;
87 size_t size;
88 _IO_off64_t pos;
89 size_t maxpos;
93 static ssize_t
94 fmemopen_read (void *cookie, char *b, size_t s)
96 fmemopen_cookie_t *c;
98 c = (fmemopen_cookie_t *) cookie;
100 if (c->pos + s > c->size)
102 if ((size_t) c->pos == c->size)
103 return 0;
104 s = c->size - c->pos;
107 memcpy (b, &(c->buffer[c->pos]), s);
109 c->pos += s;
110 if ((size_t) c->pos > c->maxpos)
111 c->maxpos = c->pos;
113 return s;
117 static ssize_t
118 fmemopen_write (void *cookie, const char *b, size_t s)
120 fmemopen_cookie_t *c;
121 int addnullc;
123 c = (fmemopen_cookie_t *) cookie;
125 addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0');
127 if (c->pos + s + addnullc > c->size)
129 if ((size_t) (c->pos + addnullc) == c->size)
131 __set_errno (ENOSPC);
132 return 0;
134 s = c->size - c->pos - addnullc;
137 memcpy (&(c->buffer[c->pos]), b, s);
139 c->pos += s;
140 if ((size_t) c->pos > c->maxpos)
142 c->maxpos = c->pos;
143 if (addnullc)
144 c->buffer[c->maxpos] = '\0';
147 return s;
151 static int
152 fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
154 _IO_off64_t np;
155 fmemopen_cookie_t *c;
157 c = (fmemopen_cookie_t *) cookie;
159 switch (w)
161 case SEEK_SET:
162 np = *p;
163 break;
165 case SEEK_CUR:
166 np = c->pos + *p;
167 break;
169 case SEEK_END:
170 np = (c->binmode ? c->size : c->maxpos) - *p;
171 break;
173 default:
174 return -1;
177 if (np < 0 || (size_t) np > c->size)
178 return -1;
180 *p = c->pos = np;
182 return 0;
186 static int
187 fmemopen_close (void *cookie)
189 fmemopen_cookie_t *c;
191 c = (fmemopen_cookie_t *) cookie;
193 if (c->mybuffer)
194 free (c->buffer);
195 free (c);
197 return 0;
201 FILE *
202 fmemopen (void *buf, size_t len, const char *mode)
204 cookie_io_functions_t iof;
205 fmemopen_cookie_t *c;
207 if (__builtin_expect (len == 0, 0))
209 einval:
210 __set_errno (EINVAL);
211 return NULL;
214 c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
215 if (c == NULL)
216 return NULL;
218 c->mybuffer = (buf == NULL);
220 if (c->mybuffer)
222 c->buffer = (char *) malloc (len);
223 if (c->buffer == NULL)
225 free (c);
226 return NULL;
228 c->buffer[0] = '\0';
230 else
232 if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0))
234 free (c);
235 goto einval;
238 c->buffer = buf;
241 c->size = len;
243 if (mode[0] == 'w')
244 c->buffer[0] = '\0';
246 c->maxpos = strlen (c->buffer);
248 if (mode[0] == 'a')
249 c->pos = c->maxpos;
250 else
251 c->pos = 0;
253 c->binmode = mode[0] != '\0' && mode[1] == 'b';
255 iof.read = fmemopen_read;
256 iof.write = fmemopen_write;
257 iof.seek = fmemopen_seek;
258 iof.close = fmemopen_close;
260 return _IO_fopencookie (c, mode, iof);
262 libc_hidden_def (fmemopen)