8000 Inconsistent use of int and size_t in C Bindings for std::vector Types · Issue #1089 · elalish/manifold · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Inconsistent use of int and size_t in C Bindings for std::vector Types #1089

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

Closed
NickUfer opened this issue Dec 3, 2024 · 1 comment · Fixed by #1090
Closed

Inconsistent use of int and size_t in C Bindings for std::vector Types #1089

NickUfer opened this issue Dec 3, 2024 · 1 comment · Fixed by #1090

Comments

@NickUfer
Copy link
Contributor
NickUfer commented Dec 3, 2024

The types ManifoldCrossSectionVec, ManifoldPolygon, ManifoldPolygons and ManifoldManifoldVec represent std::vector in the C bindings. However, their API functions mix the use of size_t and int for lengths and indices. For example:

ManifoldCrossSectionVec:

size_t manifold_cross_section_vec_length(ManifoldCrossSectionVec *csv); // uses size_t
ManifoldCrossSection *manifold_cross_section_vec_get(void *mem, ManifoldCrossSectionVec *csv, int idx); // uses int

SimplePolygon:

size_t manifold_simple_polygon_length(ManifoldSimplePolygon *p); // uses size_t
ManifoldVec2 manifold_simple_polygon_get_point(ManifoldSimplePolygon *p, int idx); // uses int

ManifoldPolygons:

size_t manifold_polygons_length(ManifoldPolygons *ps); // uses size_t
ManifoldSimplePolygon *manifold_polygons_get_simple(void *mem, ManifoldPolygons *ps, int idx); // uses int

ManifoldManifoldVec:

size_t manifold_manifold_vec_length(ManifoldManifoldVec *ms); // uses size_t
void manifold_manifold_vec_set(ManifoldManifoldVec *ms, int idx, ManifoldManifold *m); // uses int
ManifoldManifold *manifold_manifold_vec_get(void *mem, ManifoldManifoldVec *ms, int idx); // uses int

Problem

The use of int for indices is problematic because int cannot represent the full range of values that size_t can. This mismatch can lead to:

  • Potential bugs: Negative or out-of-range values for indices.
  • Type safety issues: Implicit conversions and sign mismatches.
  • Platform-specific inconsistencies: Differences in the size of int and size_t across platforms.

Suggested Solution

All int parameters used for indices in the API should be updated to size_t to ensure consistency with the std::vector representation and avoid these issues.
Would be open to contribute a fix 😄

@pca006132
Copy link
Collaborator

Yeah, this is probably some old code we forgot to change to size_t. PR is welcomed.

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 a pull request may close this issue.

2 participants
0