## Affected version 

the latest commit: https://github.com/LibreDWG/libredwg/tree/45d2a290c65ed691be0901ba2b2ef51044e07a16 and 0.12.4

## What's the problem?


A heap use after free was discovered in bit_copy_chain in src/bits.c:3318.

ASAN report:

```
$ ./dwgrewrite ./pocs/tests_64233
......
......
......
==3021191==ERROR: AddressSanitizer: heap-use-after-free on address 0x7ffff2b59800 at pc 0x0000004970c7 bp 0x7fffffffba70 sp 0x7fffffffb238
READ of size 282028 at 0x7ffff2b59800 thread T0
    #0 0x4970c6 in __asan_memcpy /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #1 0x4e732c in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:10
    #2 0x4e732c in bit_copy_chain /src/libredwg/src/bits.c:3318:7
    #3 0x102955f in obj_flush_hdlstream /src/libredwg/src/encode.c:784:3
    #4 0x159ce6f in dwg_encode_PLANESURFACE_private /src/libredwg/src/./dwg.spec:8832:3
    #5 0x12e1e11 in dwg_encode_PLANESURFACE /src/libredwg/src/./dwg.spec:8818:1
    #6 0x1005fae in dwg_encode_variable_type /src/libredwg/src/./classes.inc:243:3
    #7 0xfe71d6 in dwg_encode_add_object /src/libredwg/src/encode.c:3771:28
    #8 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #9 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #10 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #11 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
    #12 0x41c3fd in _start (/mnt/disk/out/libredwg/programs_asan/dwgrewrite+0x41c3fd)

0x7ffff2b59800 is located 0 bytes inside of 282624-byte region [0x7ffff2b59800,0x7ffff2b9e800)
freed by thread T0 here:
    #0 0x498003 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d8122 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0x4e7260 in bit_copy_chain /src/libredwg/src/bits.c:3314:5
    #3 0x102955f in obj_flush_hdlstream /src/libredwg/src/encode.c:784:3
    #4 0x159ce6f in dwg_encode_PLANESURFACE_private /src/libredwg/src/./dwg.spec:8832:3
    #5 0x12e1e11 in dwg_encode_PLANESURFACE /src/libredwg/src/./dwg.spec:8818:1
    #6 0x1005fae in dwg_encode_variable_type /src/libredwg/src/./classes.inc:243:3
    #7 0xfe71d6 in dwg_encode_add_object /src/libredwg/src/encode.c:3771:28
    #8 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #9 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #10 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #11 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

previously allocated by thread T0 here:
    #0 0x498003 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d8122 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0xfe5ec7 in dwg_encode_add_object /src/libredwg/src/encode.c:3490:5
    #3 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #4 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #5 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #6 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: heap-use-after-free /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3 in __asan_memcpy
Shadow bytes around the buggy address:
  0x10007e5632b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x10007e563300:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563310: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563320: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563330: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563340: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563350: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
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
==3021191==ABORTING

```


## Compile command

```
CC="clang" CFLAGS="-O1 -g -fsanitize=address" ./configure --enable-release --disable-shared && make
```

## How can we reproduce the issue?

```
unzip tests_64233.zip
dwgrewrite ./tests_64233
```
POC file :  
[tests_64233.zip](https://github.com/LibreDWG/libredwg/files/6622104/tests_64233.zip)


Heap use after free in bit_copy_chain  in src/bits.c:3318
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap use after free in bit_copy_chain  in src/bits.c:3318
Issue date:

--- start body ---
## Affected version 

the latest commit: https://github.com/LibreDWG/libredwg/tree/45d2a290c65ed691be0901ba2b2ef51044e07a16 and 0.12.4

## What's the problem?


A heap use after free was discovered in bit_copy_chain in src/bits.c:3318.

ASAN report:

```
$ ./dwgrewrite ./pocs/tests_64233
......
......
......
==3021191==ERROR: AddressSanitizer: heap-use-after-free on address 0x7ffff2b59800 at pc 0x0000004970c7 bp 0x7fffffffba70 sp 0x7fffffffb238
READ of size 282028 at 0x7ffff2b59800 thread T0
    #0 0x4970c6 in __asan_memcpy /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #1 0x4e732c in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:10
    #2 0x4e732c in bit_copy_chain /src/libredwg/src/bits.c:3318:7
    #3 0x102955f in obj_flush_hdlstream /src/libredwg/src/encode.c:784:3
    #4 0x159ce6f in dwg_encode_PLANESURFACE_private /src/libredwg/src/./dwg.spec:8832:3
    #5 0x12e1e11 in dwg_encode_PLANESURFACE /src/libredwg/src/./dwg.spec:8818:1
    #6 0x1005fae in dwg_encode_variable_type /src/libredwg/src/./classes.inc:243:3
    #7 0xfe71d6 in dwg_encode_add_object /src/libredwg/src/encode.c:3771:28
    #8 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #9 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #10 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #11 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
    #12 0x41c3fd in _start (/mnt/disk/out/libredwg/programs_asan/dwgrewrite+0x41c3fd)

0x7ffff2b59800 is located 0 bytes inside of 282624-byte region [0x7ffff2b59800,0x7ffff2b9e800)
freed by thread T0 here:
    #0 0x498003 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d8122 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0x4e7260 in bit_copy_chain /src/libredwg/src/bits.c:3314:5
    #3 0x102955f in obj_flush_hdlstream /src/libredwg/src/encode.c:784:3
    #4 0x159ce6f in dwg_encode_PLANESURFACE_private /src/libredwg/src/./dwg.spec:8832:3
    #5 0x12e1e11 in dwg_encode_PLANESURFACE /src/libredwg/src/./dwg.spec:8818:1
    #6 0x1005fae in dwg_encode_variable_type /src/libredwg/src/./classes.inc:243:3
    #7 0xfe71d6 in dwg_encode_add_object /src/libredwg/src/encode.c:3771:28
    #8 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #9 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #10 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #11 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

previously allocated by thread T0 here:
    #0 0x498003 in realloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    #1 0x4d8122 in bit_chain_alloc /src/libredwg/src/bits.c:3026:30
    #2 0xfe5ec7 in dwg_encode_add_object /src/libredwg/src/encode.c:3490:5
    #3 0xf98c78 in dwg_encode /src/libredwg/src/encode.c:2352:16
    #4 0x4cbcf6 in dwg_write_file /src/libredwg/src/dwg.c:429:11
    #5 0x4c939f in main /src/libredwg/programs/dwgrewrite.c:349:11
    #6 0x7ffff7c580b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: heap-use-after-free /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3 in __asan_memcpy
Shadow bytes around the buggy address:
  0x10007e5632b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x10007e5632f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x10007e563300:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563310: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563320: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563330: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563340: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x10007e563350: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
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
==3021191==ABORTING

```


## Compile command

```
CC="clang" CFLAGS="-O1 -g -fsanitize=address" ./configure --enable-release --disable-shared && make
```

## How can we reproduce the issue?

```
unzip tests_64233.zip
dwgrewrite ./tests_64233
```
POC file :  
[tests_64233.zip](https://github.com/LibreDWG/libredwg/files/6622104/tests_64233.zip)


--- end body ---
