diff --git a/src/share/bin/jli_util.h b/src/share/bin/jli_util.h index dd53e93621003d116834e18c2f5a68b0dc4054cd..388910407ffb39c086bd3b93c839a4a44b59147e 100644 --- a/src/share/bin/jli_util.h +++ b/src/share/bin/jli_util.h @@ -66,7 +66,7 @@ int JLI_GetStdArgc(); #include #define JLI_StrCaseCmp(p1, p2) stricmp((p1), (p2)) #define JLI_StrNCaseCmp(p1, p2, p3) strnicmp((p1), (p2), (p3)) -size_t JLI_Snprintf(char *buffer, size_t size, const char *format, ...); +int JLI_Snprintf(char *buffer, size_t size, const char *format, ...); void JLI_CmdToArgs(char *cmdline); #define JLI_Lseek _lseeki64 #else /* NIXES */ diff --git a/src/windows/bin/java_md.c b/src/windows/bin/java_md.c index df118717279e984680f36188e6323facffd8d066..4078ecc754bb11a8cb1abc2d699554b2fa224c52 100644 --- a/src/windows/bin/java_md.c +++ b/src/windows/bin/java_md.c @@ -526,27 +526,35 @@ jlong Counter2Micros(jlong counts) } return (counts * 1000 * 1000)/counterFrequency.QuadPart; } - /* * windows snprintf does not guarantee a null terminator in the buffer, * if the computed size is equal to or greater than the buffer size, - * as well as error conditions, this function guarantees a null terminator - * under all these conditions. An unreasonable buffer size will return - * an error value. + * as well as error conditions. This function guarantees a null terminator + * under all these conditions. An unreasonable buffer or size will return + * an error value. Under all other conditions this function will return the + * size of the bytes actually written minus the null terminator, similar + * to ansi snprintf api. Thus when calling this function the caller must + * ensure storage for the null terminator. */ -size_t -JLI_Snprintf(char* buffer, size_t size, const char* format, ...) -{ - size_t rc; +int +JLI_Snprintf(char* buffer, size_t size, const char* format, ...) { + int rc; va_list vl; - if (size <= 0) + if (size == 0 || buffer == NULL) return -1; + buffer[0] = '\0'; va_start(vl, format); - rc = vsnprintf(buffer, size - 1, format, vl); + rc = vsnprintf(buffer, size, format, vl); + va_end(vl); /* force a null terminator, if something is amiss */ - if (rc < 0 || rc >= size) + if (rc < 0) { + /* apply ansi semantics */ buffer[size - 1] = '\0'; - va_end(vl); + return size; + } else if (rc == size) { + /* force a null terminator */ + buffer[size - 1] = '\0'; + } return rc; } @@ -1441,7 +1449,10 @@ CreateApplicationArgs(JNIEnv *env, char **strv, int argc) // we add the indicator tlen = 1 + JLI_StrLen(strv[i]) + 1; nargv[i] = (char *) JLI_MemAlloc(tlen); - JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F', strv[i]); + if (JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F', + strv[i]) < 0) { + return NULL; + } JLI_TraceLauncher("%s\n", nargv[i]); } diff --git a/test/tools/launcher/ToolsOpts.java b/test/tools/launcher/ToolsOpts.java index 4535990dbca7ecfef3e1729d3ad4d96a3df758b7..f01931d5482d2bd752b20f0743a8fcbbf2714852 100644 --- a/test/tools/launcher/ToolsOpts.java +++ b/test/tools/launcher/ToolsOpts.java @@ -23,6 +23,7 @@ /* * @test + * @bug 8002091 * @summary Test options patterns for javac,javah,javap and javadoc using * javac as a test launcher. Create a dummy javac and intercept options to check * reception of options as passed through the launcher without having to launch