| ▲ | Linux eliminates the strncpy API after six years of work, 360 patches(phoronix.com) |
| 132 points by simonpure 8 hours ago | 109 comments |
| https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin... |
|
| ▲ | WalterBright 3 hours ago | parent | next [-] |
| "The strncpy function within the Linux kernel has been a "persistent source of bugs" for years due to counter-intuitive semantics and behavior around NUL termination along with performance issues due to redundant zero-filling of the destination." Huh. Whenever I've been asked to review C code, I always looked for strncpy and always found a bug with it. |
|
| ▲ | lambdaone 4 hours ago | parent | prev | next [-] |
| This sort of boring grind is where the real work of systems engineering is done. Big infrastructure projects like this work on making the Linux kernel more reliable while still keeping it workable throughout the process move on the scale of decades, not months. |
|
| ▲ | cm2187 25 minutes ago | parent | prev | next [-] |
| A lot of pain and suffering to avoid having a string datatype. |
|
| ▲ | twothreeone 2 hours ago | parent | prev | next [-] |
| wow, very humbling. I'm actually amazed how many people contributed to this. It's easy to get attribution for "cool new features", but arguable removing bad features is even more important for something as fundamental as the kernel. Cudos! I'm sure these are the sorts of things that will go down as folklore from the "founding ages", when everyone will have forgotten how to understand source code in 50 years and the Claude/Codex cruft just silently keeps piling on and burning the majority of our planets energy. |
|
| ▲ | DerSaidin 34 minutes ago | parent | prev | next [-] |
| strtomem_pad seems redundant with memcpy_and_pad, and also it requires the preprocessor:
https://github.com/torvalds/linux/blob/1a3746ccbb0a97bed3c06... I was curious: Why have it, instead of just using memcpy_and_pad? AI's answer (paraphrased) was
* Avoid possible bugs from manually write sizeof(dest)
* Enforces the __nonstring Attribute
* signals: "I am converting an actual C-string into a fixed-width legacy memory field." vs copy binary data & pad it. Interesting to learn about the __nonstring attribute: https://github.com/torvalds/linux/blob/1a3746ccbb0a97bed3c06...
https://github.com/search?q=repo%3Atorvalds%2Flinux+__nonstr... |
|
| ▲ | mrlonglong 7 hours ago | parent | prev | next [-] |
| the zero terminated string is I think is computing's biggest mistake. Pascal style strings were much safer. |
| |
| ▲ | BobbyTables2 14 minutes ago | parent | next [-] | | Partly agree but there would have been squabbling on the data type of the size, unless it was variable length. The latter would have had other issues too. For a while, 16bit would probably have seemed too extravagant. Now 32bit would probably seem too small. For a “strongly typed” language, C is pretty damn loose where would have mattered. | | |
| ▲ | poly2it 6 minutes ago | parent [-] | | No, there would not have been and this is most likely not the reason. ptrdiff_t exists for precisely this use case. It has existed since C89. |
| |
| ▲ | smackeyacky 2 hours ago | parent | prev | next [-] | | Zero terminated strings were the basis for an awful lot of useful software. Calling them the biggest mistake in computing is a bit OTT. I haven’t programmed anything Pascal related for 30+ years but I dimly remember thinking at the time that I wished the string system wasn’t so hard to use. | | |
| ▲ | asdfasgasdgasdg 2 hours ago | parent | next [-] | | That useful software would not have been less useful if the strings in it were represented as size + buf. | |
| ▲ | ComputerGuru an hour ago | parent | prev [-] | | That argument isn’t valid. The argument would be “this string design enabled a whole lot of useful software” but that’s a different matter. (And it could very well be the case.) |
| |
| ▲ | dmazzoni 40 minutes ago | parent | prev | next [-] | | 255 characters ought to be enough for everybody, right? | |
| ▲ | bsder 5 hours ago | parent | prev | next [-] | | Zero terminated string is a special case of sentinel value termination. And sentinel value terminations make a lot of sense when you have punch cards and fixed length records that you need to carve into pieces. Nobody expected any decisions they were making in the 1960s and 1970s to have any bearing on computing a half-century later. They all expected to have their mistakes long papered over by smarter people at some point. But we ALL make the mistake of underestimating inertia. | |
| ▲ | layer8 3 hours ago | parent | prev | next [-] | | Almost as bad as newline-terminated lines. ;) | |
| ▲ | jackbucks 7 hours ago | parent | prev | next [-] | | It was definitely an interesting way to allocate pointers.
I did once have a very large project where devs didnt understand this and resolved hundreds or more off by one and memory overwrites in C due to this feature. But at the same time, I think blaming the software was kind of a cop out. Devs were in a hurry and simply didnt respect the rules. Given todays software engineer at large. Nerfing programming languages so they cant destroy things might not be a bad idea. But AI will nerf everything. | | |
| ▲ | fragmede 7 hours ago | parent [-] | | why is AI gonna nerf everything? sure it could be used as the easy button, but I just spent two hours this morning learning about the neuroscience of how memory works in the brain that I didn't mean to and now I want to run studies on how memory works. Why do you assume that AI is gonna nerf everything? | | |
| ▲ | AnimalMuppet 5 hours ago | parent [-] | | AGI might. AI? No way. See, AI was trained on existing data - on all that existing C code out there (sure, and also on all the papers and articles saying what was wrong with that C code). Those bugs are in the training data, and often not marked as bugs. So when AI generates C code, is it going to avoid making the mistakes that human code made? No, it's going to generate the kind of code it was trained on. How could it be otherwise? That's not going to nerf anything. | | |
| ▲ | deathanatos 3 hours ago | parent | next [-] | | > See, AI was trained on existing data - on all that existing C code out there (sure, and also on all the papers and articles saying what was wrong with that C code). Those bugs are in the training data, and often not marked as bugs. So when AI generates C code, is it going to avoid making the mistakes that human code made? No, it's going to generate the kind of code it was trained on. How could it be otherwise? The generalization of this is why I think all these AI companies writing blog posts where the marketing department is just jer—ranting endlessly about how AI will improve itself into the singularity is just crazy talk. They generate a random statistically likely output, and the most statistically likely output is mid. Exceptional outputs — the ones that wow us or move the needle are exactly that, unlikely. AGI is sci-fi, and LLMs will not change that. You can see the same effect when AI emits bash, too, and especially so since most bash is terrible, and most users of bash do not put in the effort to learn bash and its foibles. So it outputs what most people write, which is not great. | | |
| ▲ | AnimalMuppet 3 hours ago | parent [-] | | It still could happen, if they had a way to judge the exceptional outputs from the mid and terrible ones. But I'm not sure they have that... | | |
| ▲ | ComputerGuru an hour ago | parent [-] | | In far from an AI fanatic, but I would argue training it on GitHub PRs and general software patches already provides that. Instead of just seeing the static snapshot it sees “this code was replaced by this (hopefully better) code” |
|
| |
| ▲ | CamperBob2 3 hours ago | parent | prev [-] | | When's the last time you saw a decent coding model create a buffer-overflow bug while trying to use C strings? Serious question. Anyone else seen this happen in the last 12-18 months? If so, which model and version were you using? | | |
| ▲ | smackeyacky 2 hours ago | parent | next [-] | | I had Claude write a bit of stupid C# the other day that had an off by one string truncate. Surprised the hell out of me. | |
| ▲ | macintux 3 hours ago | parent | prev | next [-] | | Would you even know? Serious question. The volume of code the models can produce, the subtle ways these bugs can manifest (or even only manifest when under attack), it seems like they would be easy to overlook. | | |
| ▲ | CamperBob2 3 hours ago | parent [-] | | I have a habit of getting GPT 5.5 to review everything Opus writes for me, and vice versa. The model in the reviewer role frequently finds things I overlooked myself. Occasionally in parts of the code I wrote. No modern LLM has found any buffer overflow bugs in parts of my code that originated from another LLM. Again, though, they have found one or two that were my fault. |
| |
| ▲ | krupan an hour ago | parent | prev [-] | | How many people are writing C code with LLMs? I get the impression it's mostly JavaScript web apps | | |
|
|
|
| |
| ▲ | msla 6 hours ago | parent | prev | next [-] | | In addition to having to pick a size for the length counter and then, later, having to differentiate between lengths in bytes, codepoints, and glyphs, you can't subdivide a Pascal string using pointer arithmetic. To pass just the end of a string into a function, you have to either copy the tail of one Pascal-style string to another with a smaller size value, or your string has to be a struct with an integer and a pointer to the actual data instead of just an integer stuck on the beginning of the string. The first is a lot of copying in some cases, the second raises the specter of structs with invalid pointers. That's not to mention the potential problems that would cause with caches. | | |
| ▲ | cornholio 5 hours ago | parent | next [-] | | You can have a universal variable length field, for example 2 bytes for strings < 32768, then four bytes, 8 bytes etc. On the critical short string path, it costs just a single bit test. The glyph vs byte issues need to be dealt with in both formats. The subdivision issue is a good perspective, but i would argue the performance impact of cloning substrings is dwarfed by the redundant full string reads to find length. | |
| ▲ | estebank 5 hours ago | parent | prev [-] | | The third option is to have a variable width length: the top most bit signals whether the next byte corresponds to the length or to the start of the string. |
| |
| ▲ | dietr1ch 7 hours ago | parent | prev | next [-] | | I think it was NULL itself. It was a long way until we realised we don't want invalid values and could use the type system to help us use special values safely. | | |
| ▲ | jkrejcha 5 hours ago | parent | next [-] | | The problem here is that null kinda is consequential of intentional design of the type system itself. In this way, I do think that null was discovered, rather than invented. Remember, C is a kinda "portable assembler" so the constructs in it are based relatively closely to how low level data structures are mapped out in memory. This is, and continues to be, an incredibly useful feature that makes C and C structs immensely useful concepts. Part of that does need an invalid value[1]. NULL is convenient for this and although there are some very weird JavaScript-trinity-meme-style consequences for this[2], it's such a useful concept that basically all languages that have the ability to construct pointers have a null pointer[3]. The alternative world looks like everyone inventing their own invalid values. Invalid, non-null, pointers are typically MUCH worse than null pointers for debuggability and security. If you unintentionally read/write/execute memory at 0x0 (by far the most common value for NULL), most operating systems will trap this, whereas may not necessarily if 0x12345678 is your invalid value. [1]: Stuff like IA64 had NaT bits which were effectively an extra bit for what I assume to be this sorta thing. The problem with this is that it costs an extra bit. I don't really know much about IA64, but presumably [NaT 1] + [don't care] would be your null pointers here. I think? [2]: Really what the standard, in my opinion, should have done is probably not make use of the null pointer UB for many different functions. A lot of compilers took the UB surrounding that to make incredibly dubious "optimizations" that broke stuff with zero actual performance benefit whatsoever [3]: Yes, even Rust. Although some (again in my opinion) unfortunate design decisions made it so that C-Rust FFI isn't zero cost because of how it treats spans/slices | |
| ▲ | atherton94027 4 hours ago | parent | prev | next [-] | | Genuinely curious, how would you handle cases where a value is unset without NULL? This is a legitimate case that happens a lot in eg data modeling | | |
| ▲ | clnhlzmn 3 hours ago | parent | next [-] | | The way we do it in modern languages with things like std::optional and even that is not the best example. | | |
| ▲ | MBCook 3 hours ago | parent [-] | | And higher level languages that works. But what do you do when you get down to low level C or assembly? You basically end up with null/0 don’t you? |
| |
| ▲ | pdimitar 3 hours ago | parent | prev | next [-] | | Sum types, of course. | |
| ▲ | jibal 2 hours ago | parent | prev [-] | | They already said: > use the type system to help us use special values safely ... but this is not the place to explain what a type system is or what sum types/maybe/optional/etc. are. |
| |
| ▲ | bellowsgulch 5 hours ago | parent | prev | next [-] | | Compared to scripting languages with actual tagged types, C doesn't really have a type system, and that's readily apparent to anyone who has written C in the last 43 years and debugged a program written in it. C pretends types exist with you, but once bytes hit the road, it's all real-life and segmentation faults. | | |
| ▲ | AlotOfReading 2 hours ago | parent [-] | | C actually does have a type system and it's one of the bigger issues with the language. If it didn't, unaligned pointers and signed overflow would be totally fine. |
| |
| ▲ | jkercher 6 hours ago | parent | prev [-] | | Meh, I think NULL is fine in C. It's an extra, valid state to represent pointers at no cost. Unlike the more hand holdy languages, it's quite rare for a pointer in C to have the ability to be NULL since, more often than not, it's pointing at something known. It's actually quite rare to see NULL checks unless it's API code or something like that. I can see this being more of a problem in a managed language where anything can be NULL at any time. | | |
| ▲ | bvrmn 5 hours ago | parent | next [-] | | NULL as a concept is fine. Inability to declare something as non-null is not. There is a huge gap between developer expectation "it's pointing at something known" and hard reality confirmed by zillions of CVE. That's the reason optionality is prevalent in modern languages and type checkers (python, typescript), nowdays even Java has sane non-nullable types. | |
| ▲ | kelnos 6 hours ago | parent | prev | next [-] | | > to represent pointers at no cost I wouldn't call "cause of bugs and security issues" "no cost". > it's quite rare for a pointer in C to have the ability to be NULL As a C programmer for more than 25 years, that is the exact opposite of my experience. | |
| ▲ | none_to_remain 4 hours ago | parent | prev | next [-] | | Struct foo has various members, including a bar*. But a foo may or may not be associated with a bar. If there's no associated bar, the bar* pointer is NULL. Seen and done this all the time | |
| ▲ | UqWBcuFx6NV4r 5 hours ago | parent | prev | next [-] | | This precise mindset is why the world has suffered for decades (wrt security/integrity/availability) at the hands of what can only be described as an industry led by completely unjustified male confidence.
Why are there still people fighting the “it’s not that bad, guys! you’ve just got to be a good developer like ME!” fight? | | |
| ▲ | IgorPartola 5 hours ago | parent [-] | | Is None OK in Python? NULL in C just doesn’t belong at the end of a string. But IMO having a “there is no value here” designation is not a bad thing. | | |
| ▲ | none_to_remain 4 hours ago | parent | next [-] | | I think you're mixing up the NULL pointer and the NULL (sometimes NUL) character. | |
| ▲ | jibal 2 hours ago | parent | prev [-] | | Python is interpreted so None is always tested for and will throw an exception if used in the wrong context. This is quite different from a SEGVIO. > But IMO having a “there is no value here” designation is not a bad thing. Sure ... if it's done via the type system so that errors are caught at compile time. There's a reason that modern languages all either do this or are moving towards doing it. (And a reason that C programmers have no idea what we're talking about when we refer to type systems.) > NULL in C just doesn’t belong at the end of a string. Different discussion. (And NUL, not NULL.) |
|
| |
| ▲ | XorNot 5 hours ago | parent | prev [-] | | The problem with let's get rid of NULL is that it's a real, required state. The vast majority of computing is actually not binary: any real input generally has at least 3 possible states: not set, true and false. In practice really 4 because "indeterminate" is a reasonable error condition you'd like to know about. And it keeps increasing anyway: e.g. not set has subcategories: not set due to lack of user input, not set because we're loading state from the backend etc. NULL is the first expression of that basic problem: it's definitely not enough to eliminate NULL because the first thing which happens is your non pointer default value takes it's place. | | |
| ▲ | lambdaone 4 hours ago | parent [-] | | What you are describing is option types, which are an entirely valid and very useful construct that helps make programs more rather than less reliable. But you need proper language type system support and compile-time enforcement to make it work, and C does neither of those. | | |
| ▲ | bnolsen 3 hours ago | parent [-] | | C++ and rust make these optionals ugly. Zig does it right. Zig also forbids null pointers and requires use of optionals. |
|
|
|
| |
| ▲ | fragmede 5 hours ago | parent | prev | next [-] | | compared to Von Newman versus Harvard architecture for LLMs? I think that's a far bigger mistake. | |
| ▲ | themafia 7 hours ago | parent | prev [-] | | > Pascal style strings were much safer. The limitations were brutal. Initially you could only have 255 bytes in a string. The length of a string and the size of the allocation are now separate and you may need to think about that unused memory in your design. The problem now doubles with the introduction of UTF-8. Your string size is in bytes and you need to track characters separately. If you want to create an array of strings you either need to specify the length of all strings and accept the memory overhead or have an array of pointers to strings. If you use an array of pointers you may end up choosing to use the 'nil' value as a sentinel that means "end of list." So we're right back where we started. -- Because someone decided to downvote this HN has limited the speed at which I can reply. This site is tragic and I'm fully done with it now. You can spread propaganda and poorly sourced zeitgeist and be among friends but if you try to have a genuine conversation about programming languages you are made to be unwelcome immediately. Screw this. -- > No other data structure works like this. The linked list. > You can't mess this up in an array C happily decomposes arrays into pointers. You can erase your length information from the type. This was an intentional decision. > Strings are the only data structure that assume there will be a NULL at end. Which is why almost every string API has a version that allows you to specify the maximum length. The fact that you can use a NUL doesn't mean you have to. Which is why the concept of "sentinel values" is broadly used in many types of applications you haven't considered here. | | |
| ▲ | dare944 an hour ago | parent | next [-] | | > You can spread propaganda and poorly sourced zeitgeist and be among friends but if you try to have a genuine conversation about programming languages you are made to be unwelcome immediately. Indeed. And the ignorance of computing history in this discussion is particularly disturbing. The context of this particular thread is "zero terminated string is ... computing's biggest mistake". This completely ignores the situation on the ground when C was developed. At the time, people were striving for a system programming language that sat above the level of assembly but was compact enough to run within the limited resources of the then emerging mini-computer systems. The PDP-11 on which C was developed was certainly not the first mini-computer, but it was among the earliest to have a regular enough instruction set and addressing model to make a general purpose, high-level system's language possible. These systems were extremely limited in memory; the PDP-11's instruction set is limited to directly addressing at most 64KiB (code and data) and many systems of the era were hardware limited to less than that. (Indeed, I regularly run an early version of Unix, including an early C compiler, on my PDP-11/05 which is maxed out at 56KiB [of actual core]). There was no way that even a brilliant engineer like Dennis Richie was going to be able to shoe-horn in "optional" types, or the mechanics of length-value strings into a compiler that has to run in such limited space, and produce code (e.g. the Unix kernel) that has to run in even less. The fact that strings and arrays are thin abstractions on top of pointers is both a brilliant compromise in design as well as a nod to then-prevalent assembly practice. It was the exactly kind of pragmatic decision that was needed to move computing along at the time. Of course the designs from this era are antiquated now. But they were not mistakes. | |
| ▲ | BigTTYGothGF 5 hours ago | parent | prev | next [-] | | > Your string size is in bytes and you need to track characters separately No worse than C strings then. | |
| ▲ | AlienRobot 6 hours ago | parent | prev [-] | | >The problem now doubles with the introduction of UTF-8. Your string size is in bytes and you need to track characters separately. That isn't really a problem. The problem with null-terminated strings is specifically what happens when you reach the end of the allocated array and there ISN'T a NULL character. Every string function is designed to keep going until it finds the NULL character, so if a hacker gets rid of the NULL character, he can exploit pretty much any standard string manipulation function being used elsewhere in the program to manipulate whatever memory comes AFTER the string data structure. No other data structure works like this. You can't mess this up in an array, because no function that manipulates arrays is just going to keep going until there is a null. That would be stupid because it would require users of the function to add a NULL to the end of their arrays before passing it to the function, so instead we just pass the size of the array to everything. Strings are the only data structure that assume there will be a NULL at end. By the way, I read once that if you use UTF-32 every code point will be 4 bytes, constantly, but even then a single code point isn't necessarily a single character. Text is just complicated. | | |
| ▲ | tredre3 6 hours ago | parent [-] | | > No other data structure works like this. In C most data structures work like this, you keep going until you find NUL (character) or NULL (pointer). E.g. Strings, array of pointers, linked lists, etc. Of course you can add length to most of those, but it isn't the canonical/traditional way of doing things. | | |
| ▲ | AlienRobot 5 hours ago | parent [-] | | That can't be true. If you have an array of pointers it can be terminated in NULL. But an array of integers can't have a NULL value, since NULL would probably be just 0 which is a normal integer. The null in a linked list is the null in the .next field, right? That's the way you would implemented linked lists independent of language. It's not the .value that is null. A string is an array of characters (well, for characters representable in one byte at least) that has a specific value to represent the end of string. It would be like if Int::MAX was reduced by 1 to make space for an Int:NUL constant that represented the end of an integer array. Or if you were creating your own ENUM, let's say for NORTH, SOUTH, EAST, WEST, and you added a fifth enumeration called Direction.NUL for use in arrays. | | |
|
|
|
|
|
| ▲ | senfiaj 5 hours ago | parent | prev | next [-] |
| I wonder, why not use a string buffer paired with its length? For example, maybe use struct that has char pointer, and 2 ints (occupied length + total buffer length). Almost like c++'s std::string. This null terminator thing really sucks, it's potentially insecure and often unperformant. |
| |
| ▲ | WalterBright 3 hours ago | parent | next [-] | | Wonder no longer! https://dlang.org/spec/arrays.html#dynamic-arrays and https://dlang.org/spec/arrays.html#strings and for C: https://digitalmars.com/articles/C-biggest-mistake.html | |
| ▲ | maxlybbert 39 minutes ago | parent | prev | next [-] | | It's definitely possible. And common, at least in some projects. The only real drawback is that sloppiness will lead to multiple slightly different nonstandard string types in the same project. | |
| ▲ | bnolsen 3 hours ago | parent | prev | next [-] | | That's called a fat pointer. Null terminated c strings is the majority of memory errors out there. | |
| ▲ | GalaxyNova 5 hours ago | parent | prev | next [-] | | Yes I have seen it happen a few times with `strlen` being called in a loop silently causing O(N) to turn to O(N^2) | | |
| ▲ | jkrejcha 3 hours ago | parent | next [-] | | Reminds me of an article[1] that described how he cut GTA Online loading times by 70% because strlen was getting called for effectively every character in a string [1]: https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times... | | |
| ▲ | sweetjuly 3 hours ago | parent [-] | | I remember reading this blog post when it was first published, but the subsequent updates are better than I would've ever expected this to turn out. Worth checking it out again if you've seen it before :) |
| |
| ▲ | senfiaj 4 hours ago | parent | prev | next [-] | | Exactly, you can't write clean concise code when working with c strings. Almost every c string manipulation requires cognitive load: "Is the buffer size enough (including null terminator), should I reallocate it?", "I need to have the offset from the last concat, to make next concats performant", "Umm, shold I put null terminator at i or i + 1?"... It really sucks, it's akin to death by thousands of cuts. | |
| ▲ | sgerenser 3 hours ago | parent | prev [-] | | Joel Spolsky coined the term “Shlemiel the Painter’s Algorithm” for this type of thing back in 2001: https://www.joelonsoftware.com/2001/12/11/back-to-basics/ |
| |
| ▲ | chiph 5 hours ago | parent | prev | next [-] | | Pascal did/does this, but eventually someone wants a string longer than the size portion can handle. Or wants the number of characters not the number of bytes. | | |
| ▲ | jerf 3 hours ago | parent | next [-] | | I wasn't a programmer in these days, so I don't know if there's some other major concern that would kill this, but I sometimes wonder about whether we could have / should have used variable-length integers. That is, something like, 0-127 byte strings get their length prefixed, 128 - 16383 get two bytes of prefix, and the probably-rare 16384 - 2097151 strings would end up with three, though proportionally by that point it's hardly anything. Or you could use the UTF-8 mechanism for packing the bytes, though that costs more and probably doesn't get anything we'd care about in the 1980s or 1990s. It's a bit of extra code, yes. Not necessarily all that much, but some. On average it is only slightly more expensive than null termination, and considered as a proportion of the size of the strings themselves it's hardly anything. It's probably better than the strings getting hard-limited to 0-255, though, which was quite frequently a user-visible quirk. | |
| ▲ | Johanx64 2 hours ago | parent | prev [-] | | Dude, every sane language out there does this. Just generally with 4byte prefix. Null-terminated stuff has always been backwards compat stuff. Pascal strings - historically and why people even remember this being an issue - were up to 255 chars in size, if not you had to use different string type. You might still want raw pointers for all sorts of low level stuff, but you almost never want to have null-terminated strings for anything but back-compat, one of the worst things ever, even on memory constrained systems. |
| |
| ▲ | none_to_remain 5 hours ago | parent | prev | next [-] | | The size overhead of that is 2*sizeof(int) while the overhead of null termination is sizeof(char). If I remember the standard right, the former is worse by at least sizeof(char), and usually more in practice. This used to matter, sometimes still does. | | |
| ▲ | kgeist 4 hours ago | parent | next [-] | | I would assume the difference is mostly negligible in practice due to the allocator rounding up the allocated memory size at least by the word size anyway (for alignment and simpler bookkeeping). You can also use variable-length encoding in the header to use 1 byte for most cases, similar to how UTF-8 does it: if the most significant bit is not set, we assume a 7-bit encoding, which can represent string lengths up to 127 using 1 byte, which is probably 99% of strings. | |
| ▲ | senfiaj 4 hours ago | parent | prev [-] | | Well, not saying to always use it, but if the string size is big enough, the overhead of 2 ints becomes relatively vanishing. For generic dynamically sized strings it probably has more advantages than disadvantages. But in any case, sure, if every single byte matters or some structure requires specific memory layout, then fine. I just don't think these things are the majority of use cases. Keep in mind that the cached lengths can increase performance, since you don't have to recalculate string lengths. |
| |
| ▲ | MBCook 3 hours ago | parent | prev [-] | | A lot of them are strings coming from or going to user space right? So wouldn’t you have to do constant conversions? |
|
|
| ▲ | D-Coder 4 hours ago | parent | prev | next [-] |
| Note that "360 Patches" is 360 uses of strncpy that have been removed, not necessarily bugs. |
| |
| ▲ | dpark 20 minutes ago | parent [-] | | I would imagine 360 patches removed way more than 360 uses of strncpy. But yeah, it’s not a given that each of these patches addressed a bug. (Also not a given that there were only 360 bugs fixed.) |
|
|
| ▲ | PlunderBunny 7 hours ago | parent | prev | next [-] |
| I worked on a Win32 app that used space-padded strings, i.e. the destination string was padded with spaces, but there was still a null on the last byte. You had to use special versions of the string functions for length, copy etc. I’m not sure why this was - the source base was so old it might have had its origins in Pascal struct behaviour. |
| |
| ▲ | jkfkfkj 6 hours ago | parent | next [-] | | It can perhaps be due to the string originating from a sql database ”char” field, I.e. not ”varchar”. Char fields in databases are space padded. | |
| ▲ | bebe83939 7 hours ago | parent | prev | next [-] | | Perhaps prevent realocation when string size changes? Or aligning cpu cache lines? | |
| ▲ | egorfine 7 hours ago | parent | prev [-] | | I think this behavior has its roots in COBOL, not pascal. | | |
| ▲ | kps 5 hours ago | parent [-] | | Which has its roots in punch cards, where pre-computer hardware operated on fixed-sized fields and an unpunched column is equivalent to a space. |
|
|
|
| ▲ | devsda 3 hours ago | parent | prev | next [-] |
| Did anybody else misunderstand the title as removing strncpy func for linux users ? For a moment, I misunderstood it as (g)libc removing strncpy and was worried about the trouble its going to cause. |
|
| ▲ | jibal 2 hours ago | parent | prev | next [-] |
| The purpose of strncpy, which was originally part of the UNIX kernel code, was to copy file names to and from directory entries that consisted of a 2 byte inode number and a 14 byte zero-padded but not zero-terminated name field. I started warning my colleagues against using it the moment I saw it for the first time about 50 years ago. |
| |
| ▲ | dare944 an hour ago | parent [-] | | strncpy appears somewhere around the Unix v7 time frame, however only as function in the standard C library. It is not used in the v7 kernel itself. |
|
|
| ▲ | naturalmovement 6 hours ago | parent | prev | next [-] |
| A reminder that we've had strlcpy[1] for ~ 30 years but it was never accepted into the Linux world because of typical petty open source bullshit. This is why we can't have nice things. [1] https://man.openbsd.org/strlcpy |
| |
|
| ▲ | qarl 3 hours ago | parent | prev | next [-] |
| Am I going to be the first person to ask this after five hours? Really? Wouldn't this work be extremely easy to implement with an LLM coder? |
| |
| ▲ | qustio 2 hours ago | parent [-] | | I don't think the bottleneck was that it took six years to Ctrl-F strncpy and type in new code for each file. | | |
| ▲ | qarl 2 hours ago | parent [-] | | It's a shame you're misrepresenting what is actually going on. In another comment here I explained that I have run a test: asking Claude Code to add a substantial feature to 270 different C programs. Despite your beliefs - it went extremely well. | | |
| ▲ | qustio an hour ago | parent [-] | | Huh, are you confusing me with someone else? I don't doubt Claude Code did that, I do the same for refactors all the time. But xscreensaver theme tweaks for personal use have a much lower standard for quality control, regression testing, side effects, etc than a kernel used by billions of devices with thousands of interconnected drivers and subsystems. Not to mention the coordination problem to get every maintainer on board and patches approved for each specific area when working on a project of that scale, even for a relatively narrow change. Claude Code doesn't really help with that so don't see why the expectation would be a significant speed up (and doing it all in a single patch would definitely be rejected). | | |
| ▲ | qarl an hour ago | parent [-] | | Yes, I understand the difference in rigor. I refuse to believe the six year delay here was getting people to test a patch. Which, actually, Claude Code will also do quite well. | | |
| ▲ | qustio an hour ago | parent [-] | | Not sure why you'd refuse to believe that when a single, simple patch in Linux can take months to make it into a kernel release. Here we're looking at 300 patches scattered throughout a kernel with millions of LoC. That's going to translate to a lot of mailing list back and forth even if every change was accepted on the first try without a fuss. | | |
| ▲ | qarl an hour ago | parent [-] | | The lag there is not due to the review time. How many maintainers were involved? 300? Because I'm still finding it hard to understand how the work of 300 people handling 300 commits cannot be parallelized into months (per your own stat.) | | |
| ▲ | qustio 40 minutes ago | parent [-] | | To be clear my original statement was that the bottleneck was most likely not mechanical code changes (where CC would have the most direct speedup) but everything else involved in the process (testing, discussion/approval, inclination towards caution, etc). Not that the Linux kernel approval procedures couldn't be streamlined, work couldn't be parallelized, or anything else like that, which would be a different discussion entirely. You made the claim that Claude Code could have massively sped up the process, so the burden of evidence here should be on how specifically these patches would have benefited/time saved from using LLMs. Hand wavingly saying "LLMs = faster" is too vague and broad of a claim without providing any evidence. | | |
| ▲ | qarl 25 minutes ago | parent [-] | | Right. And what I'm saying is I refuse to believe the Linux kernel approval procedures are that inefficient. Therefore, your belief "bottleneck was most likely not mechanical code changes" is most likely incorrect. It would be interesting to get the actual answer to this question. EDIT: Substantially changing your argument after posting isn't nice. |
|
|
|
|
|
|
|
|
|
| ▲ | Animats 3 hours ago | parent | prev | next [-] |
| This is a job for Claude! What happens if you turn a job like that over to Claude Code? A mess? Good results? Code bloat? Worth trying on existing C programs. |
| |
| ▲ | qarl 3 hours ago | parent [-] | | I ran a test where I added a "light" mode to xscreensaver: unique changes to over 270 different C programs. It mostly did an amazing job in a short period of time. EDIT: Of course I get downvoted for saying this. HN isn't interested in reality any more. | | |
| ▲ | krupan an hour ago | parent | next [-] | | These stories with no details and no proof are not interesting or helpful. | | | |
| ▲ | ninjin 2 hours ago | parent | prev [-] | | > Of course I get downvoted for saying this. HN isn't interested in reality any more. I suspect that rather many of us are simply just tired of Claude and friends getting shoehorned into any conversation about programming at this point. It is about as fun as the Rust Brigade entering any discussion about C. It adds nothing new to the discussion and it is frankly tiring since we pretty much at any time have a handful of conversations on the front page already covering "AI" topics anyway (counting four at the time of writing this). | | |
| ▲ | qarl an hour ago | parent [-] | | Well - except in this conversation it's incredibly relevant. It took six years to do this work when the work is likely mostly mechanical and could have been done much more quickly and safely with an automated system. I thought automation would be interesting to HN - given the context and the fact it was not used. | | |
| ▲ | krupan an hour ago | parent [-] | | An LLM is not a mechanical automated system. A deterministic search and replace would be a mechanical automated system. Clearly it wasn't that simple of a problem though. | | |
| ▲ | qarl an hour ago | parent [-] | | > An LLM is not a mechanical automated system. Pretty sure that's exactly what LLMs in coding harnesses are. |
|
|
|
|
|
|
| ▲ | larodi 7 hours ago | parent | prev [-] |
| Wonder when is someone going to brave and fork the linux kernel and try to ffwd it with automatic programming. |
| |
| ▲ | fragmede 7 hours ago | parent [-] | | why would you start there instead of creating something from scratch ?if you can port drivers just as easily meaning you don't especially give a shit about hardware you're running on in the first place, why even deal with linux? The battle tested LRU cache system? | | |
| ▲ | literalAardvark 6 hours ago | parent | next [-] | | It's much easier to use something with all the edge cases already handled as a starting point. | |
| ▲ | convolvatron 6 hours ago | parent | prev [-] | | I've seen several workalike kernels in various stages of completion. at least one of them was able to run some pretty substantial applications (Postgres, nginx, that kind of thing), and that is still I guess around 250kloc. but it only really has drivers to support hypervisor devices. unfortunately as time goes by, the linux api surface gets larger and more convoluted. so there's going to be some coverage you're just never going to get. but in the abstract, definitely. linux is so bloated at this point that its not clear that it can ever be 'made safe'. |
|
|