Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[BZ #1996]
2006-08-14  Ulrich Drepper  <drepper@redhat.com>
	[BZ #1996]
	* libio/memstream.c (open_memstream): Allocate initial buffer with
	calloc.
	* libio/wmemstream.c (open_wmemstream): Likewise.
	* libio/strops.c: Pretty printing.
	(_IO_str_overflow): Clear uninitialized part of the new buffer.
	(enlarge_userbuf): New function.
	(_IO_str_seekoff): Call it if seek position is larger than current
	buffer.
	* libio/wstrops.c: Likewise.
	* libio/vasprintf.c: Add comment as to why we do not have to use
	calloc instead of malloc to allocate initial buffer.
	* libio/Makefile (tests): Add bug-memstream1 and bug-wmemstream1.
	* libio/bug-memstream1.c: New file.
	* libio/bug-wmemstream1.c: New file.
  • Loading branch information
Ulrich Drepper committed Aug 14, 2006
1 parent 8999373 commit 107b8a9
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 22 deletions.
18 changes: 18 additions & 0 deletions ChangeLog
@@ -1,3 +1,21 @@
2006-08-14 Ulrich Drepper <drepper@redhat.com>

[BZ #1996]
* libio/memstream.c (open_memstream): Allocate initial buffer with
calloc.
* libio/wmemstream.c (open_wmemstream): Likewise.
* libio/strops.c: Pretty printing.
(_IO_str_overflow): Clear uninitialized part of the new buffer.
(enlarge_userbuf): New function.
(_IO_str_seekoff): Call it if seek position is larger than current
buffer.
* libio/wstrops.c: Likewise.
* libio/vasprintf.c: Add comment as to why we do not have to use
calloc instead of malloc to allocate initial buffer.
* libio/Makefile (tests): Add bug-memstream1 and bug-wmemstream1.
* libio/bug-memstream1.c: New file.
* libio/bug-wmemstream1.c: New file.

2006-08-13 Ulrich Drepper <drepper@redhat.com>

* libio/wstrops.c: Remove dead macro definitions and comments.
Expand Down
3 changes: 2 additions & 1 deletion libio/Makefile
Expand Up @@ -56,7 +56,8 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \
tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \
tst-memstream1 tst-memstream2 \
tst-wmemstream1 tst-wmemstream2
tst-wmemstream1 tst-wmemstream2 \
bug-memstream1 bug-wmemstream1
test-srcs = test-freopen

all: # Make this the default target; it will be defined in Rules.
Expand Down
133 changes: 133 additions & 0 deletions libio/bug-memstream1.c
@@ -0,0 +1,133 @@
#include <stdio.h>
#include <string.h>


static int
do_test (void)
{
size_t size;
char *buf;
FILE *fp = open_memstream (&buf, &size);
if (fp == NULL)
{
puts ("open_memstream failed");
return 1;
}

off64_t off = ftello64 (fp);
if (off != 0)
{
puts ("initial position wrong");
return 1;
}

if (fseek (fp, 32768, SEEK_SET) != 0)
{
puts ("fseek failed");
return 1;
}

if (fputs ("foo", fp) == EOF)
{
puts ("fputs failed");
return 1;
}

if (fclose (fp) == EOF)
{
puts ("fclose failed");
return 1;
}

if (size != 32768 + 3)
{
printf ("expected size %d, got %zu\n", 32768 + 3, size);
return 1;
}

for (int i = 0; i < 32768; ++i)
if (buf[i] != '\0')
{
printf ("byte at offset %d is %#hhx\n", i, buf[i]);
return 1;
}

if (memcmp (buf + 32768, "foo", 3) != 0)
{
puts ("written string incorrect");
return 1;
}

/* Mark the buffer. */
memset (buf, 'A', size);
free (buf);

/* Try again, this time with write mode enabled before the seek. */
fp = open_memstream (&buf, &size);
if (fp == NULL)
{
puts ("2nd open_memstream failed");
return 1;
}

off = ftello64 (fp);
if (off != 0)
{
puts ("2nd initial position wrong");
return 1;
}

if (fputs ("bar", fp) == EOF)
{
puts ("2nd fputs failed");
return 1;
}

if (fseek (fp, 32768, SEEK_SET) != 0)
{
puts ("2nd fseek failed");
return 1;
}

if (fputs ("foo", fp) == EOF)
{
puts ("3rd fputs failed");
return 1;
}

if (fclose (fp) == EOF)
{
puts ("2nd fclose failed");
return 1;
}

if (size != 32768 + 3)
{
printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
return 1;
}

if (memcmp (buf, "bar", 3) != 0)
{
puts ("initial string incorrect in 2nd try");
return 1;
}

for (int i = 3; i < 32768; ++i)
if (buf[i] != '\0')
{
printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
return 1;
}

if (memcmp (buf + 32768, "foo", 3) != 0)
{
puts ("written string incorrect in 2nd try");
return 1;
}

return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"
135 changes: 135 additions & 0 deletions libio/bug-wmemstream1.c
@@ -0,0 +1,135 @@
#include <stdio.h>
#include <string.h>


static int
do_test (void)
{
size_t size;
wchar_t *buf;
FILE *fp = open_wmemstream (&buf, &size);
if (fp == NULL)
{
puts ("open_wmemstream failed");
return 1;
}

off64_t off = ftello64 (fp);
if (off != 0)
{
puts ("initial position wrong");
return 1;
}

if (fseek (fp, 32768, SEEK_SET) != 0)
{
puts ("fseek failed");
return 1;
}

if (fputws (L"foo", fp) == EOF)
{
puts ("fputws failed");
return 1;
}

if (fclose (fp) == EOF)
{
puts ("fclose failed");
return 1;
}

if (size != 32768 + 3)
{
printf ("expected size %d, got %zu\n", 32768 + 3, size);
return 1;
}

for (int i = 0; i < 32768; ++i)
if (buf[i] != L'\0')
{
printf ("wide character at offset %d is %#x\n",
i, (unsigned int) buf[i]);
return 1;
}

if (wmemcmp (buf + 32768, L"foo", 3) != 0)
{
puts ("written string incorrect");
return 1;
}

/* Mark the buffer. */
wmemset (buf, L'A', size);
free (buf);

/* Try again, this time with write mode enabled before the seek. */
fp = open_wmemstream (&buf, &size);
if (fp == NULL)
{
puts ("2nd open_wmemstream failed");
return 1;
}

off = ftello64 (fp);
if (off != 0)
{
puts ("2nd initial position wrong");
return 1;
}

if (fputws (L"bar", fp) == EOF)
{
puts ("2nd fputws failed");
return 1;
}

if (fseek (fp, 32768, SEEK_SET) != 0)
{
puts ("2nd fseek failed");
return 1;
}

if (fputws (L"foo", fp) == EOF)
{
puts ("3rd fputws failed");
return 1;
}

if (fclose (fp) == EOF)
{
puts ("2nd fclose failed");
return 1;
}

if (size != 32768 + 3)
{
printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
return 1;
}

if (wmemcmp (buf, L"bar", 3) != 0)
{
puts ("initial string incorrect in 2nd try");
return 1;
}

for (int i = 3; i < 32768; ++i)
if (buf[i] != L'\0')
{
printf ("wide character at offset %d is %#x in 2nd try\n",
i, (unsigned int) buf[i]);
return 1;
}

if (wmemcmp (buf + 32768, L"foo", 3) != 0)
{
puts ("written string incorrect in 2nd try");
return 1;
}

return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"
2 changes: 1 addition & 1 deletion libio/memstream.c
Expand Up @@ -83,7 +83,7 @@ open_memstream (bufloc, sizeloc)
new_f->fp._sf._sbf._f._lock = &new_f->lock;
#endif

buf = malloc (_IO_BUFSIZ);
buf = calloc (1, _IO_BUFSIZ);
if (buf == NULL)
return NULL;
INTUSE(_IO_init) (&new_f->fp._sf._sbf._f, 0);
Expand Down

0 comments on commit 107b8a9

Please sign in to comment.