Skip to content

Commit 69502d0

Browse files
committed
C++: Add some more tests.
1 parent 6669cf8 commit 69502d0

File tree

1 file changed

+60
-0
lines changed
  • cpp/ql/test/query-tests/Security/CWE/CWE-457/semmle/tests

1 file changed

+60
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-457/semmle/tests/test.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,64 @@ void test44() {
472472
int y = 1;
473473

474474
void(x + y); // BAD
475+
}
476+
477+
enum class State { StateA, StateB, StateC };
478+
479+
int exhaustive_switch(State s) {
480+
int y;
481+
switch(s) {
482+
case State::StateA:
483+
y = 1;
484+
break;
485+
case State::StateB:
486+
y = 2;
487+
break;
488+
case State::StateC:
489+
y = 3;
490+
break;
491+
}
492+
return y; // GOOD (y is always initialized)
493+
}
494+
495+
int exhaustive_switch_2(State s) {
496+
int y;
497+
switch(s) {
498+
case State::StateA:
499+
y = 1;
500+
break;
501+
default:
502+
y = 2;
503+
break;
504+
}
505+
return y; // GOOD (y is always initialized)
506+
}
507+
508+
int non_exhaustive_switch(State s) {
509+
int y;
510+
switch(s) {
511+
case State::StateA:
512+
y = 1;
513+
break;
514+
case State::StateB:
515+
y = 2;
516+
break;
517+
}
518+
return y; // BAD [NOT DETECTED] (y is not initialized when s = StateC)
519+
}
520+
521+
int non_exhaustive_switch_2(State s) {
522+
int y;
523+
switch(s) {
524+
case State::StateA:
525+
y = 1;
526+
break;
527+
case State::StateB:
528+
y = 2;
529+
break;
530+
}
531+
if(s != State::StateC) {
532+
return y; // GOOD (y is not initialized when s = StateC, but if s = StateC we won't reach this point)
533+
}
534+
return 0;
475535
}

0 commit comments

Comments
 (0)