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

[BUGFIX beta] Negative indices in positional args proxy #19273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

buschtoens
Copy link
Contributor

@buschtoens buschtoens commented Nov 16, 2020

In ember-link I had code that potentially accessed a negative index on a positional args proxy, which used to return undefined. Starting with the 3.23.0 beta, this throws an error.

This is because only the upper bound is checked.

Copy link
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be better to change convertToInt to return null for negative indices.

@pzuraq / @krisselden - What do you think?

@@ -101,7 +101,7 @@ if (HAS_NATIVE_PROXY) {

const parsed = convertToInt(prop);

if (parsed !== null && parsed < positional.length) {
if (parsed !== null && parsed >= 0 && parsed < positional.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this fix is quite enough (we have the exact same type of check in has).

@rwjblue
Copy link
Member

rwjblue commented Nov 16, 2020

Also, we need a test for this behavior (confirming that modifiers,helpers, and components can access args[-1] and get undefined back).

@buschtoens
Copy link
Contributor Author

Cool! Let me know on the way forward regarding:

I think it may be better to change convertToInt to return null for negative indices.

I'll update the PR then and add tests. 🧪

@rwjblue
Copy link
Member

rwjblue commented Nov 16, 2020

Starting with the 3.23.0 beta, this throws an error.

Oh, also, what is the error here?

@buschtoens
Copy link
Contributor Author

buschtoens commented Nov 16, 2020

Uncaught (in promise) TypeError: ref is undefined
    valueForRef reference.js:145
    get
    normalizeLinkParams
    compute
    getValue
    ...

if (parsed !== null && parsed < positional.length) {
return valueForRef(positional[parsed]);
}

https://github.com/glimmerjs/glimmer-vm/blob/022b586405a857afdf59eebfcdae1f51428dc344/packages/%40glimmer/reference/lib/reference.ts#L153

ref is undefined as positional[-1] is undefined.

@rwjblue
Copy link
Member

rwjblue commented Nov 17, 2020

Cool! Let me know on the way forward regarding:

I think it may be better to change convertToInt to return null for negative indices.

Just chatted with @pzuraq / @krisselden about this, lets do it this way. I think that would look something like:

function convertToInt(prop: number | string | symbol): number | null {
  if (typeof prop === 'symbol') return null;

  const num = Number(prop);

  if (isNaN(num)) return null;

  return num % 1 === 0 && num > -1 ? num : null;
}

Then we just need some tests, and to update the commit message/PR title to [BUGFIX release] since 3.23 has been released.

@kategengler
Copy link
Member

Is this still relevant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants