Ignore:
Timestamp:
Dec 20, 2018, 5:29:37 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
Children:
a2b7187
Parents:
85e20fa
Message:

[sink_wavwrite] check fwrite return value, use AUBIO_STRERROR

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_wavwrite.c

    r85e20fa r14a5b9a  
    163163  unsigned char buf[5];
    164164  uint_t byterate, blockalign;
     165  size_t written = 0;
    165166
    166167  /* open output file */
    167168  s->fid = fopen((const char *)s->path, "wb");
    168169  if (!s->fid) {
    169     AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
     170    char errorstr[256];
     171    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     172    AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
    170173    goto beach;
    171174  }
    172175
    173176  // ChunkID
    174   fwrite("RIFF", 4, 1, s->fid);
     177  written += fwrite("RIFF", 4, 1, s->fid);
    175178
    176179  // ChunkSize (0 for now, actual size will be written in _close)
    177   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     180  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    178181
    179182  // Format
    180   fwrite("WAVE", 4, 1, s->fid);
     183  written += fwrite("WAVE", 4, 1, s->fid);
    181184
    182185  // Subchunk1ID
    183   fwrite("fmt ", 4, 1, s->fid);
     186  written += fwrite("fmt ", 4, 1, s->fid);
    184187
    185188  // Subchunk1Size
    186   fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
     189  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
    187190
    188191  // AudioFormat
    189   fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
     192  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
    190193
    191194  // NumChannels
    192   fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
     195  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
    193196
    194197  // SampleRate
    195   fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
     198  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
    196199
    197200  // ByteRate
    198201  byterate = s->samplerate * s->channels * s->bitspersample / 8;
    199   fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
     202  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
    200203
    201204  // BlockAlign
    202205  blockalign = s->channels * s->bitspersample / 8;
    203   fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
     206  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
    204207
    205208  // BitsPerSample
    206   fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
     209  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
    207210
    208211  // Subchunk2ID
    209   fwrite("data", 4, 1, s->fid);
     212  written += fwrite("data", 4, 1, s->fid);
    210213
    211214  // Subchunk1Size (0 for now, actual size will be written in _close)
    212   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     215  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     216
     217  // fwrite(*, *, 1, s->fid) was called 13 times, check success
     218  if (written != 13) {
     219    char errorstr[256];
     220    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     221    AUBIO_WRN("sink_wavwrite: writing header to %s failed, expected %d"
     222        " write but got only %d (%s)\n", s->path, 13, written, errorstr);
     223    return AUBIO_FAIL;
     224  }
    213225
    214226  s->scratch_size = s->max_size * s->channels;
Note: See TracChangeset for help on using the changeset viewer.