Arrays of dynarrays can't be indexed correctly

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
Post Reply
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Arrays of dynarrays can't be indexed correctly

Post by phantombeta »

When you try to index a dynarray contained inside an array with a non-constant index, it'll error out with "Invalid resizable array".

Code: Select all

    void foo () {
        Array<int> a [5];
        a.Push (555);

        a [3] [0] = 666; // This works fine
        int b = random (0, 255);
        a [b] [0] = 666; // This errors out
    }
Steps to reproduce:
  • Try to load the example PK3
I'd really like to see this bug fixed. Right now, the only way to get the array's contents is to copy it to a local variable.
I actually tried to see if I could fix it myself, but couldn't figure anything out. :\
Attachments
NestedDynArrayIndexBug.PK3
(371 Bytes) Downloaded 34 times
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Arrays of dynarrays can't be indexed correctly

Post by Graf Zahl »

phantombeta wrote: I actually tried to see if I could fix it myself, but couldn't figure anything out. :\
Welcome to the club. :( This one got me, too.
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Arrays of dynarrays can't be indexed correctly

Post by phantombeta »

One thing I did figure out is that the constant case works because it pretty much turns constant index array accesses into temporary fields, if that helps at all.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Arrays of dynarrays can't be indexed correctly

Post by Graf Zahl »

I ran into this myself when scriptifying the AutouseHealth function, but I didn't manage to fix it.
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Yet Another Array Fix By phantombeta

Post by phantombeta »

Fuckssake, the solution was sitting right there in front me the whole time.
Arrays of dynarrays simply needed to be special-cased so that the address of their size value is calculated at runtime instead of compile-time.
PR link.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Arrays of dynarrays can't be indexed correctly

Post by Graf Zahl »

That's very much appreciated.
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Arrays of dynarrays can't be indexed correctly

Post by phantombeta »

I fixed some bugs in my original fix. PR link

Due to just blindly copying part of what GZDoom did and freeing the arrayvar register before even using it, the bounds value was getting its value from inside the array's elements. Obvious problem there. It both means it doesn't work correctly, and it also means there's a serious buffer overflow exploit there.
Thankfully, though, there was actually another bug that was making it emit BOUND instead of BOUND_R, which broke it even more - but also stopped the buffer overflow exploit from actually being usable at all.
The PR fixes both bugs.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Arrays of dynarrays can't be indexed correctly

Post by Graf Zahl »

Next time, please open a new report for submitting a PR instead of bumping a thread that has been closed.
Post Reply

Return to “Closed Bugs [GZDoom]”