# HG changeset patch # User shshahma # Date 1476416184 -3600 # Node ID 17b40d99ea3665de2bfffe163b68f2dfcf675cba # Parent ae1f84d4eaddd1888777ec0fda2281ae059065c8 8162419: closed/com/oracle/jfr/runtime/TestVMInfoEvent.sh failing after JDK-8155968 Summary: Under error conditions, always return -1 and perform null termination regardless of the behavior of underlying vsnprintf() implementation. Reviewed-by: dholmes, cjplummer diff -r ae1f84d4eadd -r 17b40d99ea36 src/share/vm/prims/jvm.cpp --- a/src/share/vm/prims/jvm.cpp Fri Jul 01 09:33:34 2016 +0200 +++ b/src/share/vm/prims/jvm.cpp Fri Oct 14 04:36:24 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2016, 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 @@ -2696,7 +2696,18 @@ int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { // see bug 4399518, 4417214 if ((intptr_t)count <= 0) return -1; - return vsnprintf(str, count, fmt, args); + + int result = vsnprintf(str, count, fmt, args); + // Note: on truncation vsnprintf(3) on Unix returns number of + // characters which would have been written had the buffer been large + // enough; on Windows, it returns -1. We handle both cases here and + // always return -1, and perform null termination. + if ((result > 0 && (size_t)result >= count) || result == -1) { + str[count - 1] = '\0'; + result = -1; + } + + return result; } diff -r ae1f84d4eadd -r 17b40d99ea36 src/share/vm/runtime/deoptimization.cpp --- a/src/share/vm/runtime/deoptimization.cpp Fri Jul 01 09:33:34 2016 +0200 +++ b/src/share/vm/runtime/deoptimization.cpp Fri Oct 14 04:36:24 2016 +0100 @@ -1796,8 +1796,6 @@ trap_reason_name(reason), recomp_flag ? " recompiled" : ""); } - if (len >= buflen) - buf[buflen-1] = '\0'; return buf; } @@ -1865,8 +1863,6 @@ len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'", reason, action, unloaded_class_index); } - if (len >= buflen) - buf[buflen-1] = '\0'; return buf; } diff -r ae1f84d4eadd -r 17b40d99ea36 src/share/vm/runtime/java.cpp --- a/src/share/vm/runtime/java.cpp Fri Jul 01 09:33:34 2016 +0200 +++ b/src/share/vm/runtime/java.cpp Fri Oct 14 04:36:24 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2016, 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 @@ -772,25 +772,36 @@ } void JDK_Version::to_string(char* buffer, size_t buflen) const { + assert(buffer && buflen > 0, "call with useful buffer"); size_t index = 0; if (!is_valid()) { jio_snprintf(buffer, buflen, "%s", "(uninitialized)"); } else if (is_partially_initialized()) { jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0"); } else { - index += jio_snprintf( + int rc = jio_snprintf( &buffer[index], buflen - index, "%d.%d", _major, _minor); + if (rc == -1) return; + index += rc; if (_micro > 0) { - index += jio_snprintf(&buffer[index], buflen - index, ".%d", _micro); + rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _micro); + if (rc == -1) return; + index += rc; } if (_update > 0) { - index += jio_snprintf(&buffer[index], buflen - index, "_%02d", _update); + rc = jio_snprintf(&buffer[index], buflen - index, "_%02d", _update); + if (rc == -1) return; + index += rc; } if (_special > 0) { - index += jio_snprintf(&buffer[index], buflen - index, "%c", _special); + rc = jio_snprintf(&buffer[index], buflen - index, "%c", _special); + if (rc == -1) return; + index += rc; } if (_build > 0) { - index += jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build); + rc = jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build); + if (rc == -1) return; + index += rc; } } }