cfelde / binaryoffheaphashmap Goto Github PK
View Code? Open in Web Editor NEWA hashmap implementation for Java that stores map entries off-heap
License: GNU Lesser General Public License v3.0
A hashmap implementation for Java that stores map entries off-heap
License: GNU Lesser General Public License v3.0
Hi,
I experienced strange behaviour of OHMap after removal of element. Here is sample code to generate issue:
String KEY = "KEY";
String VALUE = "VALUE";
OHMap<String, String> ohMap = new OHMap<>(10);
ohMap.put(KEY, VALUE);
ohMap.remove(KEY);
Iterator<String> iterator = ohMap.values().iterator();
if (iterator.hasNext()) {
iterator.next();
}
It generates JVM crash:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f2a1f9c9aed, pid=25433, tid=0x00007f2a1e608700
#
# JRE version: OpenJDK Runtime Environment (8.0_242-b08) (build 1.8.0_242-8u242-b08-0ubuntu3~18.04-b08)
# Java VM: OpenJDK 64-Bit Server VM (25.242-b08 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0xa81aed]
or Exception:
java.lang.NegativeArraySizeException
at com.cfelde.bohmap.BOHMap$ValuesIterator.next(BOHMap.java:1009)
at com.cfelde.bohmap.BOHMap$ValuesIterator.next(BOHMap.java:944)
at com.cfelde.bohmap.OHMap$Values$1.next(OHMap.java:282)
at com.cfelde.bohmap.OHMap$Values$1.next(OHMap.java:274)
It looks like iterator#hasNext return true, but Iterator#next causes exception/crash.
BOHMap calculates offset into the partition using the following code:
final int hash = Math.abs(hashFunction.apply(keyData));
final long offset = hash % partitionCount;
If hashFunction
returns Integer.MIN_VALUE
(i.e. -2147483648) Math.abs will return a negative number. A native offset into a buffer most of the time will crash the process, if not worse.
Check out, for example, this explanation.
This can be easily fixed by doing something like this instead:
final long offset = Math.abs(hashFunction.apply(keyData) % partitionCount);
I actually have a proper fix. Should I open a PR?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.