Skip to content

BUG: avoid memory segfault in from_ragged_array#2291

Merged
jorisvandenbossche merged 6 commits intoshapely:mainfrom
jorisvandenbossche:from-ragged-memory-crash
May 19, 2025
Merged

BUG: avoid memory segfault in from_ragged_array#2291
jorisvandenbossche merged 6 commits intoshapely:mainfrom
jorisvandenbossche:from-ragged-memory-crash

Conversation

@jorisvandenbossche
Copy link
Copy Markdown
Member

Fixes the issue reported in #2284

We were allocating a too small temporary array for storing the intermediate rings.

@coveralls
Copy link
Copy Markdown

coveralls commented May 2, 2025

Pull Request Test Coverage Report for Build 15110636867

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 86.185%

Totals Coverage Status
Change from base Build 15110432228: 0.3%
Covered Lines: 2670
Relevant Lines: 3098

💛 - Coveralls

@jorisvandenbossche jorisvandenbossche added this to the 2.1.1 milestone May 2, 2025
@jorisvandenbossche jorisvandenbossche requested a review from mwtoews May 9, 2025 06:13
Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

I can reproduce the segfault and verify the fix. Looks like there was a mix-up with n_parts and n_geoms, which is now resolved. Only a small suggestion to apply for new NumPy code. Thanks for nicely spotted fix!

# https://github.com/shapely/shapely/discussions/2284

# one of the geometries has more rings than the total number of geometries
coords = np.random.randn(60, 2)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

New code should use the modern RNG methods (docs), also make the random coords stay the same-ish by specifying a seed.

Suggested change
coords = np.random.randn(60, 2)
rng = np.random.default_rng(2284)
coords = rng.standard_normal(120).reshape((60, 2))

Copy link
Copy Markdown
Member Author

@jorisvandenbossche jorisvandenbossche May 16, 2025

Choose a reason for hiding this comment

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

also make the random coords stay the same-ish by specifying a seed.

For testing we don't necessarily want to use a fixed seed? It seems useful to me to use varying values. Of course if you then run into an issue with certain values, it is not reproducible, so it is not necessarily very helpful, but personally I don't see the point of wanting to fix the random values for this use case. I would then maybe rather use something like https://github.com/pytest-dev/pytest-randomly to ensure the seed is fixed per run of the test suite

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It probably doesn't make any difference on the randomness, as it's the data size that matters here. A non-seeded RNG is simply rng = np.random.default_rng(), if that's preferred.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated.
(now, to be honest, I personally think this recommendation from numpy is too strong, and for testing purposes here where we don't care about the pseudo-random number generator theory (we are not doing statistical modelling), using np.random.default_rng().standard_normal(120).reshape((60, 2)) instead of np.random.randn(60, 2) is just unnecessary boilerplate and noise in the test code .. ;))

@jorisvandenbossche jorisvandenbossche merged commit 7137ecf into shapely:main May 19, 2025
26 checks passed
@jorisvandenbossche jorisvandenbossche deleted the from-ragged-memory-crash branch May 19, 2025 10:38
hillwithsmallfields pushed a commit to hillwithsmallfields/shapely that referenced this pull request Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants