Use abort() instead of assert(false) for things that should never happen

assert() is compiled out for release builds, but we don't want to
continue running in these impossible situations.

This also resolves the "implicit fallthrough" warnings for the asserts
in switch cases.
This commit is contained in:
James McCoy
2021-01-31 10:43:03 -05:00
parent 035ee868ae
commit 27a7a4d384
22 changed files with 44 additions and 44 deletions

View File

@@ -3270,7 +3270,7 @@ void maketitle(void)
case 6: buf_p = strappend(buf_p, " -"); break; case 6: buf_p = strappend(buf_p, " -"); break;
case 5: case 5:
case 7: buf_p = strappend(buf_p, " -+"); break; case 7: buf_p = strappend(buf_p, " -+"); break;
default: assert(false); default: abort();
} }
if (curbuf->b_fname != NULL) { if (curbuf->b_fname != NULL) {

View File

@@ -1747,7 +1747,7 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
goto vim_str2nr_dec; goto vim_str2nr_dec;
} }
default: { default: {
assert(false); abort();
} }
} }
} else if ((what & (STR2NR_HEX|STR2NR_OCT|STR2NR_BIN)) } else if ((what & (STR2NR_HEX|STR2NR_OCT|STR2NR_BIN))
@@ -1788,7 +1788,7 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
} }
// Do the string-to-numeric conversion "manually" to avoid sscanf quirks. // Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
assert(false); // Shouldve used goto earlier. abort(); // Shouldve used goto earlier.
#define PARSE_NUMBER(base, cond, conv) \ #define PARSE_NUMBER(base, cond, conv) \
do { \ do { \
while (!STRING_ENDED(ptr) && (cond)) { \ while (!STRING_ENDED(ptr) && (cond)) { \

View File

@@ -365,7 +365,7 @@ void eval_init(void)
eval_msgpack_type_lists[i] = type_list; eval_msgpack_type_lists[i] = type_list;
if (tv_dict_add(msgpack_types_dict, di) == FAIL) { if (tv_dict_add(msgpack_types_dict, di) == FAIL) {
// There must not be duplicate items in this dictionary by definition. // There must not be duplicate items in this dictionary by definition.
assert(false); abort();
} }
} }
msgpack_types_dict->dv_lock = VAR_FIXED; msgpack_types_dict->dv_lock = VAR_FIXED;

View File

@@ -147,7 +147,7 @@ static inline int json_decoder_pop(ValuesStackItem obj,
tv_clear(&key.val); tv_clear(&key.val);
if (tv_dict_add(last_container.container.vval.v_dict, obj_di) if (tv_dict_add(last_container.container.vval.v_dict, obj_di)
== FAIL) { == FAIL) {
assert(false); abort();
} }
obj_di->di_tv = obj.val; obj_di->di_tv = obj.val;
} else { } else {
@@ -480,7 +480,7 @@ static inline int parse_json_string(const char *const buf, const size_t buf_len,
break; break;
} }
default: { default: {
assert(false); abort();
} }
} }
} else { } else {

View File

@@ -174,7 +174,7 @@ static int conv_error(const char *const msg, const MPConvStack *const mpstack,
case kMPConvPartial: { case kMPConvPartial: {
switch (v.data.p.stage) { switch (v.data.p.stage) {
case kMPConvPartialArgs: { case kMPConvPartialArgs: {
assert(false); abort();
break; break;
} }
case kMPConvPartialSelf: { case kMPConvPartialSelf: {
@@ -237,7 +237,7 @@ bool encode_vim_list_to_buf(const list_T *const list, size_t *const ret_len,
char *const buf = xmalloc(len); char *const buf = xmalloc(len);
size_t read_bytes; size_t read_bytes;
if (encode_read_from_list(&lrstate, buf, len, &read_bytes) != OK) { if (encode_read_from_list(&lrstate, buf, len, &read_bytes) != OK) {
assert(false); abort();
} }
assert(len == read_bytes); assert(len == read_bytes);
*ret_buf = buf; *ret_buf = buf;

View File

@@ -118,7 +118,7 @@ int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2,
return OK; return OK;
} }
case VAR_UNKNOWN: { case VAR_UNKNOWN: {
assert(false); abort();
} }
} }
} }

View File

@@ -3307,7 +3307,7 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr)
} }
break; break;
case kCdScopeInvalid: // We should never get here case kCdScopeInvalid: // We should never get here
assert(false); abort();
} }
if (from) { if (from) {
@@ -4360,7 +4360,7 @@ static void f_haslocaldir(typval_T *argvars, typval_T *rettv, FunPtr fptr)
break; break;
case kCdScopeInvalid: case kCdScopeInvalid:
// We should never get here // We should never get here
assert(false); abort();
} }
} }

