]> git.sesse.net Git - ffmpeg/commit
avformat/au: Avoid allocation for metadata string
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Mon, 13 Jul 2020 17:31:46 +0000 (19:31 +0200)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Sat, 18 Jul 2020 16:18:41 +0000 (18:18 +0200)
commit1998d1d6af98f31e9ddeead4893efad8144357be
treee18d1ddbc26c5366accedd8c73bb6a04b2d3a4a6
parentc2e17e8d8446abbd5dcfebab1e63024837677ca7
avformat/au: Avoid allocation for metadata string

When there are potentially annotation (i.e. metadata) fields to write,
au_get_annotations() is called to produce a string with them. To do so,
it uses an AVBPrint which is finalized to create the string. This is
wasteful, because it always leads to an allocation even if the string
actually fits into the internal buffer of the AVBPrint. This commit
changes this by making au_get_annotations() modify an AVBPrint that
resides on the stack of the caller (i.e. of au_write_header()).

Furthermore, the AVBPrint is now checked for truncation; limiting
the allocations implicit in the AVBPrint allowed to offload the overflow
checks. Notice that these were not correct before: The size parameter of
avio_write() is an int, yet the string in the AVBPrint was allowed to
grow bigger than INT_MAX. And if the length of the string was so near
UINT_MAX that the length + 32 overflowed, the old code would write the
first eight bytes of the string and nothing more, leading to an invalid
file.

Finally, the special case in which the metadata dictionary of the
AVFormatContext is empty (in which case one still has to write eight
binary zeroes) is now no longer treated specially, because this case
no longer incurs any allocation.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
libavformat/au.c