## 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 dwg_dxf_LWPOLYLINE_private in dwg.spec.

ASAN report:

```
==25881==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x617000002d10 at pc 0x000000ef927b bp 0x7fffffffcb10 sp 0x7fffffffcb08
READ of size 8 at 0x617000002d10 thread T0
    #0 0xef927a in dwg_dxf_LWPOLYLINE_private /src/libredwg/src/./dwg.spec:4832:14
    #1 0xea80fb in dwg_dxf_LWPOLYLINE /src/libredwg/src/./dwg.spec:4790:1
    #2 0xe95e19 in dwg_dxf_object /src/libredwg/src/out_dxf.c:2747:14
    #3 0xe95916 in dxf_block_write /src/libredwg/src/out_dxf.c:3453:18
    #4 0xe67e33 in dxf_blocks_write /src/libredwg/src/out_dxf.c:3505:20
    #5 0xe41b38 in dwg_write_dxf /src/libredwg/src/out_dxf.c:3678:11
    #6 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #7 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #8 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x617000002d10 is located 0 bytes to the right of 656-byte region [0x617000002a80,0x617000002d10)
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 0x7c2f2f in dwg_decode_LWPOLYLINE_private /src/libredwg/src/./dwg.spec:4868:5
    #2 0x59d983 in dwg_decode_LWPOLYLINE /src/libredwg/src/./dwg.spec:4790:1
    #3 0x58fcdf in dwg_decode_add_object /src/libredwg/src/decode.c:5833:15
    #4 0x4efe14 in decode_R13_R2000 /src/libredwg/src/decode.c:1342:19
    #5 0x4e77c1 in dwg_decode /src/libredwg/src/decode.c:246:36
    #6 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #7 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #8 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/./dwg.spec:4832:14 in dwg_dxf_LWPOLYLINE_private
Shadow bytes around the buggy address:
  0x0c2e7fff8550: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8560: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8570: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2e7fff85a0: 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2e7fff85b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2e7fff85c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85f0: 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
==25881==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

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

Heap-buffer-overflow in dwg_dxf_LWPOLYLINE_private in dwg.spec
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in dwg_dxf_LWPOLYLINE_private in dwg.spec
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 dwg_dxf_LWPOLYLINE_private in dwg.spec.

ASAN report:

```
==25881==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x617000002d10 at pc 0x000000ef927b bp 0x7fffffffcb10 sp 0x7fffffffcb08
READ of size 8 at 0x617000002d10 thread T0
    #0 0xef927a in dwg_dxf_LWPOLYLINE_private /src/libredwg/src/./dwg.spec:4832:14
    #1 0xea80fb in dwg_dxf_LWPOLYLINE /src/libredwg/src/./dwg.spec:4790:1
    #2 0xe95e19 in dwg_dxf_object /src/libredwg/src/out_dxf.c:2747:14
    #3 0xe95916 in dxf_block_write /src/libredwg/src/out_dxf.c:3453:18
    #4 0xe67e33 in dxf_blocks_write /src/libredwg/src/out_dxf.c:3505:20
    #5 0xe41b38 in dwg_write_dxf /src/libredwg/src/out_dxf.c:3678:11
    #6 0x4c864b in main /src/libredwg/programs/dxfwrite.c:451:11
    #7 0x7ffff6ee383f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #8 0x41bc98 in _start (/mnt/disk/out/libredwg/programs_asan/dxfwrite+0x41bc98)

0x617000002d10 is located 0 bytes to the right of 656-byte region [0x617000002a80,0x617000002d10)
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 0x7c2f2f in dwg_decode_LWPOLYLINE_private /src/libredwg/src/./dwg.spec:4868:5
    #2 0x59d983 in dwg_decode_LWPOLYLINE /src/libredwg/src/./dwg.spec:4790:1
    #3 0x58fcdf in dwg_decode_add_object /src/libredwg/src/decode.c:5833:15
    #4 0x4efe14 in decode_R13_R2000 /src/libredwg/src/decode.c:1342:19
    #5 0x4e77c1 in dwg_decode /src/libredwg/src/decode.c:246:36
    #6 0x4c9f9e in dwg_read_file /src/libredwg/src/dwg.c:254:11
    #7 0x4c8121 in main /src/libredwg/programs/dxfwrite.c:321:15
    #8 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/./dwg.spec:4832:14 in dwg_dxf_LWPOLYLINE_private
Shadow bytes around the buggy address:
  0x0c2e7fff8550: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8560: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8570: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff8590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2e7fff85a0: 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2e7fff85b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2e7fff85c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2e7fff85f0: 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
==25881==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

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

--- end body ---
