Skip to content

Commit 6e19387

Browse files
author
David Holmes
committed
8303070: Memory leak in DCmdArgument<char*>::parse_value
Reviewed-by: fparain, jcking, jsjolen, eosterlund, coleenp
1 parent 9fc518f commit 6e19387

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/hotspot/share/services/diagnosticArgument.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,30 @@ template <> void DCmdArgument<bool>::init_value(TRAPS) {
178178

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

181+
template <> void DCmdArgument<char*>::destroy_value() {
182+
FREE_C_HEAP_ARRAY(char, _value);
183+
set_value(nullptr);
184+
}
185+
181186
template <> void DCmdArgument<char*>::parse_value(const char* str,
182187
size_t len, TRAPS) {
183188
if (str == nullptr) {
184-
_value = nullptr;
189+
destroy_value();
185190
} else {
186-
_value = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
191+
// Use realloc as we may have a default set.
192+
_value = REALLOC_C_HEAP_ARRAY(char, _value, len + 1, mtInternal);
187193
int n = os::snprintf(_value, len + 1, "%.*s", (int)len, str);
188194
assert((size_t)n <= len, "Unexpected number of characters in string");
189195
}
190196
}
191197

192198
template <> void DCmdArgument<char*>::init_value(TRAPS) {
193-
if (has_default() && _default_string != nullptr) {
199+
set_value(nullptr); // Must be initialized before calling parse_value
200+
if (has_default()) {
194201
this->parse_value(_default_string, strlen(_default_string), THREAD);
195-
if (HAS_PENDING_EXCEPTION) {
196-
fatal("Default string must be parsable");
197-
}
198-
} else {
199-
set_value(nullptr);
200202
}
201203
}
202204

203-
template <> void DCmdArgument<char*>::destroy_value() {
204-
FREE_C_HEAP_ARRAY(char, _value);
205-
set_value(nullptr);
206-
}
207-
208205
template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,
209206
size_t len, TRAPS) {
210207
if (str == nullptr) {

test/hotspot/jtreg/runtime/NMT/JcmdScale.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, 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
@@ -74,5 +74,12 @@ public static void main(String args[]) throws Exception {
7474
output = new OutputAnalyzer(pb.start());
7575
output.shouldContain("Incorrect scale value: apa");
7676

77+
pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale="});
78+
output = new OutputAnalyzer(pb.start());
79+
output.shouldContain("Incorrect scale value:");
80+
81+
pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale"});
82+
output = new OutputAnalyzer(pb.start());
83+
output.shouldContain("Incorrect scale value: (null)");
7784
}
7885
}

0 commit comments

Comments
 (0)