https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124911

            Bug ID: 124911
           Summary: ICE (ASan global-buffer-overflow) in
                    one_cppchar_to_utf8 when parsing invalid UCN
                    \UFFFFFFFF
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 522024330006 at smail dot nju.edu.cn
  Target Milestone: ---

## Description
When compiling a file containing an invalid 8-digit Universal Character Name
(UCN) like \UFFFFFFFF as an identifier, GCC (cc1plus) crashes with a
global-buffer-overflow in the preprocessor's charset conversion logic.

## Reproduction
Create a file named `repro.cpp`:
```c
extern int \UFFFFFFFF;
```

## Compilation Command
```bash
g++ -x c++ repro.cpp
```

## ASan Output
```
--- stderr ---
/workspace/exp/cases/33809.c:8:12: error: \UFFFFFFFF is not a valid universal
character
    8 | extern int \UFFFFFFFF; // Invalid and not supported
      |            ^
/workspace/install/gcc/src/gcc/libcpp/charset.cc:247:47: runtime error: index 6
out of bounds for type 'unsigned char [6]'
/workspace/install/gcc/src/gcc/libcpp/charset.cc:247:47: runtime error: load of
address 0x000019882586 with insufficient space for an object of type 'const
uchar'
0x000019882586: note: pointer points here
 f0 f8 fc fe 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00
00 00 00 00 00  00 00
             ^ 
=================================================================
==1122872==ERROR: AddressSanitizer: global-buffer-overflow on address
0x000019882586 at pc 0x000011f7f66d bp 0x7ffe93b66be0 sp 0x7ffe93b66bd0
READ of size 1 at 0x000019882586 thread T0
    #0 0x11f7f66c in one_cppchar_to_utf8
/workspace/install/gcc/src/gcc/libcpp/charset.cc:247
    #1 0x11f7f66c in _cpp_interpret_identifier
/workspace/install/gcc/src/gcc/libcpp/charset.cc:3067
    #2 0x12003316 in lex_identifier
/workspace/install/gcc/src/gcc/libcpp/lex.cc:2258
    #3 0x12016870 in _cpp_lex_direct
/workspace/install/gcc/src/gcc/libcpp/lex.cc:4350
    #4 0x12017ac6 in _cpp_lex_token
/workspace/install/gcc/src/gcc/libcpp/lex.cc:3736
    #5 0x120466c9 in cpp_get_token_1
/workspace/install/gcc/src/gcc/libcpp/macro.cc:3025
    #6 0x1205613c in cpp_get_token_with_location(cpp_reader*, unsigned long*)
/workspace/install/gcc/src/gcc/libcpp/macro.cc:3311
    #7 0x2be4b96 in get_token
/workspace/install/gcc/src/gcc/gcc/c-family/c-lex.cc:316
    #8 0x2be6cae in c_lex_with_flags(tree_node**, unsigned long*, unsigned
char*, int) /workspace/install/gcc/src/gcc/gcc/c-family/c-lex.cc:579
    #9 0x1e7a34e in cp_lexer_get_preprocessor_token
/workspace/install/gcc/src/gcc/gcc/cp/parser.cc:1117
    #10 0x1e77845 in cp_lexer_new_main
/workspace/install/gcc/src/gcc/gcc/cp/parser.cc:884
    #11 0x2165673 in c_parse_file()
/workspace/install/gcc/src/gcc/gcc/cp/parser.cc:55308
    #12 0x2c8c374 in c_common_parse_file()
/workspace/install/gcc/src/gcc/gcc/c-family/c-opts.cc:1418
    #13 0x62b467f in compile_file
/workspace/install/gcc/src/gcc/gcc/toplev.cc:453
    #14 0x62cab9b in do_compile
/workspace/install/gcc/src/gcc/gcc/toplev.cc:2222
    #15 0x62cc42d in toplev::main(int, char**)
/workspace/install/gcc/src/gcc/gcc/toplev.cc:2385
    #16 0x11858e05 in main /workspace/install/gcc/src/gcc/gcc/main.cc:39
    #17 0x7de2ebd251c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId:
8e9fd827446c24067541ac5390e6f527fb5947bb)
    #18 0x7de2ebd2528a in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId:
8e9fd827446c24067541ac5390e6f527fb5947bb)
    #19 0xf74004 in _start
(/workspace/install/gcc/install_15.2.0/libexec/gcc/x86_64-pc-linux-gnu/16.0.0/cc1plus+0xf74004)
(BuildId: 47a2a0370315580a8ef4ce4dea19d167db59320e)

0x000019882586 is located 58 bytes before global variable 'masks' defined in
'/workspace/install/gcc/src/gcc/libcpp/charset.cc:230:22' (0x198825c0) of size
6
0x000019882586 is located 0 bytes after global variable 'limits' defined in
'/workspace/install/gcc/src/gcc/libcpp/charset.cc:231:22' (0x19882580) of size
6
SUMMARY: AddressSanitizer: global-buffer-overflow
/workspace/install/gcc/src/gcc/libcpp/charset.cc:247 in one_cppchar_to_utf8
Shadow bytes around the buggy address:
  0x000019882300: 00 00 00 00 00 00 00 00 00 00 00 00 f9 f9 f9 f9
  0x000019882380: 00 07 f9 f9 f9 f9 f9 f9 00 07 f9 f9 f9 f9 f9 f9
  0x000019882400: 00 07 f9 f9 f9 f9 f9 f9 00 07 f9 f9 f9 f9 f9 f9
  0x000019882480: 00 07 f9 f9 f9 f9 f9 f9 00 07 f9 f9 f9 f9 f9 f9
  0x000019882500: 00 07 f9 f9 f9 f9 f9 f9 00 07 f9 f9 f9 f9 f9 f9
=>0x000019882580:[06]f9 f9 f9 f9 f9 f9 f9 06 f9 f9 f9 f9 f9 f9 f9
  0x000019882600: 06 f9 f9 f9 f9 f9 f9 f9 06 f9 f9 f9 f9 f9 f9 f9
  0x000019882680: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000019882700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000019882780: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000019882800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==1122872==ABORTING
```

## The instrumented gcc version:
```
gcc (GCC) 16.0.0 20251029 (experimental)
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
  • [Bug libgcc/124911] New:... 522024330006 at smail dot nju.edu.cn via Gcc-bugs

Reply via email to