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

Add golangci-lint and fix lint issues; add ci for macOS and windows #36

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

dongbeiouba
Copy link
Member

@dongbeiouba dongbeiouba commented Nov 1, 2024

CI adds golangci-lint check.

Delete duplicate cgo ldflags -lcrypto

Fix golangci-lint issues.

  • Set max length of line to 120
  • Test cases add t.Parallel()
  • Test helpers add t.Helper()
  • Warp the error with fmt.Errorf()
  • Rename variables from C style to Golang style
  • Add blank line.

Add CI for macOS and Windows.
Fix ldflags for windows.
Fix compile errors on windows.

@dongbeiouba dongbeiouba force-pushed the ci/win_mac branch 30 times, most recently from 0ce2bfa to 8f448d8 Compare November 4, 2024 04:45
@dongbeiouba dongbeiouba force-pushed the ci/win_mac branch 8 times, most recently from 85aaaf7 to 348c87a Compare November 4, 2024 09:39
"fmt"
"runtime"
"unsafe"
)

const (
GCM_TAG_MAXLEN = 16
GcmTagMaxLen = 16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照go的命名规格,如果本身是缩写,那就保留大写 GcmTagMaxLen => GCMTagMaxLen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -24,27 +20,37 @@ import (

// VerifyASN1 verifies ASN.1 encoded signature. Returns nil on success.
func VerifyASN1(pub crypto.PublicKey, data, sig []byte) error {
if pub.KeyType() != crypto.NID_sm2 {
return errors.New("SM2: key type is not sm2")
if pub.KeyType() != crypto.NidSm2 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理, NIDSM2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

ssl.go Outdated
@@ -32,37 +32,35 @@ const (
)

const (
OPENSSL_NPN_NEGOTIATED C.int = C.OPENSSL_NPN_NEGOTIATED
OPENSSL_NPN_NO_OVERLAP C.int = C.OPENSSL_NPN_NO_OVERLAP
NpnNegotiated C.int = C.OPENSSL_NPN_NEGOTIATED
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NPNNegotiated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

//export sni_cb_thunk
func sni_cb_thunk(p unsafe.Pointer, con *C.SSL, ad unsafe.Pointer, arg unsafe.Pointer) C.int {
//export sniCbThunk
func sniCbThunk(callback unsafe.Pointer, con *C.SSL, ad unsafe.Pointer, arg unsafe.Pointer) C.int {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func sniCbThunk(callback unsafe.Pointer, con *C.SSL, _ unsafe.Pointer, _ unsafe.Pointer)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用两个下划线会导致编译失败。

ssl_test.go Outdated
if err != nil {
t.Fatal(err)
}
_ = serverConn.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignore the error return by Close() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if err != nil {
t.Error(err)
}
tls_key, ok := tls_cert.PrivateKey.(*rsa.PrivateKey)

tlsKey, ok := tlsCert.PrivateKey.(*rsa.PrivateKey)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_, ok := tlsCert.PrivateKey.(*rsa.PrivateKey)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

ctx := C.X_EVP_MD_CTX_new()
defer C.X_EVP_MD_CTX_free(ctx)

if key.KeyType() == KeyTypeED25519 {
// do ED specific one-shot sign
if method != nil || len(data) == 0 {
return nil, errors.New("signpkcs1v15: 0-length data or non-null digest")
return nil, ErrNilParameter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不wrapper一层error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得没啥必要,ErrNilParameter表示参数为空就可以了

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,这里是keytype 是specific KeyTypeED25519的时候对应的错误,加wrapper 可能更好区分。如果上层不需要区分也OK

bio := C.BIO_new_mem_buf(unsafe.Pointer(&pem_block[0]),
C.int(len(pem_block)))

bio := C.BIO_new_mem_buf(unsafe.Pointer(&pemBlock[0]), C.int(len(pemBlock)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑 C.CBytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没必要使用C.CBytes()拷贝一份内存

conn.go Outdated
want_read_future *utils.Future
conn net.Conn
ctx *Ctx // for gc
intoSsl *crypto.ReadBio
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intoSSL
fromSSL

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return 0, func() error { return io.EOF }
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
rv, errno := C.SSL_read(c.ssl, unsafe.Pointer(&b[0]), C.int(len(b)))
rv, errno := C.SSL_read(c.ssl, unsafe.Pointer(&buf[0]), C.int(len(buf)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C.CBytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSL_read()直接写到buf里面,不用使用C.CBytes()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, 确保c 不会free go alloc 的内存就没问题

crypto/cert.go Outdated
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
bio := C.BIO_new_mem_buf(unsafe.Pointer(&pem_block[0]),
C.int(len(pem_block)))
bio := C.BIO_new_mem_buf(unsafe.Pointer(&pemBlock[0]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C.CBytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没必要使用C.CBytes()拷贝一份内存

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, 确保c 不会free go alloc 的内存就没问题

CI adds golangci-lint check.

Delete duplicate cgo ldflags -lcrypto

Fix golangci-lint issues.
- Set max length of line to 120
- Test cases add t.Parallel()
- Test helpers add t.Helper()
- Warp the error with fmt.Errorf()
- Rename variables from C style to Golang style
- Add blank line.

Add CI for macOS and Windows.
Fix ldflags for windows.
Fix compile errors on windows.
Copy link
Collaborator

@itomsawyer itomsawyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dongbeiouba dongbeiouba merged commit 7c0ad4e into Tongsuo-Project:main Nov 18, 2024
6 checks passed
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

Successfully merging this pull request may close these issues.

2 participants