Skip to content

Commit 055017d

Browse files
authored
fix how non existing locations are accounted for
1 parent 9538ac7 commit 055017d

File tree

5 files changed

+60
-50
lines changed

5 files changed

+60
-50
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ private import internal.OperandInternal
2020
private class TStageOperand =
2121
TRegisterOperand or TNonSSAMemoryOperand or TPhiOperand or TChiOperand;
2222

23+
/**
24+
* A known location. Testing `loc instanceof KnownLocation` will account for non existing locations, as
25+
* opposed to testing `not loc isntanceof UnknownLocation`
26+
*/
27+
private class KnownLocation extends Language::Location {
28+
KnownLocation() { not this instanceof Language::UnknownLocation }
29+
}
30+
2331
/**
2432
* An operand of an `Instruction`. The operand represents a use of the result of one instruction
2533
* (the defining instruction) in another instruction (the use instruction)
@@ -414,17 +422,11 @@ class ThisArgumentOperand extends ArgumentOperand {
414422
override ThisArgumentOperandTag tag;
415423

416424
// in most cases the def location makes more sense, but in some corner cases it
417-
// does not have a location: in those cases we fall back to the use location
425+
// has an unknown location: in those cases we fall back to the use location
418426
override Language::Location getLocation() {
419-
result = selectLocation(this.getAnyDef().getLocation(), this.getUse().getLocation())
420-
}
421-
422-
private Language::Location selectLocation(
423-
Language::Location preferred, Language::Location fallback
424-
) {
425-
if not exists(preferred) or preferred instanceof Language::UnknownLocation
426-
then result = fallback
427-
else result = preferred
427+
if this.getAnyDef().getLocation() instanceof KnownLocation
428+
then result = this.getAnyDef().getLocation()
429+
else result = this.getUse().getLocation()
428430
}
429431
}
430432

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/Operand.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ private import internal.OperandInternal
2020
private class TStageOperand =
2121
TRegisterOperand or TNonSSAMemoryOperand or TPhiOperand or TChiOperand;
2222

23+
/**
24+
* A known location. Testing `loc instanceof KnownLocation` will account for non existing locations, as
25+
* opposed to testing `not loc isntanceof UnknownLocation`
26+
*/
27+
private class KnownLocation extends Language::Location {
28+
KnownLocation() { not this instanceof Language::UnknownLocation }
29+
}
30+
2331
/**
2432
* An operand of an `Instruction`. The operand represents a use of the result of one instruction
2533
* (the defining instruction) in another instruction (the use instruction)
@@ -414,17 +422,11 @@ class ThisArgumentOperand extends ArgumentOperand {
414422
override ThisArgumentOperandTag tag;
415423

416424
// in most cases the def location makes more sense, but in some corner cases it
417-
// does not have a location: in those cases we fall back to the use location
425+
// has an unknown location: in those cases we fall back to the use location
418426
override Language::Location getLocation() {
419-
result = selectLocation(this.getAnyDef().getLocation(), this.getUse().getLocation())
420-
}
421-
422-
private Language::Location selectLocation(
423-
Language::Location preferred, Language::Location fallback
424-
) {
425-
if not exists(preferred) or preferred instanceof Language::UnknownLocation
426-
then result = fallback
427-
else result = preferred
427+
if this.getAnyDef().getLocation() instanceof KnownLocation
428+
then result = this.getAnyDef().getLocation()
429+
else result = this.getUse().getLocation()
428430
}
429431
}
430432

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ private import internal.OperandInternal
2020
private class TStageOperand =
2121
TRegisterOperand or TNonSSAMemoryOperand or TPhiOperand or TChiOperand;
2222

23+
/**
24+
* A known location. Testing `loc instanceof KnownLocation` will account for non existing locations, as
25+
* opposed to testing `not loc isntanceof UnknownLocation`
26+
*/
27+
private class KnownLocation extends Language::Location {
28+
KnownLocation() { not this instanceof Language::UnknownLocation }
29+
}
30+
2331
/**
2432
* An operand of an `Instruction`. The operand represents a use of the result of one instruction
2533
* (the defining instruction) in another instruction (the use instruction)
@@ -414,17 +422,11 @@ class ThisArgumentOperand extends ArgumentOperand {
414422
override ThisArgumentOperandTag tag;
415423

416424
// in most cases the def location makes more sense, but in some corner cases it
417-
// does not have a location: in those cases we fall back to the use location
425+
// has an unknown location: in those cases we fall back to the use location
418426
override Language::Location getLocation() {
419-
result = selectLocation(this.getAnyDef().getLocation(), this.getUse().getLocation())
420-
}
421-
422-
private Language::Location selectLocation(
423-
Language::Location preferred, Language::Location fallback
424-
) {
425-
if not exists(preferred) or preferred instanceof Language::UnknownLocation
426-
then result = fallback
427-
else result = preferred
427+
if this.getAnyDef().getLocation() instanceof KnownLocation
428+
then result = this.getAnyDef().getLocation()
429+
else result = this.getUse().getLocation()
428430
}
429431
}
430432

csharp/ql/src/experimental/ir/implementation/raw/Operand.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ private import internal.OperandInternal
2020
private class TStageOperand =
2121
TRegisterOperand or TNonSSAMemoryOperand or TPhiOperand or TChiOperand;
2222

23+
/**
24+
* A known location. Testing `loc instanceof KnownLocation` will account for non existing locations, as
25+
* opposed to testing `not loc isntanceof UnknownLocation`
26+
*/
27+
private class KnownLocation extends Language::Location {
28+
KnownLocation() { not this instanceof Language::UnknownLocation }
29+
}
30+
2331
/**
2432
* An operand of an `Instruction`. The operand represents a use of the result of one instruction
2533
* (the defining instruction) in another instruction (the use instruction)
@@ -414,17 +422,11 @@ class ThisArgumentOperand extends ArgumentOperand {
414422
override ThisArgumentOperandTag tag;
415423

416424
// in most cases the def location makes more sense, but in some corner cases it
417-
// does not have a location: in those cases we fall back to the use location
425+
// has an unknown location: in those cases we fall back to the use location
418426
override Language::Location getLocation() {
419-
result = selectLocation(this.getAnyDef().getLocation(), this.getUse().getLocation())
420-
}
421-
422-
private Language::Location selectLocation(
423-
Language::Location preferred, Language::Location fallback
424-
) {
425-
if not exists(preferred) or preferred instanceof Language::UnknownLocation
426-
then result = fallback
427-
else result = preferred
427+
if this.getAnyDef().getLocation() instanceof KnownLocation
428+
then result = this.getAnyDef().getLocation()
429+
else result = this.getUse().getLocation()
428430
}
429431
}
430432

csharp/ql/src/experimental/ir/implementation/unaliased_ssa/Operand.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ private import internal.OperandInternal
2020
private class TStageOperand =
2121
TRegisterOperand or TNonSSAMemoryOperand or TPhiOperand or TChiOperand;
2222

23+
/**
24+
* A known location. Testing `loc instanceof KnownLocation` will account for non existing locations, as
25+
* opposed to testing `not loc isntanceof UnknownLocation`
26+
*/
27+
private class KnownLocation extends Language::Location {
28+
KnownLocation() { not this instanceof Language::UnknownLocation }
29+
}
30+
2331
/**
2432
* An operand of an `Instruction`. The operand represents a use of the result of one instruction
2533
* (the defining instruction) in another instruction (the use instruction)
@@ -414,17 +422,11 @@ class ThisArgumentOperand extends ArgumentOperand {
414422
override ThisArgumentOperandTag tag;
415423

416424
// in most cases the def location makes more sense, but in some corner cases it
417-
// does not have a location: in those cases we fall back to the use location
425+
// has an unknown location: in those cases we fall back to the use location
418426
override Language::Location getLocation() {
419-
result = selectLocation(this.getAnyDef().getLocation(), this.getUse().getLocation())
420-
}
421-
422-
private Language::Location selectLocation(
423-
Language::Location preferred, Language::Location fallback
424-
) {
425-
if not exists(preferred) or preferred instanceof Language::UnknownLocation
426-
then result = fallback
427-
else result = preferred
427+
if this.getAnyDef().getLocation() instanceof KnownLocation
428+
then result = this.getAnyDef().getLocation()
429+
else result = this.getUse().getLocation()
428430
}
429431
}
430432

0 commit comments

Comments
 (0)