Fix some alerts raised by static analysis (lgtm.com)

Patch by Malcolm Taylor; Reviewed by Jeff Jirsa for CASSANDRA-13799
This commit is contained in:
Malcolm Taylor 2017-08-30 09:29:00 +01:00 committed by Jeff Jirsa
parent 0b50887989
commit 1c3a2d247a
14 changed files with 112 additions and 50 deletions

View File

@ -1,5 +1,6 @@
4.0
* Add nodetool cmd to print hinted handoff window (CASSANDRA-13728)
* Fix some alerts raised by static analysis (CASSANDRA-13799)
* Checksum sstable metadata (CASSANDRA-13321, CASSANDRA-13593)
* Add result set metadata to prepared statement MD5 hash calculation (CASSANDRA-10786)
* Refactor GcCompactionTest to avoid boxing (CASSANDRA-13941)

View File

@ -41,7 +41,7 @@ public class FieldIdentifier
*/
public static FieldIdentifier forUnquoted(String text)
{
return new FieldIdentifier(convert(text.toLowerCase(Locale.US)));
return new FieldIdentifier(convert(text == null ? null : text.toLowerCase(Locale.US)));
}
/**

View File

@ -25,7 +25,7 @@ public class RoleName
public void setName(String name, boolean keepCase)
{
this.name = keepCase ? name : name.toLowerCase(Locale.US);
this.name = keepCase ? name : (name == null ? name : name.toLowerCase(Locale.US));
}
public boolean hasName()

View File

@ -832,7 +832,7 @@ public abstract class ColumnCondition
checkFalse(type.isCollection(), "Slice conditions are not supported on collections containing durations");
checkFalse(type.isTuple(), "Slice conditions are not supported on tuples containing durations");
checkFalse(type.isUDT(), "Slice conditions are not supported on UDTs containing durations");
throw invalidRequest("Slice conditions are not supported on durations", operator);
throw invalidRequest("Slice conditions ( %s ) are not supported on durations", operator);
}
}
}

View File

@ -156,7 +156,7 @@ public final class FunctionResolver
return toTest;
}
}
throw invalidRequest("Ambiguous '%s' operation: use type casts to disambiguate",
throw invalidRequest("Ambiguous '%s' operation with args %s and %s: use type casts to disambiguate",
OperationFcts.getOperator(name), providedArgs.get(0), providedArgs.get(1));
}

View File

@ -411,5 +411,13 @@ public class Expression
Bound o = (Bound) other;
return value.equals(o.value) && inclusive == o.inclusive;
}
public int hashCode()
{
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(value);
builder.append(inclusive);
return builder.toHashCode();
}
}
}

View File

@ -424,19 +424,19 @@ public class CompressionMetadata
@SuppressWarnings("resource")
public CompressionMetadata open(long dataLength, long compressedLength)
{
SafeMemory offsets = this.offsets.sharedCopy();
SafeMemory tOffsets = this.offsets.sharedCopy();
// calculate how many entries we need, if our dataLength is truncated
int count = (int) (dataLength / parameters.chunkLength());
int tCount = (int) (dataLength / parameters.chunkLength());
if (dataLength % parameters.chunkLength() != 0)
count++;
tCount++;
assert count > 0;
assert tCount > 0;
// grab our actual compressed length from the next offset from our the position we're opened to
if (count < this.count)
compressedLength = offsets.getLong(count * 8L);
if (tCount < this.count)
compressedLength = tOffsets.getLong(tCount * 8L);
return new CompressionMetadata(filePath, parameters, offsets, count * 8L, dataLength, compressedLength);
return new CompressionMetadata(filePath, parameters, tOffsets, tCount * 8L, dataLength, compressedLength);
}
/**

View File

@ -824,17 +824,17 @@ public class TokenMetadata
long startedAt = System.currentTimeMillis();
// create clone of current state
BiMultiValMap<Token, InetAddress> bootstrapTokens = new BiMultiValMap<>();
Set<InetAddress> leavingEndpoints = new HashSet<>();
Set<Pair<Token, InetAddress>> movingEndpoints = new HashSet<>();
BiMultiValMap<Token, InetAddress> bootstrapTokensClone = new BiMultiValMap<>();
Set<InetAddress> leavingEndpointsClone = new HashSet<>();
Set<Pair<Token, InetAddress>> movingEndpointsClone = new HashSet<>();
TokenMetadata metadata;
lock.readLock().lock();
try
{
bootstrapTokens.putAll(this.bootstrapTokens);
leavingEndpoints.addAll(this.leavingEndpoints);
movingEndpoints.addAll(this.movingEndpoints);
bootstrapTokensClone.putAll(this.bootstrapTokens);
leavingEndpointsClone.addAll(this.leavingEndpoints);
movingEndpointsClone.addAll(this.movingEndpoints);
metadata = this.cloneOnlyTokenMap();
}
finally
@ -842,8 +842,8 @@ public class TokenMetadata
lock.readLock().unlock();
}
pendingRanges.put(keyspaceName, calculatePendingRanges(strategy, metadata, bootstrapTokens,
leavingEndpoints, movingEndpoints));
pendingRanges.put(keyspaceName, calculatePendingRanges(strategy, metadata, bootstrapTokensClone,
leavingEndpointsClone, movingEndpointsClone));
long took = System.currentTimeMillis() - startedAt;
if (logger.isDebugEnabled())

View File

@ -292,17 +292,17 @@ public class OperationFctsTest extends CQLTester
row(2, (byte) 2, (short) 2, "test"));
// tinyint, smallint and int could be used there so we need to disambiguate
assertInvalidMessage("Ambiguous '+' operation: use type casts to disambiguate",
assertInvalidMessage("Ambiguous '+' operation with args ? and 1: use type casts to disambiguate",
"SELECT * FROM %s WHERE pk = ? + 1 AND c1 = 2", 1);
assertInvalidMessage("Ambiguous '+' operation: use type casts to disambiguate",
assertInvalidMessage("Ambiguous '+' operation with args ? and 1: use type casts to disambiguate",
"SELECT * FROM %s WHERE pk = 2 AND c1 = 2 AND c2 = 1 * (? + 1)", 1);
assertRows(execute("SELECT 1 + 1, v FROM %s WHERE pk = 2 AND c1 = 2"),
row(2, "test"));
// As the output type is unknown the ? type cannot be determined
assertInvalidMessage("Ambiguous '+' operation: use type casts to disambiguate",
assertInvalidMessage("Ambiguous '+' operation with args 1 and ?: use type casts to disambiguate",
"SELECT 1 + ?, v FROM %s WHERE pk = 2 AND c1 = 2", 1);
// As the prefered type for the constants is int, the returned type will be int
@ -310,7 +310,7 @@ public class OperationFctsTest extends CQLTester
row(150, "test"));
// As the output type is unknown the ? type cannot be determined
assertInvalidMessage("Ambiguous '+' operation: use type casts to disambiguate",
assertInvalidMessage("Ambiguous '+' operation with args ? and 50: use type casts to disambiguate",
"SELECT ? + 50, v FROM %s WHERE pk = 2 AND c1 = 2", 100);
createTable("CREATE TABLE %s (a tinyint, b smallint, c int, d bigint, e float, f double, g varint, h decimal, PRIMARY KEY(a, b))"
@ -672,7 +672,7 @@ public class OperationFctsTest extends CQLTester
public void testWithNanAndInfinity() throws Throwable
{
createTable("CREATE TABLE %s (a int PRIMARY KEY, b double, c decimal)");
assertInvalidMessage("Ambiguous '+' operation: use type casts to disambiguate",
assertInvalidMessage("Ambiguous '+' operation with args ? and 1: use type casts to disambiguate",
"INSERT INTO %S (a, b, c) VALUES (? + 1, ?, ?)", 0, Double.NaN, BigDecimal.valueOf(1));
execute("INSERT INTO %S (a, b, c) VALUES ((int) ? + 1, -?, ?)", 0, Double.NaN, BigDecimal.valueOf(1));

View File

@ -2038,13 +2038,13 @@ public class InsertUpdateIfConditionTest extends CQLTester
{
createTable(" CREATE TABLE %s (k int PRIMARY KEY, v int, d duration)");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( > ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF d > 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( >= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF d >= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( <= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF d <= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( < ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF d < 1s");
execute("INSERT INTO %s (k, v, d) VALUES (1, 1, 2s)");
@ -2100,13 +2100,13 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("SELECT * FROM %s WHERE k = 1"), row(1, list(Duration.from("5s"), Duration.from("10s")), 6));
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( > ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF l[0] > 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( >= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF l[0] >= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( <= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF l[0] <= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( < ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF l[0] < 1s");
assertRows(execute("UPDATE %s SET v = 4 WHERE k = 1 IF l[0] = 2s"), row(false, list(Duration.from("5s"), Duration.from("10s"))));
@ -2218,13 +2218,13 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("SELECT * FROM %s WHERE k = 1"), row(1, map(1, Duration.from("5s"), 2, Duration.from("10s")), 6));
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( > ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF m[1] > 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( >= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF m[1] >= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( <= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF m[1] <= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( < ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF m[1] < 1s");
assertRows(execute("UPDATE %s SET v = 4 WHERE k = 1 IF m[1] = 2s"), row(false, map(1, Duration.from("5s"), 2, Duration.from("10s"))));
@ -2338,13 +2338,13 @@ public class InsertUpdateIfConditionTest extends CQLTester
assertRows(execute("SELECT * FROM %s WHERE k = 1"), row(1, userType("i", 1, "d", Duration.from("10s")), 6));
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( > ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF u.d > 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( >= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF u.d >= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( <= ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF u.d <= 1s");
assertInvalidMessage("Slice conditions are not supported on durations",
assertInvalidMessage("Slice conditions ( < ) are not supported on durations",
"UPDATE %s SET v = 3 WHERE k = 0 IF u.d < 1s");
assertRows(execute("UPDATE %s SET v = 4 WHERE k = 1 IF u.d = 2s"), row(false, userType("i", 1, "d", Duration.from("10s"))));

View File

@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.index.sasi.plan;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.index.sasi.plan.Expression.Bound;
public class ExpressionTest
{
@Test
public void testBoundHashCode()
{
ByteBuffer buf1 = UTF8Type.instance.decompose("blah");
Expression.Bound b1 = new Expression.Bound(buf1, true);
ByteBuffer buf2 = UTF8Type.instance.decompose("blah");
Expression.Bound b2 = new Expression.Bound(buf2, true);
assertTrue(b1.equals(b2));
assertTrue(b1.hashCode() == b2.hashCode());
}
@Test
public void testNotMatchingBoundHashCode()
{
ByteBuffer buf1 = UTF8Type.instance.decompose("blah");
Expression.Bound b1 = new Expression.Bound(buf1, true);
ByteBuffer buf2 = UTF8Type.instance.decompose("blah2");
Expression.Bound b2 = new Expression.Bound(buf2, true);
assertFalse(b1.equals(b2));
assertFalse(b1.hashCode() == b2.hashCode());
}
}

View File

@ -45,13 +45,13 @@ public abstract class Distribution implements Serializable
public long average()
{
double sum = 0;
int count = 0;
for (float d = 0 ; d <= 1.0d ; d += 0.02d)
double d = 0d;
for (int count = 0; count < 51 ; count++)
{
sum += inverseCumProb(d);
count += 1;
d += 0.02d;
}
return (long) (sum / count);
return (long) (sum / 51);
}
}

View File

@ -43,9 +43,9 @@ public class SeedManager
public SeedManager(StressSettings settings)
{
Distribution sample = settings.insert.revisit.get();
this.sampleOffset = Math.min(sample.minValue(), sample.maxValue());
long sampleSize = 1 + Math.max(sample.minValue(), sample.maxValue()) - sampleOffset;
Distribution tSample = settings.insert.revisit.get();
this.sampleOffset = Math.min(tSample.minValue(), tSample.maxValue());
long sampleSize = 1 + Math.max(tSample.minValue(), tSample.maxValue()) - sampleOffset;
if (sampleOffset < 0 || sampleSize > Integer.MAX_VALUE)
throw new IllegalArgumentException("sample range is invalid");
@ -76,7 +76,7 @@ public class SeedManager
this.writes = writes;
this.reads = reads;
this.sampleFrom = new LockedDynamicList<>((int) sampleSize);
this.sample = DistributionInverted.invert(sample);
this.sample = DistributionInverted.invert(tSample);
this.sampleSize = (int) sampleSize;
this.updateSampleImmediately = visits.average() > 1;
}
@ -88,7 +88,7 @@ public class SeedManager
Seed seed = reads.next(-1);
if (seed == null)
return null;
Seed managing = this.managing.get(seed);
Seed managing = this.managing.get(seed.seed);
return managing == null ? seed : managing;
}

View File

@ -32,6 +32,6 @@ public class Booleans extends Generator<Boolean>
@Override
public Boolean generate()
{
return identityDistribution.next() % 1 == 0;
return identityDistribution.next() % 2 == 0;
}
}