Skip to content

Missing fallthrough in SerializeRuntimeAddServer attributes formatting switch statement #368

Closed as not planned
@fatchan

Description

@fatchan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions