An issue was discovered in Bento4 1.5.1.0. There is a use-after-free in AP4_Sample::GetOffset in Core/Ap4Sample.h when called from Ap4LinearReader.cpp.
cpe:2.3:a:axiosys:bento4:1.5.1.0:*:*:*:*:*:*:*
83B32974-D913-4DDB-844F-C58D55ECC17E
OR
./mp42ts $poc out
poc
[test-002.zip](https://github.com/axiomatic-systems/Bento4/files/3938643/test-002.zip)
asan output
```
root@ubuntu:/home/tim/Bento4/cmakebuild# ../../Bento4-asan/cmakebuild/mp42ts overflows/test-002.mp4-double_free-idx\:0xffffffff-0x0 out
=================================================================
==2623==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000005458 at pc 0x55e3af247add bp 0x7ffd43666410 sp 0x7ffd43666400
READ of size 8 at 0x604000005458 thread T0
    #0 0x55e3af247adc in AP4_Sample::GetOffset() const /home/tim/Bento4-asan/Source/C++/Core/Ap4Sample.h:99
    #1 0x55e3af246319 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:436
    #2 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #3 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #4 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #5 0x55e3af233787 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:306
    #6 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #7 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)
    #8 0x55e3af232459 in _start (/home/tim/Bento4-asan/cmakebuild/mp42ts+0x325459)

0x604000005458 is located 8 bytes inside of 48-byte region [0x604000005450,0x604000005480)
freed by thread T0 here:
    #0 0x7fd67fdea845 in operator delete(void*, unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10f845)
    #1 0x55e3af247c40 in AP4_LinearReader::SampleBuffer::~SampleBuffer() /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.h:104
    #2 0x55e3af2466f5 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:464
    #3 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #4 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #5 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #6 0x55e3af233927 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:317
    #7 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #8 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)

previously allocated by thread T0 here:
    #0 0x7fd67fde917f in operator new(unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10e17f)
    #1 0x55e3af245fa2 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:424
    #2 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #3 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #4 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #5 0x55e3af233927 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:317
    #6 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #7 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)

SUMMARY: AddressSanitizer: heap-use-after-free /home/tim/Bento4-asan/Source/C++/Core/Ap4Sample.h:99 in AP4_Sample::GetOffset() const
Shadow bytes around the buggy address:
  0x0c087fff8a30: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a40: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a50: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a60: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a70: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
=>0x0c087fff8a80: fa fa fd fd fd fd fd fa fa fa fd[fd]fd fd fd fd
  0x0c087fff8a90: fa fa fd fd fd fd fd fa fa fa fa fa fa fa fa fa
  0x0c087fff8aa0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ab0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ac0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ad0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==2623==ABORTING
root@ubuntu:/home/tim/Bento4/cmakebuild# 

```
use after free in Ap4Sample.h
This is a GitHub Issue
repo:Bento4
owner:axiomatic-systems
Title : use after free in Ap4Sample.h
Issue date:

--- start body ---
./mp42ts $poc out
poc
[test-002.zip](https://github.com/axiomatic-systems/Bento4/files/3938643/test-002.zip)
asan output
```
root@ubuntu:/home/tim/Bento4/cmakebuild# ../../Bento4-asan/cmakebuild/mp42ts overflows/test-002.mp4-double_free-idx\:0xffffffff-0x0 out
=================================================================
==2623==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000005458 at pc 0x55e3af247add bp 0x7ffd43666410 sp 0x7ffd43666400
READ of size 8 at 0x604000005458 thread T0
    #0 0x55e3af247adc in AP4_Sample::GetOffset() const /home/tim/Bento4-asan/Source/C++/Core/Ap4Sample.h:99
    #1 0x55e3af246319 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:436
    #2 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #3 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #4 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #5 0x55e3af233787 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:306
    #6 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #7 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)
    #8 0x55e3af232459 in _start (/home/tim/Bento4-asan/cmakebuild/mp42ts+0x325459)

0x604000005458 is located 8 bytes inside of 48-byte region [0x604000005450,0x604000005480)
freed by thread T0 here:
    #0 0x7fd67fdea845 in operator delete(void*, unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10f845)
    #1 0x55e3af247c40 in AP4_LinearReader::SampleBuffer::~SampleBuffer() /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.h:104
    #2 0x55e3af2466f5 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:464
    #3 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #4 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #5 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #6 0x55e3af233927 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:317
    #7 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #8 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)

previously allocated by thread T0 here:
    #0 0x7fd67fde917f in operator new(unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10e17f)
    #1 0x55e3af245fa2 in AP4_LinearReader::Advance(bool) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:424
    #2 0x55e3af246fa0 in AP4_LinearReader::ReadNextSample(unsigned int, AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Core/Ap4LinearReader.cpp:532
    #3 0x55e3af232717 in FragmentedSampleReader::ReadSample(AP4_Sample&, AP4_DataBuffer&) /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:149
    #4 0x55e3af232aa2 in ReadSample /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:181
    #5 0x55e3af233927 in WriteSamples /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:317
    #6 0x55e3af235970 in main /home/tim/Bento4-asan/Source/C++/Apps/Mp42Ts/Mp42Ts.cpp:636
    #7 0x7fd67f91ab6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a)

SUMMARY: AddressSanitizer: heap-use-after-free /home/tim/Bento4-asan/Source/C++/Core/Ap4Sample.h:99 in AP4_Sample::GetOffset() const
Shadow bytes around the buggy address:
  0x0c087fff8a30: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a40: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a50: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a60: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
  0x0c087fff8a70: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
=>0x0c087fff8a80: fa fa fd fd fd fd fd fa fa fa fd[fd]fd fd fd fd
  0x0c087fff8a90: fa fa fd fd fd fd fd fa fa fa fa fa fa fa fa fa
  0x0c087fff8aa0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ab0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ac0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8ad0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==2623==ABORTING
root@ubuntu:/home/tim/Bento4/cmakebuild# 

```
--- end body ---

comments :
--- start comments ---

--- end comments ---