Skip to content

Commit

Permalink
Merge pull request #14 from ericlinsechs/master
Browse files Browse the repository at this point in the history
Avoid using variable-length array
  • Loading branch information
jserv authored Jun 12, 2024
2 parents 5ce76d4 + 36335ee commit d09711e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions fibdrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/version.h>

MODULE_LICENSE("Dual MIT/GPL");
Expand All @@ -25,10 +26,18 @@ static struct class *fib_class;
static DEFINE_MUTEX(fib_mutex);
static int major = 0, minor = 0;

/**
* fib_sequence() - Calculate the k-th Fibonacci number
* @k: Index of the Fibonacci number to calculate
*
* Return: The k-th Fibonacci number on success, -ENOMEM on memory allocation
* failure.
*/
static long long fib_sequence(long long k)
{
/* FIXME: C99 variable-length array (VLA) is not allowed in Linux kernel. */
long long f[k + 2];
long long *f = kmalloc(sizeof(*f) * (k + 2), GFP_KERNEL);
if (!f)
return -ENOMEM;

f[0] = 0;
f[1] = 1;
Expand All @@ -37,7 +46,11 @@ static long long fib_sequence(long long k)
f[i] = f[i - 1] + f[i - 2];
}

return f[k];
long long ret = f[k];

kfree(f);

return ret;
}

static int fib_open(struct inode *inode, struct file *file)
Expand Down

0 comments on commit d09711e

Please sign in to comment.