lint: fix null pointer dereference in invalid case expression

This commit is contained in:
rillig 2023-02-21 19:47:21 +00:00
parent fb6da963ac
commit 68d13a7aa7
2 changed files with 23 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_193.c,v 1.19 2022/06/17 18:54:53 rillig Exp $ */
/* $NetBSD: msg_193.c,v 1.20 2023/02/21 19:47:21 rillig Exp $ */
# 3 "msg_193.c"
// Test for message: statement not reached [193]
@ -704,3 +704,17 @@ test_null_statement(void)
/* expect+1: warning: statement not reached [193] */
return 0;;
}
/*
* Before func.c 1.149 from 2023-02-21, lint crashed due to a null pointer
* dereference.
*/
void
invalid_case_expression(void)
{
switch (4) {
/* expect+1: error: operand of '~' has invalid type 'double' [108] */
case ~0.0:
;
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: func.c,v 1.148 2023/02/18 15:18:49 rillig Exp $ */
/* $NetBSD: func.c,v 1.149 2023/02/21 19:47:21 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: func.c,v 1.148 2023/02/18 15:18:49 rillig Exp $");
__RCSID("$NetBSD: func.c,v 1.149 2023/02/21 19:47:21 rillig Exp $");
#endif
#include <stdlib.h>
@ -441,6 +441,7 @@ named_label(sym_t *sym)
mark_as_set(sym);
}
/* XXX: Assuming that each label is reachable is wrong. */
set_reached(true);
}
@ -495,13 +496,16 @@ check_case_label(tnode_t *tn, control_statement *cs)
return;
}
if (tn != NULL && tn->tn_op != CON) {
if (tn == NULL)
return;
if (tn->tn_op != CON) {
/* non-constant case expression */
error(197);
return;
}
if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
if (!is_integer(tn->tn_type->t_tspec)) {
/* non-integral case expression */
error(198);
return;