Skip to content

Commit 15d944c

Browse files
committed
Fix LGTM warning, potential int overflow bug
1 parent b467d29 commit 15d944c

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

jctools-core/src/main/java/org/jctools/maps/ConcurrentAutoTable.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import static org.jctools.util.UnsafeAccess.UNSAFE;
1616

1717
import java.io.Serializable;
18-
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18+
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
1919

2020

2121
/**
@@ -49,7 +49,7 @@ public class ConcurrentAutoTable implements Serializable {
4949
/** Atomically set the sum of the striped counters to specified value.
5050
* Rather more expensive than a simple store, in order to remain atomic.
5151
*/
52-
public void set( long x ) {
52+
public void set( long x ) {
5353
CAT newcat = new CAT(null,4,x);
5454
// Spin until CAS works
5555
while( !CAS_cat(_cat,newcat) ) {/*empty*/}
@@ -76,7 +76,7 @@ public void set( long x ) {
7676
* Return the counter's {@code long} value converted to a string.
7777
*/
7878
public String toString() { return _cat.toString(); }
79-
79+
8080
/**
8181
* A more verbose print than {@link #toString}, showing internal structure.
8282
* Useful for debugging.
@@ -109,18 +109,18 @@ private static int hash() {
109109

110110
// --- CAT -----------------------------------------------------------------
111111
private static class CAT implements Serializable {
112-
112+
113113
// Unsafe crud: get a function which will CAS arrays
114114
private static final int _Lbase = UNSAFE.arrayBaseOffset(long[].class);
115115
private static final int _Lscale = UNSAFE.arrayIndexScale(long[].class);
116116
private static long rawIndex(long[] ary, int i) {
117117
assert i >= 0 && i < ary.length;
118-
return _Lbase + i * _Lscale;
118+
return _Lbase + (i * (long)_Lscale);
119119
}
120120
private static boolean CAS( long[] A, int idx, long old, long nnn ) {
121121
return UNSAFE.compareAndSwapLong( A, rawIndex(A,idx), old, nnn );
122122
}
123-
123+
124124
//volatile long _resizers; // count of threads attempting a resize
125125
//static private final AtomicLongFieldUpdater<CAT> _resizerUpdater =
126126
// AtomicLongFieldUpdater.newUpdater(CAT.class, "_resizers");
@@ -136,7 +136,7 @@ private static boolean CAS( long[] A, int idx, long old, long nnn ) {
136136
_t = new long[sz];
137137
_t[0] = init;
138138
}
139-
139+
140140
// Only add 'x' to some slot in table, hinted at by 'hash'. The sum can
141141
// overflow. Value is CAS'd so no counts are lost. The CAS is attempted
142142
// ONCE.
@@ -180,7 +180,7 @@ public long add_if( long x, int hash, ConcurrentAutoTable master ) {
180180
while( master._cat == this && !master.CAS_cat(this,newcat) ) {/*empty*/}
181181
return old;
182182
}
183-
183+
184184

185185
// Return the current sum of all things in the table. Writers can be
186186
// updating the table furiously, so the sum is only locally accurate.
@@ -206,11 +206,11 @@ public long estimate_sum( ) {
206206
}
207207

208208
public String toString( ) { return Long.toString(sum()); }
209-
210-
public void print() {
209+
210+
public void print() {
211211
long[] t = _t;
212212
System.out.print("["+t[0]);
213-
for( int i=1; i<t.length; i++ )
213+
for( int i=1; i<t.length; i++ )
214214
System.out.print(","+t[i]);
215215
System.out.print("]");
216216
if( _next != null ) _next.print();

jctools-core/src/main/java/org/jctools/maps/NonBlockingIdentityHashMap.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
* @author Cliff Click
7979
* @param <TypeK> the type of keys maintained by this map
8080
* @param <TypeV> the type of mapped values
81-
*
82-
* @author Prashant Deva
81+
*
82+
* @author Prashant Deva
8383
* Modified from original NonBlockingHashMap to use identity equality.
8484
* Uses System.identityHashCode() to calculate hashMap.
8585
* Key equality is compared using '=='.
@@ -97,7 +97,7 @@ public class NonBlockingIdentityHashMap<TypeK, TypeV>
9797
private static final int _Oscale = UNSAFE.arrayIndexScale(Object[].class);
9898
private static long rawIndex(final Object[] ary, final int idx) {
9999
assert idx >= 0 && idx < ary.length;
100-
return _Obase + idx * _Oscale;
100+
return _Obase + (idx * (long)_Oscale);
101101
}
102102

103103
// --- Setup to use Unsafe
@@ -279,17 +279,17 @@ private final void initialize( int initial_sz ) {
279279

280280
/** Returns the number of key-value mappings in this map.
281281
* @return the number of key-value mappings in this map */
282-
@Override
282+
@Override
283283
public int size ( ) { return chm(_kvs).size(); }
284284
/** Returns <tt>size() == 0</tt>.
285285
* @return <tt>size() == 0</tt> */
286-
@Override
286+
@Override
287287
public boolean isEmpty ( ) { return size() == 0; }
288288

289289
/** Tests if the key in the table using the <tt>equals</tt> method.
290290
* @return <tt>true</tt> if the key is in the table using the <tt>equals</tt> method
291291
* @throws NullPointerException if the specified key is null */
292-
@Override
292+
@Override
293293
public boolean containsKey( Object key ) { return get(key) != null; }
294294

295295
/** Legacy method testing if some key maps into the specified value in this
@@ -825,7 +825,7 @@ private final Object[] resize( NonBlockingIdentityHashMap topmap, Object[] kvs)
825825
//synchronized( this ) { wait(8*megs); } // Timeout - we always wakeup
826826
// For now, sleep a tad and see if the 2 guys already trying to make
827827
// the table actually get around to making it happen.
828-
try { Thread.sleep(8*megs); } catch( Exception e ) { }
828+
try { Thread.sleep(8l*megs); } catch( Exception e ) { }
829829
}
830830
// Last check, since the 'new' below is expensive and there is a chance
831831
// that another thread slipped in a new thread while we ran the heuristic.

0 commit comments

Comments
 (0)