From 75b54da2f30b7ffa0c69c1659e3f3538bfdbd339 Mon Sep 17 00:00:00 2001 From: Thomas Schwinge Date: Tue, 4 Jul 2023 14:07:47 +0200 Subject: [PATCH] [RFC] Verify no embedded NULs in 'struct ht_identifier' --- gcc/analyzer/pending-diagnostic.cc | 2 +- gcc/c-family/c-spellcheck.h | 2 +- gcc/c/c-decl.cc | 2 +- gcc/tree.h | 2 +- libcpp/include/symtab.h | 4 ++-- libcpp/symtab.cc | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gcc/analyzer/pending-diagnostic.cc b/gcc/analyzer/pending-diagnostic.cc index e36ed4fd9c1..9452719a908 100644 --- a/gcc/analyzer/pending-diagnostic.cc +++ b/gcc/analyzer/pending-diagnostic.cc @@ -128,7 +128,7 @@ pending_diagnostic::same_tree_p (tree t1, tree t2) static bool ht_ident_eq (ht_identifier ident, const char *str) { - return (strlen (str) == ident.len + return (strlen (str) == HT_LEN (&ident) && 0 == strcmp (str, (const char *)ident.str)); } diff --git a/gcc/c-family/c-spellcheck.h b/gcc/c-family/c-spellcheck.h index e3748430b18..0f0c264ef01 100644 --- a/gcc/c-family/c-spellcheck.h +++ b/gcc/c-family/c-spellcheck.h @@ -31,7 +31,7 @@ struct edit_distance_traits { static size_t get_length (cpp_hashnode *hashnode) { - return hashnode->ident.len; + return HT_LEN (&hashnode->ident); } static const char *get_string (cpp_hashnode *hashnode) diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 1af51c4acfc..c67ae5be514 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4499,7 +4499,7 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) { const char *id = (const char *)best_macro->ident.str; tree macro_as_identifier - = get_identifier_with_length (id, best_macro->ident.len); + = get_identifier_with_length (id, HT_LEN (&best_macro->ident)); bm.set_best_so_far (macro_as_identifier, bmm.get_best_distance (), bmm.get_best_candidate_length ()); diff --git a/gcc/tree.h b/gcc/tree.h index 1854fe4a7d4..20169e7a467 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -1174,7 +1174,7 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, /* Unlike STRING_CST, in C terms this is strlen, not sizeof. */ #define IDENTIFIER_LENGTH(NODE) \ - (IDENTIFIER_NODE_CHECK (NODE)->identifier.id.len) + (HT_LEN (&IDENTIFIER_NODE_CHECK (NODE)->identifier.id)) #define IDENTIFIER_POINTER(NODE) \ ((const char *) IDENTIFIER_NODE_CHECK (NODE)->identifier.id.str) #define IDENTIFIER_HASH_VALUE(NODE) \ diff --git a/libcpp/include/symtab.h b/libcpp/include/symtab.h index c7ccc6db9f0..ea23e3dbedb 100644 --- a/libcpp/include/symtab.h +++ b/libcpp/include/symtab.h @@ -33,11 +33,11 @@ struct GTY(()) ht_identifier { 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 len_; unsigned int hash_value; }; -#define HT_LEN(NODE) ((NODE)->len) +#define HT_LEN(NODE) ({ gcc_assert ((NODE)->len_ == strlen ((const char *) HT_STR (NODE))); (NODE)->len_; }) #define HT_STR(NODE) ((NODE)->str) typedef struct ht cpp_hash_table; diff --git a/libcpp/symtab.cc b/libcpp/symtab.cc index ea9f26905a9..be5c1da9d67 100644 --- a/libcpp/symtab.cc +++ b/libcpp/symtab.cc @@ -155,7 +155,7 @@ ht_lookup_with_hash (cpp_hash_table *table, const unsigned char *str, node = (*table->alloc_node) (table); table->entries[index] = node; - HT_LEN (node) = (unsigned int) len; + node->len_ = (unsigned int) len; node->hash_value = hash; if (table->alloc_subobject) -- 2.34.1