Slab allocators: fail if ksize is called with a NULL parameter
authorChristoph Lameter <clameter@sgi.com>
Tue, 16 Oct 2007 08:24:46 +0000 (01:24 -0700)
committerLinus Torvalds <torvalds@woody.linux-foundation.org>
Tue, 16 Oct 2007 16:42:53 +0000 (09:42 -0700)
A NULL pointer means that the object was not allocated.  One cannot
determine the size of an object that has not been allocated.  Currently we
return 0 but we really should BUG() on attempts to determine the size of
something nonexistent.

krealloc() interprets NULL to mean a zero sized object.  Handle that
separately in krealloc().

Signed-off-by: Christoph Lameter <clameter@sgi.com>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Cc: Matt Mackall <mpm@selenic.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/slab.c
mm/slob.c
mm/slub.c
mm/util.c

index 6f6abef83a1af82432305245755ab690c8363081..1b240a3029d658a09ebef73fd91022073aa5a551 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -4446,7 +4446,8 @@ const struct seq_operations slabstats_op = {
  */
 size_t ksize(const void *objp)
 {
-       if (unlikely(ZERO_OR_NULL_PTR(objp)))
+       BUG_ON(!objp);
+       if (unlikely(objp == ZERO_SIZE_PTR))
                return 0;
 
        return obj_size(virt_to_cache(objp));
index a886e83e17efed89a8e89592915a08d8ad5db36a..de5d5563a46c624c82290f0c6591428d1a4fd1c2 100644 (file)
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -484,7 +484,8 @@ size_t ksize(const void *block)
 {
        struct slob_page *sp;
 
-       if (unlikely(ZERO_OR_NULL_PTR(block)))
+       BUG_ON(!block);
+       if (unlikely(block == ZERO_SIZE_PTR))
                return 0;
 
        sp = (struct slob_page *)virt_to_page(block);
index b7d3664fa3a9ca6d8cc5bafd6c7fc881e2d5c58e..d7c044dbd1576649ec61eb4e3442ef8703600023 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2449,7 +2449,8 @@ size_t ksize(const void *object)
        struct page *page;
        struct kmem_cache *s;
 
-       if (unlikely(ZERO_OR_NULL_PTR(object)))
+       BUG_ON(!object);
+       if (unlikely(object == ZERO_SIZE_PTR))
                return 0;
 
        page = get_object_page(object);
index bf340d80686884bf765dc696a73f7fd840842e8f..5f64026cbb4d0f6c053a539ffb0d723255da85f3 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -81,14 +81,16 @@ EXPORT_SYMBOL(kmemdup);
 void *krealloc(const void *p, size_t new_size, gfp_t flags)
 {
        void *ret;
-       size_t ks;
+       size_t ks = 0;
 
        if (unlikely(!new_size)) {
                kfree(p);
                return ZERO_SIZE_PTR;
        }
 
-       ks = ksize(p);
+       if (p)
+               ks = ksize(p);
+
        if (ks >= new_size)
                return (void *)p;