Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

fix NPE in PolygonShape.set when no Vec2ArrayPool is given #724

Merged
merged 2 commits into from
Jun 16, 2022
Merged

fix NPE in PolygonShape.set when no Vec2ArrayPool is given #724

merged 2 commits into from
Jun 16, 2022

Conversation

jbellis
Copy link
Contributor

@jbellis jbellis commented Jun 6, 2022

Note that the cast on line 133 is necessary for Kotlin to figure out that the actual fix on line 147 is valid

Comment on lines -215 to -217
if (vertices[i] == null) {
vertices[i] = Vec2()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because it's provably unnecessary

Copy link
Contributor

Choose a reason for hiding this comment

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

What makes you think it is unnecessary in all the cases?

Comment on lines +131 to +135
val ps : Array<Vec2?>
if (vecPool != null)
ps = vecPool[Settings.maxPolygonVertices] as Array<Vec2?>
else
arrayOfNulls<Vec2>(Settings.maxPolygonVertices)
ps = arrayOfNulls<Vec2>(Settings.maxPolygonVertices)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change shouldn't be required, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Above:

Note that the cast on line 133 is necessary for Kotlin to figure out that the actual fix on line 147 is valid

Copy link
Contributor

Choose a reason for hiding this comment

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

Now the declaration and the assignment is split but it looks the same to me and it was compiling before. What happens if you restore the original lines there?

@soywiz
Copy link
Contributor

soywiz commented Jun 6, 2022

Can you also add your failing test case here?
https://github.com/korlibs/korge-next/tree/f01a13a932e4ed4f96c39a20604df902ebf7ef38/kbox2d/src/commonTest/kotlin/org/jbox2d/utests

Something like:

class PolygonShapeTest {
  @Test
  fun testPolygonShapeTest() {
    val shape = PolygonShape()
    var zero = Vec2(0f, 0f)
    shape.set(arrayOf(zero, zero, zero), 3) // This shouldn't throw
    assertEquals(...)
  }
}

@soywiz
Copy link
Contributor

soywiz commented Jun 6, 2022

Fixes korlibs/korge#445

@jbellis
Copy link
Contributor Author

jbellis commented Jun 6, 2022

test added to PR

val vertexArray = arrayOf(Vec2(1f, 1f), Vec2(1f, 0f), Vec2(0f, 0f))
shape.set(vertexArray, vertexArray.size)

assertEquals(shape.count, vertexArray.size)
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. Thanks!

@soywiz soywiz merged commit 85814f2 into korlibs-archive:main Jun 16, 2022
@soywiz
Copy link
Contributor

soywiz commented Jun 16, 2022

Just checked that vectors: Array is already initialized, so indeed not needed. Going to merge it now.
Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants