src/solaris/native/java/lang/ProcessEnvironment_md.c

Print this page
rev 6793 : 8008118: (process) Possible null pointer dereference in jdk/src/solaris/native/java/lang/UNIXProcess_md.c
Summary: Modified the path processing code so that it detects and handles out of memory errors.
Reviewed-by: chegar, martin, christos, alanb, msheppar
Contributed-by: john.zavgren@oracle.com


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include "jni.h"
  29 #include "jni_util.h"
  30 
  31 #ifdef __APPLE__
  32 #include <crt_externs.h>
  33 #define environ (*_NSGetEnviron())
  34 #endif
  35 
  36 JNIEXPORT jobjectArray JNICALL
  37 Java_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)
  38 {
  39     /* This is one of the rare times it's more portable to declare an
  40      * external symbol explicitly, rather than via a system header.
  41      * The declaration is standardized as part of UNIX98, but there is
  42      * no standard (not even de-facto) header file where the
  43      * declaration is to be found.  See:
  44      * http://www.opengroup.org/onlinepubs/007908799/xbd/envvar.html */
  45 #ifndef __APPLE__
  46     extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */




  47 #endif
  48 



  49     jsize count = 0;
  50     jsize i, j;
  51     jobjectArray result;
  52     jclass byteArrCls = (*env)->FindClass(env, "[B");
  53 
  54     for (i = 0; environ[i]; i++) {
  55         /* Ignore corrupted environment variables */
  56         if (strchr(environ[i], '=') != NULL)
  57             count++;
  58     }
  59 
  60     result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
  61     if (result == NULL) return NULL;
  62 
  63     for (i = 0, j = 0; environ[i]; i++) {
  64         const char * varEnd = strchr(environ[i], '=');
  65         /* Ignore corrupted environment variables */
  66         if (varEnd != NULL) {
  67             jbyteArray var, val;
  68             const char * valBeg = varEnd + 1;


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include "jni.h"
  29 #include "jni_util.h"
  30 
  31 #ifdef __APPLE__
  32 #include <crt_externs.h>
  33 #define environ (*_NSGetEnviron())
  34 #else
  35 /* This is one of the rare times it's more portable to declare an




  36  * external symbol explicitly, rather than via a system header.
  37  * The declaration is standardized as part of UNIX98, but there is
  38  * no standard (not even de-facto) header file where the
  39  * declaration is to be found.  See:
  40  * http://www.opengroup.org/onlinepubs/009695399/functions/environ.html
  41  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html
  42  *
  43  * "All identifiers in this volume of IEEE Std 1003.1-2001, except
  44  * environ, are defined in at least one of the headers" (!)
  45  */
  46 extern char **environ;
  47 #endif
  48 
  49 JNIEXPORT jobjectArray JNICALL
  50 Java_java_lang_ProcessEnvironment_environ(JNIEnv *env, jclass ign)
  51 {
  52     jsize count = 0;
  53     jsize i, j;
  54     jobjectArray result;
  55     jclass byteArrCls = (*env)->FindClass(env, "[B");
  56 
  57     for (i = 0; environ[i]; i++) {
  58         /* Ignore corrupted environment variables */
  59         if (strchr(environ[i], '=') != NULL)
  60             count++;
  61     }
  62 
  63     result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0);
  64     if (result == NULL) return NULL;
  65 
  66     for (i = 0, j = 0; environ[i]; i++) {
  67         const char * varEnd = strchr(environ[i], '=');
  68         /* Ignore corrupted environment variables */
  69         if (varEnd != NULL) {
  70             jbyteArray var, val;
  71             const char * valBeg = varEnd + 1;