From 94e43a1dfec18c00679deaaab8ed90003355405a Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Wed, 17 Sep 2014 12:30:06 -0700 Subject: [PATCH] [Java] Fixed 32-bit overflowing issue when converting jlong to size_t Summary: Fixed 32-bit overflowing issue when converting jlong to size_t by capping jlong to std::numeric_limits::max(). Test Plan: make rocksdbjava make jtest Reviewers: ankgup87, ljin, sdong, igor Reviewed By: igor Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D23511 --- java/rocksjni/memtablejni.cc | 7 ++++--- java/rocksjni/options.cc | 19 ++++++++++--------- java/rocksjni/portal.h | 7 +++++++ java/rocksjni/write_batch.cc | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/java/rocksjni/memtablejni.cc b/java/rocksjni/memtablejni.cc index a0d50f5f5..9b0dc252c 100644 --- a/java/rocksjni/memtablejni.cc +++ b/java/rocksjni/memtablejni.cc @@ -5,6 +5,7 @@ // // This file implements the "bridge" between Java and C++ for MemTables. +#include "rocksjni/portal.h" #include "include/org_rocksdb_HashSkipListMemTableConfig.h" #include "include/org_rocksdb_HashLinkedListMemTableConfig.h" #include "include/org_rocksdb_VectorMemTableConfig.h" @@ -20,7 +21,7 @@ jlong Java_org_rocksdb_HashSkipListMemTableConfig_newMemTableFactoryHandle( JNIEnv* env, jobject jobj, jlong jbucket_count, jint jheight, jint jbranching_factor) { return reinterpret_cast(rocksdb::NewHashSkipListRepFactory( - static_cast(jbucket_count), + rocksdb::jlong_to_size_t(jbucket_count), static_cast(jheight), static_cast(jbranching_factor))); } @@ -33,7 +34,7 @@ jlong Java_org_rocksdb_HashSkipListMemTableConfig_newMemTableFactoryHandle( jlong Java_org_rocksdb_HashLinkedListMemTableConfig_newMemTableFactoryHandle( JNIEnv* env, jobject jobj, jlong jbucket_count) { return reinterpret_cast(rocksdb::NewHashLinkListRepFactory( - static_cast(jbucket_count))); + rocksdb::jlong_to_size_t(jbucket_count))); } /* @@ -44,7 +45,7 @@ jlong Java_org_rocksdb_HashLinkedListMemTableConfig_newMemTableFactoryHandle( jlong Java_org_rocksdb_VectorMemTableConfig_newMemTableFactoryHandle( JNIEnv* env, jobject jobj, jlong jreserved_size) { return reinterpret_cast(new rocksdb::VectorRepFactory( - static_cast(jreserved_size))); + rocksdb::jlong_to_size_t(jreserved_size))); } /* diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index da420c78f..a72eecd28 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -70,7 +70,7 @@ jboolean Java_org_rocksdb_Options_createIfMissing( void Java_org_rocksdb_Options_setWriteBufferSize( JNIEnv* env, jobject jobj, jlong jhandle, jlong jwrite_buffer_size) { reinterpret_cast(jhandle)->write_buffer_size = - static_cast(jwrite_buffer_size); + rocksdb::jlong_to_size_t(jwrite_buffer_size); } @@ -362,7 +362,7 @@ jlong Java_org_rocksdb_Options_maxLogFileSize( void Java_org_rocksdb_Options_setMaxLogFileSize( JNIEnv* env, jobject jobj, jlong jhandle, jlong max_log_file_size) { reinterpret_cast(jhandle)->max_log_file_size = - static_cast(max_log_file_size); + rocksdb::jlong_to_size_t(max_log_file_size); } /* @@ -383,7 +383,7 @@ jlong Java_org_rocksdb_Options_logFileTimeToRoll( void Java_org_rocksdb_Options_setLogFileTimeToRoll( JNIEnv* env, jobject jobj, jlong jhandle, jlong log_file_time_to_roll) { reinterpret_cast(jhandle)->log_file_time_to_roll = - static_cast(log_file_time_to_roll); + rocksdb::jlong_to_size_t(log_file_time_to_roll); } /* @@ -404,7 +404,7 @@ jlong Java_org_rocksdb_Options_keepLogFileNum( void Java_org_rocksdb_Options_setKeepLogFileNum( JNIEnv* env, jobject jobj, jlong jhandle, jlong keep_log_file_num) { reinterpret_cast(jhandle)->keep_log_file_num = - static_cast(keep_log_file_num); + rocksdb::jlong_to_size_t(keep_log_file_num); } /* @@ -509,7 +509,8 @@ void Java_org_rocksdb_Options_setTableCacheRemoveScanCountLimit( void Java_org_rocksdb_Options_useFixedLengthPrefixExtractor( JNIEnv* env, jobject jobj, jlong jhandle, jint jprefix_length) { reinterpret_cast(jhandle)->prefix_extractor.reset( - rocksdb::NewFixedPrefixTransform(static_cast(jprefix_length))); + rocksdb::NewFixedPrefixTransform( + rocksdb::jlong_to_size_t(jprefix_length))); } /* @@ -573,7 +574,7 @@ jlong Java_org_rocksdb_Options_manifestPreallocationSize( void Java_org_rocksdb_Options_setManifestPreallocationSize( JNIEnv* env, jobject jobj, jlong jhandle, jlong preallocation_size) { reinterpret_cast(jhandle)->manifest_preallocation_size = - static_cast(preallocation_size); + rocksdb::jlong_to_size_t(preallocation_size); } /* @@ -1245,7 +1246,7 @@ jlong Java_org_rocksdb_Options_arenaBlockSize( void Java_org_rocksdb_Options_setArenaBlockSize( JNIEnv* env, jobject jobj, jlong jhandle, jlong jarena_block_size) { reinterpret_cast(jhandle)->arena_block_size = - static_cast(jarena_block_size); + rocksdb::jlong_to_size_t(jarena_block_size); } /* @@ -1410,7 +1411,7 @@ void Java_org_rocksdb_Options_setInplaceUpdateNumLocks( jlong jinplace_update_num_locks) { reinterpret_cast( jhandle)->inplace_update_num_locks = - static_cast(jinplace_update_num_locks); + rocksdb::jlong_to_size_t(jinplace_update_num_locks); } /* @@ -1501,7 +1502,7 @@ void Java_org_rocksdb_Options_setMaxSuccessiveMerges( JNIEnv* env, jobject jobj, jlong jhandle, jlong jmax_successive_merges) { reinterpret_cast(jhandle)->max_successive_merges = - static_cast(jmax_successive_merges); + rocksdb::jlong_to_size_t(jmax_successive_merges); } /* diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 28fe754f0..4c7a8b9b9 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -11,12 +11,19 @@ #define JAVA_ROCKSJNI_PORTAL_H_ #include +#include #include "rocksdb/db.h" #include "rocksdb/filter_policy.h" #include "rocksdb/utilities/backupable_db.h" namespace rocksdb { +inline size_t jlong_to_size_t(const jlong& jvalue) { + return static_cast(jvalue) <= + static_cast(std::numeric_limits::max()) ? + static_cast(jvalue) : std::numeric_limits::max(); +} + // The portal class for org.rocksdb.RocksDB class RocksDBJni { public: diff --git a/java/rocksjni/write_batch.cc b/java/rocksjni/write_batch.cc index a2cb67016..ff94309fe 100644 --- a/java/rocksjni/write_batch.cc +++ b/java/rocksjni/write_batch.cc @@ -30,7 +30,7 @@ void Java_org_rocksdb_WriteBatch_newWriteBatch( JNIEnv* env, jobject jobj, jint jreserved_bytes) { rocksdb::WriteBatch* wb = new rocksdb::WriteBatch( - static_cast(jreserved_bytes)); + rocksdb::jlong_to_size_t(jreserved_bytes)); rocksdb::WriteBatchJni::setHandle(env, jobj, wb); }