Skip to content

Commit ab536f1

Browse files
committed
Avoid reassigning parameters and enable a PMD rule
1 parent 677daca commit ab536f1

File tree

8 files changed

+55
-39
lines changed

8 files changed

+55
-39
lines changed

configs/pmd-rules.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<exclude name="AvoidStringBufferField" />
1818
<exclude name="PreserveStackTrace" /> <!-- TODO -->
1919
<exclude name="WhileLoopWithLiteralBoolean" />
20-
<exclude name="AvoidReassigningParameters" />
2120
<exclude name="PositionLiteralsFirstInComparisons" />
2221
<exclude name="ForLoopCanBeForeach" />
2322
<exclude name="CheckResultSet" />

src/sqlancer/postgres/ast/PostgresBinaryLogicalOperation.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ public enum BinaryLogicalOperator implements Operator {
1313
AND {
1414
@Override
1515
public PostgresConstant apply(PostgresConstant left, PostgresConstant right) {
16-
left = left.cast(PostgresDataType.BOOLEAN);
17-
right = right.cast(PostgresDataType.BOOLEAN);
18-
if (left.isNull()) {
19-
if (right.isNull()) {
16+
PostgresConstant leftBool = left.cast(PostgresDataType.BOOLEAN);
17+
PostgresConstant rightBool = right.cast(PostgresDataType.BOOLEAN);
18+
if (leftBool.isNull()) {
19+
if (rightBool.isNull()) {
2020
return PostgresConstant.createNullConstant();
2121
} else {
22-
if (right.asBoolean()) {
22+
if (rightBool.asBoolean()) {
2323
return PostgresConstant.createNullConstant();
2424
} else {
2525
return PostgresConstant.createFalse();
2626
}
2727
}
28-
} else if (!left.asBoolean()) {
28+
} else if (!leftBool.asBoolean()) {
2929
return PostgresConstant.createFalse();
3030
}
31-
assert left.asBoolean();
32-
if (right.isNull()) {
31+
assert leftBool.asBoolean();
32+
if (rightBool.isNull()) {
3333
return PostgresConstant.createNullConstant();
3434
} else {
35-
return PostgresConstant.createBooleanConstant(right.isBoolean() && right.asBoolean());
35+
return PostgresConstant.createBooleanConstant(rightBool.isBoolean() && rightBool.asBoolean());
3636
}
3737
}
3838
},
3939
OR {
4040
@Override
4141
public PostgresConstant apply(PostgresConstant left, PostgresConstant right) {
42-
left = left.cast(PostgresDataType.BOOLEAN);
43-
right = right.cast(PostgresDataType.BOOLEAN);
44-
if (left.isBoolean() && left.asBoolean()) {
42+
PostgresConstant leftBool = left.cast(PostgresDataType.BOOLEAN);
43+
PostgresConstant rightBool = right.cast(PostgresDataType.BOOLEAN);
44+
if (leftBool.isBoolean() && leftBool.asBoolean()) {
4545
return PostgresConstant.createTrue();
4646
}
47-
if (right.isBoolean() && right.asBoolean()) {
47+
if (rightBool.isBoolean() && rightBool.asBoolean()) {
4848
return PostgresConstant.createTrue();
4949
}
50-
if (left.isNull() || right.isNull()) {
50+
if (leftBool.isNull() || rightBool.isNull()) {
5151
return PostgresConstant.createNullConstant();
5252
}
5353
return PostgresConstant.createFalse();

src/sqlancer/postgres/ast/PostgresConstant.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,16 @@ public static PostgresConstant createDoubleConstant(double val) {
576576

577577
public static PostgresConstant createRange(long left, boolean leftIsInclusive, long right,
578578
boolean rightIsInclusive) {
579+
long realLeft;
580+
long realRight;
579581
if (left > right) {
580-
long temp = right;
581-
right = left;
582-
left = temp;
582+
realRight = left;
583+
realLeft = right;
584+
} else {
585+
realLeft = left;
586+
realRight = right;
583587
}
584-
return new RangeConstant(left, leftIsInclusive, right, rightIsInclusive);
588+
return new RangeConstant(realLeft, leftIsInclusive, realRight, rightIsInclusive);
585589
}
586590

587591
public static PostgresExpression createBitConstant(long integer) {

src/sqlancer/postgres/gen/PostgresExpressionGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ public static PostgresExpression generateExpression(PostgresGlobalState globalSt
247247
return new PostgresExpressionGenerator(globalState).generateExpression(0, type);
248248
}
249249

250-
public PostgresExpression generateExpression(int depth, PostgresDataType dataType) {
250+
public PostgresExpression generateExpression(int depth, PostgresDataType originalType) {
251+
PostgresDataType dataType = originalType;
251252
if (dataType == PostgresDataType.REAL && Randomly.getBoolean()) {
252253
dataType = Randomly.fromOptions(PostgresDataType.INT, PostgresDataType.FLOAT);
253254
}

src/sqlancer/sqlite3/ast/SQLite3Cast.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public static Optional<Boolean> isTrue(SQLite3Constant value) {
4747
}
4848

4949
// SELECT CAST('-1.370998801E9' AS INTEGER) == -1
50-
public static SQLite3Constant castToInt(SQLite3Constant cons) {
50+
public static SQLite3Constant castToInt(SQLite3Constant originalCons) {
51+
SQLite3Constant cons = originalCons;
5152
if (cons.getDataType() == SQLite3DataType.BINARY) {
5253
String text = new String(cons.asBinary());
5354
cons = SQLite3Constant.createTextConstant(text);
@@ -120,8 +121,9 @@ public static SQLite3Constant castToNumeric(SQLite3Constant value) {
120121
return convertInternal(value, true, false, false);
121122
}
122123

123-
private static SQLite3Constant convertInternal(SQLite3Constant value, boolean convertRealToInt,
124+
private static SQLite3Constant convertInternal(SQLite3Constant originalValue, boolean convertRealToInt,
124125
boolean noNumIsRealZero, boolean convertIntToReal) throws AssertionError {
126+
SQLite3Constant value = originalValue;
125127
if (value.getDataType() == SQLite3DataType.BINARY) {
126128
String text = new String(value.asBinary());
127129
value = SQLite3Constant.createTextConstant(text);

src/sqlancer/sqlite3/ast/SQLite3Expression.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,14 +1041,20 @@ public String getTextRepresentation() {
10411041
return Randomly.fromOptions(textRepresentation);
10421042
}
10431043

1044-
public SQLite3Constant applyOperand(SQLite3Constant left, TypeAffinity leftAffinity, SQLite3Constant right,
1045-
TypeAffinity rightAffinity, SQLite3Expression origLeft, SQLite3Expression origRight,
1046-
boolean applyAffinity) {
1044+
public SQLite3Constant applyOperand(SQLite3Constant leftBeforeAffinity, TypeAffinity leftAffinity,
1045+
SQLite3Constant rightBeforeAffinity, TypeAffinity rightAffinity, SQLite3Expression origLeft,
1046+
SQLite3Expression origRight, boolean applyAffinity) {
10471047

1048+
SQLite3Constant left;
1049+
SQLite3Constant right;
10481050
if (applyAffinity) {
1049-
ConstantTuple vals = applyAffinities(leftAffinity, rightAffinity, left, right);
1051+
ConstantTuple vals = applyAffinities(leftAffinity, rightAffinity, leftBeforeAffinity,
1052+
rightBeforeAffinity);
10501053
left = vals.left;
10511054
right = vals.right;
1055+
} else {
1056+
left = leftBeforeAffinity;
1057+
right = rightBeforeAffinity;
10521058
}
10531059

10541060
// If either operand has an explicit collating function assignment using the
@@ -1612,10 +1618,12 @@ static class ConstantTuple {
16121618
}
16131619

16141620
public static ConstantTuple applyAffinities(TypeAffinity leftAffinity, TypeAffinity rightAffinity,
1615-
SQLite3Constant left, SQLite3Constant right) {
1621+
SQLite3Constant leftBeforeAffinity, SQLite3Constant rightBeforeAffinity) {
16161622
// If one operand has INTEGER, REAL or NUMERIC affinity and the other operand
16171623
// has TEXT or BLOB or no affinity then NUMERIC affinity is applied to other
16181624
// operand.
1625+
SQLite3Constant left = leftBeforeAffinity;
1626+
SQLite3Constant right = rightBeforeAffinity;
16191627
if (leftAffinity.isNumeric() && (rightAffinity == TypeAffinity.TEXT || rightAffinity == TypeAffinity.BLOB
16201628
|| rightAffinity == TypeAffinity.NONE)) {
16211629
right = right.applyNumericAffinity();

src/sqlancer/sqlite3/ast/SQLite3Function.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public boolean isVariadic() {
6868
public SQLite3Constant apply(SQLite3Constant... args) {
6969
return null;
7070
// SQLite3Constant binaryValue = SQLite3Cast.castToBlob(args[0]);
71-
// return SQLite3Constant.createTextConstant(binaryValue.getStringRepresentation());
71+
// return
72+
// SQLite3Constant.createTextConstant(binaryValue.getStringRepresentation());
7273
}
7374
},
7475

@@ -139,10 +140,8 @@ public SQLite3Constant apply(SQLite3Constant... args) {
139140
NULLIF(2, "NULLIF") {
140141
@Override
141142
public SQLite3Constant apply(SQLite3Constant[] args, SQLite3CollateSequence collateSequence) {
142-
if (collateSequence == null) {
143-
collateSequence = SQLite3CollateSequence.BINARY;
144-
}
145-
SQLite3Constant equals = args[0].applyEquals(args[1], collateSequence);
143+
SQLite3Constant equals = args[0].applyEquals(args[1],
144+
collateSequence == null ? SQLite3CollateSequence.BINARY : collateSequence);
146145
if (SQLite3Cast.isTrue(equals).isPresent() && SQLite3Cast.isTrue(equals).get()) {
147146
return SQLite3Constant.createNullConstant();
148147
} else {

src/sqlancer/sqlite3/ast/SQLite3UnaryOperation.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ public SQLite3Constant apply(SQLite3Constant constant) {
4848
if (constant.isNull()) {
4949
return SQLite3Constant.createNullConstant();
5050
}
51+
SQLite3Constant intConstant;
5152
if (constant.getDataType() == SQLite3DataType.TEXT
5253
|| constant.getDataType() == SQLite3DataType.BINARY) {
53-
constant = SQLite3Cast.castToNumericFromNumOperand(constant);
54+
intConstant = SQLite3Cast.castToNumericFromNumOperand(constant);
55+
} else {
56+
intConstant = constant;
5457
}
55-
if (constant.getDataType() == SQLite3DataType.INT) {
56-
if (constant.asInt() == Long.MIN_VALUE) {
58+
if (intConstant.getDataType() == SQLite3DataType.INT) {
59+
if (intConstant.asInt() == Long.MIN_VALUE) {
5760
// SELECT - -9223372036854775808; -- 9.22337203685478e+18
5861
return SQLite3Constant.createRealConstant(-(double) Long.MIN_VALUE);
5962
} else {
60-
return SQLite3Constant.createIntConstant(-constant.asInt());
63+
return SQLite3Constant.createIntConstant(-intConstant.asInt());
6164
}
6265
}
63-
if (constant.getDataType() == SQLite3DataType.REAL) {
64-
return SQLite3Constant.createRealConstant(-constant.asDouble());
66+
if (intConstant.getDataType() == SQLite3DataType.REAL) {
67+
return SQLite3Constant.createRealConstant(-intConstant.asDouble());
6568
}
66-
throw new AssertionError(constant);
69+
throw new AssertionError(intConstant);
6770
}
6871
},
6972
PLUS("+") {

0 commit comments

Comments
 (0)