Skip to content

Commit

Permalink
Fix Java JNI warnings (HDFGroup#4229)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendersonHDF authored Mar 25, 2024
1 parent be16b70 commit 2515bbd
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 298 deletions.
10 changes: 5 additions & 5 deletions java/src/jni/h5aImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,12 +1107,12 @@ Java_hdf_hdf5lib_H5_H5Awrite_1string(JNIEnv *env, jclass clss, jlong attr_id, jl
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5AreadVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf)
{
jbyte *readBuf = NULL;
void *readBuf = NULL;
hsize_t dims[H5S_MAX_RANK];
hid_t sid = H5I_INVALID_HID;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len;
jsize vl_array_len = 0;
htri_t vl_data_class;
herr_t status = FAIL;
htri_t is_variable = 0;
Expand All @@ -1136,7 +1136,7 @@ Java_hdf_hdf5lib_H5_H5AreadVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem
if (NULL == (readBuf = calloc((size_t)vl_array_len, typeSize)))
H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Aread: failed to allocate raw VL read buffer");

if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, (void *)readBuf)) < 0)
if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, readBuf)) < 0)
H5_LIBRARY_ERROR(ENVONLY);
if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
H5_LIBRARY_ERROR(ENVONLY);
Expand Down Expand Up @@ -1173,12 +1173,12 @@ Java_hdf_hdf5lib_H5_H5AreadVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5AwriteVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf)
{
jbyte *writeBuf = NULL;
void *writeBuf = NULL;
hsize_t dims[H5S_MAX_RANK];
hid_t sid = H5I_INVALID_HID;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len;
jsize vl_array_len = 0;
htri_t vl_data_class;
herr_t status = FAIL;
htri_t is_variable = 0;
Expand Down
10 changes: 5 additions & 5 deletions java/src/jni/h5dImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Java_hdf_hdf5lib_H5_H5Dread(JNIEnv *env, jclass clss, jlong dataset_id, jlong me
jbyte *readBuf = NULL;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len; // Only used by vl_data_class types
jsize vl_array_len = 0; // Only used by vl_data_class types
htri_t vl_data_class;
herr_t status = FAIL;

Expand Down Expand Up @@ -266,7 +266,7 @@ Java_hdf_hdf5lib_H5_H5Dwrite(JNIEnv *env, jclass clss, jlong dataset_id, jlong m
jbyte *writeBuf = NULL;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len; // Only used by vl_data_class types
jsize vl_array_len = 0; // Only used by vl_data_class types
htri_t vl_data_class;
herr_t status = FAIL;

Expand Down Expand Up @@ -1134,7 +1134,7 @@ JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5DreadVL(JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id,
jlong mem_space_id, jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
{
jbyte *readBuf = NULL;
void *readBuf = NULL;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ Java_hdf_hdf5lib_H5_H5DreadVL(JNIEnv *env, jclass clss, jlong dataset_id, jlong
H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DreadVL: failed to allocate raw VL read buffer");

if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id, (hid_t)file_space_id,
(hid_t)xfer_plist_id, (void *)readBuf)) < 0)
(hid_t)xfer_plist_id, readBuf)) < 0)
H5_LIBRARY_ERROR(ENVONLY);
if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
H5_LIBRARY_ERROR(ENVONLY);
Expand Down Expand Up @@ -1194,7 +1194,7 @@ JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5DwriteVL(JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id,
jlong mem_space_id, jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
{
jbyte *writeBuf = NULL;
void *writeBuf = NULL;
size_t typeSize;
H5T_class_t type_class;
jsize vl_array_len; // Only used by vl_data_class types
Expand Down
2 changes: 1 addition & 1 deletion java/src/jni/h5eImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ Java_hdf_hdf5lib_H5_H5Eget_1msg(JNIEnv *env, jclass clss, jlong msg_id, jintArra
H5_LIBRARY_ERROR(ENVONLY);
namePtr[buf_size] = '\0';

theArray[0] = error_msg_type;
theArray[0] = (jint)error_msg_type;

if (NULL == (str = ENVPTR->NewStringUTF(ENVONLY, namePtr)))
CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
Expand Down
33 changes: 19 additions & 14 deletions java/src/jni/h5pDCPLImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1chunk(JNIEnv *env, jclass clss, jlong plist, jint ndims, jbyteArray dim)
{
jboolean isCopy;
hsize_t *da = NULL;
hsize_t *lp = NULL;
size_t i;
hsize_t *chunk_dims = NULL;
uint8_t *theArray_p = NULL;
size_t rank;
jsize arrLen;
jbyte *theArray = NULL;
jlong *jlp = NULL;
herr_t status = FAIL;

UNUSED(clss);
Expand All @@ -104,22 +102,29 @@ Java_hdf_hdf5lib_H5_H5Pset_1chunk(JNIEnv *env, jclass clss, jlong plist, jint nd

PIN_BYTE_ARRAY(ENVONLY, dim, theArray, &isCopy, "H5Pset_chunk: dim array not pinned");

if (NULL == (da = lp = (hsize_t *)malloc(rank * sizeof(hsize_t))))
if (NULL == (chunk_dims = malloc(rank * sizeof(hsize_t))))
H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Pset_chunk: memory allocation failed");

jlp = (jlong *)theArray;
for (i = 0; i < rank; i++) {
*lp = (hsize_t)*jlp;
lp++;
jlp++;
} /* end if */
theArray_p = (uint8_t *)theArray;
for (size_t i = 0; i < rank; i++) {
jlong dim_val;

if ((status = H5Pset_chunk((hid_t)plist, (int)ndims, da)) < 0)
memcpy(&dim_val, theArray_p, sizeof(jlong));

if (dim_val < 0)
H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Pset_chunk: chunk dimensions can't be negative");

chunk_dims[i] = (hsize_t)dim_val;

theArray_p += sizeof(jlong);
}

if ((status = H5Pset_chunk((hid_t)plist, (int)ndims, chunk_dims)) < 0)
H5_LIBRARY_ERROR(ENVONLY);

done:
if (da)
free(da);
free(chunk_dims);

if (theArray)
UNPIN_BYTE_ARRAY(ENVONLY, dim, theArray, JNI_ABORT);

Expand Down
6 changes: 3 additions & 3 deletions java/src/jni/h5pFAPLImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,15 +1741,15 @@ Java_hdf_hdf5lib_H5_H5Pget_1mdc_1config(JNIEnv *env, jclass clss, jlong plist)
args[9].j = (jlong)cacheinfo.max_size;
args[10].j = (jlong)cacheinfo.min_size;
args[11].j = cacheinfo.epoch_length;
args[12].i = cacheinfo.incr_mode;
args[12].i = (jint)cacheinfo.incr_mode;
args[13].d = cacheinfo.lower_hr_threshold;
args[14].d = cacheinfo.increment;
args[15].z = cacheinfo.apply_max_increment;
args[16].j = (jlong)cacheinfo.max_increment;
args[17].i = cacheinfo.flash_incr_mode;
args[17].i = (jint)cacheinfo.flash_incr_mode;
args[18].d = cacheinfo.flash_multiple;
args[19].d = cacheinfo.flash_threshold;
args[20].i = cacheinfo.decr_mode;
args[20].i = (jint)cacheinfo.decr_mode;
args[21].d = cacheinfo.upper_hr_threshold;
args[22].d = cacheinfo.decrement;
args[23].z = cacheinfo.apply_max_decrement;
Expand Down
6 changes: 6 additions & 0 deletions java/src/jni/h5rImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ Java_hdf_hdf5lib_H5_H5Rdestroy(JNIEnv *env, jclass clss, jbyteArray ref)
if ((status = H5Rdestroy(&loc_ref)) < 0)
H5_LIBRARY_ERROR(ENVONLY);

/* Reset buffer to 0 to be safe. H5Rdestroy does this, but
* only on our temporary reference object that we copied
* into for alignment reasons.
*/
memset(refBuf, 0, H5R_REF_BUF_SIZE);

done:
if (refBuf)
UNPIN_BYTE_ARRAY(ENVONLY, ref, refBuf, (status < 0) ? JNI_ABORT : 0);
Expand Down
95 changes: 49 additions & 46 deletions java/src/jni/h5sImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,49 +221,52 @@ Java_hdf_hdf5lib_H5_H5Sselect_1elements(JNIEnv *env, jclass clss, jlong space_id
jbyteArray coords)
{
jboolean isCopy;
hsize_t *lp = NULL;
hsize_t *llp = NULL;
jlong *jlp = NULL;
jbyte *P = NULL;
hsize_t *coords_p = NULL;
uint8_t *pinned_arr_p = NULL;
jbyte *pinned_arr = NULL;
jsize size;
int ii;
int nlongs;
size_t nlongs;
herr_t status = FAIL;

UNUSED(clss);

if (NULL == coords)
H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Sselect_elements: coords is NULL");

PIN_BYTE_ARRAY(ENVONLY, coords, P, &isCopy, "H5Sselect_elements: coords not pinned");
PIN_BYTE_ARRAY(ENVONLY, coords, pinned_arr, &isCopy, "H5Sselect_elements: coords not pinned");

if ((size = ENVPTR->GetArrayLength(ENVONLY, coords)) < 0) {
CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Sselect_elements: coords array length < 0");
}

nlongs = (int)((size_t)size / sizeof(jlong));
nlongs = (size_t)size / sizeof(jlong);

if (NULL == (lp = (hsize_t *)malloc((size_t)nlongs * sizeof(hsize_t))))
if (NULL == (coords_p = malloc(nlongs * sizeof(hsize_t))))
H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Sselect_elements: failed to allocate coordinate buffer");

jlp = (jlong *)P;
llp = lp;
for (ii = 0; ii < nlongs; ii++) {
*lp = (hsize_t)*jlp;
lp++;
jlp++;
} /* end for */
pinned_arr_p = (uint8_t *)pinned_arr;
for (size_t i = 0; i < nlongs; i++) {
jlong coord_elem;

memcpy(&coord_elem, pinned_arr_p, sizeof(jlong));

if (coord_elem < 0)
H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Sselect_elements: coordinate element can't be negative");

if ((status = H5Sselect_elements(space_id, (H5S_seloper_t)op, (size_t)num_elemn, (const hsize_t *)llp)) <
0)
coords_p[i] = (hsize_t)coord_elem;

pinned_arr_p += sizeof(jlong);
}

if ((status = H5Sselect_elements(space_id, (H5S_seloper_t)op, (size_t)num_elemn, coords_p)) < 0)
H5_LIBRARY_ERROR(ENVONLY);

done:
if (llp)
free(llp);
if (P)
UNPIN_BYTE_ARRAY(ENVONLY, coords, P, JNI_ABORT);
free(coords_p);

if (pinned_arr)
UNPIN_BYTE_ARRAY(ENVONLY, coords, pinned_arr, JNI_ABORT);

return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5Sselect_1elements */
Expand Down Expand Up @@ -630,49 +633,49 @@ JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Soffset_1simple(JNIEnv *env, jclass clss, jlong space_id, jbyteArray offset)
{
jboolean isCopy;
hssize_t *sa = NULL;
hssize_t *lp = NULL;
hssize_t *offset_dims = NULL;
uint8_t *pinned_arr_p = NULL;
jbyte *pinned_arr = NULL;
size_t rank;
jsize i;
jbyte *P = NULL;
jlong *jlp = NULL;
herr_t status = FAIL;

UNUSED(clss);

if (NULL != offset) {
PIN_BYTE_ARRAY(ENVONLY, offset, P, &isCopy, "H5Soffset_simple: offset not pinned");
jsize size;

PIN_BYTE_ARRAY(ENVONLY, offset, pinned_arr, &isCopy, "H5Soffset_simple: offset not pinned");

if ((i = ENVPTR->GetArrayLength(ENVONLY, offset)) < 0) {
if ((size = ENVPTR->GetArrayLength(ENVONLY, offset)) < 0) {
CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Soffset_simple: offset array length < 0");
}

rank = (size_t)i / sizeof(jlong);
rank = (size_t)size / sizeof(jlong);

if (NULL == (sa = lp = (hssize_t *)malloc((size_t)rank * sizeof(hssize_t))))
if (NULL == (offset_dims = malloc((size_t)rank * sizeof(hssize_t))))
H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Soffset_simple: failed to allocate offset buffer");

jlp = (jlong *)P;
for (i = 0; (size_t)i < rank; i++) {
*lp = (hssize_t)*jlp;
lp++;
jlp++;
} /* end for */
}
else {
P = NULL;
sa = (hssize_t *)P;
pinned_arr_p = (uint8_t *)pinned_arr;
for (size_t i = 0; i < rank; i++) {
jlong offset_elem;

memcpy(&offset_elem, pinned_arr_p, sizeof(jlong));

offset_dims[i] = (hssize_t)offset_elem;

pinned_arr_p += sizeof(jlong);
}
}

if ((status = H5Soffset_simple(space_id, sa)) < 0)
if ((status = H5Soffset_simple(space_id, offset_dims)) < 0)
H5_LIBRARY_ERROR(ENVONLY);

done:
if (sa)
free(sa);
if (P)
UNPIN_BYTE_ARRAY(ENVONLY, offset, P, JNI_ABORT);
free(offset_dims);

if (pinned_arr)
UNPIN_BYTE_ARRAY(ENVONLY, offset, pinned_arr, JNI_ABORT);

return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5Soffset_1simple */
Expand Down
24 changes: 17 additions & 7 deletions java/src/jni/h5tImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,13 @@ Java_hdf_hdf5lib_H5_H5Tget_1fields_1int(JNIEnv *env, jclass clss, jlong type_id,
{
jboolean isCopy;
jsize arrLen;
jint *P = NULL;
herr_t status = FAIL;
size_t spos;
size_t epos;
size_t esize;
size_t mpos;
size_t msize;
jint *pinned_arr = NULL;
herr_t status = FAIL;

UNUSED(clss);

Expand All @@ -462,15 +467,20 @@ Java_hdf_hdf5lib_H5_H5Tget_1fields_1int(JNIEnv *env, jclass clss, jlong type_id,
if (arrLen < 5)
H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Tget_fields_int: fields input array < order 5");

PIN_INT_ARRAY(ENVONLY, fields, P, &isCopy, "H5Tget_fields_int: fields not pinned");
PIN_INT_ARRAY(ENVONLY, fields, pinned_arr, &isCopy, "H5Tget_fields_int: fields not pinned");

if ((status = H5Tget_fields((hid_t)type_id, (size_t *)&(P[0]), (size_t *)&(P[1]), (size_t *)&(P[2]),
(size_t *)&(P[3]), (size_t *)&(P[4]))) < 0)
if ((status = H5Tget_fields((hid_t)type_id, &spos, &epos, &esize, &mpos, &msize)) < 0)
H5_LIBRARY_ERROR(ENVONLY);

pinned_arr[0] = (jint)spos;
pinned_arr[1] = (jint)epos;
pinned_arr[2] = (jint)esize;
pinned_arr[3] = (jint)mpos;
pinned_arr[4] = (jint)msize;

done:
if (P)
UNPIN_INT_ARRAY(ENVONLY, fields, P, (status < 0) ? JNI_ABORT : 0);
if (pinned_arr)
UNPIN_INT_ARRAY(ENVONLY, fields, pinned_arr, (status < 0) ? JNI_ABORT : 0);

return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5Tget_1fields_1int */
Expand Down
Loading

0 comments on commit 2515bbd

Please sign in to comment.