bug-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gsl] found a bug in ode-initval/rk4.c


From: Martin Ettl
Subject: [Bug-gsl] found a bug in ode-initval/rk4.c
Date: Thu, 06 Aug 2009 23:35:09 +0200

Hello,

i have checked the sources of gsl-1.9 with the static code analysis tool 
cppcheck. It found an issue in file /ode-initval/rk4.c at line 72.

Take a look at the source:

static void *
rk4_alloc (size_t dim)
{
  rk4_state_t *state = (rk4_state_t *) malloc (sizeof (rk4_state_t));

....

  state->k = (double *) malloc (dim * sizeof (double));

.....

  state->k1 = (double *) malloc (dim * sizeof (double));

  if (state->k1 == 0)
    {
72    free (state);
      free (state->k);
      GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
    }

As you can see, the memory of state is freed BEFORE state->k. This can lead to 
an runntime error.

A possible way out is reordering the free statements:


static void *
rk4_alloc (size_t dim)
{
  rk4_state_t *state = (rk4_state_t *) malloc (sizeof (rk4_state_t));

....

  state->k = (double *) malloc (dim * sizeof (double));

.....

  state->k1 = (double *) malloc (dim * sizeof (double));

  if (state->k1 == 0)
    {
72    free (state->k);
      free (state);
      GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
    }

....


Best regards

Ettl Martin
-- 
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02




reply via email to

[Prev in Thread] Current Thread [Next in Thread]