## 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 get_bmp in programs/dwgbmp.c:182.

ASAN report:

```
$ ./dwgbmp ./pocs/tests_64219  /dev/null
=================================================================
==17975==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6290000042ef at pc 0x00000043449e bp 0x7ffde9425050 sp 0x7ffde9424818
READ of size 16520 at 0x6290000042ef thread T0
    #0 0x43449d in fwrite (/root/fuzz/dwgbmp+0x43449d)
    #1 0x4c619e in get_bmp /root/fuzz/45d2a29/programs/dwgbmp.c:182:12
    #2 0x4c5bcf in main /root/fuzz/45d2a29/programs/dwgbmp.c:301:11
    #3 0x7facb795d83f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #4 0x41ba28 in _start (/root/fuzz/dwgbmp+0x41ba28)

0x6290000042ef is located 0 bytes to the right of 16623-byte region [0x629000000200,0x6290000042ef)
allocated by thread T0 here:
    #0 0x4959f2 in calloc (/root/fuzz/dwgbmp+0x4959f2)
    #1 0x4ebf1d in decode_R13_R2000 /root/fuzz/45d2a29/src/decode.c:1028:36
    #2 0x4e48a1 in dwg_decode /root/fuzz/45d2a29/src/decode.c:246:36
    #3 0x4c75ec in dwg_read_file /root/fuzz/45d2a29/src/dwg.c:254:11
    #4 0x4c5ead in get_bmp /root/fuzz/45d2a29/programs/dwgbmp.c:120:11
    #5 0x4c5bcf in main /root/fuzz/45d2a29/programs/dwgbmp.c:301:11
    #6 0x7facb795d83f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow (/root/fuzz/dwgbmp+0x43449d) in fwrite
Shadow bytes around the buggy address:
  0x0c527fff8800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8810: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8820: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8830: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8840: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c527fff8850: 00 00 00 00 00 00 00 00 00 00 00 00 00[07]fa fa
  0x0c527fff8860: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8870: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8880: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8890: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff88a0: 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
==17975==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64219.zip
dwgbmp ./tests_64219
```
POC file :  
[tests_64219.zip](https://github.com/LibreDWG/libredwg/files/6614957/tests_64219.zip)

Heap-buffer-overflow in get_bmp in programs/dwgbmp.c:182
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : Heap-buffer-overflow in get_bmp in programs/dwgbmp.c:182
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 get_bmp in programs/dwgbmp.c:182.

ASAN report:

```
$ ./dwgbmp ./pocs/tests_64219  /dev/null
=================================================================
==17975==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6290000042ef at pc 0x00000043449e bp 0x7ffde9425050 sp 0x7ffde9424818
READ of size 16520 at 0x6290000042ef thread T0
    #0 0x43449d in fwrite (/root/fuzz/dwgbmp+0x43449d)
    #1 0x4c619e in get_bmp /root/fuzz/45d2a29/programs/dwgbmp.c:182:12
    #2 0x4c5bcf in main /root/fuzz/45d2a29/programs/dwgbmp.c:301:11
    #3 0x7facb795d83f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
    #4 0x41ba28 in _start (/root/fuzz/dwgbmp+0x41ba28)

0x6290000042ef is located 0 bytes to the right of 16623-byte region [0x629000000200,0x6290000042ef)
allocated by thread T0 here:
    #0 0x4959f2 in calloc (/root/fuzz/dwgbmp+0x4959f2)
    #1 0x4ebf1d in decode_R13_R2000 /root/fuzz/45d2a29/src/decode.c:1028:36
    #2 0x4e48a1 in dwg_decode /root/fuzz/45d2a29/src/decode.c:246:36
    #3 0x4c75ec in dwg_read_file /root/fuzz/45d2a29/src/dwg.c:254:11
    #4 0x4c5ead in get_bmp /root/fuzz/45d2a29/programs/dwgbmp.c:120:11
    #5 0x4c5bcf in main /root/fuzz/45d2a29/programs/dwgbmp.c:301:11
    #6 0x7facb795d83f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: heap-buffer-overflow (/root/fuzz/dwgbmp+0x43449d) in fwrite
Shadow bytes around the buggy address:
  0x0c527fff8800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8810: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8820: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8830: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c527fff8840: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c527fff8850: 00 00 00 00 00 00 00 00 00 00 00 00 00[07]fa fa
  0x0c527fff8860: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8870: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8880: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff8890: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c527fff88a0: 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
==17975==ABORTING
```


## Compile command

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

## How can we reproduce the issue?

```
unzip tests_64219.zip
dwgbmp ./tests_64219
```
POC file :  
[tests_64219.zip](https://github.com/LibreDWG/libredwg/files/6614957/tests_64219.zip)

--- end body ---
