Skip to content

Commit

Permalink
Issue go-gorm#3007: Fix search clone array corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 19, 2020
1 parent 345d18c commit bf55f35
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
case "postgres":
fmt.Println("testing postgres...")
if dbDSN == "" {
dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
dbDSN = "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable"
}
db, err = gorm.Open("postgres", dbDSN)
case "mssql":
Expand Down
52 changes: 51 additions & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,57 @@ type searchPreload struct {
}

func (s *search) clone() *search {
clone := *s
clone := search{
db: s.db,
whereConditions: make([]map[string]interface{}, len(s.whereConditions)),
orConditions: make([]map[string]interface{}, len(s.orConditions)),
notConditions: make([]map[string]interface{}, len(s.notConditions)),
havingConditions: make([]map[string]interface{}, len(s.havingConditions)),
joinConditions: make([]map[string]interface{}, len(s.joinConditions)),
initAttrs: make([]interface{}, len(s.initAttrs)),
assignAttrs: make([]interface{}, len(s.assignAttrs)),
selects: s.selects,
omits: make([]string, len(s.omits)),
orders: make([]interface{}, len(s.orders)),
preload: make([]searchPreload, len(s.preload)),
offset: s.offset,
limit: s.limit,
group: s.group,
tableName: s.tableName,
raw: s.raw,
Unscoped: s.Unscoped,
ignoreOrderQuery: s.ignoreOrderQuery,
}
for i, value := range s.whereConditions {
clone.whereConditions[i] = value
}
for i, value := range s.orConditions {
clone.orConditions[i] = value
}
for i, value := range s.notConditions {
clone.notConditions[i] = value
}
for i, value := range s.havingConditions {
clone.havingConditions[i] = value
}
for i, value := range s.joinConditions {
clone.joinConditions[i] = value
}
for i, value := range s.initAttrs {
clone.initAttrs[i] = value
}
for i, value := range s.assignAttrs {
clone.assignAttrs[i] = value
}
for i, value := range s.omits {
clone.omits[i] = value
}
for i, value := range s.orders {
clone.orders[i] = value
}
for i, value := range s.preload {
clone.preload[i] = value
}
return &clone
}

Expand Down
22 changes: 22 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorm

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -28,3 +29,24 @@ func TestCloneSearch(t *testing.T) {
t.Errorf("selectStr should be copied")
}
}

func TestWhereCloneCorruption(t *testing.T) {
for whereCount := 1; whereCount <= 8; whereCount++ {
t.Run(fmt.Sprintf("w=%d", whereCount), func(t *testing.T) {
s := new(search)
for w := 0; w < whereCount; w++ {
s = s.clone().Where(fmt.Sprintf("w%d = ?", w), fmt.Sprintf("value%d", w))
}
if len(s.whereConditions) != whereCount {
t.Errorf("s: where count should be %d", whereCount)
}

q1 := s.clone().Where("finalThing = ?", "THING1")
q2 := s.clone().Where("finalThing = ?", "THING2")

if reflect.DeepEqual(q1.whereConditions, q2.whereConditions) {
t.Errorf("Where conditions should be different")
}
})
}
}

0 comments on commit bf55f35

Please sign in to comment.