Skip to contentSkip to conversation

fgets function warnings fix

PATCH New Stephen Smalley 1 participant 1 message Feb 26, 2015 1 message not in the archive SELinux security module

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CA+2y1146jZMaJUCwsoKJjZSNx3NUOe69CiNoDBnX+BdHdbZGdw@mail.gmail.com

#1 lore raw

PATCH 1/1 fgets function warnings fix

-------- Forwarded Message --------
Subject: [PATCH] fgets function warnings fix
Date: Thu, 26 Feb 2015 11:06:58 +0200
From: Emre Can Kucukoglu <[email protected]>
To: Stephen Smalley <[email protected]>

2015-02-25 20:25 GMT+02:00 Stephen Smalley <[email protected] <mailto:[email protected]>>:

On 02/25/2015 10:42 AM, Emre Can Kucukoglu wrote:
> Hello,
>
> I want to use SELinux for my experimental vanilla kernel distribution in
> Pandaboard ES. To achieve that I have configured my kernel for SELinux > support. Then, I have recently cloned selinux userspace repository to > cross-compile it for arm architecture. I use arm-linux-gnueabi toolchain
> in Ubuntu 14.04 workstation.
>
> $ export ARCH=arm
> $ export CROSS_COMPILE=arm-linux-gnueabi-
>
> As mentioned in readme instruction,
>
> $ make DESTDIR=~/obj install install-pywrap
>
> command is used, however due to -Werror flags in various Makefile's, > error is thrown:
>
> dispol.c: In function ‘main’:
> dispol.c:452:8: error: ignoring return value of ‘fgets’, declared with
> attribute warn_unused_result [-Werror=unused-result]
>Â Â fgets(ans, sizeof(ans), stdin);
>Â Â Â Â Â ^
> dispol.c:479:9: error: ignoring return value of ‘fgets’, declared with
> attribute warn_unused_result [-Werror=unused-result]
>Â Â Â fgets(ans, sizeof(ans), stdin);
>Â Â Â Â Â ^
> dispol.c:490:9: error: ignoring return value of ‘fgets’, declared with
> attribute warn_unused_result [-Werror=unused-result]
>Â Â Â fgets(ans, sizeof(ans), stdin);
>Â Â Â Â Â ^
> dispol.c:517:9: error: ignoring return value of ‘fgets’, declared with
> attribute warn_unused_result [-Werror=unused-result]
>Â Â Â fgets(OutfileName, sizeof(OutfileName), stdin);
>Â Â Â Â Â ^
> cc1: all warnings being treated as errors
> make[2]: *** [dispol.o] Error 1
>
> I did this modification to get over this error which is caused by > -Werror flag:
>
> char *fgetsret; /* error is given, because return value is not captured */
> fgetsret = fgets(...);
> if (!fgetsret) { /* if fgetsret is not used, another error comes from > warning */
> // some exit message
> exit(1);
> }
>
> Did I miss something about SELinux cross-compilation for an arm > architecture, since constantly I'm taking errors.

Hmm...that's interesting. Builds just fine on Fedora 20. I do however
get the same error building on Ubuntu 14.04 (which is not something we typically test on; most SELinux developers work on Fedora).

Willing to accept patches to fix it, but it is merely a test/debugging program that you can even exclude from your build if you like.

checkpolicy/test/dismod.c and checkpolicy/test/dispol.c both have same warnings.

<mailto:[email protected]>>
---
 checkpolicy/test/dismod.c | 20 +++++++++++++++++---  checkpolicy/test/dispol.c | 24 ++++++++++++++++++++----  2 files changed, 37 insertions(+), 7 deletions(-)

checkpolicy/test/dismod.c +0 -0
index db9ae55..c55f726 100644
@@ -755,7 +755,12 @@ static void link_module(policydb_t * base, FILE *

out_fp)
 return;
 }
 printf("\nModule filename: ");
-fgets(module_name, sizeof(module_name), stdin);
+if (fgets(module_name, sizeof(module_name), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+exit(1);
+}
+
 module_name[strlen(module_name) - 1] = '\0';/* remove LF */  if (module_name[0] == '\0') {
 return;
@@ -884,7 +889,12 @@ int main(int argc, char **argv) Â menu();
 for (;;) {
 printf("\nCommand (\'m\' for menu):  "); -fgets(ans, sizeof(ans), stdin);
+if (fgets(ans, sizeof(ans), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+continue;
+}
+
 switch (ans[0]) {

 case '1':
@@ -951,7 +961,11 @@ int main(int argc, char **argv)
 case 'f':
 printf
   ("\nFilename for output (<CR> for screen output): "); -fgets(OutfileName, sizeof(OutfileName), stdin);
+if (fgets(OutfileName, sizeof(OutfileName), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+break;
+}
 OutfileName[strlen(OutfileName) - 1] = '\0';/* fix_string (remove LF) */  if (strlen(OutfileName) == 0)
 out_fp = stdout;

checkpolicy/test/dispol.c +0 -0
index 9d66358..0333bcc 100644
@@ -449,7 +449,11 @@ int main(int argc, char **argv)

 menu();
 for (;;) {
 printf("\nCommand (\'m\' for menu):  "); -fgets(ans, sizeof(ans), stdin);
+if (fgets(ans, sizeof(ans), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+continue;
+}
 switch (ans[0]) {

 case '1':
@@ -476,7 +480,11 @@ int main(int argc, char **argv) Â break;
 case '7':
 printf("name? ");
-fgets(ans, sizeof(ans), stdin);
+if (fgets(ans, sizeof(ans), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+break;
+}
 ans[strlen(ans) - 1] = 0;

 name = malloc((strlen(ans) + 1) * sizeof(char)); @@ -487,7 +495,11 @@ int main(int argc, char **argv)  strcpy(name, ans);

 printf("state? ");
-fgets(ans, sizeof(ans), stdin);
+if (fgets(ans, sizeof(ans), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+break;
+}
 ans[strlen(ans) - 1] = 0;

 if (atoi(ans))
@@ -514,7 +526,11 @@ int main(int argc, char **argv)
 case 'f':
 printf
   ("\nFilename for output (<CR> for screen output): "); -fgets(OutfileName, sizeof(OutfileName), stdin);
+if (fgets(OutfileName, sizeof(OutfileName), stdin) == NULL) { +fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__, +strerror(errno));
+break;
+}
 OutfileName[strlen(OutfileName) - 1] = '\0';/* fix_string (remove LF) */  if (strlen(OutfileName) == 0)
 out_fp = stdout;
--
1.9.1

Signature · 1 line
Emre Can Kucukoglu
Quoted 1 line

From dc27dab37d82823095971789fef105eea77aa38a Mon Sep 17 00:00:00 2001

From: Emre Can Kucukoglu <[email protected]>
Date: Thu, 26 Feb 2015 10:22:48 +0200
Subject: [PATCH] fgets function warnings fix

---
 checkpolicy/test/dismod.c | 20 +++++++++++++++++---
 checkpolicy/test/dispol.c | 24 ++++++++++++++++++++----
 2 files changed, 37 insertions(+), 7 deletions(-)
checkpolicy/test/dismod.c +17 -3
index db9ae55..c55f726 100644
@@ -755,7 +755,12 @@ static void link_module(policydb_t * base, FILE * out_fp)
 		return;
 	}
 	printf("\nModule filename: ");
-	fgets(module_name, sizeof(module_name), stdin);
+	if (fgets(module_name, sizeof(module_name), stdin) == NULL) {
+		fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+				strerror(errno));
+		exit(1);
+	}
+
 	module_name[strlen(module_name) - 1] = '\0';	/* remove LF */
 	if (module_name[0] == '\0') {
 		return;
@@ -884,7 +889,12 @@ int main(int argc, char **argv)
 	menu();
 	for (;;) {
 		printf("\nCommand (\'m\' for menu):  ");
-		fgets(ans, sizeof(ans), stdin);
+		if (fgets(ans, sizeof(ans), stdin) == NULL) {
+			fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+					strerror(errno));
+			continue;
+		}
+
 		switch (ans[0]) {
 
 		case '1':
@@ -951,7 +961,11 @@ int main(int argc, char **argv)
 		case 'f':
 			printf
 			    ("\nFilename for output (<CR> for screen output): ");
-			fgets(OutfileName, sizeof(OutfileName), stdin);
+			if (fgets(OutfileName, sizeof(OutfileName), stdin) == NULL) {
+				fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+						strerror(errno));
+				break;
+			}
 			OutfileName[strlen(OutfileName) - 1] = '\0';	/* fix_string (remove LF) */
 			if (strlen(OutfileName) == 0)
 				out_fp = stdout;
checkpolicy/test/dispol.c +20 -4
index 9d66358..0333bcc 100644
@@ -449,7 +449,11 @@ int main(int argc, char **argv)
 	menu();
 	for (;;) {
 		printf("\nCommand (\'m\' for menu):  ");
-		fgets(ans, sizeof(ans), stdin);
+		if (fgets(ans, sizeof(ans), stdin) == NULL) {
+			fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+					strerror(errno));
+			continue;
+		}
 		switch (ans[0]) {
 
 		case '1':
@@ -476,7 +480,11 @@ int main(int argc, char **argv)
 			break;
 		case '7':
 			printf("name? ");
-			fgets(ans, sizeof(ans), stdin);
+			if (fgets(ans, sizeof(ans), stdin) == NULL) {
+				fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+						strerror(errno));
+				break;
+			}
 			ans[strlen(ans) - 1] = 0;
 
 			name = malloc((strlen(ans) + 1) * sizeof(char));
@@ -487,7 +495,11 @@ int main(int argc, char **argv)
 			strcpy(name, ans);
 
 			printf("state? ");
-			fgets(ans, sizeof(ans), stdin);
+			if (fgets(ans, sizeof(ans), stdin) == NULL) {
+				fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+						strerror(errno));
+				break;
+			}
 			ans[strlen(ans) - 1] = 0;
 
 			if (atoi(ans))
@@ -514,7 +526,11 @@ int main(int argc, char **argv)
 		case 'f':
 			printf
 			    ("\nFilename for output (<CR> for screen output): ");
-			fgets(OutfileName, sizeof(OutfileName), stdin);
+			if (fgets(OutfileName, sizeof(OutfileName), stdin) == NULL) {
+				fprintf(stderr, "fgets failed at line %d: %s\n", __LINE__,
+						strerror(errno));
+				break;
+			}
 			OutfileName[strlen(OutfileName) - 1] = '\0';	/* fix_string (remove LF) */
 			if (strlen(OutfileName) == 0)
 				out_fp = stdout;

--
1.9.1