summaryrefslogtreecommitdiff
path: root/usr.bin/xlint/lint1/ckctype.c
blob: 414e9158f8d4025294f3608c1e779079e5251b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* $NetBSD: ckctype.c,v 1.5 2022/05/20 21:18:55 rillig Exp $ */

/*-
 * Copyright (c) 2021 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Roland Illig <rillig@NetBSD.org>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/cdefs.h>

#if defined(__RCSID)
__RCSID("$NetBSD: ckctype.c,v 1.5 2022/05/20 21:18:55 rillig Exp $");
#endif

#include <string.h>

#include "lint1.h"

/*
 * Check that the functions from <ctype.h> are used properly.  They must not
 * be called with an argument of type 'char'.  In such a case, the argument
 * must be converted to 'unsigned char'.  The tricky thing is that even though
 * the expected argument type is 'int', a 'char' argument must not be directly
 * cast to 'int', as that would preserve negative argument values.
 *
 * See also:
 *	ctype(3)
 *	https://stackoverflow.com/a/60696378
 */

static bool
is_ctype_function(const char *name)
{

	if (name[0] == 'i' && name[1] == 's')
		return strcmp(name, "isalnum") == 0 ||
		       strcmp(name, "isalpha") == 0 ||
		       strcmp(name, "isblank") == 0 ||
		       strcmp(name, "iscntrl") == 0 ||
		       strcmp(name, "isdigit") == 0 ||
		       strcmp(name, "isgraph") == 0 ||
		       strcmp(name, "islower") == 0 ||
		       strcmp(name, "isprint") == 0 ||
		       strcmp(name, "ispunct") == 0 ||
		       strcmp(name, "isspace") == 0 ||
		       strcmp(name, "isupper") == 0 ||
		       strcmp(name, "isxdigit") == 0;

	if (name[0] == 't' && name[1] == 'o')
		return strcmp(name, "tolower") == 0 ||
		       strcmp(name, "toupper") == 0;

	return false;
}

static bool
is_ctype_table(const char *name)
{

	/* NetBSD sys/ctype_bits.h 1.6 from 2016-01-22 */
	if (strcmp(name, "_ctype_tab_") == 0 ||
	    strcmp(name, "_tolower_tab_") == 0 ||
	    strcmp(name, "_toupper_tab_") == 0)
		return true;

	/* NetBSD sys/ctype_bits.h 1.1 from 2010-06-01 */
	return strcmp(name, "_ctype_") == 0;
}

static void
check_ctype_arg(const char *func, const tnode_t *arg)
{
	const tnode_t *on, *cn;

	for (on = arg; on->tn_op == CVT; on = on->tn_left)
		if (on->tn_type->t_tspec == UCHAR)
			return;
	if (on->tn_type->t_tspec == UCHAR)
		return;

	if (arg->tn_op == CVT && arg->tn_cast) {
		/* argument to '%s' must be cast to 'unsigned char', not to '%s' */
		warning(342, func, type_name(arg->tn_type));
		return;
	}

	cn = before_conversion(arg);
	if (cn->tn_type->t_tspec == CHAR) {
		/* argument to '%s' must be 'unsigned char' or EOF, not '%s' */
		warning(341, func, type_name(cn->tn_type));
		return;
	}
}

void
check_ctype_function_call(const tnode_t *func, const tnode_t *args)
{

	if (func->tn_op == NAME &&
	    is_ctype_function(func->tn_sym->s_name) &&
	    args != NULL &&
	    args->tn_left != NULL &&
	    args->tn_right == NULL)
		check_ctype_arg(func->tn_sym->s_name, args->tn_left);
}

void
check_ctype_macro_invocation(const tnode_t *ln, const tnode_t *rn)
{

	if (ln->tn_op == PLUS &&
	    ln->tn_left != NULL &&
	    ln->tn_left->tn_op == LOAD &&
	    ln->tn_left->tn_left != NULL &&
	    ln->tn_left->tn_left->tn_op == NAME &&
	    is_ctype_table(ln->tn_left->tn_left->tn_sym->s_name))
		check_ctype_arg("function from <ctype.h>", rn);
}