News for hackers. Stuff that matters.
Re:I'm Not a Fan of Strlcpy(3) by Pesthuf on Monday July 15, 2024 @09:10AM (#40967574)
And that's why I'm not a fan of C for writing any kind of program: Every time you need to touch a string, which is an effortless task in any semi-modern language, you aim a trillion footguns at yourself and all your users.

It's also pretty telling that every article that tries to explain how to safely copy or concat strings in C, like this one, only ever works with ASCII, no attempt whatsoever to handle UTF-8 and keep code points together, let alone grapheme clusters. No wonder almost all C software has problems with non-English strings...

Re:I'm Not a Fan of Strlcpy(3) by Murk on Monday July 15, 2024 @10:14AM (#40968084)
In my opinion, and as a general rule. You can't really ever truncate a user facing string correctly because it depends on language specifics. Hence it is my suggestion not to truncate user facing strings - and in fact you may want to consider them as binary blobs. On the other hand, as a matter of practicability sometimes you may have to, but certainly avoid doing so before it hits the presentation layer, and know that it may not be language correct.

There are many strings that are not user facing, which you expect to be of a certain nature, e.g. ASCII based protocols, and therefore you know what to do with them.

So the multi byte situation and strcpy, or std::string, or any other "standard" string function isn't really relevant as it's some other libraries duty.

the task of truncation and otherwise formatting UI strings is the preserve of the rendering layer(s).

Re:I'm Not a Fan of Strlcpy(3) by hgs3 on Monday July 15, 2024 @12:22PM (#40969223)
Yes, truncating a user facing string requires more consideration regardless of programming language. For example do you truncate at the grapheme, word, sentence, new line, paragraph, or something else? How do you indicate truncation to the user, with an ellipsis perhaps? If you use an ellipsis is it appended to the original string or drawn by the GUI toolkit?

Note that the Unicode grapheme cluster, word, sentence, and line break algorithms are locale specific. Now consider how often programmers casually truncate strings, even in high-level languages, without accounting for the locale.

Re:I'm Not a Fan of Strlcpy(3) by eadmund on Monday July 15, 2024 @10:05AM (#40968014)
> I'm not a fan of C for writing any kind of program

C is okay — not fan-worthy, but okay — for one specific kind of program: a mostly-portable implementation of a real language in which to write every other kind of program. that’s not nothing: implementing a backend for every CPU-OS pair one wants to support is a pain, and a different skillset from writing, say, a web browser or text editor.

I wonder how much C has held back the development of computing.

Re:I'm Not a Fan of Strlcpy(3) by shermantanktop on Monday July 15, 2024 @11:04AM (#40968497)
C is fine. Yes, it has undefined behaviors and encourages subtle thinkos. But it’s a tool and sometimes it’s the right tool.

It’s the C hackers who insist on using it for inappropriate applications who create the problems.

Anything string-heavy and internationalized is a terrible place for C, even if you have spent 30 years dancing through the C minefield and are sure you’ll get it right this time.

Re:I'm Not a Fan of Strlcpy(3) by fl0ki on Monday July 15, 2024 @03:15PM (#40970654)
The suckless.org initiative is a great example of this. Boasting about how certain software inherently sucks less because it has minimalist C code, rather than having defense-in-depth against security holes.

Newsflash, a few decades ago the majority of software was the minimalist C that they claim sucks less. There are many reasons we moved on from that world, security firmly among them.

Re:I'm Not a Fan of Strlcpy(3) by Zambyte on Monday July 15, 2024 @11:13AM (#40968582)
> I wonder how much C has held back the development of computing.

Lisp is older than C. It didn't just hold computing back, it actively walked backwards.

Re:I'm Not a Fan of Strlcpy(3) by hifromwork on Monday July 15, 2024 @11:06AM (#40968524)
Well, C doesn't really have strings, just pointers. Calling them "strings" is just an abstraction for us. They don't even have to be null-terminated (even though by convention they often are)!

And this is precisely what I want, in some cases. I use C when I need a low-level byte wrangling code that tightly interfaces with the operating system, or when I can't afford to allocate memory at will (like all effortless string-handling languages do under the hood), or when I don't want any runtime and need my code to behave like a shellcode, basically, or...

I guess C is still used because people find it useful. No need to be a fan of it.

Re:I'm Not a Fan of Strlcpy(3) by f1shy on Monday July 15, 2024 @11:25AM (#40968681)
> I can't afford to allocate memory at will

This was my mantra for the last 30 years. But now, even the tiniest thing has 1GB, linux and virtual memory. Except for very edge cases, this excuse is rapidly dying

