From ab6bc5dce60835700455648de758425730b3ae15 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Tue, 16 May 2017 12:27:31 -0700 Subject: [PATCH] service: Avoid underflow in logs padding calculation This command inserts a variable amount of padding in the log line: padding := strings.Repeat(" ", f.padding-getMaxLength(task.Slot)) If the service is scaled up, or the slot numbers are noncontiguous, the subtraction can underflow, causing a crash. Signed-off-by: Aaron Lehmann --- cli/command/service/logs.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/command/service/logs.go b/cli/command/service/logs.go index 50a85e24faa9..e4ccd34f9c27 100644 --- a/cli/command/service/logs.go +++ b/cli/command/service/logs.go @@ -204,8 +204,12 @@ func (f *taskFormatter) format(ctx context.Context, logCtx logContext) (string, } } - padding := strings.Repeat(" ", f.padding-getMaxLength(task.Slot)) - formatted := fmt.Sprintf("%s@%s%s", taskName, nodeName, padding) + paddingCount := f.padding - getMaxLength(task.Slot) + padding := "" + if paddingCount > 0 { + padding = strings.Repeat(" ", paddingCount) + } + formatted := taskName + "@" + nodeName + padding f.cache[logCtx] = formatted return formatted, nil }