Closed Bug 1884433 Opened 2 years ago Closed 2 years ago

GCC warning for missing default clause in a switch statement.

Categories

(MailNews Core :: General, enhancement)

x86_64
Linux
enhancement

Tracking

(thunderbird123 wontfix)

RESOLVED FIXED
125 Branch
Tracking Status
thunderbird123 --- wontfix

People

(Reporter: ishikawa, Assigned: ishikawa)

References

Details

Attachments

(1 external file)

I compile C-C TB under linux on local PC.

I get a strange warning which is handled as error since I compile C-C TB with -Werror option.

The warning (turned error) is as follows.

/NEW-SSD/NREF-COMM-CENTRAL/mozilla/comm/mailnews/base/src/MboxMsgInputStream.cpp: In member function ‘MboxParser::span MboxParser::handle(span)’:
/NEW-SSD/NREF-COMM-CENTRAL/mozilla/comm/mailnews/base/src/MboxMsgInputStream.cpp:245:3: error: control reaches end of non-void function [-Werror=return-type]
  245 |   }
      |   ^
cc1plus: all warnings being treated as errors

It turns out that GCC takes a very pessimistic view of software coding practice and the programmer may enter arbitrary scalar value to an enum variable and assume that when a value outside the declared enum range is assigned to an enum value, anything goes and a switch statement where presumably all the return will take place becomes a no-op and the control reaches the end of the function.

Thus, I am forced to add |default:' clause to the switch statement.

Problem boils down to the following handling of enum variable. A short example.
Note the assignment of -1 to the enum value in the following code. Both GCC and CLANG do not warn.
So, in real life, guarding against such programming malpractice is in order.

typedef enum typeE {
  a = 0,
  b,
  c,
} typeE;


extern float func_a();
extern float func_b();
extern float func_c();

float
test (typeE m ) {
  switch (m) {
    case a: return func_a();
    case b: return func_b();
    case c: return func_c();
  }
  m = -1;
}

The following code produces a warning with -Wall and fails with -Werror when compiled with GCC.
The message
OTOH, clang does not produce a warning and compiles even with -Werror.

Patch to add default: clause follows.

/NEW-SSD/NREF-COMM-CENTRAL/mozilla/comm/mailnews/base/src/MboxMsgInputStream.cpp: In member function ‘MboxParser::span MboxParser::handle(span)’:
/NEW-SSD/NREF-COMM-CENTRAL/mozilla/comm/mailnews/base/src/MboxMsgInputStream.cpp:245:3: error: control reaches end of non-void function [-Werror=return-type]
245 | }
| ^
cc1plus: all warnings being treated as errors

Assignee: nobody → ishikawa
Status: NEW → ASSIGNED

The following code that assigns -1 to a global enum variable also gets compiled without a warning from clang.
GCC does not warn the assignment but produces the warning (turned error) as in the original problem in this bugzilla entry.

typedef enum typeE {
  a = 0,
  b,
  c,
} typeE;

extern typeE em;

extern float func_a();
extern float func_b();
extern float func_c();

float
test (typeE m ) {
  switch (m) {
    case a: return func_a();
    case b: return func_b();
    case c: return func_c();
  }
  em = -1;
}

A global analysis across files will be necessary to deduce that m may indeed have the value of a, b, and c inside a program when the control reaches the switch statement, but that is beyond compilation of this single file.

I favor GCC's conservative attitude.
clang seems to be too naive in the presence of badly written programs in the world.

Can you use MOZ_ASSERT_UNREACHABLE() instead of MOZ_ASSERT(0)?

Duplicate of this bug: 1884533

(In reply to betterbird.project+15@gmail.com from comment #3)

Can you use MOZ_ASSERT_UNREACHABLE() instead of MOZ_ASSERT(0)?

I surely can. I was not aware of this macro.
Fixing the patch shortly.

As for 1984533. That is a very strange bugzilla. I think it got exactly the same message of my post and was posted by soneone, but then the comment 0 shows a different user? I am afraid that there is a bugzilla bug somewhere...

(In reply to betterbird.project+15@gmail.com from comment #6)

I surely can. I was not aware of this macro.

You used it in bug 1817858:
https://hg.mozilla.org/comm-central/rev/4b5db584a2be7ebd22a272e23b7d1f0bcbd36646#l1.13

Right!
Learning a new trick has become a bit difficult for an old dog, so to speak.
There are probably tons of mozilla-specific macros I am not aware of, and I am not sure if I ever get the hang of using them properly.

Attachment #9390242 - Attachment description: Bug 1884433 - add default in a switch statement to suppress GCC warning, r=benc → Bug 1884433 - add default in a switch statement to suppress GCC warning, r=BenC
Target Milestone: --- → 125 Branch

Pushed by mkmelin@iki.fi:
https://hg.mozilla.org/comm-central/rev/bf8606e12ac6
add default in a switch statement to suppress GCC warning, r=BenC

Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: