Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web.
|
2010-10-03 An API mistake Unix has made several timesUnix has generally had decent APIs, but every so often Unix people have been a bit too concerned with minimalism and storing data as efficiently as possible. Several times this has invited in string-related APIs that practically beg for code to make mistakes, because of how they specify string termination. Here is an abstracted example. Consider an API where you use the following structure:
struct dirent {
uint16_t ino;
char name[14];
}
You will notice that there is nothing explicit to tell you how long
the name is. Instead, the API has a rule: the string in You can guess what happens next. Many people who write code that has to
deal with this structure simply use (This is of course yet another example of having to be sure that something actually is a C string, as well as the fact that exceptions are hard for people to remember.) I blame this partly on minimalism because one of the ways to deal with this would have been to make some accessor functions and tell people to always use them. Instead, the structures were simply exported to people directly and every programmer using them had to get the whole access dance correct. This has the minimalism of avoiding an 'unnecessary' and obvious function in the standard library, at the cost of having people get it wrong with reasonable frequency. (Off the top of my head, I believe this mistake was made in at least the original V7 directory format and in some versions of utmp records.) My meta-moral for this is make things in your API that look like C strings actually be C strings. If people can treat them as C strings and have this work most of the time, a significant number of people will treat them as C strings regardless of what you say in your documentation. The corollary is that if you have things that are not C strings, you should consider actively frustrating attempts to use them as such by means like never null-terminating them. If you don't want to do this, make accessor functions that do it right and don't expose the raw structures.
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |