From 14ceca320065656ea31994191f9e74d254a72e04 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Wed, 2 Nov 2022 11:44:24 +0100 Subject: [PATCH] address-book-cmd: be more lenient with errors Instead of failing completely when address-book-cmd returns an invalid line, ignore the line and report a warning in the logs. Do not wait for 100 "valid" addresses before bailing out as the command could only output garbage in large quantities. The issue of the command not printing any new line characters still exists. We could address this but I think it would be overkill. Reported-by: Bence Ferdinandy Signed-off-by: Robin Jarry Tested-by: Bence Ferdinandy --- completer/completer.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/completer/completer.go b/completer/completer.go index f3f7b9f..131063e 100644 --- a/completer/completer.go +++ b/completer/completer.go @@ -162,30 +162,38 @@ func (c *Completer) getAddressCmd(s string) (*exec.Cmd, error) { func readCompletions(r io.Reader) ([]string, error) { buf := bufio.NewReader(r) completions := []string{} - for { + for i := 0; i < maxCompletionLines; i++ { line, err := buf.ReadString('\n') if errors.Is(err, io.EOF) { return completions, nil } else if err != nil { return nil, err } + if strings.TrimSpace(line) == "" { + // skip empty lines + continue + } parts := strings.SplitN(line, "\t", 3) addr, err := mail.ParseAddress(strings.TrimSpace(parts[0])) if err != nil { - return nil, err + logging.Warnf( + "line %d: %#v: could not parse address: %v", + line, err) + continue } if len(parts) > 1 { addr.Name = strings.TrimSpace(parts[1]) } decoded, err := decodeMIME(addr.String()) if err != nil { - return nil, fmt.Errorf("could not decode MIME string: %w", err) + logging.Warnf( + "line %d: %#v: could not decode MIME string: %v", + i+1, line, err) + continue } completions = append(completions, decoded) - if len(completions) >= maxCompletionLines { - return completions, tooManyLines - } } + return completions, tooManyLines } func decodeMIME(s string) (string, error) {