From: Daniel Elstner Date: Mon, 26 Oct 2015 06:29:50 +0000 (+0100) Subject: Java: Avoid dangerous writes via casted pointers X-Git-Tag: libsigrok-0.4.0~152 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=865de99391194e45a0e80ea534fc5e27d5287925;p=libsigrok.git Java: Avoid dangerous writes via casted pointers Writing to an object through a pointer of incompatible type is really evil. Even when the data size matches, it is still a violation of strict aliasing rules. Replace all instances by direct casts of the value, without the unnecessary and dangerous indirection. --- diff --git a/bindings/java/org/sigrok/core/classes/classes.i b/bindings/java/org/sigrok/core/classes/classes.i index 5a020d7e..7a61bfd5 100644 --- a/bindings/java/org/sigrok/core/classes/classes.i +++ b/bindings/java/org/sigrok/core/classes/classes.i @@ -75,7 +75,7 @@ namespace Glib { jlong value; for (auto entry : $1) { - *(CValue **) &value = new CValue(entry); + value = reinterpret_cast(new CValue(entry)); jenv->CallObjectMethod($result, Vector_add, jenv->NewObject(Value, Value_init, value, true)); } @@ -149,8 +149,7 @@ MAP_COMMON(std::string, std::shared_ptr, String, ClassName) jlong value; for (auto entry : $1) { - *(std::shared_ptr< sigrok::ClassName > **)&value = - new std::shared_ptr< sigrok::ClassName>(entry.second); + value = reinterpret_cast(new std::shared_ptr< sigrok::ClassName>(entry.second)); jenv->CallObjectMethod($result, HashMap_put, jenv->NewStringUTF(entry.first.c_str()), jenv->NewObject(Value, Value_init, value, true)); @@ -185,8 +184,8 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) jlong value; for (auto entry : $1) { - *(const sigrok::ConfigKey **) &key = entry.first; - *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second); + key = reinterpret_cast(entry.first); + value = reinterpret_cast(new Glib::VariantBase(entry.second)); jenv->CallObjectMethod($result, HashMap_put, jenv->NewObject(ConfigKey, ConfigKey_init, key, false)); jenv->NewObject(Variant, Variant_init, value, true)); @@ -220,7 +219,7 @@ MAP_COMMON(const sigrok::ConfigKey *, std::set, jlong key; for (auto map_entry : $1) { - *(const sigrok::ConfigKey **) &key = map_entry.first; + key = reinterpret_cast(map_entry.first); jobject value = jenv->NewObject(HashSet, HashSet_init); for (auto &set_entry : map_entry.second) jenv->CallObjectMethod(value, HashSet_add, @@ -268,7 +267,7 @@ typedef jobject jlogcallback; std::string message) { jlong loglevel_addr; - *(const sigrok::LogLevel **) &loglevel_addr = loglevel; + loglevel_addr = reinterpret_cast(loglevel); jobject loglevel_obj = env->NewObject( LogLevel, LogLevel_init, loglevel_addr, false); jobject message_obj = env->NewStringUTF(message.c_str()); @@ -314,10 +313,8 @@ typedef jobject jdatafeedcallback; { jlong device_addr; jlong packet_addr; - *(std::shared_ptr **) &device_addr = - new std::shared_ptr(device); - *(std::shared_ptr **) &packet_addr = - new std::shared_ptr(packet); + device_addr = reinterpret_cast(new std::shared_ptr(device)); + packet_addr = reinterpret_cast(new std::shared_ptr(packet)); jobject device_obj = env->NewObject( Device, Device_init, device_addr, true); jobject packet_obj = env->NewObject(