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

Fix mapJSHandle invalid memory address or nil pointer dereference #1544

Merged
merged 6 commits into from
Nov 26, 2024

Conversation

inancgumus
Copy link
Member

What?

Add missing function arguments check to JSHandle mapping.

Why?

#1543

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates #1543

@inancgumus inancgumus added the mapping k6 browser to Goja mapping related. label Nov 19, 2024
@inancgumus inancgumus requested a review from a team as a code owner November 19, 2024 20:09
@inancgumus inancgumus requested review from oleiade and olegbespalov and removed request for a team November 19, 2024 20:09
@inancgumus inancgumus self-assigned this Nov 19, 2024
Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! I like where it is going.

Two points:

  1. Can we look to fix the other evaluate/evaluateHandle methods on page and frame? In a new PR if you prefer, but happy to have them as separate commits in this PR 🙂
  2. This does indeed fix the issues when evaluate() is used without pageFunc. It however breaks one thing which is that you can no longer pass in a string:
// As a string that is callable
console.log(await jsHandle.evaluate("node => node.innerText"));
// As a function
console.log(await jsHandle.evaluate(node => node.innerText));
  1. Do we need tests for at least one of these methods (maybe just on page?).

@inancgumus
Copy link
Member Author

inancgumus commented Nov 20, 2024

@ankur22, sure thing!

  1. Can we look to fix the other evaluate/evaluateHandle methods on page and frame? In a new PR if you prefer, but happy to have them as separate commits in this PR 🙂

Sure :) Can you repurpose issue #1543? Maybe move this explanation to the issue description so that we can understand what the issue entails. Currently, it looks like it's about mapJSHandle.

  1. This does indeed fix the issues when evaluate() is used without pageFunc. It however breaks one thing which is that you can no longer pass in a string:

Are you sure that it doesn't work with strings? I've tried this, and it worked:

const n = await handle.evaluate('() => 5 * 42');

But this is a nice catch because now we don't accept non-functions 🙇

await handle.evaluate('document.title');

Update: After evaluation, I noticed that we already don't accept such a string. We only accept this form: () => document.title. Still, I'll modify the code to check for empty strings and nil functions instead.

  1. Do we need tests for at least one of these methods (maybe just on page?).

Would adding a test to examples/ be fine?

@inancgumus inancgumus force-pushed the fix/map-js-handle-nil-ptr branch 2 times, most recently from eb5c1b6 to 76661a5 Compare November 20, 2024 16:00
@ankur22
Copy link
Collaborator

ankur22 commented Nov 21, 2024

Sure :) Can you repurpose issue #1543? Maybe move #1543 (comment) to the issue description so that we can understand what the issue entails. Currently, it looks like it's about mapJSHandle.

Sure, done.

Are you sure that it doesn't work with strings?

Yeah, this script below no longer works with this branch:

import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
            type: 'chromium',
        },
      },
    },
  },
}

export default async function() {
  const page = await browser.newPage();

  try {
    await page.goto('https://test.k6.io/my_messages.php');

    const jsHandle = await page.waitForSelector('h2');

    // As a string that is callable
    console.log(await jsHandle.evaluate("node => node.innerText"));
    
    // As a function
    console.log(await jsHandle.evaluate(node => node.innerText));
  } finally {
    await page.close();
  }
}

Would adding a test to examples/ be fine?

I think we should have table tests (integration tests) in the tests package, unless there's a reason it's not possible to create them? A test in the examples folder would be a good addition too.

@inancgumus inancgumus force-pushed the fix/map-js-handle-nil-ptr branch 2 times, most recently from 5e588a1 to 6e67b7c Compare November 25, 2024 15:47
Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@inancgumus inancgumus merged commit 0fbb4c7 into main Nov 26, 2024
22 checks passed
@inancgumus inancgumus deleted the fix/map-js-handle-nil-ptr branch November 26, 2024 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mapping k6 browser to Goja mapping related.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants