Changeset 68b991e


Ignore:
Timestamp:
Dec 17, 2018, 4:41:01 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/crepe, fix/ffmpeg5, master
Children:
41d14b8
Parents:
0440778
Message:

[io] sink_flac: validate input sizes, avoid crash on null path and closing twice

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_flac.c

    r0440778 r68b991e  
    3535#include <FLAC/stream_encoder.h>
    3636
    37 #include <vorbis/vorbisenc.h>
    3837#include <string.h> // strerror
    3938#include <errno.h> // errno
     
    7978
    8079#if 0
    81 static void aubio_sink_vorbis_callback(const FLAC__StreamEncoder* encoder,
     80static void aubio_sink_flac_callback(const FLAC__StreamEncoder* encoder,
    8281    FLAC__uint64 bytes_written, FLAC__uint64 samples_written,
    8382    unsigned frames_writtten, unsigned total_frames_estimate,
     
    8988{
    9089  aubio_sink_flac_t * s = AUBIO_NEW(aubio_sink_flac_t);
     90
     91  if (!uri) {
     92    AUBIO_ERROR("sink_flac: Aborted opening null path\n");
     93    goto failure;
     94  }
    9195
    9296  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
     
    196200  init_status = FLAC__stream_encoder_init_file(s->encoder, s->path,
    197201      NULL, NULL);
    198       //aubio_sink_vorbis_callback, s);
     202      //aubio_sink_flac_callback, s);
    199203  if (init_status == FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE) {
    200204    AUBIO_ERR("sink_flac: failed initilizing encoder for %s"
     
    259263{
    260264  uint_t c, v;
     265  uint_t length = aubio_sink_validate_input_length("sink_flac", s->path,
     266      MAX_WRITE_SIZE, write_data->length, write);
    261267  // fill buffer
    262268  if (!write) {
    263269    return;
    264   } else if (write > MAX_WRITE_SIZE) {
    265     AUBIO_ERR("sink_flac: max_write request %dm asked for %d,"
    266        " failed writing to %s\n", write, MAX_WRITE_SIZE, s->path);
    267     return;
    268270  } else {
    269271    for (c = 0; c < s->channels; c++) {
    270       for (v = 0; v < write; v++) {
     272      for (v = 0; v < length; v++) {
    271273        s->buffer[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
    272274      }
     
    275277  // send to encoder
    276278  FLAC__stream_encoder_process_interleaved(s->encoder,
    277       (const FLAC__int32*)s->buffer, write);
     279      (const FLAC__int32*)s->buffer, length);
    278280}
    279281
     
    282284{
    283285  uint_t c, v;
    284   uint_t channels = MIN(s->channels, write_data->height);
     286  uint_t channels = aubio_sink_validate_input_channels("sink_flac", s->path,
     287      s->channels, write_data->height);
     288  uint_t length = aubio_sink_validate_input_length("sink_flac", s->path,
     289      MAX_WRITE_SIZE, write_data->length, write);
    285290  // fill buffer
    286291  if (!write) {
    287292    return;
    288   } else if (write > MAX_WRITE_SIZE) {
    289     AUBIO_ERR("sink_flac: max_write request %dm asked for %d,"
    290        " failed writing to %s\n", write, MAX_WRITE_SIZE, s->path);
    291     return;
    292293  } else {
    293294    for (c = 0; c < channels; c++) {
    294       for (v = 0; v < write; v++) {
     295      for (v = 0; v < length; v++) {
    295296        s->buffer[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
    296297      }
     
    298299    // send to encoder
    299300    FLAC__stream_encoder_process_interleaved(s->encoder,
    300         (const FLAC__int32*)s->buffer, write);
     301        (const FLAC__int32*)s->buffer, length);
    301302  }
    302303}
     
    305306{
    306307  uint_t ret = AUBIO_OK;
     308
     309  if (!s->fid) return AUBIO_FAIL;
    307310
    308311  if (s->encoder) {
     
    325328  }
    326329
    327   if (s->fid) {
    328     if (fclose(s->fid)) {
    329       AUBIO_ERR("sink_flac: Error closing file %s (%s)\n",
    330           s->path, strerror(errno));
    331       ret &= AUBIO_FAIL;
    332     }
    333   }
     330  if (s->fid && fclose(s->fid)) {
     331    AUBIO_ERR("sink_flac: Error closing file %s (%s)\n",
     332        s->path, strerror(errno));
     333    ret &= AUBIO_FAIL;
     334  }
     335  s->fid = NULL;
     336
    334337  return ret;
    335338}
    336339
    337340#if 0
    338 static void aubio_sink_vorbis_callback(const FLAC__StreamEncoder* encoder UNUSED,
     341static void aubio_sink_flac_callback(const FLAC__StreamEncoder* encoder UNUSED,
    339342    FLAC__uint64 bytes_written, FLAC__uint64 samples_written,
    340343    unsigned frames_written, unsigned total_frames_estimate,
Note: See TracChangeset for help on using the changeset viewer.