diff --git a/libselinux/src/get_context_list.c b/libselinux/src/get_context_list.c index 0ad24654..222b54c1 100644 --- a/libselinux/src/get_context_list.c +++ b/libselinux/src/get_context_list.c @@ -438,7 +438,7 @@ int get_ordered_context_list(const char *user, __fsetlocking(fp, FSETLOCKING_BYCALLER); rc = get_context_user(fp, con, user, &reachable, &nreachable); - fclose(fp); + fclose_errno_safe(fp); if (rc < 0 && errno != ENOENT) { selinux_log(SELINUX_ERROR, "%s: error in processing configuration file %s\n", @@ -451,7 +451,7 @@ int get_ordered_context_list(const char *user, if (fp) { __fsetlocking(fp, FSETLOCKING_BYCALLER); rc = get_context_user(fp, con, user, &reachable, &nreachable); - fclose(fp); + fclose_errno_safe(fp); if (rc < 0 && errno != ENOENT) { selinux_log(SELINUX_ERROR, "%s: error in processing configuration file %s\n", diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 2fad0c93..40628e2c 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -628,7 +628,7 @@ static int process_file(const char *path, const char *suffix, rc = fcontext_is_binary(fp); if (rc < 0) { - (void) fclose(fp); + fclose_errno_safe(fp); return -1; } @@ -639,7 +639,7 @@ static int process_file(const char *path, const char *suffix, rc = digest_add_specfile(digest, fp, NULL, sb.st_size, found_path); - fclose(fp); + fclose_errno_safe(fp); if (!rc) return 0; diff --git a/libselinux/src/selinux_internal.h b/libselinux/src/selinux_internal.h index 450a42c2..372837dd 100644 --- a/libselinux/src/selinux_internal.h +++ b/libselinux/src/selinux_internal.h @@ -2,7 +2,9 @@ #define SELINUX_INTERNAL_H_ #include +#include #include +#include extern int require_seusers ; @@ -131,4 +133,13 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size); #define IGNORE_DEPRECATED_DECLARATION_END #endif +static inline void fclose_errno_safe(FILE *stream) +{ + int saved_errno; + + saved_errno = errno; + (void) fclose(stream); + errno = saved_errno; +} + #endif /* SELINUX_INTERNAL_H_ */