8199010: attachListener.hpp: Fix potential null termination issue found by coverity scans

Reviewed-by: stuefe, dholmes, cjplummer
This commit is contained in:
Christoph Langer 2018-03-15 15:38:23 +01:00
parent ea27ad47f7
commit 9523d40461

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,9 +27,9 @@
#include "memory/allocation.hpp" #include "memory/allocation.hpp"
#include "utilities/debug.hpp" #include "utilities/debug.hpp"
#include "utilities/ostream.hpp"
#include "utilities/macros.hpp"
#include "utilities/globalDefinitions.hpp" #include "utilities/globalDefinitions.hpp"
#include "utilities/macros.hpp"
#include "utilities/ostream.hpp"
// The AttachListener thread services a queue of operations that are enqueued // The AttachListener thread services a queue of operations that are enqueued
// by client tools. Each operation is identified by a name and has up to 3 // by client tools. Each operation is identified by a name and has up to 3
@ -122,9 +122,10 @@ class AttachOperation: public CHeapObj<mtInternal> {
// set the operation name // set the operation name
void set_name(char* name) { void set_name(char* name) {
size_t len = strlen(name); assert(strlen(name) <= name_length_max, "exceeds maximum name length");
assert(len <= name_length_max, "exceeds maximum name length"); size_t len = MIN2(strlen(name), (size_t)name_length_max);
memcpy(_name, name, MIN2(len + 1, (size_t)name_length_max)); memcpy(_name, name, len);
_name[len] = '\0';
} }
// get an argument value // get an argument value
@ -139,9 +140,10 @@ class AttachOperation: public CHeapObj<mtInternal> {
if (arg == NULL) { if (arg == NULL) {
_arg[i][0] = '\0'; _arg[i][0] = '\0';
} else { } else {
size_t len = strlen(arg); assert(strlen(arg) <= arg_length_max, "exceeds maximum argument length");
assert(len <= arg_length_max, "exceeds maximum argument length"); size_t len = MIN2(strlen(arg), (size_t)arg_length_max);
memcpy(_arg[i], arg, MIN2(len + 1, (size_t)arg_length_max)); memcpy(_arg[i], arg, len);
_arg[i][len] = '\0';
} }
} }