Skip to content

Commit bea4d2d

Browse files
Shota AokiTiryoh
andauthored
Use kstrtoint_from_user() instead of parseFreq() and parse_count() to detect parse error (#71)
* Use kstrtoint_from_user to parse pulse frequencies and counts * Bump up version to 3.1.1 Co-authored-by: Daisuke Sato <daisuke.sato@rt-net.jp>
1 parent ea99d87 commit bea4d2d

File tree

1 file changed

+35
-122
lines changed

1 file changed

+35
-122
lines changed

src/drivers/rtmouse.c

Lines changed: 35 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* rtmouse.c
44
* Raspberry Pi Mouse device driver
55
*
6-
* Version: 3.1.0
6+
* Version: 3.1.1
77
*
88
* Copyright (C) 2015-2021 RT Corporation <shop@rt-net.jp>
99
*
@@ -55,7 +55,7 @@
5555

5656
MODULE_AUTHOR("RT Corporation");
5757
MODULE_LICENSE("GPL");
58-
MODULE_VERSION("3.0.0");
58+
MODULE_VERSION("3.1.1");
5959
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
6060

6161
/* --- Device Numbers --- */
@@ -848,59 +848,6 @@ static int i2c_dev_release(struct inode *inode, struct file *filep)
848848
return 0;
849849
}
850850

851-
/* Parse Frequency */
852-
static int parseFreq(const char __user *buf, size_t count, int *ret)
853-
{
854-
char cval;
855-
int error = 0, i = 0, tmp, bufcnt = 0, freq;
856-
size_t readcount = count;
857-
int sgn = 1;
858-
859-
char *newbuf = kmalloc(sizeof(char) * count, GFP_KERNEL);
860-
861-
while (readcount > 0) {
862-
if (copy_from_user(&cval, buf + i, sizeof(char))) {
863-
kfree(newbuf);
864-
return -EFAULT;
865-
}
866-
867-
if (cval == '-') {
868-
if (bufcnt == 0) {
869-
sgn = -1;
870-
}
871-
} else if (cval < '0' || cval > '9') {
872-
newbuf[bufcnt] = 'e';
873-
error = 1;
874-
} else {
875-
newbuf[bufcnt] = cval;
876-
}
877-
878-
i++;
879-
bufcnt++;
880-
readcount--;
881-
882-
if (cval == '\n') {
883-
break;
884-
}
885-
}
886-
887-
freq = 0;
888-
for (i = 0, tmp = 1; i < bufcnt; i++) {
889-
char c = newbuf[bufcnt - i - 1];
890-
891-
if (c >= '0' && c <= '9') {
892-
freq += (newbuf[bufcnt - i - 1] - '0') * tmp;
893-
tmp *= 10;
894-
}
895-
}
896-
897-
*ret = sgn * freq;
898-
899-
kfree(newbuf);
900-
901-
return bufcnt;
902-
}
903-
904851
/* Parse motor command */
905852
static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
906853
{
@@ -931,58 +878,6 @@ static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
931878
return count;
932879
}
933880

934-
/* Parse I2C pulse counter value */
935-
static int parse_count(const char __user *buf, size_t count, int *ret)
936-
{
937-
char cval;
938-
int error = 0, i = 0, tmp, bufcnt = 0, freq;
939-
size_t readcount = count;
940-
int sgn = 1;
941-
942-
char *newbuf = kmalloc(sizeof(char) * count, GFP_KERNEL);
943-
944-
while (readcount > 0) {
945-
if (copy_from_user(&cval, buf + i, sizeof(char))) {
946-
kfree(newbuf);
947-
return -EFAULT;
948-
}
949-
950-
if (cval == '-') {
951-
if (bufcnt == 0) {
952-
sgn = -1;
953-
}
954-
} else if (cval < '0' || cval > '9') {
955-
newbuf[bufcnt] = 'e';
956-
error = 1;
957-
} else {
958-
newbuf[bufcnt] = cval;
959-
}
960-
961-
i++;
962-
bufcnt++;
963-
readcount--;
964-
965-
if (cval == '\n') {
966-
break;
967-
}
968-
}
969-
970-
freq = 0;
971-
for (i = 0, tmp = 1; i < bufcnt; i++) {
972-
char c = newbuf[bufcnt - i - 1];
973-
974-
if (c >= '0' && c <= '9') {
975-
freq += (newbuf[bufcnt - i - 1] - '0') * tmp;
976-
tmp *= 10;
977-
}
978-
}
979-
980-
*ret = sgn * freq;
981-
982-
kfree(newbuf);
983-
984-
return bufcnt;
985-
}
986881

987882
/*
988883
* led_write - Trun ON/OFF LEDs
@@ -1019,10 +914,15 @@ static ssize_t led_write(struct file *filep, const char __user *buf,
1019914
static ssize_t buzzer_write(struct file *filep, const char __user *buf,
1020915
size_t count, loff_t *f_pos)
1021916
{
1022-
int bufcnt;
917+
int ret;
1023918
int freq, dat;
1024919

1025-
bufcnt = parseFreq(buf, count, &freq);
920+
ret = kstrtoint_from_user(buf, count, 10, &freq);
921+
if (ret) {
922+
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
923+
DRIVER_NAME, __func__);
924+
return ret;
925+
}
1026926

1027927
if (freq != 0) {
1028928
if (freq < 1) {
@@ -1043,7 +943,7 @@ static ssize_t buzzer_write(struct file *filep, const char __user *buf,
1043943
RPI_GPF_OUTPUT); // io is pwm out
1044944
}
1045945

1046-
return bufcnt;
946+
return count;
1047947
}
1048948

1049949
/*
@@ -1053,12 +953,17 @@ static ssize_t buzzer_write(struct file *filep, const char __user *buf,
1053953
static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
1054954
size_t count, loff_t *f_pos)
1055955
{
1056-
int freq, bufcnt;
1057-
bufcnt = parseFreq(buf, count, &freq);
956+
int freq, ret;
1058957

958+
ret = kstrtoint_from_user(buf, count, 10, &freq);
959+
if (ret) {
960+
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
961+
DRIVER_NAME, __func__);
962+
return ret;
963+
}
1059964
set_motor_l_freq(freq);
1060965

1061-
return bufcnt;
966+
return count;
1062967
}
1063968

1064969
/*
@@ -1068,12 +973,18 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
1068973
static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
1069974
size_t count, loff_t *f_pos)
1070975
{
1071-
int freq, bufcnt;
1072-
bufcnt = parseFreq(buf, count, &freq);
976+
int freq, ret;
977+
978+
ret = kstrtoint_from_user(buf, count, 10, &freq);
979+
if (ret) {
980+
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
981+
DRIVER_NAME, __func__);
982+
return ret;
983+
}
1073984

1074985
set_motor_r_freq(freq);
1075986

1076-
return bufcnt;
987+
return count;
1077988
}
1078989

1079990
/*
@@ -1292,13 +1203,15 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
12921203
{
12931204
struct rtcnt_device_info *dev_info = filep->private_data;
12941205

1295-
int bufcnt = 0;
12961206
int rtcnt_count = 0;
1207+
int ret;
12971208

1298-
if (count < 0)
1299-
return 0;
1300-
1301-
bufcnt = parse_count(buf, count, &rtcnt_count);
1209+
ret = kstrtoint_from_user(buf, count, 10, &rtcnt_count);
1210+
if (ret) {
1211+
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
1212+
DRIVER_NAME, __func__);
1213+
return ret;
1214+
}
13021215

13031216
i2c_counter_set(dev_info, rtcnt_count);
13041217

@@ -1308,7 +1221,7 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
13081221

13091222
printk(KERN_INFO "%s: set pulse counter value %d\n", DRIVER_NAME,
13101223
rtcnt_count);
1311-
return bufcnt;
1224+
return count;
13121225
}
13131226

13141227
/* --- Device File Operations --- */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy