Re: [RFC][PATCH] introduce SIZE_MAX

From: Andrew Morton
Date: Fri Apr 20 2012 - 19:03:37 EST


On Thu, 19 Apr 2012 00:27:51 -0400
Xi Wang <xi.wang@xxxxxxxxx> wrote:

> ULONG_MAX is often used to check for integer overflow when calculating
> allocation size. While ULONG_MAX happens to work on most systems,
> there is no guarantee that `size_t' must be the same size as `long'.
>
> This patch introduces SIZE_MAX, the maximum value of `size_t', to
> improve portability and readability for allocation size validation.
>
> ...
>
> @@ -331,7 +331,7 @@ static int build_snap_context(struct ceph_snap_realm *realm)
>
> /* alloc new snap context */
> err = -ENOMEM;
> - if (num > (ULONG_MAX - sizeof(*snapc)) / sizeof(u64))
> + if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
> goto fail;
> snapc = kzalloc(sizeof(*snapc) + num*sizeof(u64), GFP_NOFS);
> if (!snapc)

hm, yes, I suppose that's better - hardwiring the assumption that
size_t has type unsigned long is pretty ugly.

Will we need something for ssize_t also?
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/