dongwang788787 2016-04-03 00:23
浏览 58
已采纳

tls conn中的Golang恐慌阅读-仅在Linux上吗?

I'm using golang crypto/tls to process a custom line-oriented message protocol.

This approach works fine on windows:

var fullBuffer string

for {

    // If we're not connected, attempt reconnect
    if this.conn == nil {

        if this.IsSecure() {
            this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
        } else {
            this.conn, err = net.Dial("tcp", this.GetHostOnly())
        }

        if err == nil {
            // log and continue
        }
    }

    // Read from socket into our local buffer (blocking)
    if this.conn != nil {
        readBuff := make([]byte, 4096)
        nbytes, err = this.conn.Read(readBuff)
        if nbytes > 0 {
            fullBuffer += string(readBuff[0:nbytes])
        }
    }

Pretty straightforward - and it works fine on win64.

But when i try to run it on Linux (debian 8 - both i386 and amd64 - both golang 1.5 native and 1.6 crosscompiled from windows) i get the following panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]

goroutine 8 [running]:
panic(0x8237780, 0x18522030)
    C:/Go/src/runtime/panic.go:464 +0x326
crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:1023 +0x198
crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:922 +0x5e
mylib.(*MyConnection).worker(0x18512480)
    C:/gopath/src/mylib/mylib.go:342 +0x200

The Read call is failing because it's somehow passing nil to the TLS handshake.

What's going wrong here?

And, why is the problem isolated to linux?

  • 写回答

1条回答 默认 最新

  • douqianxun8540 2016-04-03 00:54
    关注

    OK, linux builds are producing the error x509: certificate signed by unknown authority.

    But

    • the certificate is valid

      • crypto/x509/root_unix.go looks in /etc/ssl/certs which is world-readable
      • but openssl s_client is hanging on -showcerts -verify, pointing to some issue with my OS network configuration
    • for some reason the if block below is being entered regardless of the error

      • but i was storing it as a net.Conn instead of a pointer, so it's not nil-able.

    Calling this solved, sorry for the noise. Hopefully this debugging story helps someone else in the future

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?