/* Fmemopen implementation.
- Copyright (C) 2000, 2002 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2002, 2005, 2006 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
* but couldn't find it in libio. The following snippet of code is an
* attempt to implement what glibc's documentation describes.
*
- * No, it isn't really tested yet. :-)
- *
*
*
* I already see some potential problems:
#include <libio.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "libioP.h"
if ((size_t) (c->pos + addnullc) == c->size)
{
__set_errno (ENOSPC);
- return -1;
+ return 0;
}
s = c->size - c->pos - addnullc;
}
break;
case SEEK_END:
- np = c->size - *p;
+ np = c->maxpos - *p;
break;
default:
if (np < 0 || (size_t) np > c->size)
return -1;
- c->pos = np;
+ *p = c->pos = np;
- return np;
+ return 0;
}
cookie_io_functions_t iof;
fmemopen_cookie_t *c;
+ if (__builtin_expect (len == 0, 0))
+ {
+ einval:
+ __set_errno (EINVAL);
+ return NULL;
+ }
+
c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
if (c == NULL)
return NULL;
c->buffer[0] = '\0';
}
else
- c->buffer = buf;
+ {
+ if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0))
+ {
+ free (c);
+ goto einval;
+ }
+
+ c->buffer = buf;
+ }
c->size = len;