Blob Blame Raw
From 1e84cb90b63bce841376140a7a80107e5ec1e1a8 Mon Sep 17 00:00:00 2001
From: Adrian Reber <areber@redhat.com>
Date: Fri, 3 May 2019 06:27:51 +0000
Subject: [PATCH] lsm: fix compiler error 'unused-result'

Reading out the xattr 'security.selinux' of checkpointed sockets with
fscanf() works (at least in theory) without checking the result of
fscanf(). There are, however, multiple CI failures when ignoring the
return value of fscanf().

This adds ferror() to check if the stream has an actual error or if '-1'
just mean EOF.

Handle all errors of fscanf() // Andrei

Signed-off-by: Adrian Reber <areber@redhat.com>
Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
 criu/lsm.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/criu/lsm.c b/criu/lsm.c
index ef6ba112b3..9c9ac7f80e 100644
--- a/criu/lsm.c
+++ b/criu/lsm.c
@@ -33,8 +33,8 @@ static int apparmor_get_label(pid_t pid, char **profile_name)
 		return -1;
 
 	if (fscanf(f, "%ms", profile_name) != 1) {
-		fclose(f);
 		pr_perror("err scanfing");
+		fclose(f);
 		return -1;
 	}
 
@@ -111,19 +111,23 @@ static int selinux_get_label(pid_t pid, char **output)
 static int selinux_get_sockcreate_label(pid_t pid, char **output)
 {
 	FILE *f;
+	int ret;
 
 	f = fopen_proc(pid, "attr/sockcreate");
 	if (!f)
 		return -1;
 
-	fscanf(f, "%ms", output);
-	/*
-	 * No need to check the result of fscanf(). If there is something
-	 * in /proc/PID/attr/sockcreate it will be copied to *output. If
-	 * there is nothing it will stay NULL. So whatever fscanf() does
-	 * it should be correct.
-	 */
-
+	ret = fscanf(f, "%ms", output);
+	if (ret == -1 && errno != 0) {
+		pr_perror("Unable to parse /proc/%d/attr/sockcreate", pid);
+		/*
+		 * Only if the error indicator is set it is a real error.
+		 * -1 could also be EOF, which would mean that sockcreate
+		 * was just empty, which is the most common case.
+		 */
+		fclose(f);
+		return -1;
+	}
 	fclose(f);
 	return 0;
 }