]> source.dussan.org Git - gitea.git/commitdiff
Fix offBy1 errors (#17606)
authorGusted <williamzijl7@hotmail.com>
Sun, 14 Nov 2021 08:11:49 +0000 (08:11 +0000)
committerGitHub <noreply@github.com>
Sun, 14 Nov 2021 08:11:49 +0000 (10:11 +0200)
* 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 <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
cmd/docs.go
models/migrations/migrations.go

index 52233c7ac872d108e12786d416af124f42499dcb..073c57497305f1a4a39a02240ac9ddd9f9651169 100644 (file)
@@ -43,7 +43,11 @@ func runDocs(ctx *cli.Context) error {
                // Clean up markdown. The following bug was fixed in v2, but is present in v1.
                // It affects markdown output (even though the issue is referring to man pages)
                // https://github.com/urfave/cli/issues/1040
-               docs = docs[strings.Index(docs, "#"):]
+               firstHashtagIndex := strings.Index(docs, "#")
+
+               if firstHashtagIndex > 0 {
+                       docs = docs[firstHashtagIndex:]
+               }
        }
 
        out := os.Stdout
index 2686dfb3cff962ff9c71661a21830c65de2787d8..c0d8f111d37648245891a83f68468efdb92c2bfa 100644 (file)
@@ -7,6 +7,7 @@ package migrations
 
 import (
        "context"
+       "errors"
        "fmt"
        "os"
        "reflect"
@@ -791,8 +792,14 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
                }
                tableSQL := string(res[0]["sql"])
 
+               // Get the string offset for column definitions: `CREATE TABLE ( column-definitions... )`
+               columnDefinitionsIndex := strings.Index(tableSQL, "(")
+               if columnDefinitionsIndex < 0 {
+                       return errors.New("couldn't find column definitions")
+               }
+
                // Separate out the column definitions
-               tableSQL = tableSQL[strings.Index(tableSQL, "("):]
+               tableSQL = tableSQL[columnDefinitionsIndex:]
 
                // Remove the required columnNames
                for _, name := range columnNames {