Blame libio/tst-ftell-partial-wide.c

Packit 6c4009
/* Verify that ftell does not go into an infinite loop when a conversion fails
Packit 6c4009
   due to insufficient space in the buffer.
Packit 6c4009
   Copyright (C) 2014-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
/* Arbitrary number large enough so that the target buffer during conversion is
Packit 6c4009
   not large enough.  */
Packit 6c4009
#define STRING_SIZE (1400)
Packit 6c4009
#define NSTRINGS (2)
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  FILE *fp = NULL;
Packit 6c4009
  wchar_t *inputs[NSTRINGS] = {NULL};
Packit 6c4009
  int ret = 1;
Packit 6c4009
Packit 6c4009
  if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot set en_US.UTF-8 locale.\n");
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Generate input from one character, chosen because it has an odd number of
Packit 6c4009
     bytes in UTF-8, making it easier to reproduce the problem:
Packit 6c4009
Packit 6c4009
     NAME    Hiragana letter GO
Packit 6c4009
     CHAR    ご
Packit 6c4009
     UTF-8   E38194
Packit 6c4009
     UCS     3054
Packit 6c4009
     MARC-8  692434  */
Packit 6c4009
  wchar_t seed = L'ご';
Packit 6c4009
  for (int i = 0; i < NSTRINGS; i++)
Packit 6c4009
    {
Packit 6c4009
      inputs[i] = malloc (STRING_SIZE * sizeof (wchar_t));
Packit 6c4009
      if (inputs[i] == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Failed to allocate memory for inputs: %m\n");
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
      wmemset (inputs[i], seed, STRING_SIZE - 1);
Packit 6c4009
      inputs[i][STRING_SIZE - 1] = L'\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char *filename;
Packit 6c4009
  int fd = create_temp_file ("tst-fseek-wide-partial.out", &filename);
Packit 6c4009
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("create_temp_file: %m\n");
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fp = fdopen (fd, "w+");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("fopen: %m\n");
Packit 6c4009
      close (fd);
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < NSTRINGS; i++)
Packit 6c4009
    {
Packit 6c4009
      printf ("offset: %ld\n", ftell (fp));
Packit 6c4009
      if (fputws (inputs[i], fp) == -1)
Packit 6c4009
	{
Packit 6c4009
	  perror ("fputws");
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  ret = 0;
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  if (fp != NULL)
Packit 6c4009
    fclose (fp);
Packit 6c4009
  for (int i = 0; i < NSTRINGS; i++)
Packit 6c4009
    free (inputs[i]);
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}