Re:I'm Not a Fan of Strlcpy(3) by cess11 on Monday July 15, 2024 @11:46AM (#40968912)
When I browse my local electronics web shops I see a lot of devices that couldn't boot a Linux kernel, some of which I wonder whether I could drive with a small consumer grade solar panel.
Re:I'm Not a Fan of Strlcpy(3) by mrkeen on Monday July 15, 2024 @02:53PM (#40970438)
But the waste adds up. There are a few projects at work whose integration tests I can't run on my work machine because it doesn't have enough ram (16GB) so I run them on my home PC instead. And that's even when I close IntelliJ, my web browsers, and kill all the background processes I can.
Re:I'm Not a Fan of Strlcpy(3) by ben_bai on Monday July 15, 2024 @07:27PM (#40972379)
by convention: if it's not NUL terminated, it's not a c-string.
Re:I'm Not a Fan of Strlcpy(3) by mrkeen on Monday July 15, 2024 @11:01AM (#40968468)
In defence of C, it should be oblivious to different string encodings (and I thought it was).

Coreutils should continue to do their job, and not need to be recompiled against whatever encoding is in fashion.

Re:I'm Not a Fan of Strlcpy(3) by johnnyanmac on Monday July 15, 2024 @11:32AM (#40968764)
The standards, as usual for C, are paper-thin, doesn't even guarantee ordinal value for the characters specified (except 0-9) despite implementing strcmp. But GCC/MSVC do add some options on top of the standard, which includes some attempt at interpreting different encodings.

Then again, I was surprised to find support for literal encodings in C23. So perhaps my knowledge was even more outdated than I anticipated.

Re:I'm Not a Fan of Strlcpy(3) by _gabe_ on Monday July 15, 2024 @10:59AM (#40968456)
> It's also pretty telling that every article that tries to explain how to safely copy or concat strings in C, like this one, only ever works with ASCII, no attempt whatsoever to handle UTF-8 and keep code points together, let alone grapheme clusters.

Can you expand on this? Why does it matter to keep code points and grapheme clusters together in the case of truncation? If you’re already truncating the string, then you can just copy as many bytes as possible. Then, later when you interpret that string, you’ll hit a malformed codepoint and ignore it. I guess what you might be getting at is that if you have a codepoint sequence, then you shouldn’t copy the bytes if they can’t all fit in the truncated string?

I feel like this is an edge case and not “the reason almost all C software has problems with non-English strings”. 99% of the time, copying up until the null byte is fine, whether or not the string is UTF-8 or ASCII. The reason most C software doesn’t work with non-English strings is because the developer never added support. The bytes are still there, they just need to be interpreted correctly in the UI portions of the code.

Re:I'm Not a Fan of Strlcpy(3) by Retr0id on Monday July 15, 2024 @11:11AM (#40968565)
Truncating mid-codepoint produces an invalid utf8 sequence, which some decoders will silently ignore or replace with <?> glyphs, and others will fail loudly.

Truncating mid-grapheme-cluster can change semantics in unexpected ways. For example, the "family" emoji(s) can be encoded as a set of conjoined codepoints for each family member: https://stackoverflow.com/questions/49958287/printing-family.... Depending on where you truncate, you might just get the father, which probably won't cause you any serious issues but it might cause confusion depending on context.

(IMHO, if you're truncating a way where this would matter, you should probably be truncating in screen-space, i.e., way higher up the stack)

Re:I'm Not a Fan of Strlcpy(3) by _gabe_ on Monday July 15, 2024 @11:33AM (#40968774)
Yep, this all makes sense and is what I was thinking as well. I also think you’re right about truncation being a better idea in the rendering state rather than in the bytes themselves. Either way, truncation of strings being displayed to the user requires extra care no matter what language you’re using.

As far as this being the reason most C code doesn’t handle UTF-8 though, I’m still skeptical.

Re:I'm Not a Fan of Strlcpy(3) by wruza on Monday July 15, 2024 @11:31AM (#40968760)
Not that C was wise with strings, but slicing strings in general is a bad idea unless you’re doing it strictly at ascii borders, e.g. to capitalize windows disk letter or to split by cr?lf. Modern text is yet another “falsehoods programmers believe in” material.
Re:I'm Not a Fan of Strlcpy(3) by taeric on Monday July 15, 2024 @10:02AM (#40967994)
Honestly, any language where you are manipulating strings is a footgun. Especially if you are manipulating text, and not character data. You can avoid many errors, but almost certainly not all. And as soon as you have someone that thinks they can piecewise localize any text, heaven help you.
Re:I'm Not a Fan of Strlcpy(3) by bruce343434 on Monday July 15, 2024 @10:04AM (#40968009)
With the up and coming browser LLM API, you could just have the LLM translate everything on the frontend. One of the few tasks that LLMs are consistently good at.
Re:I'm Not a Fan of Strlcpy(3) by taeric on Monday July 15, 2024 @10:12AM (#40968062)
I feel this successfully upgrades a gun into a canon?
Re:I'm Not a Fan of Strlcpy(3) by TeMPOraL on Monday July 15, 2024 @10:29AM (#40968192)
A modern artillery gun, where each shell costs upwards of $1K apiece, and lord have mercy on you if it ever goes off inside the barrel.
Re:I'm Not a Fan of Strlcpy(3) by layer8 on Monday July 15, 2024 @11:16AM (#40968606)
LLMs are not “consistently good” at translating UI text, because a proper translation requires understanding the application and the application context of each piece of text.
Re:I'm Not a Fan of Strlcpy(3) by colejohnson66 on Monday July 15, 2024 @12:27PM (#40969254)
Yes. Context is very important. Otherwise, you end up compressing files into a "postcode file".[0]

[0]: https://news.ycombinator.com/item?id=36231313

Re:I'm Not a Fan of Strlcpy(3) by smaudet on Monday July 15, 2024 @10:46AM (#40968333)
> like this one, only ever works with ASCII, no attempt whatsoever to handle UTF-8

I mean if your viewpoint is to handle heavy UIs then yes maybe C is not your go-to language. However, there are other solutions to the character space problem besides UTF-8. Rather than complaining that your screwdriver can't bang nails, perhaps try using a screw?

No, you won't be able to support a language like chinese within a 256 bit charset, but perhaps it's not important to in all situations. The strength of ascii is that its small, simple. If that's too anglo-centric for you, maybe there's a better symbolic tiny charset that is more applicable. I'd support something like https://lojban.io/ becoming more prevalent, but there could be advantages to having a simple(r) symbolic transmission language not burdened with anglo-centric concepts.

Re:I'm Not a Fan of Strlcpy(3) by anamexis on Monday July 15, 2024 @10:58AM (#40968442)
Advocating a new global constructed language to accomodate C's shortcomings seems like the wrong direction to be thinking.
Re:I'm Not a Fan of Strlcpy(3) by smaudet on Monday July 15, 2024 @11:03AM (#40968490)
Less a global constructed language, more a "better" encoding. Base64 works really well for arbitrary binary-in-text encoding, for instance.
Re:I'm Not a Fan of Strlcpy(3) by estebank on Monday July 15, 2024 @01:21PM (#40969745)
Unicode is that better encoding. The "small and efficient per locale encoding" that you proposed was the status quo, and was an endless source of mojibake. There is a reason we moved away from that.
Re:I'm Not a Fan of Strlcpy(3) by smaudet on Monday July 15, 2024 @02:38PM (#40970320)
I think there is a misunderstanding, which I tried to address but evidentally failed.

UTF-8 is fine for a display encoding. However, not every string encoding need be a display encoding, which the parent post seems to not be considering.

You could also have multiple display encodings, if it makes sense to (a tool only intended for use in a certain part of the world for instance), however that is not what I mean.

Re:I'm Not a Fan of Strlcpy(3) by cyberpunk on Monday July 15, 2024 @09:29AM (#40967730)
I mean just use bstrlib why is it difficult?
Re:I'm Not a Fan of Strlcpy(3) by rwmj on Monday July 15, 2024 @09:35AM (#40967772)
Because no one can agree on which string library / abstraction to use, if they even use one at all which is rare.
Re:I'm Not a Fan of Strlcpy(3) by dark-star on Monday July 15, 2024 @09:45AM (#40967851)
I mean just use C++ w/ std::string why is it difficult?
Re:I'm Not a Fan of Strlcpy(3) by smallstepforman on Monday July 15, 2024 @11:05AM (#40968509)
std::u8string
Re:I'm Not a Fan of Strlcpy(3) by account42 on Monday July 15, 2024 @11:31AM (#40968752)
No, std::string.
Re:I'm Not a Fan of Strlcpy(3) by bluGill on Monday July 15, 2024 @12:57PM (#40969541)
Most C programs compile and work correctly compiled as C++. The exceptions are often places you shouldn't write code like that, sometimes even outright bugs that C++'s stricter type system caught for you.
Re:I'm Not a Fan of Strlcpy(3) by Pesthuf on Monday July 15, 2024 @10:13AM (#40968070)
I'd be happy if developers did that, but they don't:

So many C developer seem to think they're "smart enough" to handle strings correctly using only the standard library, not needing "training wheels" like the plebs - but then where do the countless string related memory safety errors and all the broken Unicode handling come from that plague every C program, no matter how clever the programmers, no matter how tough the review process, no matter how much static analysis is used?

The issue is that many C developers simply can't seem to admit that C string handling, as it was invented many decades ago, was simply fundamentally flawed. So easy to misuse with catastrophic results, even for plain 1-byte-strings. It has nothing to do with how "smart" you are or how careful you are.

Re:I'm Not a Fan of Strlcpy(3) by johnnyanmac on Monday July 15, 2024 @11:38AM (#40968838)
Are there really people defending string manipulation in C? C is a slight abstraction on top of assembly. Assembly speaks in addresses and bytes. It's always been bad at trying to interpret human text because Assembly's job lies in the digital realm.

I think it's more a case that programming is English skewed and localization concerns are rare for the programmers these day who still need to work in the realm of C (so, mostly for older legacy software or the embedded realm).

Re:I'm Not a Fan of Strlcpy(3) by Pesthuf on Tuesday July 16, 2024 @03:17PM (#40979395)
It doesn't matter if anyone is defending it - C developers still use C strings and the broken C standard library's string functions (exclusively) rather than using one of the many good string libraries they have available.

The result is that the same mistakes with string handling get repeated again and again, often with catastrophic results.

Re:I'm Not a Fan of Strlcpy(3) by johnnyanmac on Tuesday July 16, 2024 @11:51PM (#40982391)
Developers do a lot of unoptimal things for unoptimal reasons. Ultimately what's "right" or "wrong" is a case by case basis. So I don't see much point in venting against those developers without knowing their circumstance.

I mostly ask who's defending it for holistic purposes. Right or wrong, It would be nice to hear other perspectives on the matter if possible.

Re:I'm Not a Fan of Strlcpy(3) by bluGill on Monday July 15, 2024 @01:00PM (#40969568)
Smart is not the issue. I'm smart enough, but I'm lazy and forgetful. C makes it really accidentaly once in a while.
Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:15AM (#40967620)
The code in the post does indeed have a bug if the goal is to copy UTF-8 data. However the bug is quite subtle and unlikely to occur in practice, and from your comment I suspect you don’t actually know what that problem is (because you seem to be pointing at a different issue which is actually irrelevant). Maybe you or someone else can describe what the real concern is?
Re:I'm Not a Fan of Strlcpy(3) by keybored on Monday July 15, 2024 @10:14AM (#40968086)
> However the bug is quite subtle and unlikely to occur in practice,

Unlikely to occur? Do you think that will put the naysayers at ease?

> , and from your comment I suspect you don’t actually know what that problem is (because you seem to be pointing at a different issue which is actually irrelevant). Maybe you or someone else can describe what the real concern is?

Amazing that people have the patience for this.

Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Wednesday July 17, 2024 @05:58AM (#40984142)
¯\_(ツ)_/¯
Re:I'm Not a Fan of Strlcpy(3) by Levitating on Monday July 15, 2024 @09:16AM (#40967638)
Partially copying a 2-byte character?
Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:21AM (#40967666)
UTF-8 works fine if you truncate a codepoint because the encoding scheme lets you detect this. The problem is more subtle than that (hint: it involves a 1-byte codepoint).
Re:I'm Not a Fan of Strlcpy(3) by lalaland1125 on Monday July 15, 2024 @09:30AM (#40967738)
Truncating a UTF-8 codepoint is not fine because most software is not tested with partially broken UTF-8 so international users will likely run into many bugs.

Especially because concatenation is a very common operation so those sliced codepoints will be everywhere, including in the middle of text.

Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:39AM (#40967804)
Morally I view “what do I do with my truncated string” to be a separate issue from “how do I truncate the string” as described in the article. Like, yes, you absolutely should not concatenate after doing this operation. But maybe you shouldn’t be showing the user a truncated string either even if it’s all ASCII. The question of “did you make an unparseable UTF-8 string” is answered with “no” and the more complicated but also more interesting question of “did you actually want this” remains unanswered.
Re:I'm Not a Fan of Strlcpy(3) by Levitating on Monday July 15, 2024 @10:25AM (#40968152)
This is fair, the article takes truncating a string to fit in a status bar as an example.
Re:I'm Not a Fan of Strlcpy(3) by actionfromafar on Monday July 15, 2024 @10:35AM (#40968237)
Also consider Unicode is not only international characters, but superscripts and other stuff ♥ᵃ

a: there was a list somewhere over which characters hackernews allows?

Re:I'm Not a Fan of Strlcpy(3) by Retr0id on Monday July 15, 2024 @09:26AM (#40967705)
If you're alluding to NUL, I don't really see the issue?

Yes, many languages allow strings (UTF-8 or otherwise) to contain null bytes, and C's str*() functions obviously do not, but null-termination vs not is an orthogonal issue to ASCII vs UTF-8.

i.e. Yes it's (depending on context) an issue that C str*() cannot handle strings with embedded null bytes, but that's not a UTF-8-specific issue.

Re:I'm Not a Fan of Strlcpy(3) by Pesthuf on Monday July 15, 2024 @09:39AM (#40967802)
A function that can turn a correctly formatted UTF-8 string into a malformed UTF-8 string is, in my opinion, broken.
Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:47AM (#40967864)
One problem here is that the string may not have been a correctly formatted UTF-8 string to begin with. No, not that it can contain any bytes-I mean, it might be ascribed even more than just decoding correctly. Maybe it is supposed to have the grapheme clusters preserved. Maybe the truncation should peel off the last file component because the string holds a path. The operation of “doing a dumb truncation” can be broken if you look at it from plenty of ways, and I don’t disagree with you, but I do want to make clear that the issue isn’t memcpy is breaking it but that if you need x, y, z maybe you’re reaching for the wrong tool. And conversely there is nothing inherently wrong with using it if you are going to use it in a way that is resilient to that kind of truncation.
Re:I'm Not a Fan of Strlcpy(3) by account42 on Monday July 15, 2024 @11:34AM (#40968795)
What about a function that can turn a correctly spelled english sentence into a malformed english sentence? If you truncate to a fixed length this comes with the territory.
Re:I'm Not a Fan of Strlcpy(3) by MathMonkeyMan on Monday July 15, 2024 @09:30AM (#40967743)
The null code point? That would be pedantic even by my standards.
Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:40AM (#40967814)
Look I had to include it or someone is going to do a whole pedantic comment about how C can’t actually represent UTF-8 correctly
Re:I'm Not a Fan of Strlcpy(3) by kstenerud on Monday July 15, 2024 @10:46AM (#40968340)
You could have just said it rather than going through this smug "I know something you don't know!" song and dance.

Also, by this rationale, NO string is ever safe in C, because pretty much every encoding technically supports codepoint 0 (even though you take your life into your hands should you ever use it). This is not a useful discussion.

Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Wednesday July 17, 2024 @05:59AM (#40984154)
I mean strings that don't use that codepoint are fine, and that's most strings as I mentioned above.
Re:I'm Not a Fan of Strlcpy(3) by Sebb767 on Monday July 15, 2024 @10:18AM (#40968108)
Actually, by just alluding to the bug without saying it explicitly, you managed to both be pedantic and not avoid the discussion.

This is not meant as a personal attack; I just want to point out how it looks on a casual reading :)

Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Wednesday July 17, 2024 @05:57AM (#40984134)
Well, I'm not perfect ;)
Re:I'm Not a Fan of Strlcpy(3) by pdpi on Monday July 15, 2024 @10:00AM (#40967974)
By that metric, C can't represent ASCII correctly either, because there's no particular reason you couldn't have a NUL character somewhere inside a string.
Re:I'm Not a Fan of Strlcpy(3) by TeMPOraL on Monday July 15, 2024 @11:17AM (#40968616)
Indeed it can't. Many developers were bitten by this, and still are; plenty of critical bugs and security vulnerabilities rely on this quirk too.
Re:I'm Not a Fan of Strlcpy(3) by account42 on Monday July 15, 2024 @11:37AM (#40968825)
Technically, C can. It's just C strings that are limited.
Re:I'm Not a Fan of Strlcpy(3) by pdpi on Monday July 15, 2024 @11:40AM (#40968854)
Sure, in the exact same way that C can handle unicode just fine too. The problem is, as always, C strings.
Re:I'm Not a Fan of Strlcpy(3) by moralestapia on Monday July 15, 2024 @09:24AM (#40967690)
>hint: it involves a 1-byte codepoint

Which is?

Re:I'm Not a Fan of Strlcpy(3) by saagarjha on Monday July 15, 2024 @09:49AM (#40967880)
Your sibling comments have figured it out if you’ve given up. Yes, it’s kind of stupid