hugopl/gi-crystal

`Gsk::RoundedRect#init` corrupts memory

BlobCodes opened this issue · 0 comments

The following code (just example code to create Gsk::RoundedRect):

require "gtk4"

loop do
  w = 100
  h = 100

  bounds = Graphene::Rect.new
  bounds.init(0.0f32, 0.0f32, w.to_f32, h.to_f32)

  corner = Graphene::Size.new
  corner.init(12.0f32, 12.0f32)

  rounded_rect = Gsk::RoundedRect.new
  rounded_rect.init(bounds, corner, corner, corner, corner)
end

Results in memory issues, depending on whether the GC is used or not.

If BDWGC is used:

[blobcodes@toolbox mangaba]$ crystal run src/test2.cr
Invalid memory access (signal 11) at address 0x0
[0x481b76] *Exception::CallStack::print_backtrace:Nil +118 in /var/home/blobcodes/.cache/crystal/crystal-run-test2.tmp
[0x470f7a] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +330 in /var/home/blobcodes/.cache/crystal/crystal-run-test2.tmp
[0x7f3623488ac0] ?? +139870496918208 in /lib64/libc.so.6
Invalid memory access (signal 7) at address 0x0
[0x481b76] *Exception::CallStack::print_backtrace:Nil +118 in /var/home/blobcodes/.cache/crystal/crystal-run-test2.tmp
[0x470f7a] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +330 in /var/home/blobcodes/.cache/crystal/crystal-run-test2.tmp
[0x7f3623488ac0] ?? +139870496918208 in /lib64/libc.so.6
Program received and didn't handle signal BUS (7)

..and if no GC is used:

⬢[blobcodes@toolbox mangaba]$ crystal run src/test2.cr -Dgc_none
malloc(): corrupted top size
Program received and didn't handle signal IOT (6)

While using this inside a vfunc, I also received this error:

corrupted size vs. prev_size
Program received and didn't handle signal IOT (6)

I tested on both release v0.11 and master, the behaviour doesn't change.

I think this may have something to do with RoundedRect having a fixed-size array in its struct:

  struct RoundedRect # 48 bytes long
    bounds : LibGraphene::Rect
    corner : Void[4]
  end

However: puts sizeof(Void[4]) # => 4

I think this must be Void*[4] instead. Maybe this happened because not enough space has been allocated.

EDIT: the correct awnser would have been LibGraphene::Size[4]