Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative array index incorrectly returns first element #133

Closed
pd opened this issue Sep 22, 2023 · 1 comment
Closed

Negative array index incorrectly returns first element #133

pd opened this issue Sep 22, 2023 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@pd
Copy link

pd commented Sep 22, 2023

In jrsonnet, any negative index of a non-empty array returns the first element. In go-jsonnet, it is an out-of-bounds error.

local xs = [1, 2];

// any negative index in jrsonnet returns xs[0]:
[xs[-1], xs[-2], xs[-42]]

jrsonnet:

[
   1,
   1,
   1
]

go-jsonnet on the same file:

RUNTIME ERROR: Index -1 out of bounds, not within [0, 2)
        bug.jsonnet:4:2-8       thunk from <$>
        Array element 0
        During manifestation

Negative index on an empty array does fail, but with the wrong index value:

[][-1]

jrsonnet:

// array out of bounds: 0 is not within [0,0)

go-jsonnet:

RUNTIME ERROR: Index -1 out of bounds, not within [0, 0)
        bug.jsonnet:1:1-7       $
        During evaluation
@CertainLach
Copy link
Owner

CertainLach commented Sep 22, 2023

Related to #117

Sorry, I should read properly first :D
Yes, this is a bug.

This is due to unchecked conversion from f64 (all jsonnet numbers are f64) to usize using num as usize, where negative numbers are rounded to zero, in array indexing implementation.

@CertainLach CertainLach added this to the v0.5.0 milestone Sep 22, 2023
@CertainLach CertainLach added the bug Something isn't working label Sep 22, 2023
CertainLach added a commit that referenced this issue Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

2 participants