1

I have an argument with my compilation course lecturer:

In the test that was part of this course, some of the questions referred the identification and classification code segments written in C. Each of these questions must indicate at what stage the error will expose:

a.Lexical analysis

b. Synthetic analysis

c. Semantic analysis

d. Running time (under certain conditions)

e. This is not an error.

One of the questions in this style was as follows:

Switch command that does not have the default component. For example:

switch (key){
case 1: .........
case 2: ......... 
case 3:.......... 
}

Now, in the official test solution, in the above case only option e was correct. However, I argue that Option d cannot necessarily be rejected outright, and that it is also true. As an argument, I showed (after the test) the following two examples to my lecturer:

1) enter image description here

(from : https://cwe.mitre.org/data/definitions/478.html)

2) enter image description here

(from : Should switch statements always contain a default clause?)

However, he is not yet convinced that the runtime error option is considered in this case. He said that because of the questions mentioned above, it is only for commands or snippets that are shown directly in the questions and because the code does not have the intent of the code, so in this case they are actually asking if this structure is by itself invalid, so you are denied a runtime error here (I personally do not notice any contradictory here ...).

I would be happy if you could share your views on this issue.

TRUMP
  • 103
  • 1
  • 2
  • 7
  • 1
    I don't buy your argument. You could use the same argument for every other question - "this is a runtime error because the programmer intended to do something else". – Joni Mar 25 '20 at 11:18
  • If you trap all other values with `default` you might be *introducing* an error. For example there might be other acceptable values of `x` that do not need consideration in a switch `case`, but which will get rejected by `default`. – Weather Vane Mar 25 '20 at 11:34
  • Post code, not pictures of code. That is, if you wish to have an answer and not a picture of an answer... – Lundin Mar 25 '20 at 14:50
  • @Lundin In this case, it was kinda necessary due to explanations shown there. – TRUMP Mar 25 '20 at 15:00

1 Answers1

2

Your lecturer is correct.

Omitting the default case in a switch is perfectly valid code and will not directly lead to any kind of problem. It may well be exactly what the programmer intended, and do the correct thing.

Of course, it is always possible to add some code that would cause problems, but that is a problem with this added code, not with the switch per se. Code style rules like "always add a default case" may guard against certain types of programming mistakes, but not following them does not automatically cause these mistakes - it "just" requires more caution.

From a code style perspective, it is usually better to be explicit about intentionally ignoring certain cases, or to add some default handler for guarding against unexpected values, but that does not mean that omitting such a handler is always incorrect by itself.

(Note that the currently second most upvoted answer in the question you linked to yourself argues for omitting default cases that are not doing anything useful, in order to reduce clutter - I don't fully agree with that, but it is a matter of style)

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • and what about the examples I added in my post? – TRUMP Mar 25 '20 at 14:04
  • Well, mitre.org classifies it as a "bad coding practice" that might be the cause of vulnerabilities, but that does not make this code invalid, and it will probably not cause a runtime error. In fact, mitre is probably more worried about the program continuing to work "just fine" although due to an oversight of the programmer, a security check did not return the expected state. This is a bug in the program logic, but not a runtime error. – Hulk Mar 25 '20 at 14:29
  • 1
    The NULL pointer access example will (hopefully) lead to a runtime error, but that is a bug in the code dereferencing the null pointer, not in the switch/the code shown here. Otherwise, any `if` without an `else` would fall into the same category, because some runtime problem might arise elsewhere due to forgotten things. – Hulk Mar 25 '20 at 14:32