Anonymous avatar Anonymous committed 30246cb

Fixes after code review

Comments (0)

Files changed (6)

src/FastbitCursor.cc

 
 inline static void throwException(JNIEnv * env, const char * msg)
 {
-    jclass newExcCls;
-    newExcCls = env->FindClass("com/globalpark/fastbit/exception/FastbitCursorException");
-    env->ThrowNew(newExcCls, msg);      
+    throwNamedException(env, "com/globalpark/fastbit/exception/FastbitCursorException", msg);
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitCursor_deleteCppObject(JNIEnv * env, jobject obj, jlong cp)
 {
-    if(!cp)
+    ibis::table::cursor * cur = secure_cast<ibis::table::cursor *>(cp);
+    if(!cur)
     {
         throwNullPointerException(env);
         return;
     }
-    
-    ibis::table::cursor * cur = reinterpret_cast<ibis::table::cursor *>(cp);
     delete cur;
 }
 
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitCursor_getCursor_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    ibis::table * table;
+    ibis::table::cursor * cur = 0;
 
-    if(!tp)
+    ibis::table * table = secure_cast<ibis::table *>(tp);
+
+    if(!table)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    table = reinterpret_cast<ibis::table *>(tp);
-    ibis::table::cursor * cur = table->createCursor();
-    return reinterpret_cast<jlong>(cur); 
+    try {
+        cur = table->createCursor();
+        return reinterpret_cast<jlong>(cur);
+    }
+    CATCH_DEFAULT("Create cursor failed");
+
+    if(!cur)
+    {
+        throwException(env, "Create cursor failed");
+    }
+    return 0;
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitCursor_next_1native(JNIEnv * env, jobject obj, jlong cp)
 {
-    ibis::table::cursor * cur;
+    ibis::table::cursor * cur = secure_cast<ibis::table::cursor *>(cp);
 
-    if(!cp)
+    if(!cur)
     {
         throwNullPointerException(env);
         return;
     }
-
-    cur = reinterpret_cast<ibis::table::cursor *>(cp);
     try {
         if(cur->fetch() == -1)
         {
 
 JNIEXPORT jboolean JNICALL Java_com_globalpark_fastbit_FastbitCursor_hasNext_1native(JNIEnv * env, jobject obj, jlong cp)
 {
-    if(!cp)
+    ibis::table::cursor * cur = secure_cast<ibis::table::cursor *>(cp);
+
+    if(!cur)
     {
         throwNullPointerException(env);
         return false;
 
     try
     {
-        ibis::table::cursor * cur;
-        cur = reinterpret_cast<ibis::table::cursor *>(cp);
         if(cur->nRows() == 0) return false;
         if((int)cur->getCurrentRowNumber() == -1) return true;
         return (jboolean) (cur->getCurrentRowNumber() < (cur->nRows() - 1));
     }
     CATCH_DEFAULT("hasNext failed");
+    return false;
 }

src/FastbitPartition.cc

 
 inline static void throwException(JNIEnv * env, const char * msg)
 {
-    jclass newExcCls;
-    newExcCls = env->FindClass("com/globalpark/fastbit/exception/FastbitPartitionException");
-    env->ThrowNew(newExcCls, msg);      
+    throwNamedException(env, "com/globalpark/fastbit/exception/FastbitPartitionException", msg);
 }
 
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitPartition_createCppObject(JNIEnv * env, jobject obj, jstring path)
 {
+  const char * datadir = 0;
   try
   {
-      const char * datadir = env->GetStringUTFChars(path, NULL);
+      datadir = env->GetStringUTFChars(path, NULL);
       
       ibis::part * partition = new ibis::part(datadir,true);
       env->ReleaseStringUTFChars(path, datadir);
       return reinterpret_cast<jlong>(partition);
   }
   CATCH_DEFAULT("createCppObject failed");
+  env->ReleaseStringUTFChars(path, datadir);
+  return 0;
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitPartition_deleteCppObject(JNIEnv * env, jobject obj, jlong p)
 {
-    if(!p)
-    {
-        throwNullPointerException(env);
-        return;
-    }
 
-  ibis::part * partition = reinterpret_cast<ibis::part *>(p);
+  ibis::part * partition = 0;
+  if(!(partition = secure_cast<ibis::part *>(p)))
+  {
+      throwNullPointerException(env);
+      return;
+  }
   delete partition;
 }

src/FastbitResultRow.cc

 #define SETUPENV(type)                                  \
     ibis::table::cursor * cur;                          \
     const char * cname;                                 \
-    cur = reinterpret_cast<ibis::table::cursor *>(cp);  \
+    cur = secure_cast<ibis::table::cursor *>(cp);       \
     cname = env->GetStringUTFChars(arg_cname, NULL);    \
-    type val;
+    type val;                                           \
+
 
 #define CLEARENV  \
   env->ReleaseStringUTFChars(arg_cname, cname);
     
 inline static void throwException(JNIEnv * env, const char * msg)
 {
-    jclass newExcCls;
-    newExcCls = env->FindClass("com/globalpark/fastbit/exception/FastbitResultRowException");
-    env->ThrowNew(newExcCls, msg);      
+    throwNamedException(env, "com/globalpark/fastbit/exception/FastbitResultRowException", msg);
 }
 
 
 JNIEXPORT jshort JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsShort_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(int16_t);
+    val = 0;
+    if(!cur)
     {
-        throwNullPointerException(env);
-        return 0;
+      throwNullPointerException(env);
+      return 0;
     }
-
-    SETUPENV(int16_t);
     try
     {
         cur->getColumnAsShort(cname, val);
 
 JNIEXPORT jint JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsUShort_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(uint16_t);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(uint16_t);
     try
     {
         cur->getColumnAsUShort(cname, val);
 
 JNIEXPORT jint JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsInt_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(int32_t);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(int32_t);
     try
     {
         cur->getColumnAsInt(cname, val);
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsUInt_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(uint32_t);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(uint32_t);
     try
     {
         cur->getColumnAsUInt(cname, val);
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsLong_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(int64_t);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(int64_t);
     try
     {
         cur->getColumnAsLong(cname, val);
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsULong_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(uint64_t);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(uint64_t);
     try
     {
         cur->getColumnAsULong(cname, val);
     }
     CATCH_DEFAULT("getColumnAsULong failed");    
     CLEARENV;
-    return (jlong)val;
+    return (jlong)val; // remark: if value is bigger than pow(2,63), the return value will be wrong!
 }
 
 JNIEXPORT jfloat JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsFloat_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(float);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(float);
     try
     {
         cur->getColumnAsFloat(cname, val);
     }
     CATCH_DEFAULT("getColumnAsFloat failed");    
     CLEARENV;
-    return (jfloat)val;
+    return (jfloat)val;  // this assumes that the C float type has the same range as the Java float type. No easy workaround so leave this as is
 }
 
 JNIEXPORT jdouble JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsDouble_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+
+    SETUPENV(double);
+    val = 0;
+    if(!cur)
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    SETUPENV(double);
     try
     {
         cur->getColumnAsDouble(cname, val);
     }
     CATCH_DEFAULT("getColumnAsDouble failed");    
     CLEARENV;
-    return (jdouble)val;
+    return (jdouble)val; // this assumes that the C double type has the same range as the Java double type. No easy workaround so leave this as is.
 }
 
 JNIEXPORT jstring JNICALL Java_com_globalpark_fastbit_FastbitResultRow_getColumnAsString_1native(JNIEnv * env, jobject obj, jlong cp, jstring arg_cname)
 {
-    if(!cp)
+    SETUPENV(string);
+    val = "";
+    if(!cur)
     {
         throwNullPointerException(env);
         return NULL;
     }
 
-    SETUPENV(string);
     try
     {
         cur->getColumnAsString(cname, val);
 
 JNIEXPORT jstring JNICALL Java_com_globalpark_fastbit_FastbitResultRow_asString_1native(JNIEnv * env, jobject obj, jlong cp)
 {
-    if(!cp)
+    ostringstream val;
+
+    ibis::table::cursor * cur = 0;  
+    if(!(cur = secure_cast<ibis::table::cursor *>(cp)))
     {
         throwNullPointerException(env);
         return NULL;
     }
 
-    ibis::table::cursor * cur;                          
-    const char * cname;                                 
-    ostringstream val;
-    cur = reinterpret_cast<ibis::table::cursor *>(cp);  
     try
     {
         cur->dump( val);
         return env->NewStringUTF(val.str().c_str());    
     }
     CATCH_DEFAULT("asAsSstring failed");    
+    return NULL;
 }
 
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitResultRow_dump_1native(JNIEnv * env, jobject obj, jlong cp, jobject stream)
 {
-    if(!cp)
+    ostringstream val;
+
+    ibis::table::cursor * cur = 0;
+
+    if(!(cur = secure_cast<ibis::table::cursor *>(cp)))
     {
         throwNullPointerException(env);
         return;
     }
-    ibis::table::cursor * cur;
-    const char * cname;                          
-    ostringstream val;
+
     try
     {
-        cur = reinterpret_cast<ibis::table::cursor *>(cp);
         cur->dump( val);
         jclass jstreamClass = env->GetObjectClass(stream);
         jmethodID mid = env->GetMethodID(jstreamClass, "write", "(Ljava/lang/String;)V");

src/FastbitTable.cc

 
 inline static void throwException(JNIEnv * env, const char * msg)
 {
-    jclass newExcCls;
-    newExcCls = env->FindClass("com/globalpark/fastbit/exception/FastbitTableException");
-    env->ThrowNew(newExcCls, msg);      
+    throwNamedException(env, "com/globalpark/fastbit/exception/FastbitTableException", msg);
 }
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTable_createCppObject(JNIEnv * env, jobject self, jobject partition)
 {
     try 
     {
-        ibis::table * table;
+        ibis::table * table = 0;
         jclass p = env->GetObjectClass(partition);
         
         jfieldID fid = env->GetFieldID(p,"obj", "J");
         
         jlong ptr = env->GetLongField(partition, fid);
-        ibis::part * part = reinterpret_cast<ibis::part *>(ptr);
+        ibis::part * part = 0;
         
-        if(!part)
+        if(!( part = secure_cast<ibis::part *>(ptr)))
         {
             throwNullPointerException(env);
             return 0;
         return reinterpret_cast<jlong>(table); 
     }
     CATCH_DEFAULT("Failed to read partition");
+    return 0;
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTable_deleteCppObject(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
+    ibis::table * table = 0;
+    if(!(table = secure_cast<ibis::table *>(tp)))
     {
         throwNullPointerException(env);
         return;
     }
-    ibis::table * table = reinterpret_cast<ibis::table *>(tp);
     delete table;
 }
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTable_select_1native(JNIEnv * env, jobject obj, jlong tp, jstring arg_selects, jstring arg_wheres)
 {
-    const char * selects;
-    const char * wheres;
-    ibis::table * table;
+    const char * selects = 0;
+    const char * wheres = 0;
     ibis::table * select = 0;
-    
-    if(!tp)
+
+    ibis::table * table = 0;
+
+    if(!(table = secure_cast<ibis::table *>(tp)))
     {
         throwNullPointerException(env);
         return 0;
     }
 
-
-    table = reinterpret_cast<ibis::table *>(tp);
     selects = env->GetStringUTFChars(arg_selects, NULL);
     wheres = env->GetStringUTFChars(arg_wheres, NULL);
+
     try 
     {
         select = table->select(selects, wheres);
-    }
-    catch(char * errstr)
-    {
-        throwException(env, errstr);        
+        if(!select)
+        {
+            throw "Select failed";
+        }
+
     }
     CATCH_DEFAULT("Select failed");
     
-    if(!select)
-    {
-        throwException(env, "Select failed");
-    }
-    
     env->ReleaseStringUTFChars(arg_selects, selects);
     env->ReleaseStringUTFChars(arg_wheres, wheres);
     return reinterpret_cast<jlong>(select); 
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTable_groupby_1native(JNIEnv * env, jobject obj, jlong tp, jstring arg_groupbys)
 {
-    const char * groupbys;
-    ibis::table * table;
-    ibis::table* groupby=0;
+    const char * groupbys = 0;
+    ibis::table* groupby = 0;
 
-    if(!tp)
+    ibis::table * table = 0;
+
+    if(!(table = secure_cast<ibis::table *>(tp)))
     {
         throwNullPointerException(env);
         return 0;
     }
 
-    table = reinterpret_cast<ibis::table *>(tp);
     groupbys = env->GetStringUTFChars(arg_groupbys, NULL);
     try 
     {
         groupby = table->groupby(groupbys);
+        if(!groupby) 
+        {
+            throw "Group by failed";
+        }
+
     }
     CATCH_DEFAULT("Group by failed");
 
-    if(!groupby) 
-    {
-        throwException(env, "Group by failed");      
-    }
     env->ReleaseStringUTFChars(arg_groupbys, groupbys);  
     return reinterpret_cast<jlong>(groupby); 
-
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTable_orderby_1native(JNIEnv * env, jobject obj, jlong tp, jstring arg_orderbys)
 {
-    const char * orderbys;
-    ibis::table * table;
+    const char * orderbys = 0;
+    ibis::table * table = 0;
 
-    if(!tp)
+    if(!(table = secure_cast<ibis::table *>(tp)))
     {
         throwNullPointerException(env);
         return;
     }
 
-    table = reinterpret_cast<ibis::table *>(tp);
     orderbys = env->GetStringUTFChars(arg_orderbys, NULL);
-    try 
+    try
     {
         table->orderby(orderbys);
     }
     CATCH_DEFAULT("Order by failed");
     env->ReleaseStringUTFChars(arg_orderbys, orderbys);  
-
 }
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTable_nrows_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return 0;
-    }
-
     try
     {
-        ibis::table * table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return 0;
+        }
         return table->nRows();
     }
     CATCH_DEFAULT("nrows failed");
+    return 0;
 }
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTable_ncolumns_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return 0;
-    }
-
     try
     {
-        ibis::table * table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return 0;
+        }
         return table->nColumns();
     }
     CATCH_DEFAULT("ncolumns failed")
+    return 0;
 }
 
 JNIEXPORT jobjectArray JNICALL Java_com_globalpark_fastbit_FastbitTable_columnnames_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return NULL;
-    }
 
     try 
     {
-        ibis::table * table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return NULL;
+        }
         ibis::table::stringList nms = table->columnNames();
 
         jclass stringClass = env->FindClass("java/lang/String");
         return result;
     }
     CATCH_DEFAULT("columnnames failed");
+    return NULL;
 }
 
 JNIEXPORT jlongArray JNICALL Java_com_globalpark_fastbit_FastbitTable_columntypes_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return NULL;
-    }
-
     try 
     {
-        ibis::table * table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return NULL;
+        }
         ibis::table::typeList tps = table->columnTypes();
         jlongArray result = env->NewLongArray((jsize)tps.size());
         ibis::TYPE_T * t = tps.begin();
         return result;
     }
     CATCH_DEFAULT("columntypes failed");
+    return NULL;
 }
 
 JNIEXPORT jstring JNICALL Java_com_globalpark_fastbit_FastbitTable_asString_1native(JNIEnv * env, jobject obj, jlong tp)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return NULL;
-    }
 
     try
     {
-        ibis::table * table;
-        const char * cname;
         ostringstream val;
-        table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table  = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return NULL;
+        }
+
         table->dump( val);
         return env->NewStringUTF(val.str().c_str());    
     }
     CATCH_DEFAULT("asString failed");
+    return NULL;
 }
 
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTable_dump_1native(JNIEnv * env, jobject obj, jlong tp, jobject stream)
 {
-    if(!tp)
-    {
-        throwNullPointerException(env);
-        return;
-    }
-
     try
     {
-        ibis::table * table;
-        const char * cname;
         ostringstream val;
-        table = reinterpret_cast<ibis::table *>(tp);
+        ibis::table * table = 0;
+        if(!(table = secure_cast<ibis::table *>(tp)))
+        {
+            throwNullPointerException(env);
+            return;
+        }
+        
         table->dump( val);
         jclass jstreamClass = env->GetObjectClass(stream);
+        if(!jstreamClass)
+        {
+            throw "Invalid stream";
+            return;
+        }
         jmethodID mid = env->GetMethodID(jstreamClass, "write", "(Ljava/lang/String;)V");
+
+        if(!mid) // Stream has no write method
+        {
+            throw "Invalid stream";
+            return;
+        }
+       
         jstring str = env->NewStringUTF(val.str().c_str());
         env->CallVoidMethod(stream, mid, str);
     }

src/FastbitTablex.cc

 
 inline static void throwException(JNIEnv * env, const char * msg)
 {
-    jclass newExcCls;
-    newExcCls = env->FindClass("com/globalpark/fastbit/exception/FastbitTablexException");
-    env->ThrowNew(newExcCls, msg);      
+    throwNamedException(env, "com/globalpark/fastbit/exception/FastbitTablexException", msg);
 }
 
 JNIEXPORT jlong JNICALL Java_com_globalpark_fastbit_FastbitTablex_createCppObject(JNIEnv * env, jobject obj)
         return reinterpret_cast<jlong>(table); 
     }
     CATCH_DEFAULT("createCppObject failed");
+    return 0;
 }
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTablex_deleteCppObject(JNIEnv * env, jobject obj, jlong p)
 {
-    if(!p)
+    ibis::tablex * table = 0;
+    if(!(table = secure_cast<ibis::tablex *>(p)))
     {
         throwNullPointerException(env);
         return;
     }
-    ibis::tablex * table = reinterpret_cast<ibis::tablex *>(p);
     delete table;
 }
 
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTablex_importCSV_1native(JNIEnv * env, jobject obj, jlong p, jstring csvfilename_arg, 
                                                                                    jstring targetdir_arg, jstring metadatastring_arg, jstring delimiters_arg)
 {
-    ibis::tablex * table;
-    const char * metadatastring;
-    const char * targetdir;
-    const char * csvfilename;
-    const char * delimiters;
+    ibis::tablex * table = 0;
+    const char * metadatastring = 0;
+    const char * targetdir = 0;
+    const char * csvfilename = 0;
+    const char * delimiters = 0;
     const char * tablename = 0;
     const char * tabledesc = 0;
     const char * idx = 0;
     int nrpf = 0;
     int err;
 
-    if(!p)
+    if (!(table = secure_cast<ibis::tablex *>(p)))
     {
-        throwNullPointerException(env);
-        return;
+        throwException(env, "Invalid pointer");
+        return; 
     }
-
+ 
     try
     {
-        table = reinterpret_cast<ibis::tablex *>(p);
         metadatastring = env->GetStringUTFChars(metadatastring_arg, NULL);
         csvfilename = env->GetStringUTFChars(csvfilename_arg, NULL);
         targetdir = env->GetStringUTFChars(targetdir_arg, NULL);
 JNIEXPORT void JNICALL Java_com_globalpark_fastbit_FastbitTablex_appendRow_1native(JNIEnv * env, jobject obj, jlong p, jstring row_arg, 
                                                                                    jstring targetdir_arg, jstring metadatastring_arg,jstring delimiters_arg)
 {
-    ibis::tablex * table;
-    const char * metadatastring;
-    const char * targetdir;
-    const char * row;
-    const char * delimiters;
+    ibis::tablex * table = 0;
+    const char * metadatastring = 0;
+    const char * targetdir = 0;
+    const char * row = 0;
+    const char * delimiters = 0;
     const char * tablename = 0;
     const char * tabledesc = 0;
     const char * idx = 0;
     int nrpf = 0;
     int err;
 
-    if(!p)
-    {
-        throwNullPointerException(env);
-        return;
-    }
-
     try
     {
-        table = reinterpret_cast<ibis::tablex *>(p);
+        if (!(table = secure_cast<ibis::tablex *>(p)))
+        {
+            throwNullPointerException(env);
+            return;
+        }
         metadatastring = env->GetStringUTFChars(metadatastring_arg, NULL);
         row = env->GetStringUTFChars(row_arg, NULL);
         targetdir = env->GetStringUTFChars(targetdir_arg, NULL);
 #ifndef _MACROS_H_
 #define _MACROS_H_
+#include <typeinfo>
 #include <exception>
 
+using namespace std;
+
 #define CATCH_DEFAULT(msg)                               \
-    catch(const char * errstr)                                 \
-    {                                                    \
-        throwException(env, errstr);                     \
-    }                                                    \
-    catch(std::exception&  err)                          \
-    {                                                    \
+  catch(const char * errstr)                             \
+  {                                                      \
+      throwException(env, errstr);                       \
+  }                                                      \
+  catch(std::exception&  err)                            \
+  {                                                      \
       throwException(env, err.what());                   \
-    }                                                    \
-    catch(...)                                           \
-    {                                                    \
-        throwException(env, msg);                        \
-    }                                                    \
-
+  }                                                      \
+  catch(...)                                             \
+  {                                                      \
+      throwException(env, msg);                          \
+  }                                                      \
+  
 inline static void throwNullPointerException(JNIEnv * env)
 {
     jclass newExcCls;
     env->DeleteLocalRef(newExcCls);
 }
 
+template <typename T> T secure_cast(jlong p)
+{
+    T ret;
+    if (!p)
+    {
+        return NULL;
+    }
+    ret = reinterpret_cast<T>(p);
+    if (typeid(ret)!=typeid(T))
+    {
+        return NULL;
+    }
+
+    return ret;
+}
+
+inline void throwNamedException(JNIEnv * env, const char * classpath, const char * msg)
+{
+    jclass newExcCls;
+    newExcCls = env->FindClass(classpath);
+    if(!newExcCls)
+    {
+        newExcCls = env->FindClass("java/lang/RuntimeException");
+    }
+    env->ThrowNew(newExcCls, msg);      
+}
 
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.