Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e01246a

Browse files
committed
C#: Autoformat
1 parent 702fc80 commit e01246a

11 files changed

Lines changed: 96 additions & 108 deletions

File tree

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
import csharp
22
import semmle.code.csharp.dataflow.DataFlow
3-
43
import csharp
4+
55
class ImplementsICryptoTransform extends Class {
66
ImplementsICryptoTransform() {
77
this.getABaseType*().hasQualifiedName("System.Security.Cryptography", "ICryptoTransform")
88
}
99
}
1010

11-
predicate usesICryptoTransformType( ValueOrRefType t ) {
12-
exists( ImplementsICryptoTransform ict |
13-
ict = t
14-
or usesICryptoTransformType( t.getAChild() )
11+
predicate usesICryptoTransformType(ValueOrRefType t) {
12+
exists(ImplementsICryptoTransform ict |
13+
ict = t or
14+
usesICryptoTransformType(t.getAChild())
1515
)
1616
}
1717

18-
predicate hasICryptoTransformMember( Class c) {
18+
predicate hasICryptoTransformMember(Class c) {
1919
c.getAField().getType() instanceof UsesICryptoTransform
2020
}
2121

2222
class UsesICryptoTransform extends Class {
23-
UsesICryptoTransform() {
24-
usesICryptoTransformType(this) or hasICryptoTransformMember(this)
25-
}
23+
UsesICryptoTransform() { usesICryptoTransformType(this) or hasICryptoTransformMember(this) }
2624
}
2725

2826
class LambdaCapturingICryptoTransformSource extends DataFlow::Node {
2927
LambdaCapturingICryptoTransformSource() {
30-
exists( LambdaExpr l, LocalScopeVariable lsvar, UsesICryptoTransform ict |
31-
l = this.asExpr() |
32-
ict = lsvar.getType()
33-
and lsvar.getACapturingCallable() = l
28+
exists(LambdaExpr l, LocalScopeVariable lsvar, UsesICryptoTransform ict | l = this.asExpr() |
29+
ict = lsvar.getType() and
30+
lsvar.getACapturingCallable() = l
3431
)
3532
}
3633
}
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import csharp
22
import semmle.code.csharp.dataflow.DataFlow
33

4-
abstract class ParallelSink extends DataFlow::Node {
5-
}
4+
abstract class ParallelSink extends DataFlow::Node { }
65

76
class LambdaParallelSink extends ParallelSink {
87
LambdaParallelSink() {
9-
exists( Class c, Method m, MethodCall mc, Expr e |
10-
e = this.asExpr() |
11-
c.getABaseType*().hasQualifiedName("System.Threading.Tasks", "Parallel")
12-
and c.getAMethod() = m
13-
and m.getName() = "Invoke"
14-
and m.getACall() = mc
15-
and mc.getAnArgument() = e
16-
)
8+
exists(Class c, Method m, MethodCall mc, Expr e | e = this.asExpr() |
9+
c.getABaseType*().hasQualifiedName("System.Threading.Tasks", "Parallel") and
10+
c.getAMethod() = m and
11+
m.getName() = "Invoke" and
12+
m.getACall() = mc and
13+
mc.getAnArgument() = e
14+
)
1715
}
1816
}
1917

2018
class ThreadStartParallelSink extends ParallelSink {
2119
ThreadStartParallelSink() {
22-
exists( DelegateCreation dc, Expr e |
23-
e = this.asExpr() |
24-
dc.getArgument() = e
25-
and dc.getType().getName().matches("%Start")
20+
exists(DelegateCreation dc, Expr e | e = this.asExpr() |
21+
dc.getArgument() = e and
22+
dc.getType().getName().matches("%Start")
2623
)
2724
}
2825
}
Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Class defines a field that uses an ICryptoTransform class in a way that would be unsafe for concurrent threads.
33
* @description The class has a field that directly or indirectly make use of a static System.Security.Cryptography.ICryptoTransform object.
4-
* Using this an instance of this class in concurrent threads is dangerous as it may not only result in an error,
4+
* Using this an instance of this class in concurrent threads is dangerous as it may not only result in an error,
55
* but under some circumstances may also result in incorrect results.
66
* @kind problem
77
* @problem.severity warning
@@ -20,66 +20,65 @@ class ICryptoTransform extends Class {
2020
}
2121
}
2222

23-
predicate usesICryptoTransformType( Type t ) {
24-
exists( ICryptoTransform ict |
25-
ict = t
26-
or usesICryptoTransformType( t.getAChild() )
23+
predicate usesICryptoTransformType(Type t) {
24+
exists(ICryptoTransform ict |
25+
ict = t or
26+
usesICryptoTransformType(t.getAChild())
2727
)
2828
}
2929

30-
predicate hasICryptoTransformMember( Class c) {
31-
exists( Field f |
32-
f = c.getAMember()
33-
and (
34-
exists( ICryptoTransform ict | ict = f.getType() )
35-
or hasICryptoTransformMember(f.getType())
36-
or usesICryptoTransformType(f.getType())
30+
predicate hasICryptoTransformMember(Class c) {
31+
exists(Field f |
32+
f = c.getAMember() and
33+
(
34+
exists(ICryptoTransform ict | ict = f.getType()) or
35+
hasICryptoTransformMember(f.getType()) or
36+
usesICryptoTransformType(f.getType())
3737
)
3838
)
3939
}
4040

41-
predicate hasICryptoTransformStaticMemberNested( Class c ) {
42-
exists( Field f |
43-
f = c.getAMember() |
44-
hasICryptoTransformStaticMemberNested( f.getType() )
45-
or (
46-
f.isStatic() and hasICryptoTransformMember(f.getType())
47-
and not exists( Attribute a
48-
| a = f.getAnAttribute() |
49-
a.getType().getQualifiedName() = "System.ThreadStaticAttribute"
50-
)
41+
predicate hasICryptoTransformStaticMemberNested(Class c) {
42+
exists(Field f | f = c.getAMember() |
43+
hasICryptoTransformStaticMemberNested(f.getType())
44+
or
45+
f.isStatic() and
46+
hasICryptoTransformMember(f.getType()) and
47+
not exists(Attribute a | a = f.getAnAttribute() |
48+
a.getType().getQualifiedName() = "System.ThreadStaticAttribute"
5149
)
5250
)
5351
}
5452

55-
predicate hasICryptoTransformStaticMember( Class c, string msg) {
56-
exists( Field f |
57-
f = c.getAMember*()
58-
and f.isStatic()
59-
and not exists( Attribute a
60-
| a = f.getAnAttribute()
61-
and a.getType().getQualifiedName() = "System.ThreadStaticAttribute"
62-
)
63-
and (
64-
exists( ICryptoTransform ict |
65-
ict = f.getType()
66-
and msg = "Static field " + f + " of type " + f.getType() + ", implements 'System.Security.Cryptography.ICryptoTransform', but it does not have an attribute [ThreadStatic]. The usage of this class is unsafe for concurrent threads."
53+
predicate hasICryptoTransformStaticMember(Class c, string msg) {
54+
exists(Field f |
55+
f = c.getAMember*() and
56+
f.isStatic() and
57+
not exists(Attribute a |
58+
a = f.getAnAttribute() and
59+
a.getType().getQualifiedName() = "System.ThreadStaticAttribute"
60+
) and
61+
(
62+
exists(ICryptoTransform ict |
63+
ict = f.getType() and
64+
msg = "Static field " + f + " of type " + f.getType() +
65+
", implements 'System.Security.Cryptography.ICryptoTransform', but it does not have an attribute [ThreadStatic]. The usage of this class is unsafe for concurrent threads."
6766
)
68-
or
69-
(
70-
not exists( ICryptoTransform ict | ict = f.getType() ) // Avoid dup messages
71-
and exists( Type t | t = f.getType() |
72-
usesICryptoTransformType(t)
73-
and msg = "Static field " + f + " of type " + f.getType() + " makes usage of 'System.Security.Cryptography.ICryptoTransform', but it does not have an attribute [ThreadStatic]. The usage of this class is unsafe for concurrent threads."
74-
)
67+
or
68+
not exists(ICryptoTransform ict | ict = f.getType()) and // Avoid dup messages
69+
exists(Type t | t = f.getType() |
70+
usesICryptoTransformType(t) and
71+
msg = "Static field " + f + " of type " + f.getType() +
72+
" makes usage of 'System.Security.Cryptography.ICryptoTransform', but it does not have an attribute [ThreadStatic]. The usage of this class is unsafe for concurrent threads."
7573
)
7674
)
7775
)
78-
or ( hasICryptoTransformStaticMemberNested(c)
79-
and msg = "Class" + c + " implementation depends on a static object of type 'System.Security.Cryptography.ICryptoTransform' in a way that is unsafe for concurrent threads."
80-
)
76+
or
77+
hasICryptoTransformStaticMemberNested(c) and
78+
msg = "Class" + c +
79+
" implementation depends on a static object of type 'System.Security.Cryptography.ICryptoTransform' in a way that is unsafe for concurrent threads."
8180
}
8281

83-
from Class c , string s
84-
where hasICryptoTransformStaticMember(c, s)
82+
from Class c, string s
83+
where hasICryptoTransformStaticMember(c, s)
8584
select c, s
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @name Potential usage of an object implementing ICryptoTransform class in a way that would be unsafe for concurrent threads.
3-
* @description An instance of a class that either implements or has a field of type System.Security.Cryptography.ICryptoTransform is being captured by a lambda,
3+
* @description An instance of a class that either implements or has a field of type System.Security.Cryptography.ICryptoTransform is being captured by a lambda,
44
* and used in what seems to be a thread initialization method.
5-
* Using an instance of this class in concurrent threads is dangerous as it may not only result in an error,
5+
* Using an instance of this class in concurrent threads is dangerous as it may not only result in an error,
66
* but under some circumstances may also result in incorrect results.
77
* @kind problem
88
* @problem.severity warning
@@ -18,20 +18,20 @@ import semmle.code.csharp.dataflow.DataFlow
1818
import ParallelSink
1919
import ICryptoTransform
2020

21-
class NotThreadSafeCryptoUsageIntoParallelInvokeConfig extends TaintTracking::Configuration {
22-
NotThreadSafeCryptoUsageIntoParallelInvokeConfig() { this = "NotThreadSafeCryptoUsageIntoParallelInvokeConfig" }
23-
24-
override predicate isSource(DataFlow::Node source) {
25-
source instanceof LambdaCapturingICryptoTransformSource
21+
class NotThreadSafeCryptoUsageIntoParallelInvokeConfig extends TaintTracking::Configuration {
22+
NotThreadSafeCryptoUsageIntoParallelInvokeConfig() {
23+
this = "NotThreadSafeCryptoUsageIntoParallelInvokeConfig"
2624
}
27-
28-
override predicate isSink(DataFlow::Node sink) {
29-
sink instanceof ParallelSink
25+
26+
override predicate isSource(DataFlow::Node source) {
27+
source instanceof LambdaCapturingICryptoTransformSource
3028
}
29+
30+
override predicate isSink(DataFlow::Node sink) { sink instanceof ParallelSink }
3131
}
3232

3333
from Expr e, string m, LambdaExpr l, NotThreadSafeCryptoUsageIntoParallelInvokeConfig config
34-
where
35-
config.hasFlow(DataFlow::exprNode(l), DataFlow::exprNode(e))
36-
and m = "A $@ seems to be used to start a new thread is capturing a local variable that either implements 'System.Security.Cryptography.ICryptoTransform' or has a field of this type."
34+
where
35+
config.hasFlow(DataFlow::exprNode(l), DataFlow::exprNode(e)) and
36+
m = "A $@ seems to be used to start a new thread is capturing a local variable that either implements 'System.Security.Cryptography.ICryptoTransform' or has a field of this type."
3737
select e, m, l, "lambda expression"

csharp/ql/src/semmle/code/cil/CallableReturns.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ private predicate alwaysNullExpr(Expr expr) {
3636
or
3737
alwaysNullMethod(expr.(StaticCall).getTarget())
3838
or
39-
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) |
40-
alwaysNullExpr(vu.getSource())
41-
)
39+
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) | alwaysNullExpr(vu.getSource()))
4240
}
4341

4442
/** Holds if expression `expr` always evaluates to non-null. */

csharp/ql/src/semmle/code/cil/Method.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class MethodImplementation extends EntryPoint, @cil_method_implementation {
5353

5454
/** Gets a string representing the disassembly of this implementation. */
5555
string getDisassembly() {
56-
result = concat(Instruction i | i = this.getAnInstruction() | i.toString(), ", " order by i.getIndex())
56+
result = concat(Instruction i |
57+
i = this.getAnInstruction()
58+
|
59+
i.toString(), ", " order by i.getIndex()
60+
)
5761
}
5862
}
5963

@@ -69,9 +73,7 @@ class Method extends DotNet::Callable, Element, Member, TypeContainer, DataFlowN
6973
MethodImplementation getAnImplementation() { result.getMethod() = this }
7074

7175
/** Gets the "best" implementation of this method, if any. */
72-
BestImplementation getImplementation() {
73-
result = getAnImplementation()
74-
}
76+
BestImplementation getImplementation() { result = getAnImplementation() }
7577

7678
override Method getMethod() { result = this }
7779

@@ -241,9 +243,7 @@ class TrivialGetter extends Method {
241243
}
242244

243245
/** Gets the underlying field of this getter. */
244-
Field getField() {
245-
getImplementation().getAnInstruction().(FieldReadAccess).getTarget() = result
246-
}
246+
Field getField() { getImplementation().getAnInstruction().(FieldReadAccess).getTarget() = result }
247247
}
248248

249249
/** A setter. */

csharp/ql/src/semmle/code/csharp/commons/Constants.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ private predicate isConstantCondition0(ControlFlow::Node cfn, boolean b) {
1616
* Holds if `e` is a condition that always evaluates to Boolean value `b`.
1717
*/
1818
predicate isConstantCondition(Expr e, boolean b) {
19-
forex(ControlFlow::Node cfn | cfn = e.getAControlFlowNode() |
20-
isConstantCondition0(cfn, b)
21-
)
19+
forex(ControlFlow::Node cfn | cfn = e.getAControlFlowNode() | isConstantCondition0(cfn, b))
2220
}
2321

2422
/**

csharp/ql/src/semmle/code/csharp/controlflow/ControlFlowElement.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class ControlFlowElement extends ExprOrStmtParent, @control_flow_element {
2020
Callable getEnclosingCallable() { none() }
2121

2222
/** Gets the assembly that this element was compiled into. */
23-
Assembly getAssembly() {
24-
result = this.getEnclosingCallable().getDeclaringType().getALocation()
25-
}
23+
Assembly getAssembly() { result = this.getEnclosingCallable().getDeclaringType().getALocation() }
2624

2725
/**
2826
* Gets a control flow node for this element. That is, a node in the

csharp/ql/src/semmle/code/csharp/dataflow/SSA.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,9 +2254,7 @@ module Ssa {
22542254
* except for pathological `out`/`ref` assignments like `M(out x, out x)`,
22552255
* where there may be more than one underlying definition.
22562256
*/
2257-
AssignableDefinition getADefinition() {
2258-
result = getADefinition(this)
2259-
}
2257+
AssignableDefinition getADefinition() { result = getADefinition(this) }
22602258

22612259
/**
22622260
* Holds if this definition updates a captured local scope variable, and the updated

csharp/ql/src/semmle/code/csharp/frameworks/EntityFramework.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ module EntityFramework {
5353
}
5454

5555
/** The class `Microsoft.EntityFrameworkCore.DbSet<>` or `System.Data.Entity.DbSet<>`. */
56-
class DbSet extends EFClass, UnboundGenericClass { DbSet() { this.getName() = "DbSet<>" } }
56+
class DbSet extends EFClass, UnboundGenericClass {
57+
DbSet() { this.getName() = "DbSet<>" }
58+
}
5759

5860
/** The class `Microsoft.EntityFrameworkCore.DbQuery<>` or `System.Data.Entity.DbQuery<>`. */
59-
class DbQuery extends EFClass, UnboundGenericClass { DbQuery() { this.hasName("DbQuery<>") } }
61+
class DbQuery extends EFClass, UnboundGenericClass {
62+
DbQuery() { this.hasName("DbQuery<>") }
63+
}
6064

6165
/** A generic type or method that takes a mapped type as its type argument. */
6266
private predicate usesMappedType(UnboundGeneric g) {

0 commit comments

Comments
 (0)