Re: memory waste in fs/devices.c/kdevname()

Rogier Wolff (R.E.Wolff@BitWizard.nl)
Sat, 19 Dec 1998 15:09:22 +0100 (MET)


Tigran Aivazian wrote:
> Hi,
>
> The current (2.1.131) code is:
>
> char * kdevname(kdev_t dev)
> {
> static char buffer[32];
> sprintf(buffer, "%02x:%02x", MAJOR(dev), MINOR(dev));
> return buffer;
> }
>
> But the maximum length of "%02x:%02x" is 6 bytes (including '\0').
> We can throw in a couple of bytes "for good measure" and have
> static char buffer[8] but why on earth do we declare it as [32]?

Because the programmer didn't want to think one second longer about
wether 6, 7 or 8 would be enough.

This is a good thing. Suppose you declare it [8], and minor/majors get
bumped to 16 bits (*). Now the max length of %02x is not 2, but 4. So
you would be overflowing the buffer (by one byte) when this called
(rare) if you have devices in the 4-hex-digit-range (rare).

In short you'd have a bug that would show up only every once a year
somewhere around the globe..... Trust me, Linux is as stable as it is
because in lots of places we don't save one or two bytes, but take a
safe margin.

I find a comment

/* Currently only 6 bytes of this buffer are really used */

just fine. But please keep the safe margin in the declaration.

(*) In this case, it's pretty realistic that we will bump device
numbers a bunch of bits in the near future. However, you should
be prepared for the unexpected.

-- 
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
*   Never blow in a cat's ear because if you do, usually after three or  *
*   four times, they will bite your lips!  And they don't let go for at  *
*   least a minute. -- Lisa Coburn, age 9

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/