Skip to content

Commit d2163df

Browse files
Gustedwxiaoguangdelvhlunny
authored
Fix offBy1 errors (#17606)
* Fix offBy1 errors - Partially resolves #17596 - Resolve errors from go-critic `offBy1: Index() can return -1; maybe you wanted to do Index()+1`. * Match golang spec * Remove comments * Update migrations.go * Apply suggestions from code review Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 8eddb75 commit d2163df

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

cmd/docs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ 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+
if firstHashtagIndex > 0 {
49+
docs = docs[firstHashtagIndex:]
50+
}
4751
}
4852

4953
out := os.Stdout

models/migrations/migrations.go

Lines changed: 8 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,14 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
791792
}
792793
tableSQL := string(res[0]["sql"])
793794

795+
// Get the string offset for column definitions: `CREATE TABLE ( column-definitions... )`
796+
columnDefinitionsIndex := strings.Index(tableSQL, "(")
797+
if columnDefinitionsIndex < 0 {
798+
return errors.New("couldn't find column definitions")
799+
}
800+
794801
// Separate out the column definitions
795-
tableSQL = tableSQL[strings.Index(tableSQL, "("):]
802+
tableSQL = tableSQL[columnDefinitionsIndex:]
796803

797804
// Remove the required columnNames
798805
for _, name := range columnNames {

0 commit comments

Comments
 (0)