Subject: build fails on make-prime-list when asan is enabled

From: Yury Usishchev <[email-masked]>
Date: Thu, 05 Feb 2015 18:21:06 +0300

Hello!

We tried to build coreutils with address sanitizer enabled and 
encountered an error:

   GEN      src/primes.h
==12657== ERROR: AddressSanitizer: heap-buffer-overflow

This can be reproduced on git master using gcc-4.8 or gcc-4.9 by
git clone
export CFLAGS="-fsanitize=address"
./bootstrap
./configure
make

and is caused by line
src/make-prime-list.c:214:      while (i < size && sieve[++i] == 0)

When 'i' reaches 'size-1' it gets incremented and then 
(unallocated)memory is accessed.

I attached patch that can fix this issue.

-- 
BR,
Yury Usishchev

diff --git a/src/make-prime-list.c b/src/make-prime-list.c
index 4ec01cf..d293e50 100644
--- a/src/make-prime-list.c
+++ b/src/make-prime-list.c
@@ -195,8 +195,8 @@ main (int argc, char **argv)
 
   size = (limit-1)/2;
   /* sieve[i] represents 3+2*i */
-  sieve = xalloc (size);
-  memset (sieve, 1, size);
+  sieve = xalloc (size+1);
+  memset (sieve, 1, size+1);
 
   prime_list = xalloc (size * sizeof (*prime_list));
   nprimes = 0;