Author: damjan
Date: Sun Aug 20 06:22:29 2017
New Revision: 1805538

URL: http://svn.apache.org/viewvc?rev=1805538&view=rev
Log:
#i32564# - Java UnoRuntime.getUniqueKey/generateOid do not work reliably

In the Java UNO bridge, UnoRuntime.generateOid() generated the
object-specific part of the OID using java.lang.Object.hashCode(),
which is only 32 bits long, and is commonly overriden and could thus
return values from an even smaller range, so OID collisions were quite
likely.

This changes UnoRuntime.generateOid() to use 128 bit UUIDs for the
object-specific part of the OID, and store these in an object => oid
java.util.WeakHashMap, making OID collisions almost impossible.

Patch by: me
Suggested by: Stephan Bergmann (stephan dot bergmann dot secondary at
googlemail dot com)



Modified:
    openoffice/trunk/main/ridljar/com/sun/star/uno/UnoRuntime.java

Modified: openoffice/trunk/main/ridljar/com/sun/star/uno/UnoRuntime.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/ridljar/com/sun/star/uno/UnoRuntime.java?rev=1805538&r1=1805537&r2=1805538&view=diff
==============================================================================
--- openoffice/trunk/main/ridljar/com/sun/star/uno/UnoRuntime.java (original)
+++ openoffice/trunk/main/ridljar/com/sun/star/uno/UnoRuntime.java Sun Aug 20 
06:22:29 2017
@@ -28,6 +28,8 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.UUID;
+import java.util.WeakHashMap;
 import com.sun.star.lib.uno.typedesc.TypeDescription;
 import com.sun.star.lib.util.WeakMap;
 
@@ -109,7 +111,16 @@ public class UnoRuntime {
         if (object instanceof IQueryInterface) {
             oid = ((IQueryInterface) object).getOid();
         }
-        return oid == null ? object.hashCode() + oidSuffix : oid;
+        if (oid == null) {
+            synchronized (oidMap) {
+                 oid = oidMap.get(object);
+                 if (oid == null) {
+                     oid = UUID.randomUUID().toString() + oidSuffix;
+                     oidMap.put(object, oid);
+                 }
+            }
+        }
+        return oid;
     }
 
     /**
@@ -677,6 +688,7 @@ public class UnoRuntime {
         private final IBridge bridge;
     }
 
+    private static final WeakHashMap<Object,String> oidMap = new 
WeakHashMap<Object,String>();
     private static final String uniqueKeyHostPrefix
     = Integer.toString(new Object().hashCode(), 16) + ":";
     private static final Object uniqueKeyLock = new Object();


Reply via email to