11
11
#include <linux/slab.h>
12
12
#include <linux/string.h>
13
13
14
+ /*
15
+ * max size needed by different bases to express U64
16
+ * HEX: "0xFFFFFFFFFFFFFFFF" --> 18
17
+ * DEC: "18446744073709551615" --> 20
18
+ * OCT: "01777777777777777777777" --> 23
19
+ * pick the max one to define NUMBER_BUF_LEN
20
+ */
21
+ #define NUMBER_BUF_LEN 24
22
+
14
23
/**
15
24
* match_one - Determines if a string matches a simple pattern
16
25
* @s: the string to examine for presence of the pattern
@@ -129,14 +138,12 @@ EXPORT_SYMBOL(match_token);
129
138
static int match_number (substring_t * s , int * result , int base )
130
139
{
131
140
char * endp ;
132
- char * buf ;
141
+ char buf [ NUMBER_BUF_LEN ] ;
133
142
int ret ;
134
143
long val ;
135
144
136
- buf = match_strdup (s );
137
- if (!buf )
138
- return - ENOMEM ;
139
-
145
+ if (match_strlcpy (buf , s , NUMBER_BUF_LEN ) >= NUMBER_BUF_LEN )
146
+ return - ERANGE ;
140
147
ret = 0 ;
141
148
val = simple_strtol (buf , & endp , base );
142
149
if (endp == buf )
@@ -145,7 +152,6 @@ static int match_number(substring_t *s, int *result, int base)
145
152
ret = - ERANGE ;
146
153
else
147
154
* result = (int ) val ;
148
- kfree (buf );
149
155
return ret ;
150
156
}
151
157
@@ -163,18 +169,15 @@ static int match_number(substring_t *s, int *result, int base)
163
169
*/
164
170
static int match_u64int (substring_t * s , u64 * result , int base )
165
171
{
166
- char * buf ;
172
+ char buf [ NUMBER_BUF_LEN ] ;
167
173
int ret ;
168
174
u64 val ;
169
175
170
- buf = match_strdup (s );
171
- if (!buf )
172
- return - ENOMEM ;
173
-
176
+ if (match_strlcpy (buf , s , NUMBER_BUF_LEN ) >= NUMBER_BUF_LEN )
177
+ return - ERANGE ;
174
178
ret = kstrtoull (buf , base , & val );
175
179
if (!ret )
176
180
* result = val ;
177
- kfree (buf );
178
181
return ret ;
179
182
}
180
183
@@ -206,14 +209,12 @@ EXPORT_SYMBOL(match_int);
206
209
*/
207
210
int match_uint (substring_t * s , unsigned int * result )
208
211
{
209
- int err = - ENOMEM ;
210
- char * buf = match_strdup (s );
212
+ char buf [NUMBER_BUF_LEN ];
211
213
212
- if (buf ) {
213
- err = kstrtouint (buf , 10 , result );
214
- kfree (buf );
215
- }
216
- return err ;
214
+ if (match_strlcpy (buf , s , NUMBER_BUF_LEN ) >= NUMBER_BUF_LEN )
215
+ return - ERANGE ;
216
+
217
+ return kstrtouint (buf , 10 , result );
217
218
}
218
219
EXPORT_SYMBOL (match_uint );
219
220
0 commit comments