Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8303070: Memory leak in DCmdArgument<char*>::parse_value
Reviewed-by: fparain, jcking, jsjolen, eosterlund, coleenp
  • Loading branch information
David Holmes committed Mar 1, 2023
1 parent 9fc518f commit 6e19387
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/hotspot/share/services/diagnosticArgument.cpp
Expand Up @@ -178,33 +178,30 @@ template <> void DCmdArgument<bool>::init_value(TRAPS) {

template <> void DCmdArgument<bool>::destroy_value() { }

template <> void DCmdArgument<char*>::destroy_value() {
FREE_C_HEAP_ARRAY(char, _value);
set_value(nullptr);
}

template <> void DCmdArgument<char*>::parse_value(const char* str,
size_t len, TRAPS) {
if (str == nullptr) {
_value = nullptr;
destroy_value();
} else {
_value = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
// Use realloc as we may have a default set.
_value = REALLOC_C_HEAP_ARRAY(char, _value, len + 1, mtInternal);
int n = os::snprintf(_value, len + 1, "%.*s", (int)len, str);
assert((size_t)n <= len, "Unexpected number of characters in string");
}
}

template <> void DCmdArgument<char*>::init_value(TRAPS) {
if (has_default() && _default_string != nullptr) {
set_value(nullptr); // Must be initialized before calling parse_value
if (has_default()) {
this->parse_value(_default_string, strlen(_default_string), THREAD);
if (HAS_PENDING_EXCEPTION) {
fatal("Default string must be parsable");
}
} else {
set_value(nullptr);
}
}

template <> void DCmdArgument<char*>::destroy_value() {
FREE_C_HEAP_ARRAY(char, _value);
set_value(nullptr);
}

template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,
size_t len, TRAPS) {
if (str == nullptr) {
Expand Down
9 changes: 8 additions & 1 deletion test/hotspot/jtreg/runtime/NMT/JcmdScale.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -74,5 +74,12 @@ public static void main(String args[]) throws Exception {
output = new OutputAnalyzer(pb.start());
output.shouldContain("Incorrect scale value: apa");

pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale="});
output = new OutputAnalyzer(pb.start());
output.shouldContain("Incorrect scale value:");

pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale"});
output = new OutputAnalyzer(pb.start());
output.shouldContain("Incorrect scale value: (null)");
}
}

3 comments on commit 6e19387

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JesperIRL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/tag jdk-21+12

@openjdk
Copy link

@openjdk openjdk bot commented on 6e19387 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JesperIRL The tag jdk-21+12 was successfully created.

Please sign in to comment.