Skip to content

Commit ef9d096

Browse files
authored
Merge pull request github#5796 from smowton/smowton/feature/apache-mutable-flow
Java: Add synthetic fields; model Commons Lang's MutableObject type
2 parents a02a82c + 9cde13b commit ef9d096

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added models for the Apache Commons Lang Mutable types. This may lead to more results from any query using data-flow analysis where a relevant path uses one of these container types.

java/ql/src/semmle/code/java/frameworks/apache/Lang.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,17 @@ private class ApacheTripleModel extends SummaryModelCsv {
861861
]
862862
}
863863
}
864+
865+
/**
866+
* Value-propagating models for `MutableObject`.
867+
*/
868+
private class ApacheMutableObjectModel extends SummaryModelCsv {
869+
override predicate row(string row) {
870+
row =
871+
[
872+
"org.apache.commons.lang3.mutable;MutableObject;false;MutableObject;;;Argument[0];SyntheticField[org.apache.commons.lang3.mutable.MutableObject.value] of Argument[-1];value",
873+
"org.apache.commons.lang3.mutable;MutableObject;false;setValue;;;Argument[0];SyntheticField[org.apache.commons.lang3.mutable.MutableObject.value] of Argument[-1];value",
874+
"org.apache.commons.lang3.mutable;MutableObject;false;getValue;;;SyntheticField[org.apache.commons.lang3.mutable.MutableObject.value] of Argument[-1];ReturnValue;value"
875+
]
876+
}
877+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.apache.commons.lang3.mutable.Mutable;
2+
import org.apache.commons.lang3.mutable.MutableObject;
3+
4+
class MutableTest {
5+
String taint() { return "tainted"; }
6+
7+
void sink(Object o) {}
8+
9+
void test() throws Exception {
10+
11+
MutableObject<String> tainted = new MutableObject<>(taint());
12+
MutableObject<String> taintSet = new MutableObject<>("clean");
13+
MutableObject<String> taintCleared = new MutableObject<>(taint());
14+
taintSet.setValue(taint());
15+
taintCleared.setValue("clean");
16+
Mutable<String> taintedAlias = tainted;
17+
Mutable<String> taintSetAlias = taintSet;
18+
Mutable<String> taintClearedAlias = taintCleared;
19+
20+
sink(tainted.getValue()); // $hasValueFlow
21+
sink(taintedAlias.getValue()); // $hasValueFlow
22+
sink(taintSet.getValue()); // $hasValueFlow
23+
sink(taintSetAlias.getValue()); // $hasValueFlow
24+
// These two cases don't work currently because synthetic fields are always weakly updated,
25+
// so no taint clearing takes place.
26+
sink(taintCleared.getValue()); // $SPURIOUS: hasValueFlow
27+
sink(taintClearedAlias.getValue()); // $SPURIOUS: hasValueFlow
28+
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.mutable;
19+
20+
/**
21+
* Provides mutable access to a value.
22+
* <p>
23+
* <code>Mutable</code> is used as a generic interface to the implementations in this package.
24+
* <p>
25+
* A typical use case would be to enable a primitive or string to be passed to a method and allow that method to
26+
* effectively change the value of the primitive/string. Another use case is to store a frequently changing primitive in
27+
* a collection (for example a total in a map) without needing to create new Integer/Long wrapper objects.
28+
*
29+
* @param <T> the type to set and get
30+
* @since 2.1
31+
* @version $Id$
32+
*/
33+
public interface Mutable<T> {
34+
35+
/**
36+
* Gets the value of this mutable.
37+
*
38+
* @return the stored value
39+
*/
40+
T getValue();
41+
42+
/**
43+
* Sets the value of this mutable.
44+
*
45+
* @param value
46+
* the value to store
47+
* @throws NullPointerException
48+
* if the object is null and null is invalid
49+
* @throws ClassCastException
50+
* if the type is invalid
51+
*/
52+
void setValue(T value);
53+
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.mutable;
19+
20+
import java.io.Serializable;
21+
22+
/**
23+
* A mutable <code>Object</code> wrapper.
24+
*
25+
* @param <T> the type to set and get
26+
* @since 2.1
27+
* @version $Id$
28+
*/
29+
public class MutableObject<T> implements Mutable<T>, Serializable {
30+
31+
/**
32+
* Constructs a new MutableObject with the default value of <code>null</code>.
33+
*/
34+
public MutableObject() {
35+
super();
36+
}
37+
38+
/**
39+
* Constructs a new MutableObject with the specified value.
40+
*
41+
* @param value the initial value to store
42+
*/
43+
public MutableObject(final T value) {
44+
}
45+
46+
//-----------------------------------------------------------------------
47+
/**
48+
* Gets the value.
49+
*
50+
* @return the value, may be null
51+
*/
52+
@Override
53+
public T getValue() {
54+
return null;
55+
}
56+
57+
/**
58+
* Sets the value.
59+
*
60+
* @param value the value to set
61+
*/
62+
@Override
63+
public void setValue(final T value) {
64+
}
65+
66+
}

0 commit comments

Comments
 (0)