Skip to content

Commit

Permalink
Merge pull request #7008 from JLLeitschuh/feat/JLL/java_optional_lamb…
Browse files Browse the repository at this point in the history
…da_support

Java: Model java.util.Optional lambda methods
  • Loading branch information
aschackmull authored Nov 1, 2021
2 parents 64acd02 + b59f666 commit e88bbfd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions java/change-notes/2021-10-29-optional-lambda-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lgtm,codescanning
* Added data flow models for lambda methods on `java.util.Optional`.
9 changes: 9 additions & 0 deletions java/ql/lib/semmle/code/java/frameworks/Optional.qll
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ private class OptionalModel extends SummaryModelCsv {
s =
[
"java.util;Optional;false;filter;;;Element of Argument[-1];Element of ReturnValue;value",
"java.util;Optional;false;filter;;;Element of Argument[-1];Parameter[0] of Argument[0];value",
"java.util;Optional;false;flatMap;;;Element of Argument[-1];Parameter[0] of Argument[0];value",
"java.util;Optional;false;flatMap;;;ReturnValue of Argument[0];ReturnValue;value",
"java.util;Optional;false;get;;;Element of Argument[-1];ReturnValue;value",
"java.util;Optional;false;ifPresent;;;Element of Argument[-1];Parameter[0] of Argument[0];value",
"java.util;Optional;false;ifPresentOrElse;;;Element of Argument[-1];Parameter[0] of Argument[0];value",
"java.util;Optional;false;map;;;Element of Argument[-1];Parameter[0] of Argument[0];value",
"java.util;Optional;false;map;;;ReturnValue of Argument[0];Element of ReturnValue;value",
"java.util;Optional;false;of;;;Argument[0];Element of ReturnValue;value",
"java.util;Optional;false;ofNullable;;;Argument[0];Element of ReturnValue;value",
"java.util;Optional;false;or;;;Element of Argument[-1];Element of ReturnValue;value",
"java.util;Optional;false;or;;;ReturnValue of Argument[0];ReturnValue;value",
"java.util;Optional;false;orElse;;;Element of Argument[-1];ReturnValue;value",
"java.util;Optional;false;orElse;;;Argument[0];ReturnValue;value",
"java.util;Optional;false;orElseGet;;;Element of Argument[-1];ReturnValue;value",
"java.util;Optional;false;orElseGet;;;ReturnValue of Argument[0];ReturnValue;value",
"java.util;Optional;false;orElseThrow;;;Element of Argument[-1];ReturnValue;value",
"java.util;Optional;false;stream;;;Element of Argument[-1];Element of ReturnValue;value"
]
Expand Down
58 changes: 58 additions & 0 deletions java/ql/test/library-tests/optional/FunctionalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.Optional;

public class FunctionalTest {
String source() {
return null;
}

void sink(Object o) {
}

void test() {
Optional<String> o = Optional.of(source());
o.ifPresent(v -> {
sink(v); // $hasValueFlow
});
o.ifPresentOrElse(v -> {
sink(v); // $hasValueFlow
}, () -> {
// no-op
});
o.map(v -> {
sink(v); // $hasValueFlow
return v;
}).ifPresent(v -> {
sink(v); // $hasValueFlow
});
o.flatMap(v -> {
sink(v); // $hasValueFlow
return Optional.of(v);
}).ifPresent(v -> {
sink(v); // $hasValueFlow
});
o.flatMap(v -> {
sink(v); // $hasValueFlow
return Optional.of("safe");
}).ifPresent(v -> {
sink(v); // no value flow
});
o.filter(v -> {
sink(v); // $hasValueFlow
return true;
}).ifPresent(v -> {
sink(v); // $hasValueFlow
});
Optional.of("safe").map(v -> {
sink(v); // no value flow
return v;
}).or(() -> o).ifPresent(v -> {
sink(v); // $hasValueFlow
});
Optional<String> safe = Optional.of("safe");
o.or(() -> safe).ifPresent(v -> {
sink(v); // $hasValueFlow
});
String value = safe.orElseGet(() -> source());
sink(value); // $hasValueFlow
}
}

0 comments on commit e88bbfd

Please sign in to comment.