mirror of
https://sourceware.org/git/glibc.git
synced 2025-07-29 11:41:21 +03:00
Update.
* libio/fmemopen.c (fmemopen_seek): SEEK_END should count from maximum used address, not maximum buffer position.
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
2005-01-05 Ulrich Drepper <drepper@redhat.com>
|
2005-01-05 Ulrich Drepper <drepper@redhat.com>
|
||||||
|
|
||||||
|
* libio/fmemopen.c (fmemopen_seek): SEEK_END should count from
|
||||||
|
maximum used address, not maximum buffer position.
|
||||||
|
|
||||||
* libio/iofopncook.c (_IO_cookie_seekoff): Define. Mark offset as
|
* libio/iofopncook.c (_IO_cookie_seekoff): Define. Mark offset as
|
||||||
invalid to disable optimizations in fileops which won't work here.
|
invalid to disable optimizations in fileops which won't work here.
|
||||||
(_IO_cookie_jumps): Use it.
|
(_IO_cookie_jumps): Use it.
|
||||||
|
@ -164,7 +164,7 @@ fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case SEEK_END:
|
case SEEK_END:
|
||||||
np = c->size - *p;
|
np = c->maxpos - *p;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -201,6 +201,13 @@ fmemopen (void *buf, size_t len, const char *mode)
|
|||||||
cookie_io_functions_t iof;
|
cookie_io_functions_t iof;
|
||||||
fmemopen_cookie_t *c;
|
fmemopen_cookie_t *c;
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
einval:
|
||||||
|
__set_errno (EINVAL);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
|
c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
|
||||||
if (c == NULL)
|
if (c == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -218,7 +225,12 @@ fmemopen (void *buf, size_t len, const char *mode)
|
|||||||
c->buffer[0] = '\0';
|
c->buffer[0] = '\0';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
c->buffer = buf;
|
{
|
||||||
|
if ((uintptr_t) len > -(uintptr_t) buf)
|
||||||
|
goto einval;
|
||||||
|
|
||||||
|
c->buffer = buf;
|
||||||
|
}
|
||||||
|
|
||||||
c->size = len;
|
c->size = len;
|
||||||
|
|
||||||
|
67
stdio-common/tst-fmemopen2.c
Normal file
67
stdio-common/tst-fmemopen2.c
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
do_test (void)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
char buf[100];
|
||||||
|
FILE *fp = fmemopen (buf, sizeof (buf), "w");
|
||||||
|
if (fp == NULL)
|
||||||
|
{
|
||||||
|
puts ("fmemopen failed");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
static const char str[] = "hello world";
|
||||||
|
#define nstr (sizeof (str) - 1)
|
||||||
|
fputs (str, fp);
|
||||||
|
off_t o = ftello (fp);
|
||||||
|
if (o != nstr)
|
||||||
|
{
|
||||||
|
printf ("first ftello returned %ld, expected %zu\n", o, nstr);
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
rewind (fp);
|
||||||
|
o = ftello (fp);
|
||||||
|
if (o != 0)
|
||||||
|
{
|
||||||
|
printf ("second ftello returned %ld, expected %zu\n", o, 0);
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
if (fseeko (fp, 0, SEEK_END) != 0)
|
||||||
|
{
|
||||||
|
puts ("fseeko failed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
o = ftello (fp);
|
||||||
|
if (o != nstr)
|
||||||
|
{
|
||||||
|
printf ("third ftello returned %ld, expected %zu\n", o, nstr);
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
rewind (fp);
|
||||||
|
static const char str2[] = "just hello";
|
||||||
|
#define nstr2 (sizeof (str2) - 1)
|
||||||
|
assert (nstr2 < nstr);
|
||||||
|
fputs (str2, fp);
|
||||||
|
o = ftello (fp);
|
||||||
|
if (o != nstr2)
|
||||||
|
{
|
||||||
|
printf ("fourth ftello returned %ld, expected %zu\n", o, nstr2);
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
fclose (fp);
|
||||||
|
static const char str3[] = "just hellod";
|
||||||
|
if (strcmp (buf, str3) != 0)
|
||||||
|
{
|
||||||
|
printf ("final string is \"%s\", expected \"%s\"\n",
|
||||||
|
buf, str3);
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST_FUNCTION do_test ()
|
||||||
|
#include "../test-skeleton.c"
|
Reference in New Issue
Block a user