changeset 5693:b09d649c1b88

7200499: Better data validation for options Reviewed-by: darcy, jjh, mschoene
author ksrini
date Thu, 08 Nov 2012 14:29:58 -0800
parents 7a4e2abdd290
children f0d1a7da963d
files src/share/bin/jli_util.h src/windows/bin/java_md.c
diffstat 2 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/bin/jli_util.h	Mon Oct 15 14:06:54 2012 +0400
+++ b/src/share/bin/jli_util.h	Thu Nov 08 14:29:58 2012 -0800
@@ -66,7 +66,7 @@
 #include <io.h>
 #define JLI_StrCaseCmp(p1, p2)          stricmp((p1), (p2))
 #define JLI_StrNCaseCmp(p1, p2, p3)     strnicmp((p1), (p2), (p3))
-#define JLI_Snprintf                    _snprintf
+size_t  JLI_Snprintf(char *buffer, size_t size, const char *format, ...);
 void JLI_CmdToArgs(char *cmdline);
 #else
 #include <unistd.h>
--- a/src/windows/bin/java_md.c	Mon Oct 15 14:06:54 2012 +0400
+++ b/src/windows/bin/java_md.c	Thu Nov 08 14:29:58 2012 -0800
@@ -101,7 +101,6 @@
 /* funtion in awt.dll (src/windows/native/sun/java2d/d3d/D3DPipelineManager.cpp) */
 #define D3D_PRELOAD_FUNC "preloadD3D"
 
-
 /* Extracts value of a parameter with the specified name
  * from command line argument (returns pointer in the argument).
  * Returns NULL if the argument does not contains the parameter.
@@ -202,7 +201,7 @@
     }
 
     JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%s%sjvm.cfg",
-        jrepath, FILESEP, FILESEP, (char*)GetArch(), FILESEP);
+                 jrepath, FILESEP, FILESEP, (char*)GetArch(), FILESEP);
 
     /* Find the specified JVM type */
     if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
@@ -276,7 +275,8 @@
 #endif
 #ifdef CRT_DLL
         if (GetJREPath(crtpath, MAXPATHLEN)) {
-            if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") + JLI_StrLen(CRT_DLL) >= MAXPATHLEN) {
+            if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") +
+                    JLI_StrLen(CRT_DLL) >= MAXPATHLEN) {
                 JLI_ReportErrorMessage(JRE_ERROR11);
                 return JNI_FALSE;
             }
@@ -347,7 +347,8 @@
     if (JLI_StrChr(jvmtype, '/') || JLI_StrChr(jvmtype, '\\')) {
         JLI_Snprintf(jvmpath, jvmpathsize, "%s\\" JVM_DLL, jvmtype);
     } else {
-        JLI_Snprintf(jvmpath, jvmpathsize, "%s\\bin\\%s\\" JVM_DLL, jrepath, jvmtype);
+        JLI_Snprintf(jvmpath, jvmpathsize, "%s\\bin\\%s\\" JVM_DLL,
+                     jrepath, jvmtype);
     }
     if (stat(jvmpath, &s) == 0) {
         return JNI_TRUE;
@@ -526,6 +527,29 @@
     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.
+ */
+size_t
+JLI_Snprintf(char* buffer, size_t size, const char* format, ...)
+{
+    size_t rc;
+    va_list vl;
+    if (size <= 0)
+        return -1;
+    va_start(vl, format);
+    rc = vsnprintf(buffer, size - 1, format, vl);
+    /* force a null terminator, if something is amiss */
+    if (rc < 0 || rc >= size)
+        buffer[size - 1] = '\0';
+    va_end(vl);
+    return rc;
+}
+
 void
 JLI_ReportErrorMessage(const char* fmt, ...) {
     va_list vl;
@@ -880,7 +904,7 @@
  */
 void
 ExecJRE(char *jre, char **argv) {
-    int     len;
+    jint     len;
     char    path[MAXPATHLEN + 1];
 
     const char *progname = GetProgramName();