From 03d055d6ff38993c04fcc3ec24becda35b119a52 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Wed, 18 Oct 2017 15:42:00 -0700 Subject: [PATCH] liblogcat: avoid double close. Bug: http://b/67960705 Test: mma Change-Id: I0b9015f0b4e64631a3476966decce3b6210bb801 --- logcat/logcat.cpp | 5 ++--- logcat/logcat_system.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index 3d5647231..07981fcc8 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp @@ -1831,11 +1831,10 @@ int android_logcat_destroy(android_logcat_context* ctx) { } android::close_output(context); android::close_error(context); + if (context->fds[1] >= 0) { - // NB: could be closed by the above fclose(s), ignore error. - int save_errno = errno; + // NB: this should be closed by close_output, but just in case... close(context->fds[1]); - errno = save_errno; context->fds[1] = -1; } diff --git a/logcat/logcat_system.cpp b/logcat/logcat_system.cpp index ea393bd67..6dfd110c7 100644 --- a/logcat/logcat_system.cpp +++ b/logcat/logcat_system.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -98,8 +99,12 @@ FILE* android_logcat_popen(android_logcat_context* ctx, const char* command) { return NULL; } - FILE* retval = fdopen(fd, "reb"); - if (!retval) android_logcat_destroy(ctx); + int duped = dup(fd); + FILE* retval = fdopen(duped, "reb"); + if (!retval) { + close(duped); + android_logcat_destroy(ctx); + } return retval; }