From 42d3b48366a9bb691861b4f0a62891a592f28431 Mon Sep 17 00:00:00 2001 From: Jared Curtis Date: Mon, 21 Sep 2020 16:30:10 -0700 Subject: [PATCH] BUG/MINOR Detect partial read from socket It's possible if an external process causes a reload/restart of HAProxy that the data from the socket will be truncated. If it is not trucated on a new line then other functions processing the results will have malformed data and likely cause a panic. Resolves haproxytech/client-native#44 --- runtime/runtime_single_client.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/runtime/runtime_single_client.go b/runtime/runtime_single_client.go index 31f76757..4918bf64 100644 --- a/runtime/runtime_single_client.go +++ b/runtime/runtime_single_client.go @@ -16,6 +16,7 @@ package runtime import ( + "errors" "fmt" "net" "strings" @@ -101,7 +102,17 @@ func (s *SingleRuntime) readFromSocket(command string) (string, error) { if err != nil { return "", err } - result := strings.TrimSuffix(data.String(), "\n> ") + + // Check that last 2 bytes are a LF, otherwise we received an incomplete response + results := data.String() + if len(results) > 2 { + if results[len(results)-2:] != "\n\n" { + err := errors.New("Incomplete read from socket") + return results, err + } + } + + result := strings.TrimSuffix(results, "\n> ") result = strings.TrimSuffix(result, "\n") return result, nil }