< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp

Print this page
rev 52432 : 8213622: Windows VS2013 build failure - "'snprintf': identifier not found"
Summary:
Reviewed-by:


   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 #include <stdlib.h>

  26 
  27 #include "ExceptionCheckingJniEnv.hpp"
  28 
  29 namespace {
  30 
  31 template<class T = void*>
  32 class JNIVerifier {
  33  public:
  34   JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg)
  35       : _env(env), _base_msg(base_msg), _return_error(NULL) {
  36   }
  37 
  38   ~JNIVerifier() {
  39     JNIEnv* jni_env = _env->GetJNIEnv();
  40     if (jni_env->ExceptionCheck()) {
  41       _env->HandleError(_base_msg);
  42       return;
  43     }
  44 
  45     if (_return_error != NULL) {
  46       ProcessReturnError();
  47     }
  48   }
  49 
  50   void ProcessReturnError() {
  51     int len = snprintf(NULL, 0, "%s : %s", _base_msg, _return_error) + 1;











  52 
  53     if (len <= 0) {
  54       _env->HandleError(_return_error);
  55       return;
  56     }
  57 
  58     char* full_message = (char*) malloc(len);
  59     if (full_message == NULL) {
  60       _env->HandleError(_return_error);
  61       return;
  62     }
  63 
  64     snprintf(full_message, len, "%s : %s", _base_msg, _return_error);











  65 
  66     _env->HandleError(full_message);
  67     free(full_message);
  68   }
  69 
  70   T ResultNotNull(T ptr) {
  71     if (ptr == NULL) {
  72       _return_error = "Return is NULL";
  73     }
  74     return ptr;
  75   }
  76 
  77  private:
  78   ExceptionCheckingJniEnv* _env;
  79   const char* const _base_msg;
  80   const char* _return_error;
  81 };
  82 
  83 }
  84 




   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 #include <stdlib.h>
  26 #include <string.h>
  27 
  28 #include "ExceptionCheckingJniEnv.hpp"
  29 
  30 namespace {
  31 
  32 template<class T = void*>
  33 class JNIVerifier {
  34  public:
  35   JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg)
  36       : _env(env), _base_msg(base_msg), _return_error(NULL) {
  37   }
  38 
  39   ~JNIVerifier() {
  40     JNIEnv* jni_env = _env->GetJNIEnv();
  41     if (jni_env->ExceptionCheck()) {
  42       _env->HandleError(_base_msg);
  43       return;
  44     }
  45 
  46     if (_return_error != NULL) {
  47       ProcessReturnError();
  48     }
  49   }
  50 
  51   void ProcessReturnError() {
  52     // This is error prone, but:
  53     //   - Seems like we cannot use std::string (due to windows/solaris not
  54     //   building when used, seemingly due to exception libraries not linking).
  55     //   - Seems like we cannot use sprintf due to VS2013 (JDK-8213622).
  56     //
  57     //   We are aiming to do:
  58     //     snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
  59     //   but will use strlen + strncpy instead.
  60     size_t base_len = strlen(_base_msg);
  61     const char* between_msg = " : ";
  62     size_t between_len = strlen(between_msg);
  63     size_t return_len = strlen(_return_error) + 1;
  64 
  65     size_t len = base_len + between_len + return_len;



  66 
  67     char* full_message = (char*) malloc(len);
  68     if (full_message == NULL) {
  69       _env->HandleError(_return_error);
  70       return;
  71     }
  72 
  73     // Now we construct the string using strcpy to not use sprintf/std::string
  74     // instead of:
  75     //     snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
  76     strncpy(full_message, _base_msg, base_len);
  77     strncpy(full_message + base_len, between_msg, between_len);
  78     strncpy(full_message + base_len + between_len, _return_error, return_len);
  79     full_message[len] = '\0';
  80 
  81     // -1 due to the '\0' not counted by strlen but is counted for the allocation.
  82     if (strlen(full_message) != len - 1) {
  83       _env->GetJNIEnv()->FatalError("Length of message is not what was expected");
  84     }
  85 
  86     _env->HandleError(full_message);
  87     free(full_message);
  88   }
  89 
  90   T ResultNotNull(T ptr) {
  91     if (ptr == NULL) {
  92       _return_error = "Return is NULL";
  93     }
  94     return ptr;
  95   }
  96 
  97  private:
  98   ExceptionCheckingJniEnv* _env;
  99   const char* const _base_msg;
 100   const char* _return_error;
 101 };
 102 
 103 }
 104 


< prev index next >