diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 4d02488b19787b2caf2c0a9fda76bcd0eef18b68..eadf65302873107f47cfcbbec263968c2a71ee6a 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -639,16 +639,19 @@ static int ebml_read_float(AVIOContext *pb, int size, double *num) */ static int ebml_read_ascii(AVIOContext *pb, int size, char **str) { - av_free(*str); + char *res; + /* EBML strings are usually not 0-terminated, so we allocate one * byte more, read the string and NULL-terminate it ourselves. */ - if (!(*str = av_malloc(size + 1))) + if (!(res = av_malloc(size + 1))) return AVERROR(ENOMEM); - if (avio_read(pb, (uint8_t *) *str, size) != size) { - av_freep(str); + if (avio_read(pb, (uint8_t *) res, size) != size) { + av_free(res); return AVERROR(EIO); } - (*str)[size] = '\0'; + (res)[size] = '\0'; + av_free(*str); + *str = res; return 0; }