• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include <locale.h>
24 #include <math.h>
25 #include <float.h>
26 #include <string.h>
27 #include "functionalext.h"
28 #include <iconv.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 
test_euc_kr_error(void)33 int test_euc_kr_error(void)
34 {
35     iconv_t cd = iconv_open("UTF-8", "EUC-KR");
36     if (cd == (iconv_t)-1) {
37         perror("iconv_open");
38         return -1;
39     }
40     // 测试输入:无效的EUC-KR序列 "\xC8\x41"
41     char input[] = "\xC8\x41"; // 第二个字节 0x41 无效
42     size_t in_len = 2;         // 明确指定输入长度(避免依赖字符串终止符)
43     // 分配输出缓冲区(足够容纳可能的转换结果)
44     size_t outbuf_size = 6; // EUC-KR最多转3字节/字符,2字符则预留6字节
45     char *outbuf = malloc(outbuf_size);
46     if (!outbuf) {
47         perror("malloc failed");
48         iconv_close(cd);
49         return -1;
50     }
51     char *in_ptr = input;
52     size_t in_left = in_len;
53     char *out_ptr = outbuf;
54     size_t out_left = outbuf_size;
55 
56     // 执行转换
57     size_t ret = iconv(cd, &in_ptr, &in_left, &out_ptr, &out_left);
58     if (ret == (size_t)-1) {
59         if (errno != EILSEQ) {
60             t_error("test failed expect errno=EILSEQ actually get errno= %d\n", errno);
61         }
62     } else {
63         size_t len = outbuf_size - out_left;
64         if (len < 0 && len >= 10) {
65             t_error("test failed expect 0<=len<=10 actually len=%d\n", (int)len);
66         }
67     }
68     free(outbuf);
69     iconv_close(cd);
70     return t_status;
71 }
72 
main(void)73 int main(void)
74 {
75     return test_euc_kr_error();
76 }
77