walnux/libs/libc/wchar/lib_wcsrtombs.c
Tiago Medicci Serrano fdc0b608b5 libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard
According to https://pubs.opengroup.org/onlinepubs/9799919799/, the
expected behavior for the wcstombs() function is that it should
convert the wide-character codes stoping conversion either when a
character sequence exceeds the limit of n total bytes or if a null
byte is stored. In the first case, the null-terminated value should
not be appended to the dst buffer.

Currently, no null-terminator is appended to the dst buffer, even
when it's expected to be appended, generating erroneous output.
2025-02-06 10:24:55 +01:00

53 lines
1.9 KiB
C

/****************************************************************************
* libs/libc/wchar/lib_wcsrtombs.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <wchar.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: wcsrtombs
*
* Description:
* Convert a wide-characterto a multibyte string string
****************************************************************************/
size_t wcsrtombs(FAR char *dst, FAR const wchar_t **src,
size_t len, FAR mbstate_t *ps)
{
size_t ret;
ret = wcsnrtombs(dst, src, SIZE_MAX, len, ps);
if (dst != NULL && ret != (size_t)-1 && ret != len)
{
dst[ret] = '\0';
}
return ret;
}