clay icon indicating copy to clipboard operation
clay copied to clipboard

`CLAY__STRING_LENGTH` internal macro uses incorrect formula

Open cD1rtX3 opened this issue 10 months ago • 3 comments

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 avatar Jan 13 '25 00:01 cD1rtX3

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

cD1rtX3 avatar Jan 13 '25 00:01 cD1rtX3

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

nicbarker avatar Jan 13 '25 02:01 nicbarker

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.

cD1rtX3 avatar Jan 13 '25 02:01 cD1rtX3