gmem.c bug in glib-1.1.3



Attached is a little patch to fix a bug that causes g_malloc0 to break
its invariants when ENABLE_MEM_CHECK is defined and ENABLE_MEM_PROFILE
is not.  (The important part is the third chunk.)  The bug causes a
coredump in testglib.c using the glib-1.1.3 source.

The same bug was present in glib-1.1.2, but I guess the patch I sent
was overlooked.  The patch doesn't quite work there because other
things have changed, but you can apply the changes by hand.

I'm pretty sure this patch is correct.  Can somebody with commit
access please consider it?

--
Martin Pool


*** gmem.c.~1~	Sun Sep  6 09:33:26 1998
--- gmem.c	Tue Oct  6 16:26:46 1998
***************
*** 1,5 ****
  /* GLIB - Library of useful routines for C programming
!  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Library General Public
--- 1,5 ----
  /* GLIB - Library of useful routines for C programming
!  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Library General Public
***************
*** 24,29 ****
--- 24,46 ----
  /* #define ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS */
  /* #define ENABLE_MEM_CHECK */
  
+ /*
+  * This library can check for some attempts to do illegal things to
+  * memory (ENABLE_MEM_CHECK), and can do profiling
+  * (ENABLE_MEM_PROFILE).  Both features are implemented by storing
+  * words before the start of the memory chunk.
+  *
+  * The first, at offset -2*SIZEOF_LONG, is used only if
+  * ENABLE_MEM_CHECK is set, and stores 0 after the memory has been
+  * allocated and 1 when it has been freed.  The second, at offset
+  * -SIZEOF_LONG, is used if either flag is set and stores the size of
+  * the block.
+  *
+  * The MEM_CHECK flag is checked when memory is realloc'd and free'd,
+  * and it can be explicitly checked before using a block by calling
+  * g_mem_check().
+  */
+ 
  #if defined(ENABLE_MEM_PROFILE) && defined(ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS)
  #define ENTER_MEM_CHUNK_ROUTINE() allocating_for_mem_chunk++
  #define LEAVE_MEM_CHUNK_ROUTINE() allocating_for_mem_chunk--
***************
*** 184,192 ****
      return NULL;
    
    
! #ifdef ENABLE_MEM_PROFILE
    size += SIZEOF_LONG;
! #endif /* ENABLE_MEM_PROFILE */
    
  #ifdef ENABLE_MEM_CHECK
    size += SIZEOF_LONG;
--- 201,209 ----
      return NULL;
    
    
! #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
    size += SIZEOF_LONG;
! #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
    
  #ifdef ENABLE_MEM_CHECK
    size += SIZEOF_LONG;
***************
*** 381,387 ****
    t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
    
    if (*t >= 1)
!     g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
  #endif /* ENABLE_MEM_CHECK */
  }
  
--- 398,404 ----
    t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
    
    if (*t >= 1)
!     g_warning ("mem: 0x%08lx has been freed: %lx\n", (gulong) mem, *t);
  #endif /* ENABLE_MEM_CHECK */
  }
  



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]