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

当SQL语句太长的时候,sniffer会异常 #16

Open
chao opened this issue Dec 5, 2018 · 3 comments
Open

当SQL语句太长的时候,sniffer会异常 #16

chao opened this issue Dec 5, 2018 · 3 comments

Comments

@chao
Copy link

chao commented Dec 5, 2018

例如,捕捉到一个有1000个参数的Insert的SQL语句的时候出错:

, @p906, @p907, @p908, @p909, @p910, @p911, @p912, @p913, @p914, @p915, @p916, @p917, @p918, @p919, @p920, @p921, @p922, @p923, @p924, @p925, @p926, @p927, @p928, @p929, @p930, @p931, @p932, @p933, @p934, @p935, @p936, @p937, @p938, @p939, @p940, @p941, @p942, @p943, @p944, @p945, @p946, @p947, @p948, @p949, @p950, @p951, @p952, @p953, @p954, @p955, @p956, @p957, @p958, @p959, @p960, @p961, @p962, @p963, @p964, @p965, @p966, @p967, @p968, @p969, @p970, @p971, @p972, @p973, @p974, @p975, @p976, @p977, @p978, @p979, @p980, @p981, @p982, @p983, @p984, @p985, @p986, @p987, @p988, @p989, @p990, @p991, @p992, @p993, @p994, @p995, @p996, @p997, @p998, @p999;
Drop stm id[4];

panic: runtime error: index out of range

goroutine 1411 [running]:
encoding/binary.binary.littleEndian.Uint32(...)
	/usr/local/go/src/encoding/binary/binary.go:63
github.com/40t/go-sniffer/plugSrc/mysql/build.LengthBinary(0xc4206d6c01, 0x32, 0x5ff, 0x9b47d8, 0xc420044c00)
	/root/go/src/github.com/40t/go-sniffer/plugSrc/mysql/build/util.go:41 +0xc2
github.com/40t/go-sniffer/plugSrc/mysql/build.(*stream).resolveServerPacket(0xc4200571c0, 0xc4206d6c00, 0x33, 0x600, 0x1)
	/root/go/src/github.com/40t/go-sniffer/plugSrc/mysql/build/entry.go:230 +0x2fc
github.com/40t/go-sniffer/plugSrc/mysql/build.(*stream).resolve(0xc4200571c0)
	/root/go/src/github.com/40t/go-sniffer/plugSrc/mysql/build/entry.go:194 +0xc0
created by github.com/40t/go-sniffer/plugSrc/mysql/build.(*Mysql).ResolveStream
	/root/go/src/github.com/40t/go-sniffer/plugSrc/mysql/build/entry.go:71 +0x3b8
@cctse
Copy link

cctse commented Jan 7, 2019

go-sniffer
查询语句,数据并没多少

@40t
Copy link
Owner

40t commented Jun 6, 2019

代码 pull 一下,麻烦check一下

@hhkens
Copy link

hhkens commented Oct 21, 2024

改了下这个文件 暂时解决
cat /root/go-sniffer-master/plugSrc/mysql/build/util.go 
package build

import (
        "bytes"
        "encoding/binary"
        "io"
        "time"
)

func GetNowStr(isClient bool) string {
        var msg string
        msg += time.Now().Format("2006-01-02 15:04:05")
        if isClient {
                msg += "| cli -> ser |"
        }else{
                msg += "| ser -> cli |"
        }
        return msg
}

func ReadStringFromByte(b []byte) (string,int) {

        var l int
        l = bytes.IndexByte(b, 0x00)
        if l == -1 {
                l = len(b)
        }
        return string(b[0:l]), l
}

func LengthBinary(b []byte) (uint32, int) {

        var first = int(b[0])
        if first > 0 && first <= 250 {
                return uint32(first), 1
        }
        if first == 251 {
                return 0,1
        }
        if first == 252 {
                return binary.LittleEndian.Uint32(b[1:2]),1
        }
        if first == 253 {
                return binary.LittleEndian.Uint32(b[1:4]),3
        }
        if first == 254 {
                return binary.LittleEndian.Uint32(b[1:9]),8
        }
        return 0,0
}

func LengthEncodedInt(input []byte) (num uint64, isNull bool, n int) {
    if len(input) == 0 {
        // 输入为空,返回默认值,表示无效
        return 0, false, 0
    }

    switch input[0] {
    case 0xfb:
        n = 1
        isNull = true
        return
    case 0xfc:
        if len(input) < 3 {
            return 0, false, 0 // 数据不足,返回默认值
        }
        num = uint64(input[1]) | uint64(input[2])<<8
        n = 3
        return
    case 0xfd:
        if len(input) < 4 {
            return 0, false, 0 // 数据不足,返回默认值
        }
        num = uint64(input[1]) | uint64(input[2])<<8 | uint64(input[3])<<16
        n = 4
        return
    case 0xfe:
        if len(input) < 9 {
            return 0, false, 0 // 数据不足,返回默认值
        }
        num = uint64(input[1]) | uint64(input[2])<<8 | uint64(input[3])<<16 |
            uint64(input[4])<<24 | uint64(input[5])<<32 | uint64(input[6])<<40 |
            uint64(input[7])<<48 | uint64(input[8])<<56
        n = 9
        return
    }

    num = uint64(input[0])
    n = 1
    return
}


func LengthEncodedString(b []byte) ([]byte, bool, int, error) {

        num, isNull, n := LengthEncodedInt(b)
        if num < 1 {
                return nil, isNull, n, nil
        }

        n += int(num)

        if len(b) >= n {
                return b[n-int(num) : n], false, n, nil
        }
        return nil, false, n, io.EOF

}

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

5 participants
@chao @40t @cctse @hhkens and others