-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Slice Out-of-Bound Check (#40)
* basic bound check * handling safe context (i.e., table test looping)
- Loading branch information
Showing
11 changed files
with
423 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package formatter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gnoswap-labs/lint/internal" | ||
tt "github.com/gnoswap-labs/lint/internal/types" | ||
) | ||
|
||
type SliceBoundsCheckFormatter struct{} | ||
|
||
func (f *SliceBoundsCheckFormatter) Format( | ||
issue tt.Issue, | ||
snippet *internal.SourceCode, | ||
) string { | ||
var result strings.Builder | ||
|
||
maxLineNumWidth := calculateMaxLineNumWidth(issue.End.Line) | ||
padding := strings.Repeat(" ", maxLineNumWidth+1) | ||
|
||
startLine := issue.Start.Line | ||
endLine := issue.End.Line | ||
for i := startLine; i <= endLine; i++ { | ||
line := expandTabs(snippet.Lines[i-1]) | ||
result.WriteString(lineStyle.Sprintf("%s%d | ", padding[:maxLineNumWidth-len(fmt.Sprintf("%d", i))], i)) | ||
result.WriteString(line + "\n") | ||
} | ||
|
||
result.WriteString(lineStyle.Sprintf("%s| ", padding)) | ||
result.WriteString(messageStyle.Sprintf("%s\n", strings.Repeat("~", calculateMaxLineLength(snippet.Lines, startLine, endLine)))) | ||
result.WriteString(lineStyle.Sprintf("%s| ", padding)) | ||
result.WriteString(messageStyle.Sprintf("%s\n\n", issue.Message)) | ||
|
||
result.WriteString(warningStyle.Sprint("warning: ")) | ||
if issue.Category == "index-access" { | ||
result.WriteString("Index access without bounds checking can lead to runtime panics.\n") | ||
} else if issue.Category == "slice-expression" { | ||
result.WriteString("Slice expressions without proper length checks may cause unexpected behavior.\n\n") | ||
} | ||
|
||
return result.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.