Skip to content

Missing fallthrough in SerializeRuntimeAddServer attributes formatting switch statement #368

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

Open
fatchan opened this issue Mar 16, 2025 · 0 comments

Comments

@fatchan
Copy link

fatchan commented Mar 16, 2025

Hi,

It seems to me this switch case in SerializeRuntimeAddServer which formats attributes to pass to the "add server" command doesn't fallthrough on each case:

func SerializeRuntimeAddServer(srv *models.RuntimeAddServer) string { //nolint:cyclop,maintidx
b := &strings.Builder{}
push := func(s string) {
b.WriteByte(' ')
b.WriteString(s)
}
pushi := func(key string, val *int64) {
fmt.Fprintf(b, " %s %d", key, *val)
}
// push a quoted string
pushq := func(key, val string) {
fmt.Fprintf(b, ` %s "%s"`, key, val)
}
enabled := func(s string) bool {
return s == "enabled"
}
// Address is mandatory and must come first, with an optional port number.
addr := srv.Address
if srv.Port != nil {
addr += fmt.Sprintf(":%d", *srv.Port)
}
push(addr)
switch {
case enabled(srv.AgentCheck):
push("agent-check")
case srv.AgentAddr != "":
pushq("agent-addr", srv.AgentAddr)
case srv.AgentPort != nil:

Therefore, the first case which matches, the switch is finished and won't insert the rest of the desired parameters into the resulting output string.

Golang switch statements break and don't fallthrough by default afaik, so this can be easily remedied by adding the fallthrough label to each case or using if statements.

Sorry, I've not got the time to make a PR myself.

Edit: I think it also misquotes some params using pushq, e.g. "verify" should not become verify "required", but simply verify required. The former will be rejected by the runtime API.

Cheers.

@fatchan fatchan changed the title Missing fallthrough in AddServer attributes formatting switch statement Missing fallthrough in SerializeRuntimeAddServer attributes formatting switch statement Mar 16, 2025
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

1 participant