News for hackers. Stuff that matters.
Re:A Tale of Two Libcs by rightbyte on Saturday September 26, 2020 @05:44AM (#24597554)
I guess the glibc complexity with a LUT is to support non-ascii character sets? It seems quite silly to compare a general solution to a hard coded special case.
Re:A Tale of Two Libcs by buster on Saturday September 26, 2020 @05:59AM (#24597612)
I think one major point is the unreadable mess, though. Is there a reason for this?
Re:A Tale of Two Libcs by johannes1234321 on Saturday September 26, 2020 @08:15AM (#24598232)
There are a number of reasons:

- Support for weird compilers

- Focus on performance, thus playing "weird" tricks with macros, lookup tables etc.

- especially in headers: protect against users doing crazy sh*t

- standards sometimes change or see extensions

- certainly more :)

First two items can be re-evaluated form time to time, but especially GNU often aims to run "anywhere"

The third item comes from the fact that users can create macros for many things and only some names (leading _ and capital letter or __) are reserved for standards or C++ allowing to overload the comma operator, which can have weird effects. Thus to be conformant to the standards some ugliness is required.

The fourth point combined with different compatibility requirements requires tricks to switch between different implementations based on compiler flags/macros (for instance `#define _GNU_SOURCE` or `#define _POSIX_C_SOURCE=...`)

Re:A Tale of Two Libcs by rightbyte on Saturday September 26, 2020 @06:04AM (#24597631)
Any big standard lib I laid my eyes on (gcc, clang, MS headers) seems to be a mess. I too wonder if the complexity is really necessary, but I guess so? Macros in my experience is ofent to fool the compiler to generate better code. Maybe that is a legacy reason?
Re:A Tale of Two Libcs by ThreeFx on Saturday September 26, 2020 @08:04AM (#24598160)
99% of the time the answer is speed. It's just way faster to evaluate and build all of this at compile time than at runtime.

I understand both sides of the argument here - on one hand you have fast and unreadable code and on the other hand slower and more readable code. The problem here is 1. the leaky abstraction of characters and code points and 2. loss of type safety due to C's weak typing rules, which is a bit lost in the "hurr durr messy code."

Re:A Tale of Two Libcs by DaiPlusPlus on Saturday September 26, 2020 @08:09AM (#24598190)
Windows.h is still full of obsolete ancient macros that can’t be removed for compatibility. Things like references to near and far pointers (still!) and how every Win32 function with string params is a macro for the real A/W-suffixed functions, and all the A-suffixed functions now intentionally crash on post-98 systems.
Re:A Tale of Two Libcs by tjalfi on Monday September 28, 2020 @12:27AM (#24612954)
> all the A-suffixed functions now intentionally crash on post-98 systems.

Do you have a source for this claim?

I find it hard to believe that Microsoft would drop support for ANSI applications.

Re:A Tale of Two Libcs by birb007 on Saturday September 26, 2020 @06:10AM (#24597661)
If that were the case, wouldn't a developer either be using uchar types or wchar_t which have their own specific variants of character related functions?
Re:A Tale of Two Libcs by masklinn on Saturday September 26, 2020 @06:18AM (#24597683)
No? Extended-ascii does not require wchar (which is, incidentally, utter dreck) and isalnum is defined as taking

> an unsigned char or […] EOF

so isalnum is "their own specific variants of character related functions" for uchar, and locale-aware.

Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @06:41AM (#24597771)
But what the duck? How does that make sense for Chinese?

This is bullshit. Great, now isalnum returns true for ä if I'm using de_DE.latin1 or whatever, but is still nonsense for any non-western language. And the price we pay for that is a messed up pile of garbage that nobody understands and can segfault.

Re:A Tale of Two Libcs by masklinn on Saturday September 26, 2020 @06:52AM (#24597817)
> But what the duck? How does that make sense for Chinese?

It doesn't.

> This is bullshit.

Welcome to POSIX locales.

> Great, now isalnum returns true for ä if I'm using de_DE.latin1 or whatever, but is still nonsense for any non-western language.

Technically it's already nonsense for western languages as ISO-8859 is long outdated, and any codepage other than ISO-8859-1 will not fit inside a uchar when decoded from UTF-8. Also there were non-western languages which fit in 8-bit encodings for which isalnum would work fine.

> And the price we pay for that is a messed up pile of garbage that nobody understands and can segfault.

Welcome to POSIX locales. And also C, where not reading and understanding the implication of every word in the specification means you're bad and therefore deserve everything you get. In this case, POSIX clearly specifies that input values outside of EOF or "unsigned char" is UB.

Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @06:59AM (#24597851)
:-) I'm a C dev. Systems mostly but I still hate locales.
Re:A Tale of Two Libcs by viraptor on Saturday September 26, 2020 @09:35AM (#24598729)
Re:A Tale of Two Libcs by bonzini on Saturday September 26, 2020 @06:50AM (#24597809)
It's not just Western languages. It's how text was encoded until the 90s and early 2000s for pretty much every language except Chinese, Japanese and Korean (maybe Thai, I am not sure). That's the "famous" CJK acronym for languages that really did require double byte character sets. For example Russian, Greek, Hebrew, and Indic languages all have alphabets that will fit very easily in 128 codepoints (Vietnamese too, which is Latin but with a large number of diacritics).
Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @06:55AM (#24597828)
You're right. I faintly remember these times. Japanese even had an 8 bit charset that simply dropped kanji, iirc.

But all the platforms that use glibc have moved to utf8 over a decade ago. So even for western languages that code doesn't make sense anymore.

Re:A Tale of Two Libcs by bonzini on Saturday September 26, 2020 @06:58AM (#24597843)
Not sure, I still see mail that is not UTF-8 encoded, probably some clients are "optimizing" if they only see an occasional accented letter. And for non-Latin languages UTF-8 is pretty much a two-fold increase in size so there may still be usage of single-byte character sets; legacy DBCS aren't entirely dead in China and Japan either.
Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @07:10AM (#24597902)
The default locale for pretty much any language on most Linux distros is utf8 at least. Legacy systems exist, as you point out especially in those CJK countries, I guess the transition to Unicode was much more annoying there. But incidentally those old MBCS charsets are those where isalnum et al never made sense in the first place.

I'd argue the usefulness of all that glibc code is very close to zero. Sure, the glibc folks can pat themselves on the back for covering the POSIX spec so well but I really prefer the Linux approach here; follow POSIX where it makes sense but omit the insanity.

Re:A Tale of Two Libcs by bonzini on Saturday September 26, 2020 @07:15AM (#24597918)
They would pat themselves on the back for writing useful code when it was useful. They didn't write it in 2010, it dates back to the 80s. The comparison to Linux also makes little sense; talking about locales, Linux still supports Microsoft code pages for the FAT file system.
Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @07:44AM (#24598032)
Then it is time to consider removing this code. If you need it use an old glibc.

Your comparison to Linux makes no sense either. It's not that the code pages themselves are the problem, you can use them for conversion and whatnot. But pretending a collection of functions that is unable to do anything meaningful in the present day and crashes for extra points is fine, because it was written long ago just isn't. The musl solution is correct. If you need to handle strings in a locale-aware manner, use a dedicated lib. Better yet don't use C.

Re:A Tale of Two Libcs by bonzini on Saturday September 26, 2020 @10:01AM (#24598918)
With musl, an Italian person wouldn't be able to use "grep -w ciò" and a Greek person wouldn't be able to use "grep -w" on Greek text at all. So okay it's not a big deal, a Thai person cannot use "grep -w" on Thai text either, but the musl solution is correct for specific use cases where locale doesn't matter.

And the crash is a problem with C but it's not a problem with the function. There's a different function, wisalnum, that doesn't crash with out of bounds values, but it also doesn't do Unicode on musl.

Re:A Tale of Two Libcs by iforgotpassword on Saturday September 26, 2020 @11:04AM (#24599346)
grep -w is pretty useless either way. Ok I might sound like I just try to brush off the issue here but it's not intuitive anyways that it needs to know the locale to properly match in the first place. The Italian person might be caught off guard when trying to find a Swedish word in a file, because you need to remember to switch the locale first. And if grep actually uses isalnum for -w then the Italian and Greek and Thai people will be out of luck either way since they all use utf8 today and ò is two bytes. Good look if you work with a multi-language file and have to constantly switch. But that's not even a problem specifically with POSIX or glibc but language in general; there are languages that don't have any separators between words so you already lost.

The crash is a problem with the function, specifically with glibc. You might even put into question why POSIX defines out of range values as UB instead of requiring it to return false but that's yet another can of worms we better not open here. I fully blame not doing any range checks and doing an oob array access on glibc. UB doesn't force you to create an implementation that crash and burns... Returning false would be great. Maybe abort if you're anal. Have it return something random if you're concerned about speed. But don't freaking crash! The libc should work with the dev, not against them.

Re:A Tale of Two Libcs by birb007 on Saturday September 26, 2020 @06:35AM (#24597746)
Fair point