From mi@aldan.algebra.com  Sun Feb  3 22:36:06 2002
Return-Path: <mi@aldan.algebra.com>
Received: from aldan.algebra.com (aldan.algebra.com [216.254.65.224])
	by hub.freebsd.org (Postfix) with ESMTP id 77B0F37B405
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  3 Feb 2002 22:36:05 -0800 (PST)
Received: (from mi@localhost)
	by aldan.algebra.com (8.11.6/8.11.5) id g146ZY907522;
	Mon, 4 Feb 2002 01:35:34 -0500 (EST)
	(envelope-from mi)
Message-Id: <200202040635.g146ZY907522@aldan.algebra.com>
Date: Mon, 4 Feb 2002 01:35:34 -0500 (EST)
From: Mikhail Teterin <mi@aldan.algebra.com>
Reply-To: Mikhail Teterin <mi@aldan.algebra.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: bc(1)'s multi-line file parsing problem
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         34601
>Category:       bin
>Synopsis:       bc(1)'s multi-line file parsing problem
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    sheldonh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Feb 03 22:40:01 PST 2002
>Closed-Date:    Thu Jul 04 07:30:45 PDT 2002
>Last-Modified:  Thu Jul 04 07:30:45 PDT 2002
>Originator:     Mikhail Teterin
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
Virtual Estates, Inc.
>Environment:
System: FreeBSD aldan.algebra.com 5.0-CURRENT FreeBSD 5.0-CURRENT #1: Thu Jan 3 21:38:15 EST 2002 mi@aldan.algebra.com:/ccd/obj/ccd/src/sys/DEBUG i386


>Description:

	Depending on the weather (or compilation flags) bc's parsing
	of multi-line files involving function definitions may break.
	This is best manifested, when bc is parsing its own libmath --
	triggered by the ``-l'' flag (use_math).

	The /usr/src/contrib/bc/bc/libmath.h contains the code of bc's
	math-library, which is parsed if the -l flag is given. The code
	contains several functions, which are, evidently, defined using
	multiple lines -- between the opening ``['' and the closing ``]''.

	The code in load_code is not prepared for this. When it sees the
	closing ``]'' at line 280 it assumes, that save_adr contains the
	address saved when it saw the opening ``[''.

	However, save_adr is an atomic variable, which is not initialized
	if the opening ``['' occured in one of the previous lines.

	Depending on save_adr's value bc will or will not crash shortly
	after.

>How-To-Repeat:

	I compile everything with
	CFLAGS= -O -pipe -march=i686 ${PROG:C/.+/-fomit-frame-pointer/}
	which means that bc, whose Makefile declares PROG, was built with
	CFLAGS=-O -pipe -march=i686 -fomit-frame-pointer
	this was enough for me to stumble upon this on both 5-CURRENT and
	4.4-STABLE machines.

		echo 20 | bc -l

	However, I was able to reproduce it with simple -g.

	It works properly on freefall...

>Fix:

	Either the save_adr should be made static or the whole libmath needs
	to be parsed as one big string. For the former, the patch would be
	simple. Not sure about about the latter or something third...

RCS file: /home/ncvs/src/contrib/bc/bc/load.c,v
retrieving revision 1.1.1.2
diff -U2 -r1.1.1.2 load.c
--- load.c      26 Feb 2001 07:12:53 -0000      1.1.1.2
+++ load.c      4 Feb 2002 06:31:12 -0000
@@ -157,5 +157,5 @@
   long  vaf_name;      /* variable, array or function number. */
   long  func;
-  program_counter save_adr;
+  static program_counter save_adr;
 
   /* Initialize. */
>Release-Note:
>Audit-Trail:

From: Mikhail Teterin <mi@aldan.algebra.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/34601: bc(1)'s multi-line file parsing problem
Date: Mon, 04 Feb 2002 02:11:42 -0500

 save_adr is not an "atomic", of course. I meant, "auto".
 

From: Sheldon Hearn <sheldonh@starjuice.net>
To: Mikhail Teterin <mi@aldan.algebra.com>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/34601: bc(1)'s multi-line file parsing problem 
Date: Mon, 04 Feb 2002 16:50:45 +0200

 On Mon, 04 Feb 2002 01:35:34 EST, Mikhail Teterin wrote:
 
 > >Number:         34601
 > >Category:       bin
 > >Synopsis:       bc(1)'s multi-line file parsing problem
 
 Hi Mikhail,
 
 Have you tried contacting the distribution maintainers of bc,
 <bug-bc@gnu.org>?  The load.c file is still on the BC vendor branch, so
 it'd be nice to get the patch officially approved by them and then apply
 it on the BC branch, with 'cvs commit -r BC load.c'.
 
 Ciao,
 Sheldon.
Responsible-Changed-From-To: freebsd-bugs->kris 
Responsible-Changed-By: mi 
Responsible-Changed-When: Mon Feb 4 07:17:18 PST 2002 
Responsible-Changed-Why:  
Kris seems to be the one, who made the last import. Looks like it is 
time for the new one. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=34601 

From: Mikhail Teterin <mi@aldan.algebra.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/34601: bc(1)'s multi-line file parsing problem
Date: Mon, 4 Feb 2002 10:15:40 -0500 (EST)

 The software's author says, this is already fixed in the main
 repository...
 
 Time for importing again.
 
 	-mi
 
 ------ Forwarded message ------
     From: Phil Nelson <phil@cs.wwu.edu>
  Subject: Re: a bug in bc?
     Date: Sun, 3 Feb 2002 23:47:40 -0800 (PST)
       To: mi@aldan.algebra.com
       Cc: philnelson@acm.org
 
 Thanks.  That has been already fixed in the master sources.
 
 --Phil
 
 -- 
 Phil Nelson                       NetBSD: http://www.netbsd.org
 e-mail: phil@cs.wwu.edu           Coda: http://www.coda.cs.cmu.edu
 http://cs.wwu.edu/faculty/nelson
 
Responsible-Changed-From-To: kris->sheldonh 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Mon Feb 4 07:28:59 PST 2002 
Responsible-Changed-Why:  
Kris is pretty overloaded.  I'll take care of this one.  Just waiting 
for Mikhail to confirm that the patch is vendor-sanctioned, and 
which version the patch appears or will appear in. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=34601 
State-Changed-From-To: open->analyzed 
State-Changed-By: sheldonh 
State-Changed-When: Tue Feb 5 01:18:03 PST 2002 
State-Changed-Why:  
Committed on the BC branch as rev 1.1.1.3.  I forgot to include an 
MFC reminder, so it's very important that this PR remain in this 
state and assigned to me. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=34601 
State-Changed-From-To: analyzed->closed 
State-Changed-By: sheldonh 
State-Changed-When: Thu Jul 4 07:28:35 PDT 2002 
State-Changed-Why:  
Merged onto the RELENG_4 branch in rev 1.1.1.1.6.2. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=34601 
>Unformatted:
