Skip to content

Commit b37173e

Browse files
committed
Silence "no return value"/"unreachable statement" warnings.
clang incorrectly treats a default label as unreachable in a switch on an enum that has all the cases covered. gcc and msvc complain about missing return values if there is no default statement. By making the default case fall through to the JS_TUNDEFINED case we silence all the compilers and handle corrupt data (an enum that isn't one of the enumerated values) safely.
1 parent 89dc0c8 commit b37173e

4 files changed

Lines changed: 7 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ incdir ?= $(prefix)/include
88
libdir ?= $(prefix)/lib
99

1010
CC := clang
11-
CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wunreachable-code -Wno-unused-parameter
11+
CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -Wunreachable-code
1212

1313
ifeq "$(build)" "debug"
1414
CFLAGS += -g

jsi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <math.h>
1313
#include <float.h>
1414

15+
/* Microsoft Visual C */
1516
#ifdef _MSC_VER
1617
#pragma warning(disable:4996) /* _CRT_SECURE_NO_WARNINGS */
1718
#pragma warning(disable:4244) /* implicit conversion from double to int */

jsrun.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ static const char *js_typeof(js_State *J, int idx)
206206
{
207207
const js_Value *v = stackidx(J, idx);
208208
switch (v->type) {
209+
default:
209210
case JS_TUNDEFINED: return "undefined";
210211
case JS_TNULL: return "object";
211212
case JS_TBOOLEAN: return "boolean";

jsvalue.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ js_Value jsV_toprimitive(js_State *J, const js_Value *v, int preferred)
112112
int jsV_toboolean(js_State *J, const js_Value *v)
113113
{
114114
switch (v->type) {
115+
default:
115116
case JS_TUNDEFINED: return 0;
116117
case JS_TNULL: return 0;
117118
case JS_TBOOLEAN: return v->u.boolean;
@@ -169,6 +170,7 @@ double jsV_stringtonumber(js_State *J, const char *s)
169170
double jsV_tonumber(js_State *J, const js_Value *v)
170171
{
171172
switch (v->type) {
173+
default:
172174
case JS_TUNDEFINED: return NAN;
173175
case JS_TNULL: return 0;
174176
case JS_TBOOLEAN: return v->u.boolean;
@@ -242,6 +244,7 @@ const char *jsV_numbertostring(js_State *J, double f)
242244
const char *jsV_tostring(js_State *J, const js_Value *v)
243245
{
244246
switch (v->type) {
247+
default:
245248
case JS_TUNDEFINED: return "undefined";
246249
case JS_TNULL: return "null";
247250
case JS_TBOOLEAN: return v->u.boolean ? "true" : "false";
@@ -283,6 +286,7 @@ static js_Object *jsV_newstring(js_State *J, const char *v)
283286
js_Object *jsV_toobject(js_State *J, const js_Value *v)
284287
{
285288
switch (v->type) {
289+
default:
286290
case JS_TUNDEFINED: js_typeerror(J, "cannot convert undefined to object");
287291
case JS_TNULL: js_typeerror(J, "cannot convert null to object");
288292
case JS_TBOOLEAN: return jsV_newboolean(J, v->u.boolean);

0 commit comments

Comments
 (0)