## Affected version 

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

## What's the problem?


A heap buffer overflow was discovered in bit_write_TF in bits.c.

ASAN report:

```
==25604==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x626000008bbf at pc 0x000000495c07 bp 0x7fffffffc790 sp 0x7fffffffbf58
READ of size 209 at 0x626000008bbf thread T0
    #0 0x495c06 in __asan_memcpy /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #1 0x4de8e4 in memcpy /usr/include/x86_64-linux-gnu/bits/string3.h:53:10
    #2 0x4de8e4 in bit_write_TF /src/libredwg/src/bits.c:1525:7
    #3 0xe3ffca in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2043:15
    #4 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #5 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #6 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #7 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #8 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #9 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #10 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #11 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #12 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x626000008bbf is located 0 bytes to the right of 10943-byte region [0x626000006100,0x626000008bbf)
allocated by thread T0 here:
    #0 0x4969e2 in calloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x6d69d4 in decode_3dsolid /src/libredwg/src/./dwg.spec:2081:57
    #2 0x6d7fa1 in dwg_decode__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #3 0x59888d in dwg_decode__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #4 0x5901b0 in dwg_decode_add_object /src/libredwg/src/decode.c:5719:15
    #5 0xc839d6 in read_2007_section_handles /src/libredwg/src/decode_r2007.c:1694:19
    #6 0xc7b417 in read_r2007_meta_data /src/libredwg/src/decode_r2007.c:2388:12
    #7 0x4fd4be in decode_R2007 /src/libredwg/src/decode.c:3836:11
    #8 0x4e789b in dwg_decode /src/libredwg/src/decode.c:248:29
    #9 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #10 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #11 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /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:
  0x0c4c7fff9120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c4c7fff9170: 00 00 00 00 00 00 00[07]fa fa fa fa fa fa fa fa
  0x0c4c7fff9180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff9190: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91c0: 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
==25604==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64243.zip
dxfwrite -I DWG -o /dev/null -y ./tests_64243
```
POC file :  
[tests_64243.zip](https://github.com/LibreDWG/libredwg/files/6629920/tests_64243.zip)
Heap-buffer-overflow in bit_write_TF in bits.c call by dwg_convert_SAB_to_SAT1
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in bit_write_TF in bits.c call by dwg_convert_SAB_to_SAT1
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 buffer overflow was discovered in bit_write_TF in bits.c.

ASAN report:

```
==25604==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x626000008bbf at pc 0x000000495c07 bp 0x7fffffffc790 sp 0x7fffffffbf58
READ of size 209 at 0x626000008bbf thread T0
    #0 0x495c06 in __asan_memcpy /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #1 0x4de8e4 in memcpy /usr/include/x86_64-linux-gnu/bits/string3.h:53:10
    #2 0x4de8e4 in bit_write_TF /src/libredwg/src/bits.c:1525:7
    #3 0xe3ffca in dwg_convert_SAB_to_SAT1 /src/libredwg/src/out_dxf.c:2043:15
    #4 0xeebd9e in dxf_3dsolid /src/libredwg/src/out_dxf.c:2334:20
    #5 0xeecb01 in dwg_dxf__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #6 0xea23e2 in dwg_dxf__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #7 0xe9677d in dwg_dxf_object /src/libredwg/src/out_dxf.c:2690:14
    #8 0xe6817c in dxf_entities_write /src/libredwg/src/out_dxf.c:3532:16
    #9 0xe41b4c in dwg_write_dxf /src/libredwg/src/out_dxf.c:3682:7
    #10 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #11 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #12 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x626000008bbf is located 0 bytes to the right of 10943-byte region [0x626000006100,0x626000008bbf)
allocated by thread T0 here:
    #0 0x4969e2 in calloc /src/llvm-project-12.0.0.src/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    #1 0x6d69d4 in decode_3dsolid /src/libredwg/src/./dwg.spec:2081:57
    #2 0x6d7fa1 in dwg_decode__3DSOLID_private /src/libredwg/src/./dwg.spec:2370:3
    #3 0x59888d in dwg_decode__3DSOLID /src/libredwg/src/./dwg.spec:2369:1
    #4 0x5901b0 in dwg_decode_add_object /src/libredwg/src/decode.c:5719:15
    #5 0xc839d6 in read_2007_section_handles /src/libredwg/src/decode_r2007.c:1694:19
    #6 0xc7b417 in read_r2007_meta_data /src/libredwg/src/decode_r2007.c:2388:12
    #7 0x4fd4be in decode_R2007 /src/libredwg/src/decode.c:3836:11
    #8 0x4e789b in dwg_decode /src/libredwg/src/decode.c:248:29
    #9 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #10 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #11 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /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:
  0x0c4c7fff9120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4c7fff9160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c4c7fff9170: 00 00 00 00 00 00 00[07]fa fa fa fa fa fa fa fa
  0x0c4c7fff9180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff9190: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4c7fff91c0: 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
==25604==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64243.zip
dxfwrite -I DWG -o /dev/null -y ./tests_64243
```
POC file :  
[tests_64243.zip](https://github.com/LibreDWG/libredwg/files/6629920/tests_64243.zip)
--- end body ---
