shithub: aubio

Download patch

ref: f5a97edfd0b985796c8c34f047abe02b70708f59
parent: a2b71879d6434af56419a2b3a2ca90539ac52cd1
author: Paul Brossier <piem@piem.org>
date: Thu Dec 20 12:31:05 EST 2018

[sink_wavwrite] check fseek and fwrite return values

--- a/src/io/sink_wavwrite.c
+++ b/src/io/sink_wavwrite.c
@@ -288,16 +288,25 @@
 uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
   uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
   unsigned char buf[5];
+  size_t written = 0, err = 0;
   if (!s->fid) return AUBIO_FAIL;
   // ChunkSize
-  fseek(s->fid, 4, SEEK_SET);
-  fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
+  err += fseek(s->fid, 4, SEEK_SET);
+  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
   // Subchunk2Size
-  fseek(s->fid, 40, SEEK_SET);
-  fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
+  err += fseek(s->fid, 40, SEEK_SET);
+  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
+  if (written != 2 || err != 0) {
+    char errorstr[256];
+    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
+    AUBIO_WRN("sink_wavwrite: updating header of %s failed, expected %d"
+        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
+  }
   // close file
   if (fclose(s->fid)) {
-    AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
+    char errorstr[256];
+    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
+    AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
   }
   s->fid = NULL;
   return AUBIO_OK;