Closed Bug 1884533 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

(Not tracked)

RESOLVED DUPLICATE of bug 1884433

People

(Reporter: asaldegar2010, Unassigned)

References

()

Details

Attachments

(2 files)

Attached file C-C TB unde —

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.

Attached file mb_convert_encoding() —
Flags: needinfo?(asaldegar2010)
Flags: needinfo?(asaldegar2010)

Comment #0 is almost a carbon copy of bug 1884433, comment #0.

Status: UNCONFIRMED → RESOLVED
Closed: 2 years ago
Duplicate of bug: 1884433
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: