github-vet / github-vet/rangeloop-pointer-findings

iScript/etcd-cr: embed/etcd.go; 101 LoC

Open
#9,350 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [iScript/etcd-cr](https://www.github.com/iScript/etcd-cr) at [embed/etcd.go](https://github.com/iScript/etcd-cr/blob/a05884a31e1dcdc0d3d12b2fe34d2847eaf88913/embed/etcd.go#L467-L567)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable u used in defer or goroutine at line 544

[Click here to see the code in its original context.](https://github.com/iScript/etcd-cr/blob/a05884a31e1dcdc0d3d12b2fe34d2847eaf88913/embed/etcd.go#L467-L567)

Click here to show the 101 line(s) of Go which triggered the analyzer.

```go
for _, u := range cfg.LCUrls {
sctx := newServeCtx(cfg.logger)

if u.Scheme == "http" || u.Scheme == "unix" {
// 如果是http,但是配置了证书 , 则忽略
if !cfg.ClientTLSInfo.Empty() {
if cfg.logger != nil {
cfg.logger.Warn("scheme is HTTP while key and cert files are present; ignoring key and cert files", zap.String("client-url", u.String()))
}
}
// 如果是http,开启了ClientCertAuth,则忽略
if cfg.ClientTLSInfo.ClientCertAuth {
if cfg.logger != nil {
cfg.logger.Warn("scheme is HTTP while --client-cert-auth is enabled; ignoring client cert auth for this URL", zap.String("client-url", u.String()))
}
}
}
// 如果是https , 但是没配置证书 , 则错误
if (u.Scheme == "https" || u.Scheme == "unixs") && cfg.ClientTLSInfo.Empty() {
return nil, fmt.Errorf("TLS key/cert (--cert-file, --key-file) must be provided for client url %s with HTTPS scheme", u.String())
}

network := "tcp"
addr := u.Host
if u.Scheme == "unix" || u.Scheme == "unixs" {
network = "unix"
addr = u.Host + u.Path
}

sctx.network = network
sctx.secure = u.Scheme == "https" || u.Scheme == "unixs"
sctx.insecure = !sctx.secure

// 如果addr有值,更新secure后直接下一个循环
if oldctx := sctxs[addr]; oldctx != nil {
oldctx.secure = oldctx.secure || sctx.secure
oldctx.insecure = oldctx.insecure || sctx.insecure
continue
}

if sctx.l, err = net.Listen(network, addr); err != nil {
return nil, err
}

sctx.addr = addr

// 检测进程打开文件数限制
if fdLimit, fderr := runtimeutil.FDLimit(); fderr == nil {
// 如果小于
if fdLimit <= reservedInternalFDNum {
if cfg.logger != nil {
cfg.logger.Fatal(
"file descriptor limit of etcd process is too low; please set higher",
zap.Uint64("limit", fdLimit),
zap.Int("recommended-limit", reservedInternalFDNum),
)
}
}
sctx.l = transport.LimitListener(sctx.l, int(fdLimit-reservedInternalFDNum))
}

// tcp or unix
if network == "tcp" {
if sctx.l, err = transport.NewKeepAliveListener(sctx.l, network, nil); err != nil {
return nil, err
}
}

// 函数结束时判断有没错误
defer func() {
if err == nil {
return
}
sctx.l.Close()
if cfg.logger != nil {
cfg.logger.Warn(
"closing peer listener",
zap.String("address", u.Host),
zap.Error(err),
)
}
}()

//默认map空?
for k := range cfg.UserHandlers {
sctx.userHandlers[k] = cfg.UserHandlers[k]
}
//
sctx.serviceRegister = cfg.ServiceRegister

// 是否启用pprof
if cfg.EnablePprof || cfg.Debug {
sctx.registerPprof()
}
// 是否debug 模式
if cfg.Debug {
sctx.registerTrace()
}
sctxs[addr] = sctx

}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: a05884a31e1dcdc0d3d12b2fe34d2847eaf88913

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.