View File

@@ -2521,7 +2521,7 @@ void tv_item_lock(typval_T *const tv, const int deep, const bool lock)
break; break;
} }
case VAR_UNKNOWN: { case VAR_UNKNOWN: {
assert(false); abort();
} }
} }
#undef CHANGE_LOCK #undef CHANGE_LOCK
@@ -2693,7 +2693,7 @@ bool tv_equal(typval_T *const tv1, typval_T *const tv2, const bool ic,
} }
} }
assert(false); abort();
return false; return false;
} }
@@ -2746,7 +2746,7 @@ bool tv_check_str_or_nr(const typval_T *const tv)
return false; return false;
} }
} }
assert(false); abort();
return false; return false;
} }
@@ -2791,7 +2791,7 @@ bool tv_check_num(const typval_T *const tv)
return false; return false;
} }
} }
assert(false); abort();
return false; return false;
} }
@@ -2836,7 +2836,7 @@ bool tv_check_str(const typval_T *const tv)
return false; return false;
} }
} }
assert(false); abort();
return false; return false;
} }

View File

@@ -7591,7 +7591,7 @@ void post_chdir(CdScope scope, bool trigger_dirchanged)
curwin->w_localdir = (char_u *)xstrdup(cwd); curwin->w_localdir = (char_u *)xstrdup(cwd);
break; break;
case kCdScopeInvalid: case kCdScopeInvalid:
assert(false); abort();
} }
shorten_fnames(true); shorten_fnames(true);

View File

@@ -6383,7 +6383,7 @@ int hist_type2char(int type)
return '>'; return '>';
} }
default: { default: {
assert(false); abort();
} }
} }
return NUL; return NUL;

View File

@@ -1595,7 +1595,7 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope, bool changed_window)
} }
case kCdScopeInvalid: { case kCdScopeInvalid: {
// Should never happen. // Should never happen.
assert(false); abort();
} }
} }

View File

@@ -245,7 +245,7 @@ bool nlua_pop_typval(lua_State *lstate, typval_T *ret_tv)
} else { } else {
dictitem_T *const di = tv_dict_item_alloc_len(s, len); dictitem_T *const di = tv_dict_item_alloc_len(s, len);
if (tv_dict_add(cur.tv->vval.v_dict, di) == FAIL) { if (tv_dict_add(cur.tv->vval.v_dict, di) == FAIL) {
assert(false); abort();
} }
kv_push(stack, cur); kv_push(stack, cur);
cur = (TVPopStackItem) { &di->di_tv, false, false, 0 }; cur = (TVPopStackItem) { &di->di_tv, false, false, 0 };
@@ -391,7 +391,7 @@ bool nlua_pop_typval(lua_State *lstate, typval_T *ret_tv)
break; break;
} }
default: { default: {
assert(false); abort();
} }
} }
nlua_pop_typval_table_processing_end: nlua_pop_typval_table_processing_end:
@@ -1200,7 +1200,7 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err)
break; break;
} }
default: { default: {
assert(false); abort();
} }
} }
break; break;

View File

@@ -2623,7 +2623,7 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
} }
// NOTREACHED // NOTREACHED
case kMTUnknown: case kMTUnknown:
assert(false); abort();
} }
} }
@@ -6092,7 +6092,7 @@ static void set_clipboard(int name, yankreg_T *reg)
break; break;
} }
case kMTUnknown: { case kMTUnknown: {
assert(false); abort();
} }
} }

View File

@@ -97,7 +97,7 @@ void os_microdelay(uint64_t us, bool ignoreinput)
const int rv = uv_cond_timedwait(&delay_cond, &delay_mutex, ns_delta); const int rv = uv_cond_timedwait(&delay_cond, &delay_mutex, ns_delta);
if (0 != rv && UV_ETIMEDOUT != rv) { if (0 != rv && UV_ETIMEDOUT != rv) {
assert(false); abort();
break; break;
} // Else: Timeout proceeded normally. } // Else: Timeout proceeded normally.

View File

@@ -5648,7 +5648,7 @@ static int get_qfline_items(qfline_T *qfp, list_T *list)
== FAIL)) { == FAIL)) {
// tv_dict_add* fail only if key already exist, but this is a newly // tv_dict_add* fail only if key already exist, but this is a newly
// allocated dictionary which is thus guaranteed to have no existing keys. // allocated dictionary which is thus guaranteed to have no existing keys.
assert(false); abort();
} }
return OK; return OK;

View File

@@ -765,7 +765,7 @@ static ShaDaReadResult sd_reader_skip(ShaDaReadDef *const sd_reader,
(uint64_t) offset); (uint64_t) offset);
return kSDReadStatusNotShaDa; return kSDReadStatusNotShaDa;
} }
assert(false); abort();
} }
return kSDReadStatusSuccess; return kSDReadStatusSuccess;
} }
@@ -1224,7 +1224,7 @@ static void shada_read(ShaDaReadDef *const sd_reader, const int flags)
} }
case kSDReadStatusFinished: { case kSDReadStatusFinished: {
// Should be handled by the while condition. // Should be handled by the while condition.
assert(false); abort();
} }
case kSDReadStatusNotShaDa: case kSDReadStatusNotShaDa:
case kSDReadStatusReadError: { case kSDReadStatusReadError: {
@@ -1236,7 +1236,7 @@ static void shada_read(ShaDaReadDef *const sd_reader, const int flags)
} }
switch (cur_entry.type) { switch (cur_entry.type) {
case kSDItemMissing: { case kSDItemMissing: {
assert(false); abort();
} }
case kSDItemUnknown: { case kSDItemUnknown: {
break; break;
@@ -1628,7 +1628,7 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
((size_t) (!CHECK_DEFAULT(entry, attr))) ((size_t) (!CHECK_DEFAULT(entry, attr)))
switch (entry.type) { switch (entry.type) {
case kSDItemMissing: { case kSDItemMissing: {
assert(false); abort();
} }
case kSDItemUnknown: { case kSDItemUnknown: {
if (spacker->callback(spacker->data, entry.data.unknown_item.contents, if (spacker->callback(spacker->data, entry.data.unknown_item.contents,
@@ -1850,7 +1850,7 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
break; break;
} }
default: { default: {
assert(false); abort();
} }
} }
} }
@@ -2147,7 +2147,7 @@ static inline ShaDaWriteResult shada_read_when_writing(
} }
case kSDReadStatusFinished: { case kSDReadStatusFinished: {
// Should be handled by the while condition. // Should be handled by the while condition.
assert(false); abort();
} }
case kSDReadStatusNotShaDa: { case kSDReadStatusNotShaDa: {
ret = kSDWriteReadNotShada; ret = kSDWriteReadNotShada;
@@ -2184,7 +2184,7 @@ static inline ShaDaWriteResult shada_read_when_writing(
} }
case kSDItemHeader: case kSDItemHeader:
case kSDItemBufferList: { case kSDItemBufferList: {
assert(false); abort();
} }
case kSDItemUnknown: { case kSDItemUnknown: {
ret = shada_pack_entry(packer, entry, 0); ret = shada_pack_entry(packer, entry, 0);
@@ -4044,7 +4044,7 @@ shada_read_next_item_start:
} }
case kSDItemMissing: case kSDItemMissing:
case kSDItemUnknown: { case kSDItemUnknown: {
assert(false); abort();
} }
} }
entry->type = (ShadaEntryType) type_u64; entry->type = (ShadaEntryType) type_u64;

View File

@@ -1461,7 +1461,7 @@ find_tags(
p_ic = ignorecase_opt(pat, true, true); p_ic = ignorecase_opt(pat, true, true);
break; break;
default: default:
assert(false); abort();
} }
help_save = curbuf->b_help; help_save = curbuf->b_help;

View File

@@ -297,7 +297,7 @@ static void forward_mouse_event(TermInput *input, TermKeyKey *key)
len += (size_t)snprintf(buf + len, sizeof(buf) - len, "Release"); len += (size_t)snprintf(buf + len, sizeof(buf) - len, "Release");
break; break;
case TERMKEY_MOUSE_UNKNOWN: case TERMKEY_MOUSE_UNKNOWN:
assert(false); abort();
} }
len += (size_t)snprintf(buf + len, sizeof(buf) - len, "><%d,%d>", col, row); len += (size_t)snprintf(buf + len, sizeof(buf) - len, "><%d,%d>", col, row);

View File

@@ -157,7 +157,7 @@ bool ui_comp_put_grid(ScreenGrid *grid, int row, int col, int height, int width,
#ifndef NDEBUG #ifndef NDEBUG
for (size_t i = 0; i < kv_size(layers); i++) { for (size_t i = 0; i < kv_size(layers); i++) {
if (kv_A(layers, i) == grid) { if (kv_A(layers, i) == grid) {
assert(false); abort();
} }
} }
#endif #endif

View File

@@ -2078,7 +2078,7 @@ viml_pexpr_parse_process_token:
case kExprLexMissing: case kExprLexMissing:
case kExprLexSpacing: case kExprLexSpacing:
case kExprLexEOC: { case kExprLexEOC: {
assert(false); abort();
} }
case kExprLexInvalid: { case kExprLexInvalid: {
ERROR_FROM_TOKEN(cur_token); ERROR_FROM_TOKEN(cur_token);
@@ -3028,7 +3028,7 @@ viml_pexpr_parse_end:
// Until trailing "}" it is impossible to distinguish curly braces // Until trailing "}" it is impossible to distinguish curly braces
// identifier and dictionary, so it must not appear in the stack like // identifier and dictionary, so it must not appear in the stack like
// this. // this.
assert(false); abort();
} }
case kExprNodeInteger: case kExprNodeInteger:
case kExprNodeFloat: case kExprNodeFloat:
@@ -3042,7 +3042,7 @@ viml_pexpr_parse_end:
// These are plain values and not containers, for them it should only // These are plain values and not containers, for them it should only
// be possible to show up in the topmost stack element, but it was // be possible to show up in the topmost stack element, but it was
// unconditionally popped at the start. // unconditionally popped at the start.
assert(false); abort();
} }
case kExprNodeComma: case kExprNodeComma:
case kExprNodeColon: case kExprNodeColon:

