Skip to content

Commit 828df46

Browse files
authored
Fix segfault by calling ftell instead of tell_off (#1217)
[`sox_format_t.tell_off`](https://fossies.org/dox/sox-14.4.2/structsox__format__t.html#a2016a9fa839f3139e3c2f64381b0c445) should be representing current offset in file, but there are cases it does not. This was causing segmentation fault in some cases. This PR fixes it by replacing it with `ftell` call and add extra check so that if the same thing should happen, it will throw runtime error instead of segmentation fault.
1 parent 17aa81e commit 828df46

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

torchaudio/csrc/sox/effects_chain.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,17 @@ int fileobj_input_drain(sox_effect_t* effp, sox_sample_t* obuf, size_t* osamp) {
335335
// |**********|-----------------|++++++++++++|
336336
// ^ ftell
337337

338-
const auto num_consumed = sf->tell_off;
338+
// NOTE:
339+
// Do not use `sf->tell_off` here. Presumably, `tell_off` and `fseek` are
340+
// supposed to be in sync, but there are cases (Vorbis) they are not
341+
// in sync and `tell_off` has seemingly uninitialized value, which
342+
// leads num_remain to be negative and cause segmentation fault
343+
// in `memmove`.
344+
const auto num_consumed = ftell((FILE*)sf->fp);
345+
if (num_consumed > priv->buffer_size) {
346+
throw std::runtime_error("Internal Error: buffer overrun.");
347+
}
348+
339349
const auto num_remain = priv->buffer_size - num_consumed;
340350

341351
// 1.1. Fetch the data to see if there is data to fill the buffer

0 commit comments

Comments
 (0)