From 91e8c456964d5ff4b3b7afe2f1cb074de9dfe592 Mon Sep 17 00:00:00 2001 From: Walter Lozano Date: Fri, 14 Oct 2022 09:13:00 -0300 Subject: Fix error handling with getline The function getline is used to read data from different streams, however, the error handling is not accurate. From the man page, getline returns -1 on failure and sets errno accordingly, but the current implementation only checks errno to see if there was a failure. With this approach, in case getline returns success but also sets errno it is consider and error. Fix the issue but also checking the return value of getline. Signed-off-by: Walter Lozano --- apt-pkg/contrib/fileutl.cc | 2 +- apt-pkg/contrib/gpgv.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 4dd7b1ac3..85997275c 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1003,7 +1003,7 @@ bool StartsWithGPGClearTextSignature(string const &FileName) errno = 0; DEFER([&] { fclose(gpg); free(lineptr); }); ssize_t const result = getline(&lineptr, &n, gpg); - if (errno != 0) + if (result < 0 && errno != 0) { _error->Errno("getline", "Could not read from %s", FileName.c_str()); return false; diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc index b555331d6..0341521ac 100644 --- a/apt-pkg/contrib/gpgv.cc +++ b/apt-pkg/contrib/gpgv.cc @@ -76,7 +76,7 @@ class LineBuffer /*{{{*/ { errno = 0; line_length = getline(&buffer, &buffer_size, stream); - if (errno != 0) + if (line_length < 0 && errno != 0) return _error->Errno("getline", "Could not read from %s", InFile.c_str()); if (line_length == -1) { -- cgit v1.2.3-70-g09d2