File tree 2 files changed +17
-3
lines changed
2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ int main()
29
29
for (int i = 0 ; i <= offset ; i ++ ) {
30
30
lseek (fd , i , SEEK_SET );
31
31
sz = read (fd , buf , 1 );
32
+ if (sz < 0 ) {
33
+ perror ("Failed to read character device" );
34
+ exit (1 );
35
+ }
32
36
printf ("Reading from " FIB_DEV
33
37
" at offset %d, returned the sequence "
34
38
"%lld.\n" ,
@@ -38,6 +42,10 @@ int main()
38
42
for (int i = offset ; i >= 0 ; i -- ) {
39
43
lseek (fd , i , SEEK_SET );
40
44
sz = read (fd , buf , 1 );
45
+ if (sz < 0 ) {
46
+ perror ("Failed to read character device" );
47
+ exit (1 );
48
+ }
41
49
printf ("Reading from " FIB_DEV
42
50
" at offset %d, returned the sequence "
43
51
"%lld.\n" ,
Original file line number Diff line number Diff line change 6
6
#include <linux/kernel.h>
7
7
#include <linux/module.h>
8
8
#include <linux/mutex.h>
9
+ #include <linux/slab.h>
9
10
#include <linux/version.h>
10
11
11
12
MODULE_LICENSE ("Dual MIT/GPL" );
@@ -27,8 +28,9 @@ static int major = 0, minor = 0;
27
28
28
29
static long long fib_sequence (long long k )
29
30
{
30
- /* FIXME: C99 variable-length array (VLA) is not allowed in Linux kernel. */
31
- long long f [k + 2 ];
31
+ long long * f = kmalloc (sizeof (* f ) * (k + 2 ), GFP_KERNEL );
32
+ if (!f )
33
+ return -1 ;
32
34
33
35
f [0 ] = 0 ;
34
36
f [1 ] = 1 ;
@@ -37,7 +39,11 @@ static long long fib_sequence(long long k)
37
39
f [i ] = f [i - 1 ] + f [i - 2 ];
38
40
}
39
41
40
- return f [k ];
42
+ long long ret = f [k ];
43
+
44
+ kfree (f );
45
+
46
+ return ret ;
41
47
}
42
48
43
49
static int fib_open (struct inode * inode , struct file * file )
You can’t perform that action at this time.
0 commit comments