View | Details | Raw Unified | Return to bug 51171
Collapse All | Expand All

(-)./Makefile (-1 / +1 lines)
Lines 19-23 Link Here
19
19
20
LFLAGS= -8	# 8-bit lex scanner for arithmetic
20
LFLAGS= -8	# 8-bit lex scanner for arithmetic
21
CFLAGS+=-DSHELL -I. -I${.CURDIR}
21
CFLAGS+=-DSHELL -I. -I${.CURDIR} -DOVERFLOW
22
# for debug:
22
# for debug:
23
#CFLAGS+= -g -DDEBUG=2 -pg
23
#CFLAGS+= -g -DDEBUG=2 -pg
(-)./arith.h (-1 / +4 lines)
Lines 35-38 Link Here
35
 */
35
 */
36
36
37
int arith(char *);
37
/* XXX some day probably should go to /usr/include/machine/_inttypes.h */
38
#define MAXINT_LEN 20
39
40
intmax_t arith(char *);
38
int expcmd(int , char **);
41
int expcmd(int , char **);
(-)./arith.y (-10 / +48 lines)
Lines 1-2 Link Here
1
%{
2
#include <stdint.h>
3
#ifdef OVERFLOW
4
#include "options.h"
5
#endif
6
7
#define YYSTYPE intmax_t
8
9
static intmax_t arith_res;
10
%}
11
1
%token ARITH_NUM ARITH_LPAREN ARITH_RPAREN
12
%token ARITH_NUM ARITH_LPAREN ARITH_RPAREN
2
13
Lines 15-19 Link Here
15
26
16
exp:	expr = {
27
exp:	expr = {
17
			return ($1);
28
			arith_res = $1;
29
			return (0);
18
		}
30
		}
19
	;
31
	;
Lines 34-40 Link Here
34
	| expr ARITH_LSHIFT expr = { $$ = $1 << $3; }
46
	| expr ARITH_LSHIFT expr = { $$ = $1 << $3; }
35
	| expr ARITH_RSHIFT expr = { $$ = $1 >> $3; }
47
	| expr ARITH_RSHIFT expr = { $$ = $1 >> $3; }
36
	| expr ARITH_ADD expr	= { $$ = $1 + $3; }
48
	| expr ARITH_ADD expr	= {
37
	| expr ARITH_SUB expr	= { $$ = $1 - $3; }
49
				    $$ = $1 + $3;
38
	| expr ARITH_MUL expr	= { $$ = $1 * $3; }
50
#ifdef OVERFLOW
51
				    if (Oflag && is_add_overflow($1, $3, $$))
52
					yyerror("overflow in");
53
#endif
54
				  }
55
	| expr ARITH_SUB expr	= {
56
				    $$ = $1 - $3;
57
#ifdef OVERFLOW
58
				    if (Oflag && is_add_overflow($1, -$3, $$))
59
					yyerror("overflow in");
60
#endif
61
				  }
62
	| expr ARITH_MUL expr	= {
63
				    $$ = $1 * $3;
64
#ifdef OVERFLOW
65
				    if (Oflag && $$/$1 != $3 )
66
					yyerror("overflow in");
67
#endif
68
				  }
39
	| expr ARITH_DIV expr	= {
69
	| expr ARITH_DIV expr	= {
40
			if ($3 == 0)
70
			if ($3 == 0)
Lines 110-125 Link Here
110
140
111
int
141
int
112
arith(char *s)
142
is_add_overflow(intmax_t a, intmax_t b, intmax_t s)
113
{
143
{
114
	long result;
144
 if (a > 0 && b > 0 && s <= 0)
145
	return 1;
146
 if (a < 0 && b < 0 && s >= 0)
147
	return 1;
148
 return 0;
149
}
115
150
151
intmax_t
152
arith(char *s)
153
{
116
	arith_buf = arith_startbuf = s;
154
	arith_buf = arith_startbuf = s;
117
155
118
	INTOFF;
156
	INTOFF;
119
	result = yyparse();
157
	yyparse();
120
	arith_lex_reset();	/* reprime lex */
158
	arith_lex_reset();	/* reprime lex */
121
	INTON;
159
	INTON;
122
160
123
	return (result);
161
	return (arith_res);
124
}
162
}
125
163
Lines 143-147 Link Here
143
	char *concat;
181
	char *concat;
144
	char **ap;
182
	char **ap;
145
	long i;
183
	intmax_t i;
146
184
147
	if (argc > 1) {
185
	if (argc > 1) {
Lines 168-172 Link Here
168
	i = arith(p);
206
	i = arith(p);
169
207
170
	out1fmt("%ld\n", i);
208
	out1fmt("%jd\n", i);
171
	return (! i);
209
	return (! i);
172
}
210
}
(-)./arith_lex.l (-2 / +3 lines)
Lines 44-51 Link Here
44
#endif /* not lint */
44
#endif /* not lint */
45
45
46
#include <stdint.h>
46
#include "y.tab.h"
47
#include "y.tab.h"
47
#include "error.h"
48
#include "error.h"
48
49
49
extern int yylval;
50
extern intmax_t yylval;
50
extern char *arith_buf, *arith_startbuf;
51
extern char *arith_buf, *arith_startbuf;
51
#undef YY_INPUT
52
#undef YY_INPUT
Lines 57-61 Link Here
57
%%
58
%%
58
[ \t\n]	{ ; }
59
[ \t\n]	{ ; }
59
[0-9]+	{ yylval = atol(yytext); return(ARITH_NUM); }
60
[0-9]+	{ yylval = strtoll(yytext, NULL, 10); return(ARITH_NUM); }
60
"("	{ return(ARITH_LPAREN); }
61
"("	{ return(ARITH_LPAREN); }
61
")"	{ return(ARITH_RPAREN); }
62
")"	{ return(ARITH_RPAREN); }
(-)./expand.c (-6 / +7 lines)
Lines 46-49 Link Here
46
#include <sys/time.h>
46
#include <sys/time.h>
47
#include <sys/stat.h>
47
#include <sys/stat.h>
48
#include <stdint.h>
48
#include <errno.h>
49
#include <errno.h>
49
#include <dirent.h>
50
#include <dirent.h>
Lines 367-371 Link Here
367
{
368
{
368
	char *p, *start;
369
	char *p, *start;
369
	int result;
370
	intmax_t result;
370
	int begoff;
371
	int begoff;
371
	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
372
	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
Lines 383-390 Link Here
383
	 * characters have to be processed left to right.
384
	 * characters have to be processed left to right.
384
	 */
385
	 */
385
#if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
386
//#if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
386
#error "integers with more than 10 digits are not supported"
387
//#error "integers with more than 10 digits are not supported"
387
#endif
388
//#endif
388
	CHECKSTRSPACE(12 - 2, expdest);
389
	CHECKSTRSPACE(MAXINT_LEN, expdest);
389
	USTPUTC('\0', expdest);
390
	USTPUTC('\0', expdest);
390
	start = stackblock();
391
	start = stackblock();
Lines 408-412 Link Here
408
		rmescapes(p+2);
409
		rmescapes(p+2);
409
	result = arith(p+2);
410
	result = arith(p+2);
410
	fmtstr(p, 12, "%d", result);
411
	fmtstr(p, MAXINT_LEN + 2, "%qd", result);
411
	while (*p++)
412
	while (*p++)
412
		;
413
		;
(-)./options.h (+10 lines)
Lines 67-72 Link Here
67
#define	Tflag optlist[16].val
67
#define	Tflag optlist[16].val
68
#define	Pflag optlist[17].val
68
#define	Pflag optlist[17].val
69
#ifdef OVERFLOW
70
#define	Oflag optlist[18].val
71
#endif
69
72
73
#ifdef OVERFLOW
74
#define NOPTS	19
75
#else
70
#define NOPTS	18
76
#define NOPTS	18
77
#endif
71
78
72
struct optent {
79
struct optent {
Lines 96-99 Link Here
96
	{ "trapsasync",	'T',	0 },
103
	{ "trapsasync",	'T',	0 },
97
	{ "physical",	'P',	0 },
104
	{ "physical",	'P',	0 },
105
#ifdef OVERFLOW
106
	{ "overflow",	'O',	0 },
107
#endif
98
};
108
};
99
#else
109
#else
(-)./sh.1 (-1 / +3 lines)
Lines 44-48 Link Here
44
.Sh SYNOPSIS
44
.Sh SYNOPSIS
45
.Nm
45
.Nm
46
.Op Fl /+abCEefIimnPpsTuVvx
46
.Op Fl /+abCEefIimnOPpsTuVvx
47
.Op Fl /+o Ar longname
47
.Op Fl /+o Ar longname
48
.Op Fl c Ar string
48
.Op Fl c Ar string
Lines 226-229 Link Here
226
execute them.  This is useful for checking the
226
execute them.  This is useful for checking the
227
syntax of shell scripts.
227
syntax of shell scripts.
228
.It Fl O Li interactive
229
If compiled with the overflow checks, turn them during arithmetic operations on.
228
.It Fl P Li physical
230
.It Fl P Li physical
229
Change the default for the
231
Change the default for the

Return to bug 51171