11/*
2- * Copyright (c) 1997, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1997, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -236,7 +236,23 @@ unsigned int SymbolTable::hash_symbol(const char* s, int len) {
236236// entries in the symbol table during normal execution (only during
237237// safepoints).
238238
239+ // Symbols should represent entities from the constant pool that are
240+ // limited to <64K in length, but usage errors creep in allowing Symbols
241+ // to be used for arbitrary strings. For debug builds we will assert if
242+ // a string is too long, whereas product builds will truncate it.
243+ static int check_length (const char * name, int len) {
244+ assert (len <= Symbol::max_length (),
245+ " String length exceeds the maximum Symbol length" );
246+ if (len > Symbol::max_length ()) {
247+ warning (" A string \" %.80s ... %.80s\" exceeds the maximum Symbol "
248+ " length of %d and has been truncated" , name, (name + len - 80 ), Symbol::max_length ());
249+ len = Symbol::max_length ();
250+ }
251+ return len;
252+ }
253+
239254Symbol* SymbolTable::lookup (const char * name, int len, TRAPS) {
255+ len = check_length (name, len);
240256 unsigned int hashValue = hash_symbol (name, len);
241257 int index = the_table ()->hash_to_index (hashValue);
242258
@@ -367,6 +383,7 @@ void SymbolTable::add(ClassLoaderData* loader_data, constantPoolHandle cp,
367383 for (int i=0 ; i<names_count; i++) {
368384 int index = table->hash_to_index (hashValues[i]);
369385 bool c_heap = !loader_data->is_the_null_class_loader_data ();
386+ assert (lengths[i] <= Symbol::max_length (), " must be - these come from the constant pool" );
370387 Symbol* sym = table->basic_add (index, (u1*)names[i], lengths[i], hashValues[i], c_heap, CHECK);
371388 cp->symbol_at_put (cp_indices[i], sym);
372389 }
@@ -375,7 +392,8 @@ void SymbolTable::add(ClassLoaderData* loader_data, constantPoolHandle cp,
375392
376393Symbol* SymbolTable::new_permanent_symbol (const char * name, TRAPS) {
377394 unsigned int hash;
378- Symbol* result = SymbolTable::lookup_only ((char *)name, (int )strlen (name), hash);
395+ int len = check_length (name, (int )strlen (name));
396+ Symbol* result = SymbolTable::lookup_only ((char *)name, len, hash);
379397 if (result != NULL ) {
380398 return result;
381399 }
@@ -384,13 +402,14 @@ Symbol* SymbolTable::new_permanent_symbol(const char* name, TRAPS) {
384402
385403 SymbolTable* table = the_table ();
386404 int index = table->hash_to_index (hash);
387- return table->basic_add (index, (u1*)name, ( int ) strlen (name) , hash, false , THREAD);
405+ return table->basic_add (index, (u1*)name, len , hash, false , THREAD);
388406}
389407
390408Symbol* SymbolTable::basic_add (int index_arg, u1 *name, int len,
391409 unsigned int hashValue_arg, bool c_heap, TRAPS) {
392410 assert (!Universe::heap ()->is_in_reserved (name),
393411 " proposed name of symbol must be stable" );
412+ assert (len <= Symbol::max_length (), " caller should have ensured this" );
394413
395414 // Don't allow symbols to be created which cannot fit in a Symbol*.
396415 if (len > Symbol::max_length ()) {
0 commit comments