From d68e83c356626664988ad28c3916c02c08ac2e78 Mon Sep 17 00:00:00 2001 From: fyrz Date: Wed, 14 Jan 2015 21:36:43 +0100 Subject: [PATCH 1/2] [RocksJava] DirectSlice String termination fix DirectSlice fix for non terminated String copy. This lead sometimes to problems with DirectSliceTest. --- java/rocksjni/slice.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/java/rocksjni/slice.cc b/java/rocksjni/slice.cc index e4b7cf03b..162e868d1 100644 --- a/java/rocksjni/slice.cc +++ b/java/rocksjni/slice.cc @@ -28,9 +28,11 @@ void Java_org_rocksdb_AbstractSlice_createNewSliceFromString( JNIEnv* env, jobject jobj, jstring jstr) { const auto* str = env->GetStringUTFChars(jstr, 0); - const size_t len = strlen(str); + const size_t len = strlen(str) + 1; char* buf = new char[len]; - memcpy(buf, str, len); + memcpy(buf, str, len - 1); + buf[len-1]='\0'; + env->ReleaseStringUTFChars(jstr, str); const auto* slice = new rocksdb::Slice(buf); From ea25ff7158b26e2b0acb9f26be206b6b57af4abb Mon Sep 17 00:00:00 2001 From: fyrz Date: Sat, 17 Jan 2015 01:22:29 +0100 Subject: [PATCH 2/2] [RocksJava] Integrated proposed simplificiation --- java/rocksjni/slice.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/java/rocksjni/slice.cc b/java/rocksjni/slice.cc index 162e868d1..811117397 100644 --- a/java/rocksjni/slice.cc +++ b/java/rocksjni/slice.cc @@ -28,11 +28,10 @@ void Java_org_rocksdb_AbstractSlice_createNewSliceFromString( JNIEnv* env, jobject jobj, jstring jstr) { const auto* str = env->GetStringUTFChars(jstr, 0); - const size_t len = strlen(str) + 1; - char* buf = new char[len]; - memcpy(buf, str, len - 1); - buf[len-1]='\0'; - + const size_t len = strlen(str); + char* buf = new char[len + 1]; + memcpy(buf, str, len); + buf[len] = 0; env->ReleaseStringUTFChars(jstr, str); const auto* slice = new rocksdb::Slice(buf);