Skip to content

Commit 9b0163c

Browse files
authored
Merge pull request github#11624 from jcogs33/jcogs33/exclude-paramless-constructors-from-dataflowtargetapi
Java/C#: exclude parameterless constructors from `DataFlowTargetApi`
2 parents bfe9ee3 + 22f8d97 commit 9b0163c

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ class Constructor extends DotNet::Constructor, Callable, Member, Attributable, @
358358
if this.isStatic() then result = this.getParameter(i) else result = this.getParameter(i - 1)
359359
}
360360

361+
/** Holds if this is a constructor without parameters. */
362+
predicate isParameterless() { this.getNumberOfParameters() = 0 }
363+
361364
override string getUndecoratedName() { result = ".ctor" }
362365
}
363366

csharp/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ private predicate isRelevantForModels(CS::Callable api) {
3838
api.getDeclaringType().getNamespace().getFullName() != "" and
3939
not api instanceof CS::ConversionOperator and
4040
not api instanceof Util::MainMethod and
41-
not api instanceof CS::Destructor
41+
not api instanceof CS::Destructor and
42+
not api.(CS::Constructor).isParameterless()
4243
}
4344

4445
/**

csharp/ql/test/utils/model-generator/dataflow/NoSummaries.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,15 @@ public Dictionary<int, int> ReturnSimpleTypeDictionary(Dictionary<int, int> a)
131131
{
132132
return a;
133133
}
134-
}
134+
}
135+
136+
// A neutral model should not be created for a parameterless constructor.
137+
public class ParameterlessConstructor
138+
{
139+
public bool IsInitialized;
140+
141+
public ParameterlessConstructor()
142+
{
143+
IsInitialized = true;
144+
}
145+
}

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,9 @@ class Constructor extends Callable, @constructor {
628628
/** Holds if this is a default constructor, not explicitly declared in source code. */
629629
predicate isDefaultConstructor() { isDefConstr(this) }
630630

631+
/** Holds if this is a constructor without parameters. */
632+
predicate isParameterless() { this.getNumberOfParameters() = 0 }
633+
631634
override Constructor getSourceDeclaration() { constrs(this, _, _, _, _, result) }
632635

633636
override string getSignature() { constrs(this, _, result, _, _, _) }

java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ private predicate isRelevantForModels(J::Callable api) {
5959
not isInTestFile(api.getCompilationUnit().getFile()) and
6060
not isJdkInternal(api.getCompilationUnit()) and
6161
not api instanceof J::MainMethod and
62-
not api instanceof J::StaticInitializer
62+
not api instanceof J::StaticInitializer and
63+
not api.(J::Constructor).isParameterless()
6364
}
6465

6566
/**

java/ql/test/utils/model-generator/dataflow/CaptureNeutralModels.expected

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
| p;AbstractImplOfExternalSPI;AbstractImplOfExternalSPI;();generated |
21
| p;Factory;getIntValue;();generated |
3-
| p;FinalClass;FinalClass;();generated |
42
| p;FinalClass;returnsConstant;();generated |
5-
| p;FluentAPI$Inner;Inner;();generated |
63
| p;FluentAPI$Inner;notThis;(String);generated |
7-
| p;FluentAPI;FluentAPI;();generated |
84
| p;ImmutablePojo;getX;();generated |
9-
| p;ImplOfExternalSPI;ImplOfExternalSPI;();generated |
10-
| p;InnerClasses$CaptureMe;CaptureMe;();generated |
11-
| p;InnerClasses;InnerClasses;();generated |
12-
| p;InnerHolder;InnerHolder;();generated |
135
| p;Joiner;length;();generated |
14-
| p;MultipleImpls$Strat1;Strat1;();generated |
15-
| p;MultipleImpls$Strat2;Strat2;();generated |
16-
| p;MultipleImpls$Strat3;Strat3;();generated |
176
| p;MultipleImpls$Strategy;doSomething;(String);generated |
18-
| p;MultipleImpls;MultipleImpls;();generated |
19-
| p;ParamFlow;ParamFlow;();generated |
207
| p;ParamFlow;ignorePrimitiveReturnValue;(String);generated |
218
| p;ParamFlow;mapType;(Class);generated |
22-
| p;Pojo;Pojo;();generated |
239
| p;Pojo;doNotSetValue;(String);generated |
2410
| p;Pojo;getBigDecimal;();generated |
2511
| p;Pojo;getBigInt;();generated |
@@ -31,13 +17,10 @@
3117
| p;Pojo;getPrimitiveArray;();generated |
3218
| p;PrivateFlowViaPublicInterface$SPI;openStream;();generated |
3319
| p;PrivateFlowViaPublicInterface$SPI;openStreamNone;();generated |
34-
| p;PrivateFlowViaPublicInterface;PrivateFlowViaPublicInterface;();generated |
3520
| p;PrivateFlowViaPublicInterface;createAnSPIWithoutTrackingFile;(File);generated |
36-
| p;Sinks;Sinks;();generated |
3721
| p;Sinks;copyFileToDirectory;(Path,Path,CopyOption[]);generated |
3822
| p;Sinks;propagate;(String);generated |
3923
| p;Sinks;readUrl;(URL,Charset);generated |
40-
| p;Sources;Sources;();generated |
4124
| p;Sources;readUrl;(URL);generated |
4225
| p;Sources;socketStream;();generated |
4326
| p;Sources;sourceToParameter;(InputStream[],List);generated |

0 commit comments

Comments
 (0)