Skip to content

Commit 5b63a44

Browse files
author
Gusted
committed
Fix offBy1 errors
- Partially resolves #17596 - Resolve errors from go-critic `offBy1: Index() can return -1; maybe you wanted to do Index()+1`.
1 parent 43bbc54 commit 5b63a44

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

cmd/docs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ func runDocs(ctx *cli.Context) error {
4343
// Clean up markdown. The following bug was fixed in v2, but is present in v1.
4444
// It affects markdown output (even though the issue is referring to man pages)
4545
// https://github.com/urfave/cli/issues/1040
46-
docs = docs[strings.Index(docs, "#"):]
46+
firstHashtagIndex := strings.Index(docs, "#")
47+
48+
// Only replace docs, when firstHashtagIndex isn't -1,
49+
// as -1 shouldn't be passed into a slice.
50+
if firstHashtagIndex != -1 {
51+
docs = docs[firstHashtagIndex:]
52+
}
4753
}
4854

4955
out := os.Stdout

models/migrations/migrations.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package migrations
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
"os"
1213
"reflect"
@@ -791,8 +792,18 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
791792
}
792793
tableSQL := string(res[0]["sql"])
793794

795+
// Get the index after the column definitions.
796+
endColumnsIndex := strings.Index(tableSQL, "(")
797+
798+
// If endColumnsIndex is -1, which shouldn't be passed
799+
// into a slice. We should return a error, as that also
800+
// means that the substring wasn't found.
801+
if endColumnsIndex == -1 {
802+
return errors.New("Couldn't get index after column defintions")
803+
}
804+
794805
// Separate out the column definitions
795-
tableSQL = tableSQL[strings.Index(tableSQL, "("):]
806+
tableSQL = tableSQL[endColumnsIndex:]
796807

797808
// Remove the required columnNames
798809
for _, name := range columnNames {

0 commit comments

Comments
 (0)