4 ms·
It needs to do so to interoperate with the OS, so using those avoids having multiple conventions and converting between them.
by JoshTriplett 12y ago
It needs to do so to interoperate with the OS, so using those avoids having multiple conventions and converting between them.
- eddyb 12y agoAFAIK Linux doesn't use zero-terminated strings anywhere in its syscalls, or at least not in those like write, where you pass a size alongside the buffer.
- reipahb 12y agoHowever (almost?) all syscalls dealing with filesystem paths take null-terminated strings. See for example the implementation of the open() syscall: https://github.com/torvalds/linux/blob/fb65d872d7a8dc629837a49513911d0281577bfd/fs/open.c#L999 https://github.com/torvalds/linux/blob/fb65d872d7a8dc629837a... (Hence the need for the strncpy_from_user()-function: https://github.com/torvalds/linux/blob/fb65d872d7a8dc629837a49513911d0281577bfd/lib/strncpy_from_user.c#L99 https://github.com/torvalds/linux/blob/fb65d872d7a8dc629837a...)
- eddyb 12y agoAhh, I totally missed the path arguments, my bad.
- ranit 12y agowrite() syscall writes a sequence of bytes (not string) therefore cannot use zero-terminated convention.
- JoshTriplett 12y agoopen takes a NUL-terminated filename.
- JoshTripIsWrong 12y agoNo it doesn't? Networking syscalls generally take length-specified strings. Unless this calls into libc or something...
- dang 12y agoPlease don't make usernames that attack another user. It's uncivil and distracts from the topic.
- TheLoneWolfling 12y agoI'm surprised it doesn't do length-prefixed strings with a null terminator anyways. Makes a whole lot of things easier.
- KMag 12y 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 12y 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.