Changeset 2589ea9 for src


Ignore:
Timestamp:
Dec 20, 2018, 6:28:29 PM (12 months ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/timestretch, master
Children:
2a94eca
Parents:
9b5aa50
Message:

[source_sndfile] validate input sizes to prevent invalid reads, remove channel copying for now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_sndfile.c

    r9b5aa50 r2589ea9  
    2727#include "fvec.h"
    2828#include "fmat.h"
     29#include "ioutils.h"
    2930#include "source_sndfile.h"
    3031
     
    170171  uint_t i,j, input_channels = s->input_channels;
    171172  /* read from file into scratch_data */
    172   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
     173  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
     174      s->hop_size, read_data->length);
     175  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
     176      s->input_channels * length);
     177
     178  length = MIN(read_samples / s->input_channels, length);
    173179
    174180  /* where to store de-interleaved data */
     
    184190
    185191  /* de-interleaving and down-mixing data  */
    186   for (j = 0; j < read_samples / input_channels; j++) {
     192  for (j = 0; j < length; j++) {
    187193    ptr_data[j] = 0;
    188194    for (i = 0; i < input_channels; i++) {
     
    198204#endif /* HAVE_SAMPLERATE */
    199205
    200   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
    201 
    202   if (*read < s->hop_size) {
    203     for (j = *read; j < s->hop_size; j++) {
     206  *read = (int)FLOOR(s->ratio * length + .5);
     207
     208  if (*read < read_data->length) {
     209    for (j = *read; j < read_data->length; j++) {
    204210      read_data->data[j] = 0;
    205211    }
     
    211217  uint_t i,j, input_channels = s->input_channels;
    212218  /* do actual reading */
    213   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
     219  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
     220      s->hop_size, read_data->length);
     221  uint_t channels = aubio_source_validate_input_channels("source_sndfile",
     222      s->path, s->input_channels, read_data->height);
     223  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
     224      length * s->input_channels);
     225
     226  length = MIN(read_samples / s->input_channels, length);
    214227
    215228  /* where to store de-interleaved data */
     
    224237  }
    225238
    226   if (read_data->height < input_channels) {
    227     // destination matrix has less channels than the file; copy only first
    228     // channels of the file, de-interleaving data
    229     for (j = 0; j < read_samples / input_channels; j++) {
    230       for (i = 0; i < read_data->height; i++) {
    231         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    232       }
    233     }
    234   } else {
    235     // destination matrix has as many or more channels than the file; copy each
    236     // channel from the file to the destination matrix, de-interleaving data
    237     for (j = 0; j < read_samples / input_channels; j++) {
    238       for (i = 0; i < input_channels; i++) {
    239         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    240       }
    241     }
    242   }
    243 
    244   if (read_data->height > input_channels) {
    245     // destination matrix has more channels than the file; copy last channel
    246     // of the file to each additional channels, de-interleaving data
    247     for (j = 0; j < read_samples / input_channels; j++) {
    248       for (i = input_channels; i < read_data->height; i++) {
    249         ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
    250       }
     239  for (j = 0; j < length; j++) {
     240    for (i = 0; i < channels; i++) {
     241      ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    251242    }
    252243  }
     
    265256#endif /* HAVE_SAMPLERATE */
    266257
    267   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
    268 
    269   if (*read < s->hop_size) {
     258  *read = (int)FLOOR(s->ratio * length + .5);
     259
     260  if (*read < read_data->length) {
    270261    for (i = 0; i < read_data->height; i++) {
    271       for (j = *read; j < s->hop_size; j++) {
     262      for (j = *read; j < read_data->length; j++) {
    272263        read_data->data[i][j] = 0.;
    273264      }
Note: See TracChangeset for help on using the changeset viewer.