View File

@@ -62,7 +62,7 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
goto vim_str2nr_dec; goto vim_str2nr_dec;
} }
default: { default: {
assert(false); abort();
} }
} }
} else if ((what & (STR2NR_HEX|STR2NR_OCT|STR2NR_BIN)) } else if ((what & (STR2NR_HEX|STR2NR_OCT|STR2NR_BIN))
@@ -102,7 +102,7 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
} }
// Do the string-to-numeric conversion "manually" to avoid sscanf quirks. // Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
assert(false); // Shouldve used goto earlier. abort(); // Shouldve used goto earlier.
#define PARSE_NUMBER(base, cond, conv) \ #define PARSE_NUMBER(base, cond, conv) \
do { \ do { \
while (!STRING_ENDED(ptr) && (cond)) { \ while (!STRING_ENDED(ptr) && (cond)) { \

View File

@@ -45,7 +45,7 @@ void xfree(void *const p)
return; return;
} }
} }
assert(false); abort();
} }
void *xrealloc(void *const p, size_t new_size) void *xrealloc(void *const p, size_t new_size)
@@ -63,7 +63,7 @@ void *xrealloc(void *const p, size_t new_size)
return ret; return ret;
} }
} }
assert(false); abort();
return (void *)(intptr_t)1; return (void *)(intptr_t)1;
} }