Skip to content

Commit 989b372

Browse files
committed
cmd/asm: fix shifts again, this time for sure
There are two conditions to worry about: 1) The shift count cannot be negative. Since the evaluator uses unsigned arithmetic throughout, this means checking that the high bit of the shift count is always off, which is done by converting to int64 and seeing if the result is negative. 2) For right shifts, the value cannot be negative. We don't want a high bit in the value because right shifting a value depends on the sign, and for clarity we always want unsigned shifts. Next step is to build some testing infrastructure for the parser. Change-Id: I4c46c79989d02c107fc64954403fc18613763f1d Reviewed-on: https://go-review.googlesource.com/11326 Reviewed-by: Russ Cox <[email protected]>
1 parent 4bba672 commit 989b372

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/cmd/asm/internal/asm/parse.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ func (p *Parser) term() uint64 {
797797
value *= p.factor()
798798
case '/':
799799
p.next()
800-
if value&(1<<63) != 0 {
801-
p.errorf("divide with high bit set")
800+
if int64(value) < 0 {
801+
p.errorf("divide of value with high bit set")
802802
}
803803
value /= p.factor()
804804
case '%':
@@ -808,14 +808,17 @@ func (p *Parser) term() uint64 {
808808
p.next()
809809
shift := p.factor()
810810
if int64(shift) < 0 {
811-
p.errorf("left shift with high bit set")
811+
p.errorf("negative left shift count")
812812
}
813813
return value << shift
814814
case lex.RSH:
815815
p.next()
816816
shift := p.term()
817817
if int64(shift) < 0 {
818-
p.errorf("right shift with high bit set")
818+
p.errorf("negative right shift count")
819+
}
820+
if int64(value) < 0 {
821+
p.errorf("right shift of value with high bit set")
819822
}
820823
value >>= shift
821824
case '&':

0 commit comments

Comments
 (0)