NetBSD Problem Report #13943

Received: (qmail 22249 invoked from network); 13 Sep 2001 08:57:02 -0000
Message-Id: <20010913085700.1866819602@lepracaun.iij.ad.jp>
Date: Thu, 13 Sep 2001 17:57:00 +0900 (JST)
From: h-suzuki@iij.ad.jp
Reply-To: h-suzuki@iij.ad.jp
To: gnats-bugs@gnats.netbsd.org
Cc: h-suzuki@iij.ad.jp
Subject: /bin/sh: Arithmetic Expansion possibly overflow (a hotbed of Sept. 9 date problem)
X-Send-Pr-Version: 3.95

>Number:         13943
>Category:       bin
>Synopsis:       /bin/sh: Arithmetic Expansion possibly overflow (a hotbed of Sept. 9 date problem)
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 13 08:58:01 +0000 2001
>Closed-Date:    Wed Sep 19 06:41:37 +0000 2001
>Last-Modified:  Wed Sep 19 06:41:37 +0000 2001
>Originator:     SUZUKI Hideaki
>Release:        NetBSD 1.5.1
>Organization:

>Environment:

System: NetBSD lepracaun 1.5.1 NetBSD 1.5.1 (LEPRACAUN) #2: Fri Jul 13 22:05:16 JST 2001 root@lepracaun.iij.ad.jp:/usr/src/sys/arch/i386/compile/LEPRACAUN i386

>Description:

The following sample script executes successfully when $today has a
value under 1,000,000,000. However, if $today has 1,000,000,000 or
more, the script returns incorrect result.

sample script:

----- cut here -----  
#!/bin/sh

today=$(date +"%s")
echo $today

# if use $((expression))
yesterday=$((${today} - 86400))
echo $yesterday

# if use $(expr expression)
yesterdayoncemore=$(expr ${today} - 86400)
echo $yesterdayoncemore
# eof
----- cut here -----

The reason is in NetBSD/src/bin/sh/expand.c:expari():

   int result;

   (snip)

   result = arith(p+2);
   fmtstr(p, 10, "%d", result);

I believe fmtstr() function must not round off 'result' on 10 digits.
because 'int' valiable ranges from -2,147,483,648 to 2,147,483,647.


>How-To-Repeat:

Run something like as follows:

----- cut here -----
#!/bin/sh

today=$(date +"%s")
today2=$(date -r $today)
echo 'today:'
echo 'unixtime =' $today
echo $today2
echo 

# if use $((expression))
#
yesterday=$((${today} - 86400))
yesterday2=$(date -r $yesterday)
echo 'yesterday:'
echo 'unixtime =' $yesterday
echo $yesterday2
echo

# if use $(expr expression)
#
yesterdayoncemore=$(expr ${today} - 86400)
yesterdayoncemore2=$(date -r $yesterdayoncemore)
echo 'yesterdayoncemore:'
echo 'unixtime =' $yesterdayoncemore
echo $yesterdayoncemore2
# eof
----- cut here -----

>Fix:

*** /usr/src/bin/sh/expand.c.orig	Wed Sep 12 19:38:03 2001
--- /usr/src/bin/sh/expand.c	Wed Sep 12 19:38:25 2001
***************
*** 393,399 ****
  	if (quotes)
  		rmescapes(p+2);
  	result = arith(p+2);
! 	fmtstr(p, 10, "%d", result);

  	while (*p++)
  		;
--- 393,399 ----
  	if (quotes)
  		rmescapes(p+2);
  	result = arith(p+2);
! 	fmtstr(p, 12, "%d", result);

  	while (*p++)
  		;
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: itojun 
State-Changed-When: Tue Sep 18 23:41:22 PDT 2001 
State-Changed-Why:  
committed to -current, pullup request sent for 1.5 and 1.4 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.49 2026/05/14 01:52:41 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.