## 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_convert_TU in bits.c.

ASAN report:

```
==20028==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x616000001a80 at pc 0x0000004e2780 bp 0x7fffffffcab0 sp 0x7fffffffcaa8
READ of size 2 at 0x616000001a80 thread T0
    #0 0x4e277f in bit_convert_TU /src/libredwg/src/bits.c:2277:15
    #1 0xe7e1c2 in dwg_dxf_LTYPE_private /src/libredwg/src/./dwg.spec:3161:17
    #2 0xe6c89c in dwg_dxf_LTYPE /src/libredwg/src/./dwg.spec:3132:1
    #3 0xe632db in dxf_tables_write /src/libredwg/src/out_dxf.c:3118:22
    #4 0xe41b24 in dwg_write_dxf /src/libredwg/src/out_dxf.c:3675:11
    #5 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #6 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #7 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x616000001a81 is located 0 bytes to the right of 513-byte region [0x616000001880,0x616000001a81)
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 0x4de2ad in bit_read_TF /src/libredwg/src/bits.c:1456:25
    #2 0x749d6f in dwg_decode_LTYPE_private /src/libredwg/src/./dwg.spec:3212:7
    #3 0x59ae8d in dwg_decode_LTYPE /src/libredwg/src/./dwg.spec:3132:1
    #4 0x5901c2 in dwg_decode_add_object /src/libredwg/src/decode.c:5779:15
    #5 0x5d2c9b in read_2004_section_handles /src/libredwg/src/decode.c:2928:19
    #6 0x4fa481 in decode_R2004 /src/libredwg/src/decode.c:3778:12
    #7 0x4e77f3 in dwg_decode /src/libredwg/src/decode.c
    #8 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #9 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/libredwg/src/bits.c:2277:15 in bit_convert_TU
Shadow bytes around the buggy address:
  0x0c2c7fff8300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8310: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8340: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2c7fff8350:[01]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8360: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8370: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8380: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff83a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==20028==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

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

Heap-buffer-overflow in bit_convert_TU in bits.c
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in bit_convert_TU in bits.c
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_convert_TU in bits.c.

ASAN report:

```
==20028==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x616000001a80 at pc 0x0000004e2780 bp 0x7fffffffcab0 sp 0x7fffffffcaa8
READ of size 2 at 0x616000001a80 thread T0
    #0 0x4e277f in bit_convert_TU /src/libredwg/src/bits.c:2277:15
    #1 0xe7e1c2 in dwg_dxf_LTYPE_private /src/libredwg/src/./dwg.spec:3161:17
    #2 0xe6c89c in dwg_dxf_LTYPE /src/libredwg/src/./dwg.spec:3132:1
    #3 0xe632db in dxf_tables_write /src/libredwg/src/out_dxf.c:3118:22
    #4 0xe41b24 in dwg_write_dxf /src/libredwg/src/out_dxf.c:3675:11
    #5 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #6 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #7 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x616000001a81 is located 0 bytes to the right of 513-byte region [0x616000001880,0x616000001a81)
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 0x4de2ad in bit_read_TF /src/libredwg/src/bits.c:1456:25
    #2 0x749d6f in dwg_decode_LTYPE_private /src/libredwg/src/./dwg.spec:3212:7
    #3 0x59ae8d in dwg_decode_LTYPE /src/libredwg/src/./dwg.spec:3132:1
    #4 0x5901c2 in dwg_decode_add_object /src/libredwg/src/decode.c:5779:15
    #5 0x5d2c9b in read_2004_section_handles /src/libredwg/src/decode.c:2928:19
    #6 0x4fa481 in decode_R2004 /src/libredwg/src/decode.c:3778:12
    #7 0x4e77f3 in dwg_decode /src/libredwg/src/decode.c
    #8 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #9 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #10 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/libredwg/src/bits.c:2277:15 in bit_convert_TU
Shadow bytes around the buggy address:
  0x0c2c7fff8300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8310: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8340: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2c7fff8350:[01]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8360: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2c7fff8370: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8380: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff8390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2c7fff83a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==20028==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

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

--- end body ---
