diff options
author | Lewis Hyatt <lhyatt@gmail.com> | 2022-10-14 13:52:12 -0400 |
---|---|---|
committer | Lewis Hyatt <lhyatt@gmail.com> | 2022-10-19 09:26:09 -0400 |
commit | f3b957ea8b9dadfb1ed30f24f463529684b7a36a (patch) | |
tree | 3834aa6c8ae665d37326b8e06543cb77cd9a9524 /libcpp | |
parent | 09fed44cabd50f3d8e050f91cc2db02364ce9176 (diff) | |
download | gcc-f3b957ea8b9dadfb1ed30f24f463529684b7a36a.zip gcc-f3b957ea8b9dadfb1ed30f24f463529684b7a36a.tar.gz gcc-f3b957ea8b9dadfb1ed30f24f463529684b7a36a.tar.bz2 |
pch: Fix streaming of strings with embedded null bytes
When a GTY'ed struct is streamed to PCH, any plain char* pointers it contains
(whether they live in GC-controlled memory or not) will be marked for PCH
output by the routine gt_pch_note_object in ggc-common.cc. This routine
special-cases plain char* strings, and in particular it uses strlen() to get
their length. Thus it does not handle strings with embedded null bytes, but it
is possible for something PCH cares about (such as a string literal token in a
macro definition) to contain such embedded nulls. To fix that up, add a new
GTY option "string_length" so that gt_pch_note_object can be informed the
actual length it ought to use, and use it in the relevant libcpp structs
(cpp_string and ht_identifier) accordingly.
gcc/ChangeLog:
* gengtype.cc (output_escaped_param): Add missing const.
(get_string_option): Add missing check for option type.
(walk_type): Support new "string_length" GTY option.
(write_types_process_field): Likewise.
* ggc-common.cc (gt_pch_note_object): Add optional length argument.
* ggc.h (gt_pch_note_object): Adjust prototype for new argument.
(gt_pch_n_S2): Declare...
* stringpool.cc (gt_pch_n_S2): ...new function.
* doc/gty.texi: Document new GTY((string_length)) option.
libcpp/ChangeLog:
* include/cpplib.h (struct cpp_string): Use new "string_length" GTY.
* include/symtab.h (struct ht_identifier): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/pch/pch-string-nulls.C: New test.
* g++.dg/pch/pch-string-nulls.Hs: New test.
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/include/cpplib.h | 6 | ||||
-rw-r--r-- | libcpp/include/symtab.h | 5 |
2 files changed, 9 insertions, 2 deletions
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index d5ef12a..1d34c00 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -179,7 +179,11 @@ enum c_lang {CLK_GNUC89 = 0, CLK_GNUC99, CLK_GNUC11, CLK_GNUC17, CLK_GNUC2X, /* Payload of a NUMBER, STRING, CHAR or COMMENT token. */ struct GTY(()) cpp_string { unsigned int len; - const unsigned char *text; + + /* TEXT is always null terminated (terminator not included in len); but this + GTY markup arranges that PCH streaming works properly even if there is a + null byte in the middle of the string. */ + const unsigned char * GTY((string_length ("1 + %h.len"))) text; }; /* Flags for the cpp_token structure. */ diff --git a/libcpp/include/symtab.h b/libcpp/include/symtab.h index 53efe6c..8b45fd5 100644 --- a/libcpp/include/symtab.h +++ b/libcpp/include/symtab.h @@ -29,7 +29,10 @@ along with this program; see the file COPYING3. If not see typedef struct ht_identifier ht_identifier; typedef struct ht_identifier *ht_identifier_ptr; struct GTY(()) ht_identifier { - const unsigned char *str; + /* This GTY markup arranges that the null-terminated identifier would still + stream to PCH correctly, if a null byte were to make its way into an + identifier somehow. */ + const unsigned char * GTY((string_length ("1 + %h.len"))) str; unsigned int len; unsigned int hash_value; }; |