6 ms·
It's been closer to 20 years since I last read a complete program in x86 assembly, so this is quite fun to look at. I'm somehow disappointed (quite unreasonabl
by pavlov 12y ago
It's been closer to 20 years since I last read a complete program in x86 assembly, so this is quite fun to look at.
I'm somehow disappointed (quite unreasonably, of course) that the code uses plain old zero-terminated C strings instead of something more exotic. One of the fun things about assembly is that you get to reinvent basic language features on the fly -- calling conventions, data layout, strings, everything.
- JoshTriplett 12y agoIt 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.
- maguirre 12y agoOut of curiosity What would you have done for strings?
- FreeFull 12y agoCould have a pointer, length pair. That's how it's done in some non-C languages.
- bkeroack 12y agoPointer, length, encoding. In 2015, giving me a bag of bytes labeled "string" is about as meaningful as saying it's "music" or "a picture".
- vardump 12y agoIt's 2015. UTF-8 won.
- kinghajj 12y agoIf only Java and NT would get the message...
- danbruc 12y agoUTF-8 is not well suited for a general purpose string implementation because it is a variable length encoding and therefore addressing a character becomes a linear time operation. UTF-16 would probably be a better choice in most cases.
- pjc50 12y agoIt needs to be security-hole compatible.