This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH,RFA] Ensure check_may_shrink_heap always returns the final value


check_may_shrink_heap's initialization of may_shrink_heap was racy;
before computing the final value of may_shrink_heap, it would store a
value in it that other threads would mistake as final.  Fixed by using
a temporary automatic variable.

for ChangeLog:

	* sysdeps/unix/sysv/linux/sysdep-malloc.h
	(check_may_shrink_heap): Fix race condition in initialization.
---
 sysdeps/unix/sysv/linux/malloc-sysdep.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sysdeps/unix/sysv/linux/malloc-sysdep.h b/sysdeps/unix/sysv/linux/malloc-sysdep.h
index 737ca0e..346c93c 100644
--- a/sysdeps/unix/sysv/linux/malloc-sysdep.h
+++ b/sysdeps/unix/sysv/linux/malloc-sysdep.h
@@ -33,7 +33,8 @@
 static inline bool
 check_may_shrink_heap (void)
 {
-  static int may_shrink_heap = -1;
+  static int global_may_shrink_heap = -1;
+  int may_shrink_heap = global_may_shrink_heap;
 
   if (__builtin_expect (may_shrink_heap >= 0, 1))
     return may_shrink_heap;
@@ -53,6 +54,7 @@ check_may_shrink_heap (void)
 	}
     }
 
+  global_may_shrink_heap = may_shrink_heap;
   return may_shrink_heap;
 }
 

-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist      Red Hat Brazil Compiler Engineer


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]