10 ms·
Well, the wider problem then is using C.
by eru 3y ago
Well, the wider problem then is using C.
- ricardo81 3y agoAs a user posting from a Linux machine, I disagree. Though it seems the "don't use C" crowd often delegate the important decisions to somewheres else. I guess the answer is "some people's C is good enough, but not yours"
- tremon 3y agoIf the problem is "you're using nul-terminated strings" as the GP said, then "don't use C" a good step towards fixing that problem, no?
- ricardo81 3y agoPerhaps, but also realistic to accept that you're using code where other people do/have and that the same logic would apply to them.
- jchw 3y agoYou only have to care about it at boundaries though, for the most part. Like, when calling a C API. That's easy to handle. Even C++'s std::string can do that, as the c_str method always returns a null-terminated string. That inherently kills the need for things like strcat.
- lelanthran 3y agoThe return from c_str cannot be used everywhere you would normally use a null terminated string, because the return is const. For example, you couldn't pass it to strtok, or any other function that needs to even temporarily modify the string.
- jchw 3y agoWhile this is true, passing a string to a C function that is manipulating the string would defeat the point of not using C string manipulation.
- AstralStorm 3y agoYou may not know the function is doing C string manipulation, since const correctness in APIs is not a 100% thing.
- jchw 3y agoIf it's just incidental mutation that is a concern, rather than intentionally mutating C strings, no problem: it is common-place to defensively clone strings and other memory when passing them to untrusted interfaces. In fact, if this is your fear, you have literally no alternative but to do so, even when programming directly in C. Then again, if there's no contract for who owns or mutates a given piece of memory, there's no safe way to use said API from any language or environment and you should probably stop using it. Failing that, you'd just have to check the source code and find out what it actually does and hope that it does not change later. (Of course, this has no bearing on whether or not you should use C strings or C string manipulation: You shouldn't, even if you're touching unsafe APIs. It's extremely error prone at best, and also pretty inefficient in a lot of cases.)
- ricardo81 3y agoTurtles all the way down isn't it? At some point, someone has to take responsibility.
- jchw 3y agoLet me reframe this. What we're saying to do is stop using C string manipulation such as strcat, strcpy, etc. Particularly, I'm saying simply don't use C-style null terminated strings until you actually go to call a C ABI interface where it is necessary. The argument against this is that you might call something that already internally does this, to your inputs directly, without making a copy. Yes, sure, that IS true, but what this betrays is the fact that you have to deal with that regardless of whether or not you add additional error-prone C string manipulation code on top of having to worry about memory ownership, mutation, etc. when passing blobs of memory to "untrusted" APIs. It's not about passing the buck. Passing a blob of memory to an API that might do horrible things not defined by an API contract is not safe if you do strcat to construct the string or you clone it out of an std::string or you marshal it from Go or Rust. All this is about, is simply not creating a bigger mess than you already have. Okay fine, but what if someone hates C++ and Rust and Go and Zig? No problem. There are a slew of options for C that can all handle safer, less error-prone string manipulation, including interoperability with null-terminated C strings. Like this one used in Redis: https://github.com/antirez/sds https://github.com/antirez/sds And on top of everything else, it's quite ergonomic, so it seems silly to not consider it. This entire line of thinking deeply reminds me of Technology Connection's video The LED Traffic Light and the Danger of "But Sometimes!". https://youtube.com/watch?v=GiYO1TObNz8 https://youtube.com/watch?v=GiYO1TObNz8 I think hypothetically you can construct some scenarios where not using C strings for string manipulation requires more care, but justifying error prone C string manipulation with "well, I might call something that might do something unreasonable" as if that isn't still your problem regardless of how you get there makes zero sense to me. And besides, these hypothetical incorrect APIs would crash horrifically on the DS9K anyways.
- flohofwoe 3y agoPretty much all operating system APIs use C-style zero-terminated strings. So while C may be historically responsible for the problem, not using C doesn't help much if you need to talk to OS APIs.
- deleted 3y ago[deleted]
- GoblinSlayer 3y agoI suspect null terminated strings predate C, C is just just one of many languages that can use them.
- masklinn 3y agoThe PDP-10 and PDP-11 assemblers had direct support for nul-terminated strings (ASCIZ directives, and OUTSTR in MACRO10) which Ritchie adopted as-is, not unlike Lisp’s CAR/CDR. It’s not entirely clear that other “high-level” languages at the time also used such a type. Although later ISA added support for it for C compatibility, whereas older ISAs tended to only support fixed-length or length-prefixed, for instance the Z80 has LDIR, which is essentially a memcpy, copying a terminated string required a manual loop.
- wruza 3y agonot using C doesn't help much if you need to talk to OS APIs This means cdecl, stdcall or whatever modern ABIs OSes use, not C. Many languages and runtimes can call APIs and DLLs, though you may rightfully argue that their FFI or wrappers were likely compiled from C using the same ABI flags. But ABI is no magic, just a well-defined set of conventions. And then, no one prohibits to use length-aware strings and either have safety null at the end or only copy to null-terminated before a call. Most OS calls are usually io-bound and incomparably heavy anyway.
- joosters 3y agoThe problem is, a null-terminated string is a very simple concept for an ABI. A string with a length count seems simple, but there is a big step up in complexity, and you can't just wistfully imagine effortlessly passing String objects around to your ABI. For a start, String objects are going to be different everywhere. Even in C++, one library's String object isn't going to be binary compatible with another. How is the data laid out, does it do small string optimisation, etc? Are there other internal fields? So you won't be passing objects around. At the ABI, you'll have to pass a pointer and a length. Calling an ABI will involve unwrapping and wrapping objects to pretend you are dealing with 'your' strings. Simple C-style ABIs make memory management straightforward (n.b. but error-prone, and certainly not easy). If this new style ABI returns a 'string' (pointer and length) of some sort, you have to package it up in your own object format, and manage the memory. Will you need an extra object type to represent 'string I got from an ABI, whose memory is managed differently'? None of these are insurmountable, but they are a complexity that is rarely thought of when people declare 'C style ABIs are terrible!'
- LanternLight83 3y agoThis thread reminds me of the essay, "Some were meant for C" https://www.humprog.org/~stephen/research/papers/kell17some-preprint.pdf https://www.humprog.org/~stephen/research/papers/kell17some-...
- heisenbit 3y agoC the needle contaminated now often with deadly RCE virus. Historically it was used to inject life into the first bytes of the twisted self perpetuating bootstrapping chain of an eco system dominating today the planet and the space around it.
- bayindirh 3y agoAll processors are C VMs at the end of the day. They are designed for it, and it's a great language to access raw hardware and raw hardware performance. I still fail to label C as evil. P.S.: Don't start with all memory management and related stuff. We have solutions for these everywhere, incl., but not limited to GCs, Rust, etc. Their existence do not invalidate C, and we don't need to abandon it. Horses for courses.
- chriswarbo 3y ago> All processors are C VMs at the end of the day. That would be a poor argument back in the 80s; and is increasingly wrong for modern processors. Compiler intrinsics can paper-over some of the conceptual gap, but dropping down to inline assembly can't be entirely eliminated (even if it's relegated to core libraries). Lots of C code relies on certain patterns compiling down to specific instructions, e.g. for vectorising; since C itself has no concept of such things. C is based around a 1D memory model which has no concept of cache hierarchies. C has no representation of branch prediction, out-of-order instructions, or pipelines; let alone hyperthreading or multi-core programming. After all, if processors were "C VMs", then GCC/LLVM/etc. wouldn't be such herculean feats of engineering!
- bayindirh 3y agoThis is a subject I love to discuss. Exactly. C is based around 1D memory, has no understanding of caches. All of your other arguments are true, too. This is why most of the things; caches, memory hierarchies and other modern things are hidden from C (and other languages, or software in general) itself, to trick C, and make it think it's still running on a PDP-11. All caches (L1, L2, L3, even disk and caches, and various caches built in RAM) are handled by hardware or OS kernels themselves. Unless they provide an API to talk with, they are invisible and untouchable, unmanageable, and this is by design (esp. the ones baked into hardware like Lx and other buffers). All the compilers are the interface perpetuating this smoke and mirrors to not upset C about its assumptions about the machine underlying itself. Even then, a compiler can only command the processor upto a certain point. You can't say that I want these in caches, and evict these. These are automagic processes. Exactly, because of these reason, CPUs are C VMs. They do work completely different than a PDP-11, but behave like one at the uppermost level, where compilers are the topmost layer in this toolchain. Compilers are such a herculean feats of engineering, because we need to trick that the programs we're building, to make them think they're running on a much simpler hardware. In turn, hardware tries hard to keep this management ovherhead handled by compilers at a bare minimum while allowing higher and higher performance. More ponderings, and foundation of my assertion is here: https://dl.acm.org/doi/10.1145/3212477.3212479 https://dl.acm.org/doi/10.1145/3212477.3212479 Paper is titled: C Is Not a Low-level Language: Your computer is not a fast PDP-11.