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 OOB access in math.Vector swizzle; add '0' and '1' swizzles #2024

Merged
merged 1 commit into from Aug 19, 2020

Conversation

lordmauve
Copy link
Contributor

@lordmauve lordmauve commented Jul 24, 2020

Fixes #2023.

This adds bounds checks on swizzles like Vector2().xyz.

What I actually dived into this code for was to add swizzles with the digits 0 and 1. I can't easily make the two changes independent now so it's one PR.

>>> Vector2(3, 2).xy1
Vector3(3, 2, 1) 
>>> Vector2(3, 2).xy01
(3, 2, 0, 1) 

These will be useful to prepare homogenous coordinates for affine transforms by matrix multiplication. Here is a matrix representing a 2x scale + (3, -1) translation:

>>> import numpy as np
>>> from pygame.math import Vector2
>>> v = Vector2(0.5, 0.8)
>>> mat = np.array([
...   [2, 0, 3],
...   [0, 2, -1],
... ])
>>> mat @ v.xy1
array([4. , 0.6])

Given that the intended usage would be .xy1, .xyz1 or .xy01, it's not really a disadvantage that we can't syntactically write .10yx or something strange.

Copy link
Contributor

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

This all seems to make sense to me.

Will leave it up for awhile in case anyone else has any feedback before merging.

@MyreMylar MyreMylar merged commit 1476009 into pygame:master Aug 19, 2020
@illume illume changed the title Fix OOB access in swizzle; add '0' and '1' swizzles Fix OOB access in math.Vector swizzle; add '0' and '1' swizzles Sep 17, 2020
@illume illume added the math pygame.math label Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
math pygame.math
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Swizzle access to uninitialised memory
3 participants