ref: b37173e52eb390fb0601be3cc149fa708c748e75
parent: 89dc0c8cd1e3729cd579fbb179db9db1ac9f944b
author: Tor Andersson <[email protected]>
date: Wed Mar 26 10:27:53 EDT 2014
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.
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@
libdir ?= $(prefix)/lib
CC := clang
-CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wunreachable-code -Wno-unused-parameter
+CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -Wunreachable-code
ifeq "$(build)" "debug"
CFLAGS += -g
--- a/jsi.h
+++ b/jsi.h
@@ -12,6 +12,7 @@
#include <math.h>
#include <float.h>
+/* Microsoft Visual C */
#ifdef _MSC_VER
#pragma warning(disable:4996) /* _CRT_SECURE_NO_WARNINGS */
#pragma warning(disable:4244) /* implicit conversion from double to int */
--- a/jsrun.c
+++ b/jsrun.c
@@ -206,6 +206,7 @@
{
const js_Value *v = stackidx(J, idx);
switch (v->type) {
+ default:
case JS_TUNDEFINED: return "undefined";
case JS_TNULL: return "object";
case JS_TBOOLEAN: return "boolean";
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -112,6 +112,7 @@
int jsV_toboolean(js_State *J, const js_Value *v)
{
switch (v->type) {
+ default:
case JS_TUNDEFINED: return 0;
case JS_TNULL: return 0;
case JS_TBOOLEAN: return v->u.boolean;
@@ -169,6 +170,7 @@
double jsV_tonumber(js_State *J, const js_Value *v)
{
switch (v->type) {
+ default:
case JS_TUNDEFINED: return NAN;
case JS_TNULL: return 0;
case JS_TBOOLEAN: return v->u.boolean;
@@ -242,6 +244,7 @@
const char *jsV_tostring(js_State *J, const js_Value *v)
{
switch (v->type) {
+ default:
case JS_TUNDEFINED: return "undefined";
case JS_TNULL: return "null";
case JS_TBOOLEAN: return v->u.boolean ? "true" : "false";
@@ -283,6 +286,7 @@
js_Object *jsV_toobject(js_State *J, const js_Value *v)
{
switch (v->type) {
+ default:
case JS_TUNDEFINED: js_typeerror(J, "cannot convert undefined to object");
case JS_TNULL: js_typeerror(J, "cannot convert null to object");
case JS_TBOOLEAN: return jsV_newboolean(J, v->u.boolean);