Skip to content

fix: initialize assignments vector after resize in kkmeans - #3175

Merged
davisking merged 2 commits into
davisking:masterfrom
lippinj:upstream-fix-6
Oct 17, 2026
Merged

davisking merged 2 commits into
davisking:masterfrom
lippinj:upstream-fix-6

Conversation

@lippinj

@lippinj lippinj commented Oct 16, 2026

Copy link
Copy Markdown

resize() allocates but does not zero-initialize the vector, leaving assignments in an indeterminate state before the first iteration. Add an explicit fill to zero.

resize() allocates but does not zero-initialize the vector, leaving
assignments in an indeterminate state before the first iteration.
Add an explicit fill to zero.
Comment thread dlib/svm/kkmeans.h

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Without initialization, UB read here.

@lippinj lippinj changed the title fix(s: initialize assignments vector after resize in kkmeans fix: initialize assignments vector after resize in kkmeans Oct 16, 2026
Discard old scratch labels before resizing and force every sample to count as newly assigned on the first iteration. Cover one-cluster training, repeated and resized training, imbalanced initial assignments, kernel centroids, and iteration budgets.

Copy link
Copy Markdown
Owner

Thanks @lippinj for catching the uninitialized assignments. I changed the initial labels to an invalid cluster index so the first pass always recomputes the centers, including single-cluster and heavily imbalanced cases. I also discard the old scratch labels before resizing and added regression tests for repeated training and iteration limits.

@davisking
davisking merged commit ca06f11 into davisking:master Oct 17, 2026
13 checks passed
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.

2 participants