Bernhard R. Link: C Code to avoid
One of the bad aspects of the the C Programing language is that it silently
allows many bad C programs. Together with the widespread use of an architecture
that is very bad at catching errors (i386), this sometimes leads to common
ideoms that are only working accidentially. This is bad as they often break
on other architectures and can break with every new optimisations or new feature
the compiler adds.
Take for example a look at the code there (I tried to leave a comment there but did not succeed):
If you see something like this:
char buffer[1000];
struct thingy *header;
header = (struct thingy *)buffer;
then it is time to run. I hope you do not depend on this software,
because it is a pure accident if this is doing anything at all.
While you can cast a char * to a struct, that is only allowed
if that memory actually was this struct (or one compatible, like a struct
with the same initial part and you are only accessing that part).
In this case it is obviously not (it's just an array of char),
so you might see bus errors or random values if the compiler does no
optimisations and you are on a architecture where alignment matters.
Or the compiler might optimize it to whatever it wants to, because the
C compiler is allowed to do everything with code like that.
The next problem is the one that post was about: You are not allowed
to access an array after its end. Something like
struct thingy
int magic;
char data[4];
means you may only access the first 4 bytes of data.
If you access more than that it may work now on your machine,
but it can stop tomorrow with the next revision of the compiler
or on another machine.
If you have a struct with a variable length data block, then you
can use the new C99 feature of char data[] or the old
gcc extension of char data[0]. Or you can use unions.
(Or in some case use the behaviour of structs with the same initial
parts).
If you use C code with undefined semantics then every new compiler
might break it with some optimisation.
There is often the tempting option of just using a slightly different
code that currently works. But in the not too distant future the compiler
(or even some processor) might again get some new optimisations and the
code will break again. Fixing it properly might be harder but it's less
likely it will fail to compile again and it also reduces the chances that
it will not fail to compile but simply do something you did not expect.
I've recently spent five days debugging a problem in
As I'm not currently working on any suitable paper, I'll just post this to my blog so it does not completely slip off my radar ;-) Also, it might be interesting to my reader. Readers? Oh, there are two of you now? Good!
Yesterday, I learnt
Sometimes,
I have an essay in the latest
Error -36 seems to be a generic I/O error in OSX and you can find many hits when you do a search in your favorite search engine. The Apache logs report lots of these lines:
I know ecologists often come with great ideas how to save some random resource. There was a "green" font, which contained holes and was supposed to save ink/toner from printers. It turned out not to be that big saver as the font itself was actually a bit wider than usual...
Today I got to know about "new" file format called
The kernel needs to be compiled with CONFIG_USB_SERIAL_CONSOLE=y
Debian kernels do not appear to be. Somewhat strangely, the config option
doesn't even wind up in the config file in the typical commented-out manner,
so you don't even know the option exists until you discover it from random
Web searching, and then looking at drivers/usb/serial/Kconfig in
the kernel source.
Hace ya alg n tiempo escrib sobre el uso de
i suspect a lot of people are used to forwarding TCP sockets with SSH -- for example, to connect locally to a mysql daemon that runs only on the loopback interface of a remote machine (this is debian's default