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

CLAY__STRING_LENGTH internal macro uses incorrect formula #189

Open
cD1rtX3 opened this issue Jan 13, 2025 · 3 comments
Open

CLAY__STRING_LENGTH internal macro uses incorrect formula #189

cD1rtX3 opened this issue Jan 13, 2025 · 3 comments

Comments

@cD1rtX3
Copy link

cD1rtX3 commented Jan 13, 2025

CLAY__STRING_LENGTH is defined as:

#define CLAY__STRING_LENGTH(s) ((sizeof(s) / sizeof((s)[0])) - sizeof((s)[0]))

However, this is incorrect. It technically works under most circumstances, which is why it's gone unnoticed, however, this returns the wrong length for non-UTF-8 strings. Looking at the corrected macro makes it clear why:

#define CLAY__STRING_LENGTH(s) ((sizeof(s) / sizeof((s)[0])) - 1)

Instead of simply subtracting the teminating null after calculating the array length, the current definition overthinks things and instead subtracts the size of the characters in bytes.

While this admittedly isn't a very critical bug, it's still a silly one, it stil actually cropped up in my environment somehow, and it is a one-line fix.

@cD1rtX3
Copy link
Author

cD1rtX3 commented Jan 13, 2025

This cropped up because I was porting Clay to UTF-16 to work faster with Direct2D.

@nicbarker
Copy link
Owner

Thanks for finding this one! We have some work to do around utf16 and 32 handling, should come later this week 🙂

@cD1rtX3
Copy link
Author

cD1rtX3 commented Jan 13, 2025

For now, should I drop the version I'm using here? All I really did was change all the chars to wchar_ts along with a few other minor tweaks to get it working, but it does work.

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

No branches or pull requests

2 participants