Skip to content

Calling inject_at with an element already in the array copies the wrong value #4604

@jace0x21

Description

@jace0x21

Context

package main

import "core:fmt"

// Need struct with at least 3 ints. Issue didn't manifest with 2.
Foo :: struct {a, x, y, z: int}

main :: proc() {
    foo_arr := [dynamic]Foo{Foo{1, 1, 1, 1}, Foo{2, 2, 2, 2}, Foo{3, 3, 3, 3}}
    reserve(&foo_arr, 4)
    fmt.printf("%v\n", foo_arr)
    inject_at(&foo_arr, 0, foo_arr[2])
    fmt.printf("%v\n", foo_arr)
}

The above program prints

[Foo{a = 1, x = 1, y = 1, z = 1}, Foo{a = 2, x = 2, y = 2, z = 2}, Foo{a = 3, x = 3, y = 3, z = 3}]
[Foo{a = 2, x = 2, y = 2, z = 2}, Foo{a = 1, x = 1, y = 1, z = 1}, Foo{a = 2, x = 2, y = 2, z = 2}, Foo{a = 3, x = 3, y = 3, z = 3}]
  • Operating System & Odin Version:
    Odin: dev-2024-12:0a29d36aa
    OS: macOS Sonoma 14.4.1 (build 23E224, kernel 23.4.0)
    CPU: Apple M2
    RAM: 24576 MiB
    Backend: LLVM 18.1.8

Expected Behavior

I expect the output to be:

[Foo{a = 1, x = 1, y = 1, z = 1}, Foo{a = 2, x = 2, y = 2, z = 2}, Foo{a = 3, x = 3, y = 3, z = 3}]
[Foo{a = 3, x = 3, y = 3, z = 3}, Foo{a = 1, x = 1, y = 1, z = 1}, Foo{a = 2, x = 2, y = 2, z = 2}, Foo{a = 3, x = 3, y = 3, z = 3}]

Current Behavior

inject_at will move down all elements in the array and then the arg pointer will be pointing at the wrong element

Failure Information (for bugs)

You need a struct with more than two fields for this bug to manifest. As I understand it, this is because small structs don't need to be passed by pointer.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Compile the above program
  2. Run it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions