Received: from mail.onstor.com ([66.201.51.107]) by onstor-exch02.onstor.net with Microsoft SMTPSVC(6.0.3790.1830);
	 Sat, 19 Jul 2008 08:21:36 -0700
Received: from ausesmta2-2.messageone.com ([64.20.241.45]) by mail.onstor.com with Microsoft SMTPSVC(6.0.3790.1830);
	 Sat, 19 Jul 2008 08:21:36 -0700
Received: from ftp.linux-mips.org (ftp.linux-mips.org [213.58.128.207])
	by ausesmta2-2.messageone.com (8.13.8/8.13.8) with ESMTP id m6JFLZsk005542
	for <andy.sharp@onstor.com>; Sat, 19 Jul 2008 10:21:35 -0500
Received: from localhost.localdomain ([127.0.0.1]:19939 "EHLO
	ftp.linux-mips.org") by ftp.linux-mips.org with ESMTP
	id S20025632AbYGSPVA (ORCPT <rfc822;andy.sharp@onstor.com>);
	Sat, 19 Jul 2008 16:21:00 +0100
Received: with ECARTIS (v1.0.0; list linux-mips); Sat, 19 Jul 2008 16:20:43 +0100 (BST)
Received: from mba.ocn.ne.jp ([122.1.235.107]:12251 "HELO smtp.mba.ocn.ne.jp")
	by ftp.linux-mips.org with SMTP id S20025642AbYGSPUk (ORCPT
	<rfc822;linux-mips@linux-mips.org>); Sat, 19 Jul 2008 16:20:40 +0100
Received: from localhost (p1004-ipad301funabasi.chiba.ocn.ne.jp [122.17.251.4])
	by smtp.mba.ocn.ne.jp (Postfix) with ESMTP
	id ED8A2B0F8; Sun, 20 Jul 2008 00:20:33 +0900 (JST)
Date: 	Sun, 20 Jul 2008 00:22:24 +0900 (JST)
Message-Id: <20080720.002224.108306935.anemo@mba.ocn.ne.jp>
To: linux-sparse@vger.kernel.org
Cc: linux-mips@linux-mips.org, sam@ravnborg.org, viro@ZenIV.linux.org.uk
Subject: [PATCH] sparse: Make pre_buffer dynamically increasable
From: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
X-Fingerprint: 6ACA 1623 39BD 9A94 9B1A  B746 CA77 FE94 2874 D52F
X-Pgp-Public-Key: http://wwwkeys.pgp.net/pks/lookup?op=get&search=0x2874D52F
X-Mailer: Mew version 5.2 on Emacs 21.4 / Mule 5.0 (SAKAKI)
Mime-Version: 1.0
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-archive-position: 19901
X-ecartis-version: Ecartis v1.0.0
Sender: linux-mips-bounce@linux-mips.org
Errors-to: linux-mips-bounce@linux-mips.org
X-original-sender: anemo@mba.ocn.ne.jp
Precedence: bulk
X-list: 	linux-mips
X-MessageOne-Virus-Version: vendor=fsecure engine=4.65.7161:2.4.4,1.2.40,4.0.164 definitions=2008-07-19_01:2008-07-16,2008-07-19,2008-07-18 signatures=0
X-MessageOne-Virus-Scanned: Clean
X-MessageOne-Envelope-Sender: linux-mips-bounce@linux-mips.org
X-MessageOne-Spam-Details: rule=m773emszm_notspam policy=m773emszm score=0 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=3.1.0-0805090000 definitions=main-0807190038
X-MessageOne-Spam-Score: 0
X-MessageOne-Spam-Bar: 
Return-Path: linux-mips-bounce@linux-mips.org
X-OriginalArrivalTime: 19 Jul 2008 15:21:36.0303 (UTC) FILETIME=[223EFBF0:01C8E9B3]

I got this error when running sparse on mips kernel with gcc 4.3:

builtin:272:1: warning: Newline in string or character constant

The linux-mips kernel uses '$(CC) -dM -E' to generates arguments for
sparse.  With gcc 4.3, it generates lot of '-D' options and causes
pre_buffer overflow.  The linux-mips kernel can filter unused symbols
out to avoid overflow, but sparse should be fixed anyway.

This patch make pre_buffer dynamically increasable and add extra
checking for overflow instead of silently truncating.

Signed-off-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
---
diff --git a/lib.c b/lib.c
index 0abcc9a..6e8d09b 100644
--- a/lib.c
+++ b/lib.c
@@ -186,7 +186,8 @@ void die(const char *fmt, ...)
 }
 
 static unsigned int pre_buffer_size;
-static char pre_buffer[8192];
+static unsigned int pre_buffer_alloc_size;
+static char *pre_buffer;
 
 int Waddress_space = 1;
 int Wbitwise = 0;
@@ -232,12 +233,20 @@ void add_pre_buffer(const char *fmt, ...)
 	unsigned int size;
 
 	va_start(args, fmt);
+	if (pre_buffer_alloc_size < pre_buffer_size + getpagesize()) {
+		pre_buffer_alloc_size += getpagesize();
+		pre_buffer = realloc(pre_buffer, pre_buffer_alloc_size);
+		if (!pre_buffer)
+			die("Unable to allocate more pre_buffer space");
+	}
 	size = pre_buffer_size;
 	size += vsnprintf(pre_buffer + size,
-		sizeof(pre_buffer) - size,
+		pre_buffer_alloc_size - size,
 		fmt, args);
 	pre_buffer_size = size;
 	va_end(args);
+	if (pre_buffer_size >= pre_buffer_alloc_size - 1)
+		die("pre_buffer overflow");
 }
 
 static char **handle_switch_D(char *arg, char **next)

