Bug in gri_fft.cc

Hi all,

I found a bug in gnuradio-core/src/lib/general/gri_fft.cc, in function:
static const char * wisdom_filename ()

It returns pointer to char string from temporary object created by call
to path.string(). Obviously, this pointer is pointing to garbage after
exit
from wisdom_filename() function.

I propose something like this:

gnuradio-core/src/lib/general/gri_fft.cc | 10 +++++±—
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/gnuradio-core/src/lib/general/gri_fft.cc
b/gnuradio-core/src/lib/general/gri_fft.cc
index 68e7e69…906279b 100644
— a/gnuradio-core/src/lib/general/gri_fft.cc
+++ b/gnuradio-core/src/lib/general/gri_fft.cc
@@ -73,18 +73,19 @@ gri_fft_planner::mutex()
return s_planning_mutex;
}

-static const char *
+static std::string
wisdom_filename ()
{
static fs::path path;
path = fs::path(gr_appdata_path()) / “.gr_fftw_wisdom”;

  • return path.string().c_str();
  • return path.string();
    }

static void
gri_fftw_import_wisdom ()
{

  • const char *filename = wisdom_filename ();
  • const std::string & s = wisdom_filename ();
  • const char *filename = s.c_str ();
    FILE *fp = fopen (filename, “r”);
    if (fp != 0){
    int r = fftwf_import_wisdom_from_file (fp);
    @@ -114,7 +115,8 @@ gri_fftw_config_threading (int nthreads)
    static void
    gri_fftw_export_wisdom ()
    {
  • const char *filename = wisdom_filename ();
  • const std::string & s = wisdom_filename ();
  • const char *filename = s.c_str ();
    FILE *fp = fopen (filename, “w”);
    if (fp != 0){
    fftwf_export_wisdom_to_file (fp);

On Thu, Jun 27, 2013 at 1:43 AM, Marek C. [email protected]
wrote:

I found a bug in gnuradio-core/src/lib/general/gri_fft.cc, in function:
static const char * wisdom_filename ()

It returns pointer to char string from temporary object created by call
to path.string(). Obviously, this pointer is pointing to garbage after exit
from wisdom_filename() function.

The current usage is correct. The object “path” is declared (in line
79)
with “static local” scope, which means that it persists in between calls
to
the function. In line 80, it is assigned a value, and the return value
from
the function is a const pointer to the internal storage of the object.

A reasonable explanation of “static local” scope is here: