]> sourceware.org Git - glibc.git/blame - iconv/gconv.c
syslog: Fix integer overflow in __vsyslog_internal (CVE-2023-6780)
[glibc.git] / iconv / gconv.c
CommitLineData
6973fc01
UD
1/* Convert characters in input buffer using conversion descriptor to
2 output buffer.
dff8da6b 3 Copyright (C) 1997-2024 Free Software Foundation, Inc.
6973fc01 4 This file is part of the GNU C Library.
6973fc01
UD
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
6973fc01
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
6973fc01 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
6973fc01 19
8619129f 20#include <assert.h>
b3fc5f84 21#include <dlfcn.h>
b2b28911 22#include <stddef.h>
915a6c51
UD
23#include <sys/param.h>
24
25#include <gconv_int.h>
88f4b692 26#include <pointer_guard.h>
915a6c51 27
6973fc01
UD
28
29int
d64b6ad0
UD
30__gconv (__gconv_t cd, const unsigned char **inbuf,
31 const unsigned char *inbufend, unsigned char **outbuf,
38677ace 32 unsigned char *outbufend, size_t *irreversible)
6973fc01 33{
eacde9d0 34 size_t last_step;
6973fc01
UD
35 int result;
36
d64b6ad0
UD
37 if (cd == (__gconv_t) -1L)
38 return __GCONV_ILLEGAL_DESCRIPTOR;
e34b0f29 39
eacde9d0
UD
40 last_step = cd->__nsteps - 1;
41
38677ace
UD
42 assert (irreversible != NULL);
43 *irreversible = 0;
6973fc01 44
c63598bf 45 cd->__data[last_step].__outbuf = outbuf != NULL ? *outbuf : NULL;
4a422921
UD
46 cd->__data[last_step].__outbufend = outbufend;
47
915a6c51 48 __gconv_fct fct = cd->__steps->__fct;
915a6c51
UD
49 if (cd->__steps->__shlib_handle != NULL)
50 PTR_DEMANGLE (fct);
915a6c51 51
8619129f 52 if (inbuf == NULL || *inbuf == NULL)
55ea8790
UD
53 {
54 /* We just flush. */
55 result = DL_CALL_FCT (fct,
56 (cd->__steps, cd->__data, NULL, NULL, NULL,
57 irreversible,
58 cd->__data[last_step].__outbuf == NULL ? 2 : 1,
59 0));
60
61 /* If the flush was successful clear the rest of the state. */
62 if (result == __GCONV_OK)
63 for (size_t cnt = 0; cnt <= last_step; ++cnt)
64 cd->__data[cnt].__invocation_counter = 0;
65 }
8619129f
UD
66 else
67 {
e573146a 68 const unsigned char *last_start;
6973fc01 69
8619129f 70 assert (outbuf != NULL && *outbuf != NULL);
6973fc01 71
8619129f
UD
72 do
73 {
8619129f 74 last_start = *inbuf;
915a6c51 75 result = DL_CALL_FCT (fct,
55985355 76 (cd->__steps, cd->__data, inbuf, inbufend,
f1d5c60d 77 NULL, irreversible, 0, 0));
8619129f 78 }
d64b6ad0
UD
79 while (result == __GCONV_EMPTY_INPUT && last_start != *inbuf
80 && *inbuf + cd->__steps->__min_needed_from <= inbufend);
4bca4c17 81 }
6973fc01 82
8619129f 83 if (outbuf != NULL && *outbuf != NULL)
d64b6ad0 84 *outbuf = cd->__data[last_step].__outbuf;
8619129f 85
6973fc01
UD
86 return result;
87}
This page took 0.562513 seconds and 5 git commands to generate.