Skip to content

Commit

Permalink
Merge pull request #28 from klauspost/fix-line-comment-crash
Browse files Browse the repository at this point in the history
Fix crash on line comments that start with a star.
  • Loading branch information
klauspost authored Jun 10, 2016
2 parents 2962f2f + f2b22b4 commit eee587f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
23 changes: 16 additions & 7 deletions asmfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ func (f *fstate) addLine(b []byte) error {
return fmt.Errorf("zero (0) byte in input. file is unlikely an assembler file")
}
s := string(b)
s = strings.TrimSpace(s)
// Inside block comment
if f.insideBlock {
defer func() {
f.lastComment = true
}()
if strings.Contains(s, "*/") {
ends := strings.Index(s, "*/")
end := strings.TrimSpace(s[:ends])
if f.lastStar {
end = end + " */"
} else {
end = end + "*/"
end := s[:ends]
if strings.HasPrefix(strings.TrimSpace(s), "*") && f.lastStar {
end = strings.TrimSpace(end) + " "
}
end = end + "*/"
f.insideBlock = false
s = strings.TrimSpace(s[ends+2:])
if strings.HasSuffix(s, "\\") {
Expand All @@ -95,7 +93,8 @@ func (f *fstate) addLine(b []byte) error {
}
} else {
// Insert a space on lines that begin with '*'
if strings.HasPrefix(s, "*") {
if strings.HasPrefix(strings.TrimSpace(s), "*") {
s = strings.TrimSpace(s)
f.out.WriteByte(' ')
f.lastStar = true
} else {
Expand All @@ -105,6 +104,7 @@ func (f *fstate) addLine(b []byte) error {
return nil
}
}
s = strings.TrimSpace(s)

// Comment is the the only line content.
if strings.HasPrefix(s, "//") {
Expand Down Expand Up @@ -141,6 +141,15 @@ func (f *fstate) addLine(b []byte) error {
if strings.Contains(s, "/*") && !strings.HasSuffix(s, `\`) {
starts := strings.Index(s, "/*")
ends := strings.Index(s, "*/")
lineComment := strings.Index(s, "//")
if lineComment >= 0 {
if lineComment < starts {
goto exitcomm
}
if lineComment < ends && !f.insideBlock {
goto exitcomm
}
}
pre := s[:starts]
pre = strings.TrimSpace(pre)

Expand Down
4 changes: 2 additions & 2 deletions testdata/mod_math_amd64.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Quicker way of loading MOD_P ?
MOVL $-1, R8
NEGQ R8
MOVL $-1, R8
NEGQ R8
*/
#define MOD_P 0xFFFFFFFF00000001

Expand Down
28 changes: 28 additions & 0 deletions testdata/nested_comment.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "textflag.h"

TEXT ·FailsFormatting(SB), NOSPLIT, $0
RET // */

TEXT ·FailsFormatting(SB), NOSPLIT, $0
/*
RET //*/

TEXT ·FailsFormatting(SB), NOSPLIT, $0
RET // Some longer comment*/

TEXT ·FailsFormatting(SB), NOSPLIT, $0
/*
RET // Some longer comment*/

TEXT ·FailsFormatting(SB), NOSPLIT, $0
/* RET // */

/*
TESTL BX, BX
JEQ move_0
CMPL BX, $2
JBE move_1or2
CMPL BX, $4
JBE move_3or4
CMPL BX, $8
*/
30 changes: 30 additions & 0 deletions testdata/nested_comment.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "textflag.h"

TEXT ·FailsFormatting(SB), NOSPLIT, $0
RET //*/


TEXT ·FailsFormatting(SB), NOSPLIT, $0
/*
RET //*/

TEXT ·FailsFormatting(SB), NOSPLIT, $0
RET // Some longer comment*/


TEXT ·FailsFormatting(SB), NOSPLIT, $0
/*
RET // Some longer comment*/

TEXT ·FailsFormatting(SB), NOSPLIT, $0
/* RET //*/

/*
TESTL BX, BX
JEQ move_0
CMPL BX, $2
JBE move_1or2
CMPL BX, $4
JBE move_3or4
CMPL BX, $8
*/

0 comments on commit eee587f

Please sign in to comment.