6390477: (launcher) replace unsafe usages of sprintf with snprintf
authorksrini
Thu, 09 Sep 2010 11:50:40 -0700
changeset 6535 77ffd0e75bfb
parent 6534 ad71f5af4022
child 6536 2694e08f83ea
child 6537 7aa4e7bb5dae
6390477: (launcher) replace unsafe usages of sprintf with snprintf Reviewed-by: darcy, mchung
jdk/src/share/bin/java.c
jdk/src/solaris/bin/java_md.c
jdk/src/windows/bin/java_md.c
--- a/jdk/src/share/bin/java.c	Wed Sep 08 17:10:04 2010 -0700
+++ b/jdk/src/share/bin/java.c	Thu Sep 09 11:50:40 2010 -0700
@@ -712,19 +712,19 @@
     struct stat statbuf;
 
     /* return if jre/lib/rt.jar exists */
-    sprintf(pathname, "%s%slib%srt.jar", jrepath, separator, separator);
+    JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%srt.jar", jrepath, separator, separator);
     if (stat(pathname, &statbuf) == 0) {
         return;
     }
 
     /* return if jre/classes exists */
-    sprintf(pathname, "%s%sclasses", jrepath, separator);
+    JLI_Snprintf(pathname, sizeof(pathname), "%s%sclasses", jrepath, separator);
     if (stat(pathname, &statbuf) == 0) {
         return;
     }
 
     /* modularized jre */
-    sprintf(pathname, "%s%slib%s*", jrepath, separator, separator);
+    JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%s*", jrepath, separator, separator);
     s = (char *) JLI_WildcardExpandClasspath(pathname);
     def = JLI_MemAlloc(sizeof(format)
                        - 2 /* strlen("%s") */
@@ -1624,11 +1624,8 @@
     if (JLI_IsTraceLauncher()) {
         start = CounterGet();
     }
-
-    JLI_StrCpy(jvmCfgName, jrepath);
-    JLI_StrCat(jvmCfgName, FILESEP "lib" FILESEP);
-    JLI_StrCat(jvmCfgName, arch);
-    JLI_StrCat(jvmCfgName, FILESEP "jvm.cfg");
+    JLI_Snprintf(jvmCfgName, sizeof(jvmCfgName), "%s%slib%s%s%sjvm.cfg",
+        jrepath, FILESEP, FILESEP, arch, FILESEP);
 
     jvmCfg = fopen(jvmCfgName, "r");
     if (jvmCfg == NULL) {
--- a/jdk/src/solaris/bin/java_md.c	Wed Sep 08 17:10:04 2010 -0700
+++ b/jdk/src/solaris/bin/java_md.c	Thu Sep 09 11:50:40 2010 -0700
@@ -535,7 +535,7 @@
 GetApplicationHome(char *buf, jint bufsize)
 {
     if (execname != NULL) {
-        JLI_StrNCpy(buf, execname, bufsize-1);
+        JLI_Snprintf(buf, bufsize, "%s", execname);
         buf[bufsize-1] = '\0';
     } else {
         return JNI_FALSE;
--- a/jdk/src/windows/bin/java_md.c	Wed Sep 08 17:10:04 2010 -0700
+++ b/jdk/src/windows/bin/java_md.c	Thu Sep 09 11:50:40 2010 -0700
@@ -162,6 +162,10 @@
 #endif
 #ifdef CRT_DLL
         if (GetJREPath(crtpath, MAXPATHLEN)) {
+            if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") + JLI_StrLen(CRT_DLL) >= MAXPATHLEN) {
+                JLI_ReportErrorMessage(JRE_ERROR11);
+                return JNI_FALSE;
+            }
             (void)JLI_StrCat(crtpath, "\\bin\\" CRT_DLL);   /* Add crt dll */
             JLI_TraceLauncher("CRT path is %s\n", crtpath);
             if (_access(crtpath, 0) == 0) {
@@ -852,8 +856,7 @@
     /*
      * If this isn't the selected version, exec the selected version.
      */
-    (void)JLI_StrCat(JLI_StrCat(JLI_StrCpy(path, jre), "\\bin\\"), progname);
-    (void)JLI_StrCat(path, ".exe");
+    JLI_Snprintf(path, sizeof(path), "%s\\bin\\%s.exe", jre, progname);
 
     /*
      * Although Windows has an execv() entrypoint, it doesn't actually