Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic (index out of range) while performing a select query #363

Closed
martin-viggiano opened this issue May 12, 2023 · 4 comments
Closed

Panic (index out of range) while performing a select query #363

martin-viggiano opened this issue May 12, 2023 · 4 comments

Comments

@martin-viggiano
Copy link

martin-viggiano commented May 12, 2023

I am getting an index out of range panic while performing a select for update query using go-ora with the xorm library.

I am using:

  • xorm.io/xorm v1.3.3-0.20230406134759-94fcec7f65f8
  • github.com/sijms/go-ora/v2 v2.7.4
  • Oracle 21.3.0
Panic details
panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
github.com/sijms/go-ora/v2.(*DataSet).Next(0xc0002c8120, {0xc0002966e0, 0x2, 0xc000296700?})
        /home/martin/go/pkg/mod/github.com/sijms/go-ora/v2@v2.7.4/data_set.go:327 +0x3a5
database/sql.(*Rows).nextLocked(0xc0002a0e80)
        /usr/local/go/src/database/sql/sql.go:2974 +0x11a
database/sql.(*Rows).Next.func1()
        /usr/local/go/src/database/sql/sql.go:2952 +0x2f
database/sql.withLock({0x9ea398, 0xc0002a0eb0}, 0xc000207a28)
        /usr/local/go/src/database/sql/sql.go:3405 +0x8c
database/sql.(*Rows).Next(0xc0002a0e80)
        /usr/local/go/src/database/sql/sql.go:2951 +0x6f
xorm.io/xorm.(*Session).rows2Beans(0xc0002941a0, 0xc00028c7c0, {0xc000296660, 0x2, 0x2}, {0xc00028c7d0, 0x2, 0x2}, 0xc00028c690?, 0xc000207c48, ...)
        /home/martin/Documents/xorm/session.go:425 +0x85
xorm.io/xorm.(*Session).noCacheFind(0xc0002941a0, 0xc00025e000, {0x89eb40?, 0xc00028e198?, 0x1?}, {0xc0002801e0, 0x52}, {0x0, 0x0, 0x0})
        /home/martin/Documents/xorm/session_find.go:241 +0x606
xorm.io/xorm.(*Session).find(0xc0002941a0, {0x892720?, 0xc00028e198?}, {0x0, 0x0, 0xc000207e37?})
        /home/martin/Documents/xorm/session_find.go:161 +0xa1f
xorm.io/xorm.(*Session).Find(0xc000290240?, {0x892720?, 0xc00028e198?}, {0x0?, 0x0?, 0x0?})
        /home/martin/Documents/xorm/session_find.go:31 +0x7b
main.main()
        /home/martin/Documents/test/oracle.go:57 +0x41b
exit status 2
POC that reproduces the error
package main

import (
	"log"

	go_ora "github.com/sijms/go-ora/v2"
	"xorm.io/xorm"
)

type myStruct2 struct {
	ID int64  `xorm:"pk autoincr <- 'id'"`
	T  string `xorm:"string"`
}

func (myStruct2) TableName() string { return "panic1" }

func main() {
	dsn := go_ora.BuildUrl("localhost", 1521, "XEPDB1", "root", "root", nil)
	engine, err := xorm.NewEngine("oracle", dsn)
	if err != nil {
		log.Fatal(err)
	}

	if err := engine.CreateTables(&myStruct2{}); err != nil {
		log.Fatal("failed to create table: ", err)
	}
	defer engine.DropTables(&myStruct2{})

	test2 := myStruct2{ID: 1, T: "str2"}
	test3 := myStruct2{ID: 2, T: "str2"}

	_, err = engine.Insert(test2, test3)
	if err != nil {
		log.Fatal("insert error:", err)
	}

	var result []myStruct2
	query := engine.NoAutoCondition().Table(myStruct2{}).Where(`"string"='str2' AND "id"<>0`).ForUpdate()

	err = query.Find(&result)
	if err != nil {
		log.Fatal("find error: ", err)
	}
}

Thanks!

Edit: also tested with 2.7.5, same error.

@sijms
Copy link
Owner

sijms commented May 12, 2023

I fix in next release

@sijms
Copy link
Owner

sijms commented May 12, 2023

there is an issue related to xorm.io

Write packet:
00000000  00 00 00 63 06 00 00 00  00 00 03 5e 00 02 80 21  |...c.......^...!|
00000010  00 01 01 16 01 01 0d 00  00 00 00 04 3f ff ff ff  |............?...|
00000020  00 00 00 00 00 00 00 00  00 00 00 01 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 16 44 52 4f 50  |............DROP|
00000040  20 54 41 42 4c 45 20 60  49 53 53 55 45 5f 33 36  | TABLE `ISSUE_36|
00000050  33 60 01 01 01 01 00 00  00 00 00 00 00 02 80 00  |3`..............|
00000060  00 00 00                                          |...|

table name is surround with back quote
which produce error

00000000  00 00 00 55 06 20 00 00  00 00 04 01 02 01 1f 00  |...U. ..........|
00000010  02 03 8f 00 00 01 06 01  0b 00 00 00 00 00 00 03  |................|
00000020  02 14 93 00 00 02 9a cf  01 01 00 00 00 00 00 00  |................|
00000030  00 00 00 02 03 8f 00 1d  4f 52 41 2d 30 30 39 31  |........ORA-0091|
00000040  31 3a 20 69 6e 76 61 6c  69 64 20 63 68 61 72 61  |1: invalid chara|
00000050  63 74 65 72 0a                                    |cter.|
2023-05-12T20:09:12.9623: Summary: RetCode:911, Error Message:"ORA-00911: invalid character"

so table doesn't drop

@martin-viggiano
Copy link
Author

martin-viggiano commented May 12, 2023

Yes, I forked the xorm library and made the following changes:

diff --git a/dialects/oracle.go b/dialects/oracle.go
index 72c26ce..b0c5c38 100644
--- a/dialects/oracle.go
+++ b/dialects/oracle.go
@@ -609,7 +609,7 @@ func (db *oracle) IsReserved(name string) bool {
 }
 
 func (db *oracle) DropTableSQL(tableName string) (string, bool) {
-	return fmt.Sprintf("DROP TABLE `%s`", tableName), false
+	return fmt.Sprintf("DROP TABLE \"%s\"", tableName), false
 }
 
 func (db *oracle) CreateTableSQL(ctx context.Context, queryer core.Queryer, table *schemas.Table, tableName string) (string, bool, error) {
@@ -645,6 +645,10 @@ func (db *oracle) CreateTableSQL(ctx context.Context, queryer core.Queryer, tabl
 	return sql, false, nil
 }
 
+func (db *oracle) IsSequenceExist(ctx context.Context, queryer core.Queryer, seqName string) (bool, error) {
+	return db.HasRecords(queryer, ctx, `SELECT sequence_name FROM user_sequences WHERE sequence_name = :1`, seqName)
+}
+
 func (db *oracle) SetQuotePolicy(quotePolicy QuotePolicy) {
 	switch quotePolicy {
 	case QuotePolicyNone:

@sijms
Copy link
Owner

sijms commented May 13, 2023

fixed in v2.7.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants