]> git.ipfire.org Git - thirdparty/glibc.git/blame - wcsmbs/wcstok.c
iconv: ISO-2022-CN-EXT: fix out-of-bound writes when writing escape sequence (CVE...
[thirdparty/glibc.git] / wcsmbs / wcstok.c
CommitLineData
dff8da6b 1/* Copyright (C) 1995-2024 Free Software Foundation, Inc.
33a934a3 2 This file is part of the GNU C Library.
a482b5a5 3
33a934a3 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
a482b5a5 8
33a934a3
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
a482b5a5 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
a482b5a5 17
299a95b9 18#include <wchar.h>
a482b5a5
RM
19#include <errno.h>
20
21
299a95b9
RM
22/* Parse WCS into tokens separated by characters in DELIM. If WCS is
23 NULL, the last string wcstok() was called with is used. */
a482b5a5 24wchar_t *
9d46370c 25wcstok (wchar_t *wcs, const wchar_t *delim, wchar_t **save_ptr)
a482b5a5 26{
299a95b9 27 wchar_t *result;
a482b5a5
RM
28
29 if (wcs == NULL)
6e4c40ba
UD
30 {
31 if (*save_ptr == NULL)
32 {
33 __set_errno (EINVAL);
34 return NULL;
35 }
36 else
37 wcs = *save_ptr;
38 }
a482b5a5
RM
39
40 /* Scan leading delimiters. */
41 wcs += wcsspn (wcs, delim);
42 if (*wcs == L'\0')
43 {
59dd8641 44 *save_ptr = NULL;
a482b5a5
RM
45 return NULL;
46 }
47
48 /* Find the end of the token. */
299a95b9
RM
49 result = wcs;
50 wcs = wcspbrk (result, delim);
a482b5a5
RM
51 if (wcs == NULL)
52 /* This token finishes the string. */
59dd8641 53 *save_ptr = NULL;
a482b5a5
RM
54 else
55 {
59dd8641 56 /* Terminate the token and make *SAVE_PTR point past it. */
a482b5a5 57 *wcs = L'\0';
59dd8641 58 *save_ptr = wcs + 1;
a482b5a5 59 }
299a95b9 60 return result;
a482b5a5 61}