Skip to content

Commit 9289bfe

Browse files
authored
Fix the warnings raised by Sparse (#92)
Sparse[1] is a semantic parser, capable of finding out the potential problems of Linux kernel code. This patch fixed the warnings. [1] https://www.kernel.org/doc/html/latest/dev-tools/sparse.html
1 parent 21ca08a commit 9289bfe

18 files changed

+57
-55
lines changed

examples/bottomhalf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void bottomhalf_tasklet_fn(unsigned long data)
4545
pr_info("Bottom half tasklet ends\n");
4646
}
4747

48-
DECLARE_TASKLET_OLD(buttontask, bottomhalf_tasklet_fn);
48+
static DECLARE_TASKLET_OLD(buttontask, bottomhalf_tasklet_fn);
4949

5050
/* interrupt function triggered when a button is pressed */
5151
static irqreturn_t button_isr(int irq, void *data)

examples/chardev.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
/* Prototypes - this would normally go in a .h file */
1717
static int device_open(struct inode *, struct file *);
1818
static int device_release(struct inode *, struct file *);
19-
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
20-
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
19+
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
20+
static ssize_t device_write(struct file *, const char __user *, size_t,
21+
loff_t *);
2122

2223
#define SUCCESS 0
2324
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
@@ -105,7 +106,7 @@ static int device_release(struct inode *inode, struct file *file)
105106
* read from it.
106107
*/
107108
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
108-
char *buffer, /* buffer to fill with data */
109+
char __user *buffer, /* buffer to fill with data */
109110
size_t length, /* length of the buffer */
110111
loff_t *offset)
111112
{
@@ -134,8 +135,8 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
134135
}
135136

136137
/* Called when a process writes to dev file: echo "hi" > /dev/hello */
137-
static ssize_t device_write(struct file *filp, const char *buff, size_t len,
138-
loff_t *off)
138+
static ssize_t device_write(struct file *filp, const char __user *buff,
139+
size_t len, loff_t *off)
139140
{
140141
pr_alert("Sorry, this operation is not supported.\n");
141142
return -EINVAL;

examples/chardev2.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ static ssize_t device_write(struct file *file, const char __user *buffer,
121121
* If the ioctl is write or read/write (meaning output is returned to the
122122
* calling process), the ioctl call returns the output of this function.
123123
*/
124-
long device_ioctl(struct file *file, /* ditto */
125-
unsigned int ioctl_num, /* number and param for ioctl */
126-
unsigned long ioctl_param)
124+
static long
125+
device_ioctl(struct file *file, /* ditto */
126+
unsigned int ioctl_num, /* number and param for ioctl */
127+
unsigned long ioctl_param)
127128
{
128129
int i;
129130
char *temp;
@@ -139,23 +140,23 @@ long device_ioctl(struct file *file, /* ditto */
139140
temp = (char *)ioctl_param;
140141

141142
/* Find the length of the message */
142-
get_user(ch, temp);
143+
get_user(ch, (char __user *)temp);
143144
for (i = 0; ch && i < BUF_LEN; i++, temp++)
144-
get_user(ch, temp);
145+
get_user(ch, (char __user *)temp);
145146

146-
device_write(file, (char *)ioctl_param, i, 0);
147+
device_write(file, (char __user *)ioctl_param, i, NULL);
147148
break;
148149

149150
case IOCTL_GET_MSG:
150151
/* Give the current message to the calling process - the parameter
151152
* we got is a pointer, fill it.
152153
*/
153-
i = device_read(file, (char *)ioctl_param, 99, 0);
154+
i = device_read(file, (char __user *)ioctl_param, 99, NULL);
154155

155156
/* Put a zero at the end of the buffer, so it will be properly
156157
* terminated.
157158
*/
158-
put_user('\0', (char *)ioctl_param + i);
159+
put_user('\0', (char __user *)ioctl_param + i);
159160
break;
160161

161162
case IOCTL_GET_NTH_BYTE:
@@ -176,7 +177,7 @@ long device_ioctl(struct file *file, /* ditto */
176177
* is kept in the devices table, it can't be local to init_module. NULL is
177178
* for unimplemented functions.
178179
*/
179-
struct file_operations fops = {
180+
static struct file_operations fops = {
180181
.read = device_read,
181182
.write = device_write,
182183
.unlocked_ioctl = device_ioctl,

examples/completions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static int completions_init(void)
6161
return -1;
6262
}
6363

64-
void completions_exit(void)
64+
static void completions_exit(void)
6565
{
6666
wait_for_completion(&machine.crank_comp);
6767
wait_for_completion(&machine.flywheel_comp);

examples/cryptosha256.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void show_hash_result(char *plaintext, char *hash_sha256)
1818
pr_info("%s\n", str);
1919
}
2020

21-
int cryptosha256_init(void)
21+
static int cryptosha256_init(void)
2222
{
2323
char *plaintext = "This is a test";
2424
char hash_sha256[SHA256_LENGTH];
@@ -53,7 +53,7 @@ int cryptosha256_init(void)
5353
return 0;
5454
}
5555

56-
void cryptosha256_exit(void)
56+
static void cryptosha256_exit(void)
5757
{
5858
}
5959

examples/cryptosk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static int test_skcipher_encrypt(char *plaintext, char *password,
171171
return ret;
172172
}
173173

174-
int cryptoapi_init(void)
174+
static int cryptoapi_init(void)
175175
{
176176
/* The world's favorite password */
177177
char *password = "password123";
@@ -186,7 +186,7 @@ int cryptoapi_init(void)
186186
return 0;
187187
}
188188

189-
void cryptoapi_exit(void)
189+
static void cryptoapi_exit(void)
190190
{
191191
test_skcipher_finish(&sk);
192192
}

examples/example_mutex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/module.h>
77
#include <linux/mutex.h>
88

9-
DEFINE_MUTEX(mymutex);
9+
static DEFINE_MUTEX(mymutex);
1010

1111
static int example_mutex_init(void)
1212
{

examples/example_rwlock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <linux/kernel.h>
66
#include <linux/module.h>
77

8-
DEFINE_RWLOCK(myrwlock);
8+
static DEFINE_RWLOCK(myrwlock);
99

1010
static void example_read_lock(void)
1111
{

examples/example_spinlock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <linux/module.h>
88
#include <linux/spinlock.h>
99

10-
DEFINE_SPINLOCK(sl_static);
11-
spinlock_t sl_dynamic;
10+
static DEFINE_SPINLOCK(sl_static);
11+
static spinlock_t sl_dynamic;
1212

1313
static void example_spinlock_static(void)
1414
{

examples/example_tasklet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void tasklet_fn(unsigned long data)
2020
pr_info("Example tasklet ends\n");
2121
}
2222

23-
DECLARE_TASKLET_OLD(mytask, tasklet_fn);
23+
static DECLARE_TASKLET_OLD(mytask, tasklet_fn);
2424

2525
static int example_tasklet_init(void)
2626
{

0 commit comments

Comments
 (0)