Golang在Linux上的tls conn Read中出现panic吗?

huangapple go评论106阅读模式
英文:

Golang panic in tls conn Read - only on linux?

问题

我正在使用golang的crypto/tls来处理自定义的基于行的消息协议。

这种方法在Windows上运行良好:

  1. var fullBuffer string
  2. for {
  3. // 如果未连接,则尝试重新连接
  4. if this.conn == nil {
  5. if this.IsSecure() {
  6. this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
  7. } else {
  8. this.conn, err = net.Dial("tcp", this.GetHostOnly())
  9. }
  10. if err == nil {
  11. // 记录日志并继续
  12. }
  13. }
  14. // 从套接字读取到本地缓冲区(阻塞)
  15. if this.conn != nil {
  16. readBuff := make([]byte, 4096)
  17. nbytes, err = this.conn.Read(readBuff)
  18. if nbytes > 0 {
  19. fullBuffer += string(readBuff[0:nbytes])
  20. }
  21. }
  22. }

非常简单直接 - 在win64上运行良好。

但是当我尝试在Linux上运行它(debian 8 - i386和amd64 - golang 1.5本机和从Windows交叉编译的1.6)时,我遇到以下恐慌:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]
  3. goroutine 8 [running]:
  4. panic(0x8237780, 0x18522030)
  5. C:/Go/src/runtime/panic.go:464 +0x326
  6. crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
  7. C:/Go/src/crypto/tls/conn.go:1023 +0x198
  8. crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
  9. C:/Go/src/crypto/tls/conn.go:922 +0x5e
  10. mylib.(*MyConnection).worker(0x18512480)
  11. C:/gopath/src/mylib/mylib.go:342 +0x200

Read调用失败,因为它在TLS握手时传递了nil。

这里出了什么问题?

而且,为什么问题只出现在Linux上?

英文:

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

This approach works fine on windows:

  1. var fullBuffer string
  2. for {
  3. // If we're not connected, attempt reconnect
  4. if this.conn == nil {
  5. if this.IsSecure() {
  6. this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
  7. } else {
  8. this.conn, err = net.Dial("tcp", this.GetHostOnly())
  9. }
  10. if err == nil {
  11. // log and continue
  12. }
  13. }
  14. // Read from socket into our local buffer (blocking)
  15. if this.conn != nil {
  16. readBuff := make([]byte, 4096)
  17. nbytes, err = this.conn.Read(readBuff)
  18. if nbytes > 0 {
  19. fullBuffer += string(readBuff[0:nbytes])
  20. }
  21. }

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:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]
  3. goroutine 8 [running]:
  4. panic(0x8237780, 0x18522030)
  5. C:/Go/src/runtime/panic.go:464 +0x326
  6. crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
  7. C:/Go/src/crypto/tls/conn.go:1023 +0x198
  8. crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
  9. C:/Go/src/crypto/tls/conn.go:922 +0x5e
  10. mylib.(*MyConnection).worker(0x18512480)
  11. 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

得分: 0

好的,以下是翻译好的内容:

好的,Linux构建出现了“x509: certificate signed by unknown authority”错误。

但是:

  • 证书是有效的

    • crypto/x509/root_unix.go/etc/ssl/certs中查找,该目录是可读的
    • 但是openssl s_client-showcerts -verify上挂起,指向我的操作系统网络配置存在问题
  • 由于某种原因,无论错误如何,下面的if块都会被执行

    • 但是我将其存储为net.Conn而不是指针,所以它不能为nil

问题已解决,对于噪音表示抱歉。希望这个调试故事能帮助其他人解决类似问题。

英文:

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

huangapple
  • 本文由 发表于 2016年4月3日 08:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/36380233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定