# HG changeset patch # User John W. Eaton # Date 1420830679 18000 # Node ID 701b43ce446730d4798e10ff85e1e04e6825df6d # Parent 0b068273340f1a099b87eaa0917a86adf7c8a7de properly close audio files * audioread.cc (Faudioread, Faudiowrite, Faudioinfo): Check that file was opened. Use unwind_protect to close file. diff -r 0b068273340f -r 701b43ce4467 libinterp/dldfcn/audioread.cc --- a/libinterp/dldfcn/audioread.cc Fri Jan 09 13:50:25 2015 -0500 +++ b/libinterp/dldfcn/audioread.cc Fri Jan 09 14:11:19 2015 -0500 @@ -28,6 +28,7 @@ #include #include "oct-locbuf.h" +#include "unwind-prot.h" #include "defun-dld.h" #include "error.h" @@ -40,6 +41,14 @@ #include #endif +#ifdef HAVE_SNDFILE +static void +safe_close (SNDFILE *file) +{ + sf_close (file); +} +#endif + DEFUN_DLD (audioread, args, , "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ @@ -83,6 +92,16 @@ info.format = 0; SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); + if (! file) + { + error ("audioread: failed to open input file %s", filename.c_str ()); + return retval; + } + + unwind_protect frame; + + frame.add_fcn (safe_close, file); + OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); sf_read_float (file, data, info.frames * info.channels); @@ -363,16 +382,18 @@ } } - const char *out = filename.c_str (); - - SNDFILE *file = sf_open (out, SFM_WRITE, &info); + SNDFILE *file = sf_open (filename.c_str (), SFM_WRITE, &info); if (! file) { - error ("audiowrite: failed to open output file %s", out); + error ("audiowrite: failed to open output file %s", filename.c_str ()); return retval; } + unwind_protect frame; + + frame.add_fcn (safe_close, file); + if (title != "") sf_set_string (file, SF_STR_TITLE, title.c_str ()); @@ -440,6 +461,16 @@ info.format = 0; SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); + if (! file) + { + error ("audioinfo: failed to open file %s", filename.c_str ()); + return retval; + } + + unwind_protect frame; + + frame.add_fcn (safe_close, file); + octave_scalar_map result; result.assign ("Filename", filename);