4 ms·
I'm surprised it doesn't do length-prefixed strings with a null terminator anyways. Makes a whole lot of things easier.
by TheLoneWolfling 11y ago
I'm surprised it doesn't do length-prefixed strings with a null terminator anyways. Makes a whole lot of things easier.
- KMag 11y agoAs a general principle, bugs are reduced when you use representations that don't allow inconsistent representations. Unless you have an overriding reason, it's best to use a data representation that doesn't allow non-canonical representations (if such exists). If you null-terminate a length-prefixed string, what if there's a null in the middle of the string? (1) Allow inconsistency, and go with the length prefix in case of inconsistency. You could allow null bytes in the middle, treating it as a normal length-prefixed string, but then why do you null-terminate the string? (Is it so that you can still pass the string to functions that will choke on embedded nulls? Why would you do that?) This is just asking for kernel bugs. (2) Allow inconsistency and go with the position of the first null byte in the case of inconsistency. If the length prefix is inconsistent with the position of the first null byte, you could go with the position of the first null byte, but then why even have the length prefix? (3) Disallow inconsistency. You could disallow embedded nulls, but then the length prefix is just there as a place to cache strlen calls? If you're defining a syscall interface and requiring the length and first null to be consistent, then you need to run strlen anyway in order to sanity check what userspace gave you... why not simplify the external interface to just be either null-terminated or length-prefixed?
- TheLoneWolfling 11y agoI should have specified a little more, perhaps. Don't think of it as a null-terminated + length-prefixed string. It's effectively a purely length-prefixed string. There just happens to always be a null one byte after the end of the string. The length prefix is the only thing you use, ordinarily. The only time the null comes into play is if you've already had a bug. Think of it like a stack protector.
- KMag 11y ago> Think of it like a stack protector. ... but one that doesn't terminate execution, but instead hides your bugs. In most use cases, I'd prefer to find my bugs in the majority of cases, rather than to hide the bugs except for corner cases.