Commit 1bd200bc authored by Timothy B. Terriberry's avatar Timothy B. Terriberry
Browse files

Fix to avoid technically undefined behavior.

The C standard says that calling library functions (including
 memcpy) with invalid arguments (including a NULL pointer) is
 undefined behavior unless otherwise noted (which memcpy doesn't).
op_filter_read_native() invokes op_read_native() with NULL for the
 _pcm buffer, which triggers such a memcpy invocation.
Even though it should be perfectly fine in practice to pass NULL to
 memcpy when copying zero bytes, don't do it.

Thanks to a person who did not wish to be credited for the report.
parent 24cb5eae
......@@ -2818,10 +2818,16 @@ static int op_read_native(OggOpusFile *_of,
/*If we have buffered samples, return them.*/
if(nsamples>0){
if(nsamples*nchannels>_buf_size)nsamples=_buf_size/nchannels;
memcpy(_pcm,_of->od_buffer+nchannels*od_buffer_pos,
sizeof(*_pcm)*nchannels*nsamples);
od_buffer_pos+=nsamples;
_of->od_buffer_pos=od_buffer_pos;
/*Check nsamples again so we don't pass NULL to memcpy() if _buf_size
is zero.
That would technically be undefined behavior, even if the number of
bytes to copy were zero.*/
if(nsamples>0){
memcpy(_pcm,_of->od_buffer+nchannels*od_buffer_pos,
sizeof(*_pcm)*nchannels*nsamples);
od_buffer_pos+=nsamples;
_of->od_buffer_pos=od_buffer_pos;
}
if(_li!=NULL)*_li=_of->cur_link;
return nsamples;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment