BUG: NpyString comparison should use memcmp instead of strncmp by ngoldbaum · Pull Request #31662 · numpy/numpy · GitHub
Skip to content

BUG: NpyString comparison should use memcmp instead of strncmp - #31662

Merged
ngoldbaum merged 3 commits into
numpy:mainfrom
ngoldbaum:fix-npystring-comparison
Jun 17, 2026
Merged

ngoldbaum merged 3 commits into
numpy:mainfrom
ngoldbaum:fix-npystring-comparison

Conversation

@ngoldbaum

Copy link
Copy Markdown
Member

strncmp compares C strings, and stops comparisons after hitting an embedded null. We should be using memcmp for our strings, which can contain embedded nulls.

Fixes that and adds tests for comparisons and conversions

I used AI assistance to identify and help fix the bug.

Ping @mhvk since you were involved with the design here. C is hard!

@ngoldbaum ngoldbaum added this to the 2.5.0 Release milestone Jun 16, 2026
@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.strings String dtypes and functions labels Jun 16, 2026

@seberg seberg left a comment

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.

LGTM, can merge if you like with that nit applied (or wait for Marten).

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.

Suggested change
StringDType comparisons now correctly handle embedded NULL bytes.
* StringDType comparisons now correctly handle embedded NULL bytes.

I know, I know...

Comment thread numpy/_core/tests/test_stringdtype.py Outdated

@seberg seberg Jun 17, 2026

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.

Honestly, I would just use a clearly large enough size "S20" or so. (but also fine without)

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good!

@ngoldbaum
ngoldbaum merged commit 87772ec into numpy:main Jun 17, 2026
85 of 87 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jun 18, 2026
charris added a commit that referenced this pull request Jun 18, 2026
BUG: NpyString comparison should use memcmp instead of strncmp (#31662)
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Aug 4, 2026
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug component: numpy.strings String dtypes and functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants