(__wcsrtombs): When computing maximum number of wide characters to convert do

it right and don't overestimate by multiplying with MB_CUR_MAX.
This commit is contained in:
Ulrich Drepper 1999-07-28 04:36:21 +00:00
parent eb23f97ef0
commit 0e42499f6f

View File

@ -1,6 +1,6 @@
/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
/* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@ -40,19 +40,23 @@ __wcsrtombs (dst, src, len, ps)
size_t len;
mbstate_t *ps;
{
struct gconv_step_data data;
struct __gconv_step_data data;
int status;
size_t result;
struct __gconv_step *tomb;
/* Tell where we want the result. */
data.invocation_counter = 0;
data.internal_use = 1;
data.is_last = 1;
data.statep = ps ?: &state;
data.__invocation_counter = 0;
data.__internal_use = 1;
data.__is_last = 1;
data.__statep = ps ?: &state;
/* Make sure we use the correct function. */
update_conversion_ptrs ();
/* Get the structure with the function pointers. */
tomb = __wcsmbs_gconv_fcts.tomb;
/* We have to handle DST == NULL special. */
if (dst == NULL)
{
@ -62,27 +66,25 @@ __wcsrtombs (dst, src, len, ps)
size_t dummy;
result = 0;
data.outbufend = buf + sizeof (buf);
data.__outbufend = buf + sizeof (buf);
do
{
data.outbuf = buf;
data.__outbuf = buf;
status = (*__wcsmbs_gconv_fcts.tomb->fct) (__wcsmbs_gconv_fcts.tomb,
&data,
(const unsigned char **) &inbuf,
(const unsigned char *) srcend,
&dummy, 0);
status = (*tomb->__fct) (__wcsmbs_gconv_fcts.tomb, &data,
(const unsigned char **) &inbuf,
(const unsigned char *) srcend, &dummy, 0);
/* Count the number of bytes. */
result += data.outbuf - buf;
result += data.__outbuf - buf;
}
while (status == GCONV_FULL_OUTPUT);
while (status == __GCONV_FULL_OUTPUT);
if (status == GCONV_OK || status == GCONV_EMPTY_INPUT)
if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
{
/* There better should be a NUL byte at the end. */
assert (data.outbuf[-1] == '\0');
assert (data.__outbuf[-1] == '\0');
/* Don't count the NUL character in. */
--result;
}
@ -92,29 +94,27 @@ __wcsrtombs (dst, src, len, ps)
/* This code is based on the safe assumption that all internal
multi-byte encodings use the NUL byte only to mark the end
of the string. */
const wchar_t *srcend = *src + __wcsnlen (*src, len * MB_CUR_MAX) + 1;
const wchar_t *srcend = *src + __wcsnlen (*src, len) + 1;
size_t dummy;
data.outbuf = dst;
data.outbufend = dst + len;
data.__outbuf = dst;
data.__outbufend = dst + len;
status = (*__wcsmbs_gconv_fcts.tomb->fct) (__wcsmbs_gconv_fcts.tomb,
&data,
(const unsigned char **) src,
(const unsigned char *) srcend,
&dummy, 0);
status = (*tomb->__fct) (__wcsmbs_gconv_fcts.tomb, &data,
(const unsigned char **) src,
(const unsigned char *) srcend, &dummy, 0);
/* Count the number of bytes. */
result = data.outbuf - (unsigned char *) dst;
result = data.__outbuf - (unsigned char *) dst;
/* We have to determine whether the last character converted
is the NUL character. */
if ((status == GCONV_OK || status == GCONV_EMPTY_INPUT
|| status == GCONV_FULL_OUTPUT)
&& data.outbuf[-1] == '\0')
if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
|| status == __GCONV_FULL_OUTPUT)
&& data.__outbuf[-1] == '\0')
{
assert (data.outbuf != (unsigned char *) dst);
assert (__mbsinit (data.statep));
assert (data.__outbuf != (unsigned char *) dst);
assert (__mbsinit (data.__statep));
*src = NULL;
--result;
}
@ -122,12 +122,13 @@ __wcsrtombs (dst, src, len, ps)
/* There must not be any problems with the conversion but illegal input
characters. */
assert (status == GCONV_OK || status == GCONV_EMPTY_INPUT
|| status == GCONV_ILLEGAL_INPUT
|| status == GCONV_INCOMPLETE_INPUT || status == GCONV_FULL_OUTPUT);
assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
|| status == __GCONV_ILLEGAL_INPUT
|| status == __GCONV_INCOMPLETE_INPUT
|| status == __GCONV_FULL_OUTPUT);
if (status != GCONV_OK && status != GCONV_FULL_OUTPUT
&& status != GCONV_EMPTY_INPUT)
if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
&& status != __GCONV_EMPTY_INPUT)
{
result = (size_t) -1;
__set_errno (EILSEQ);