Skip to content

Commit 3d0d0e6

Browse files
author
David Holmes
committed
8345012: os::build_agent_function_name potentially wastes a byte when allocating the buffer
Reviewed-by: stuefe, shade
1 parent 525f33b commit 3d0d0e6

File tree

3 files changed

+58
-97
lines changed

3 files changed

+58
-97
lines changed

src/hotspot/os/posix/os_posix.cpp

-46
Original file line numberDiff line numberDiff line change
@@ -941,51 +941,6 @@ void os::naked_yield() {
941941
sched_yield();
942942
}
943943

944-
// Builds a platform dependent Agent_OnLoad_<lib_name> function name
945-
// which is used to find statically linked in agents.
946-
// Parameters:
947-
// sym_name: Symbol in library we are looking for
948-
// lib_name: Name of library to look in, null for shared libs.
949-
// is_absolute_path == true if lib_name is absolute path to agent
950-
// such as "/a/b/libL.so"
951-
// == false if only the base name of the library is passed in
952-
// such as "L"
953-
char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
954-
bool is_absolute_path) {
955-
char *agent_entry_name;
956-
size_t len;
957-
size_t name_len;
958-
size_t prefix_len = strlen(JNI_LIB_PREFIX);
959-
size_t suffix_len = strlen(JNI_LIB_SUFFIX);
960-
const char *start;
961-
962-
if (lib_name != nullptr) {
963-
name_len = strlen(lib_name);
964-
if (is_absolute_path) {
965-
// Need to strip path, prefix and suffix
966-
if ((start = strrchr(lib_name, *os::file_separator())) != nullptr) {
967-
lib_name = ++start;
968-
}
969-
if (strlen(lib_name) <= (prefix_len + suffix_len)) {
970-
return nullptr;
971-
}
972-
lib_name += prefix_len;
973-
name_len = strlen(lib_name) - suffix_len;
974-
}
975-
}
976-
len = (lib_name != nullptr ? name_len : 0) + strlen(sym_name) + 2;
977-
agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
978-
if (agent_entry_name == nullptr) {
979-
return nullptr;
980-
}
981-
strcpy(agent_entry_name, sym_name);
982-
if (lib_name != nullptr) {
983-
strcat(agent_entry_name, "_");
984-
strncat(agent_entry_name, lib_name, name_len);
985-
}
986-
return agent_entry_name;
987-
}
988-
989944
// Sleep forever; naked call to OS-specific sleep; use with CAUTION
990945
void os::infinite_sleep() {
991946
while (true) { // sleep forever ...
@@ -2231,4 +2186,3 @@ const void* os::get_saved_assert_context(const void** sigInfo) {
22312186
*sigInfo = nullptr;
22322187
return nullptr;
22332188
}
2234-

src/hotspot/os/windows/os_windows.cpp

-51
Original file line numberDiff line numberDiff line change
@@ -5835,57 +5835,6 @@ void* os::get_default_process_handle() {
58355835
return (void*)GetModuleHandle(nullptr);
58365836
}
58375837

5838-
// Builds a platform dependent Agent_OnLoad_<lib_name> function name
5839-
// which is used to find statically linked in agents.
5840-
// Parameters:
5841-
// sym_name: Symbol in library we are looking for
5842-
// lib_name: Name of library to look in, null for shared libs.
5843-
// is_absolute_path == true if lib_name is absolute path to agent
5844-
// such as "C:/a/b/L.dll"
5845-
// == false if only the base name of the library is passed in
5846-
// such as "L"
5847-
char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
5848-
bool is_absolute_path) {
5849-
char *agent_entry_name;
5850-
size_t len;
5851-
size_t name_len;
5852-
size_t prefix_len = strlen(JNI_LIB_PREFIX);
5853-
size_t suffix_len = strlen(JNI_LIB_SUFFIX);
5854-
const char *start;
5855-
5856-
if (lib_name != nullptr) {
5857-
len = name_len = strlen(lib_name);
5858-
if (is_absolute_path) {
5859-
// Need to strip path, prefix and suffix
5860-
if ((start = strrchr(lib_name, *os::file_separator())) != nullptr) {
5861-
lib_name = ++start;
5862-
} else {
5863-
// Need to check for drive prefix
5864-
if ((start = strchr(lib_name, ':')) != nullptr) {
5865-
lib_name = ++start;
5866-
}
5867-
}
5868-
if (len <= (prefix_len + suffix_len)) {
5869-
return nullptr;
5870-
}
5871-
lib_name += prefix_len;
5872-
name_len = strlen(lib_name) - suffix_len;
5873-
}
5874-
}
5875-
len = (lib_name != nullptr ? name_len : 0) + strlen(sym_name) + 2;
5876-
agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
5877-
if (agent_entry_name == nullptr) {
5878-
return nullptr;
5879-
}
5880-
5881-
strcpy(agent_entry_name, sym_name);
5882-
if (lib_name != nullptr) {
5883-
strcat(agent_entry_name, "_");
5884-
strncat(agent_entry_name, lib_name, name_len);
5885-
}
5886-
return agent_entry_name;
5887-
}
5888-
58895838
/*
58905839
All the defined signal names for Windows.
58915840

src/hotspot/share/runtime/os.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -2481,3 +2481,61 @@ jint os::set_minimum_stack_sizes() {
24812481
}
24822482
return JNI_OK;
24832483
}
2484+
2485+
// Builds a platform dependent Agent_OnLoad_<lib_name> function name
2486+
// which is used to find statically linked in agents.
2487+
// Parameters:
2488+
// sym_name: Symbol in library we are looking for
2489+
// lib_name: Name of library to look in, null for shared libs.
2490+
// is_absolute_path == true if lib_name is absolute path to agent
2491+
// such as "C:/a/b/L.dll" or "/a/b/libL.so"
2492+
// == false if only the base name of the library is passed in
2493+
// such as "L"
2494+
char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
2495+
bool is_absolute_path) {
2496+
char *agent_entry_name;
2497+
size_t len = 0;
2498+
size_t name_len = 0;
2499+
size_t prefix_len = strlen(JNI_LIB_PREFIX);
2500+
size_t suffix_len = strlen(JNI_LIB_SUFFIX);
2501+
size_t underscore_len = 0; // optional underscore if lib_name is set
2502+
const char *start;
2503+
2504+
if (lib_name != nullptr) {
2505+
if (is_absolute_path) {
2506+
// Need to strip path, prefix and suffix
2507+
if ((start = strrchr(lib_name, *os::file_separator())) != nullptr) {
2508+
lib_name = ++start;
2509+
}
2510+
#ifdef WINDOWS
2511+
else { // Need to check for drive prefix e.g. C:L.dll
2512+
if ((start = strchr(lib_name, ':')) != nullptr) {
2513+
lib_name = ++start;
2514+
}
2515+
}
2516+
#endif
2517+
name_len = strlen(lib_name);
2518+
if (name_len <= (prefix_len + suffix_len)) {
2519+
return nullptr;
2520+
}
2521+
lib_name += prefix_len;
2522+
name_len = strlen(lib_name) - suffix_len;
2523+
} else {
2524+
name_len = strlen(lib_name);
2525+
}
2526+
underscore_len = 1;
2527+
}
2528+
// Total buffer length to allocate - includes null terminator.
2529+
len = strlen(sym_name) + underscore_len + name_len + 1;
2530+
agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
2531+
if (agent_entry_name == nullptr) {
2532+
return nullptr;
2533+
}
2534+
2535+
strcpy(agent_entry_name, sym_name);
2536+
if (lib_name != nullptr) {
2537+
strcat(agent_entry_name, "_");
2538+
strncat(agent_entry_name, lib_name, name_len);
2539+
}
2540+
return agent_entry_name;
2541+
}

0 commit comments

Comments
 (0)