Skip to contentSkip to conversation

Possible solution for min()/max() war

IDEA+RFC David desJardins 1 participant 1 message Sep 3, 2001

lore
Share & tools
lore
#0 lore raw
Quoted 11 lines · Linus Torvalds

For example, let's look at this perfectly natural code:

static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
{
if (len <= sizeof(short) || len > sizeof(*sunaddr))
return -EINVAL;
...

code that is totally correct, and that it would make _no_ sense in writing any other way.

The code is correct, but if one is adding explicit types, for clarity and to avoid introducing bugs, then I think that code like this is exactly where they most belong:

  if ((size_t) len <= sizeof(short) || (size_t) len > sizeof(*sunaddr))

If that prevents one person from later writing buggy code like:

if ((size_t) len <= sizeof(short))

then it's a Good Thing.

-- David desJardins