aboutsummaryrefslogtreecommitdiffstats
path: root/include/jau
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-11-26 18:21:31 +0100
committerSven Gothel <[email protected]>2022-11-26 18:21:31 +0100
commit5704719f0296320d3d494bacee66c425d5daf2c5 (patch)
tree053ccbcfc645c27f979bd61c8ad0376274bfd752 /include/jau
parentb2f9af7c56cb860dfe5942d003e4e47a96d69ac2 (diff)
clang-tidy: helper_jni.hpp: Cover potential size_t -> int/jsize overflow
Diffstat (limited to 'include/jau')
-rw-r--r--include/jau/jni/helper_jni.hpp40
1 files changed, 28 insertions, 12 deletions
diff --git a/include/jau/jni/helper_jni.hpp b/include/jau/jni/helper_jni.hpp
index 279eb08..3e891de 100644
--- a/include/jau/jni/helper_jni.hpp
+++ b/include/jau/jni/helper_jni.hpp
@@ -26,6 +26,7 @@
#ifndef JAU_HELPER_JNI_HPP_
#define JAU_HELPER_JNI_HPP_
+#include <limits>
#include <vector>
#include <memory>
#include <functional>
@@ -723,12 +724,17 @@ namespace jau::jni {
jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array,
const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, const std::shared_ptr<U>&)> ctor)
{
- unsigned int array_size = array.size();
+ const size_t array_size = array.size();
+ if( array_size > std::numeric_limits<jsize>::max() ) {
+ throw jau::RuntimeException("Native array size "+std::to_string(array_size)+
+ " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()), E_FILE_LINE);
+ }
+ const jsize jarray_size = array_size;
jmethodID arraylist_add;
- jobject result = get_new_arraylist(env, array_size, &arraylist_add);
+ jobject result = get_new_arraylist(env, jarray_size, &arraylist_add);
- if (array_size == 0)
+ if (jarray_size == 0)
{
return result;
}
@@ -736,7 +742,7 @@ namespace jau::jni {
jclass clazz = search_class(env, U::java_class().c_str());
jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false);
- for (unsigned int i = 0; i < array_size; ++i)
+ for (jsize i = 0; i < jarray_size; ++i)
{
jobject object = ctor(env, clazz, clazz_ctor, array[i] /* const std::shared_ptr<U>& */);
if (!object)
@@ -752,17 +758,22 @@ namespace jau::jni {
template <typename T, typename U>
jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array, std::function<jobject(JNIEnv*, const std::shared_ptr<U>&)> ctor)
{
- unsigned int array_size = array.size();
+ const size_t array_size = array.size();
+ if( array_size > std::numeric_limits<jsize>::max() ) {
+ throw jau::RuntimeException("Native array size "+std::to_string(array_size)+
+ " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()), E_FILE_LINE);
+ }
+ const jsize jarray_size = array_size;
jmethodID arraylist_add;
- jobject result = get_new_arraylist(env, array_size, &arraylist_add);
+ jobject result = get_new_arraylist(env, jarray_size, &arraylist_add);
- if (array_size == 0)
+ if (jarray_size == 0)
{
return result;
}
- for (unsigned int i = 0; i < array_size; ++i)
+ for (jsize i = 0; i < jarray_size; ++i)
{
jobject object = ctor(env, array[i] /* const std::shared_ptr<U>& */);
if (!object)
@@ -778,17 +789,22 @@ namespace jau::jni {
template <typename T, typename U>
jobject convert_vector_to_jarraylist(JNIEnv *env, T& array, std::function<jobject(JNIEnv*, const U&)> ctor)
{
- unsigned int array_size = array.size();
+ const size_t array_size = array.size();
+ if( array_size > std::numeric_limits<jsize>::max() ) {
+ throw jau::RuntimeException("Native array size "+std::to_string(array_size)+
+ " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()), E_FILE_LINE);
+ }
+ const jsize jarray_size = array_size;
jmethodID arraylist_add;
- jobject result = get_new_arraylist(env, array_size, &arraylist_add);
+ jobject result = get_new_arraylist(env, jarray_size, &arraylist_add);
- if (array_size == 0)
+ if (jarray_size == 0)
{
return result;
}
- for (unsigned int i = 0; i < array_size; ++i)
+ for (jsize i = 0; i < jarray_size; ++i)
{
jobject object = ctor(env, array[i] /* const U& */);
if (!object)