linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] ASN.1: Fine-tuning for three function implementations
@ 2017-11-10 12:45 SF Markus Elfring
  2017-11-10 12:46 ` [PATCH 1/4] ASN.1: Adjust two function calls together with a variable assignment SF Markus Elfring
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-10 12:45 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton; +Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 10 Nov 2017 13:38:43 +0100

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
  Adjust two function calls together with a variable assignment
  Improve exiting from parse_type()
  Improve exiting from build_type_list()
  Use common error handling code in main()

 scripts/asn1_compiler.c | 100 ++++++++++++++++++++++++------------------------
 1 file changed, 51 insertions(+), 49 deletions(-)

-- 
2.15.0

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] ASN.1: Adjust two function calls together with a variable assignment
  2017-11-10 12:45 [PATCH 0/4] ASN.1: Fine-tuning for three function implementations SF Markus Elfring
@ 2017-11-10 12:46 ` SF Markus Elfring
  2017-11-10 12:48 ` [PATCH 2/4] ASN.1: Improve exiting from parse_type() SF Markus Elfring
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-10 12:46 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton; +Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 10 Nov 2017 10:40:17 +0100

The script "checkpatch.pl" pointed information out like the following.

ERROR: do not use assignment in if condition

Thus fix the affected source code places.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 scripts/asn1_compiler.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/scripts/asn1_compiler.c b/scripts/asn1_compiler.c
index c1b7ef3e24c1..e37e5ef3fabb 100644
--- a/scripts/asn1_compiler.c
+++ b/scripts/asn1_compiler.c
@@ -606,12 +606,14 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	if (!(buffer = malloc(st.st_size + 1))) {
+	buffer = malloc(st.st_size + 1);
+	if (!buffer) {
 		perror(NULL);
 		exit(1);
 	}
 
-	if ((readlen = read(fd, buffer, st.st_size)) < 0) {
+	readlen = read(fd, buffer, st.st_size);
+	if (readlen < 0) {
 		perror(filename);
 		exit(1);
 	}
-- 
2.15.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] ASN.1: Improve exiting from parse_type()
  2017-11-10 12:45 [PATCH 0/4] ASN.1: Fine-tuning for three function implementations SF Markus Elfring
  2017-11-10 12:46 ` [PATCH 1/4] ASN.1: Adjust two function calls together with a variable assignment SF Markus Elfring
@ 2017-11-10 12:48 ` SF Markus Elfring
  2017-11-10 12:49 ` [PATCH 3/4] ASN.1: Improve exiting from build_type_list() SF Markus Elfring
  2017-11-10 12:50 ` [PATCH 4/4] ASN.1: Use common error handling code in main() SF Markus Elfring
  3 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-10 12:48 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton; +Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 10 Nov 2017 11:21:13 +0100

* Add a jump target so that a call of the function "exit" is stored
  only once at the end of this function.

* Replace nine calls by goto statements.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 scripts/asn1_compiler.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/scripts/asn1_compiler.c b/scripts/asn1_compiler.c
index e37e5ef3fabb..0eea270fcaef 100644
--- a/scripts/asn1_compiler.c
+++ b/scripts/asn1_compiler.c
@@ -896,7 +896,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		default:
 			fprintf(stderr, "%s:%d: Unrecognised tag class token '%s'\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 
 		if (cursor >= end)
@@ -904,7 +904,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		if (cursor->token_type != TOKEN_NUMBER) {
 			fprintf(stderr, "%s:%d: Missing tag number '%s'\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 
 		element->tag &= ~0x1f;
@@ -919,7 +919,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		if (cursor->token_type != TOKEN_CLOSE_SQUARE) {
 			fprintf(stderr, "%s:%d: Missing closing square bracket '%s'\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 		cursor++;
 		if (cursor >= end)
@@ -1020,7 +1020,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		if (!ref) {
 			fprintf(stderr, "%s:%d: Type '%s' undefined\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 		cursor->type = *ref;
 		(*ref)->ref_count++;
@@ -1070,7 +1070,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 	default:
 		fprintf(stderr, "%s:%d: Token '%s' does not introduce a type\n",
 			filename, cursor->line, cursor->content);
-		exit(1);
+		goto exit;
 	}
 
 	/* Handle elements that are optional */
@@ -1088,13 +1088,13 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		if (cursor->token_type != TOKEN_ELEMENT_NAME) {
 			fprintf(stderr, "%s:%d: Token '%s' is not an action function name\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 
 		action = malloc(sizeof(struct action));
 		if (!action) {
 			perror(NULL);
-			exit(1);
+			goto exit;
 		}
 		action->index = 0;
 		action->name = cursor->content;
@@ -1129,7 +1129,7 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 		if (cursor->token_type != TOKEN_CLOSE_ACTION) {
 			fprintf(stderr, "%s:%d: Missing close action, got '%s'\n",
 				filename, cursor->line, cursor->content);
-			exit(1);
+			goto exit;
 		}
 		cursor++;
 	}
@@ -1140,10 +1140,11 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 parse_error:
 	fprintf(stderr, "%s:%d: Unexpected token '%s'\n",
 		filename, cursor->line, cursor->content);
-	exit(1);
+	goto exit;
 
 overrun_error:
 	fprintf(stderr, "%s: Unexpectedly hit EOF\n", filename);
+exit:
 	exit(1);
 }
 
-- 
2.15.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] ASN.1: Improve exiting from build_type_list()
  2017-11-10 12:45 [PATCH 0/4] ASN.1: Fine-tuning for three function implementations SF Markus Elfring
  2017-11-10 12:46 ` [PATCH 1/4] ASN.1: Adjust two function calls together with a variable assignment SF Markus Elfring
  2017-11-10 12:48 ` [PATCH 2/4] ASN.1: Improve exiting from parse_type() SF Markus Elfring
@ 2017-11-10 12:49 ` SF Markus Elfring
  2017-11-10 12:50 ` [PATCH 4/4] ASN.1: Use common error handling code in main() SF Markus Elfring
  3 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-10 12:49 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton; +Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 10 Nov 2017 12:56:33 +0100

* Add jump targets so that a call of the function "perror" and "exit"
  is stored only once in an if branch of this function.

* Replace two calls by goto statements.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 scripts/asn1_compiler.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/scripts/asn1_compiler.c b/scripts/asn1_compiler.c
index 0eea270fcaef..06dc5397d8c8 100644
--- a/scripts/asn1_compiler.c
+++ b/scripts/asn1_compiler.c
@@ -762,18 +762,19 @@ static void build_type_list(void)
 
 	if (nr == 0) {
 		fprintf(stderr, "%s: No defined types\n", filename);
-		exit(1);
+		goto exit;
 	}
 
 	nr_types = nr;
 	types = type_list = calloc(nr + 1, sizeof(type_list[0]));
-	if (!type_list) {
-		perror(NULL);
-		exit(1);
-	}
+	if (!type_list)
+		goto report_failure;
+
 	type_index = calloc(nr, sizeof(type_index[0]));
 	if (!type_index) {
+report_failure:
 		perror(NULL);
+exit:
 		exit(1);
 	}
 
-- 
2.15.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] ASN.1: Use common error handling code in main()
  2017-11-10 12:45 [PATCH 0/4] ASN.1: Fine-tuning for three function implementations SF Markus Elfring
                   ` (2 preceding siblings ...)
  2017-11-10 12:49 ` [PATCH 3/4] ASN.1: Improve exiting from build_type_list() SF Markus Elfring
@ 2017-11-10 12:50 ` SF Markus Elfring
  3 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-10 12:50 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton; +Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 10 Nov 2017 13:31:22 +0100

* Add jump targets so that a bit of exception handling can be better reused
  in this function implementation.

* Replace ten calls of the function "exit" by goto statements.

* Replace two function calls by return statements.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 scripts/asn1_compiler.c | 66 ++++++++++++++++++++++++-------------------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/scripts/asn1_compiler.c b/scripts/asn1_compiler.c
index 06dc5397d8c8..4df8d0b85d3c 100644
--- a/scripts/asn1_compiler.c
+++ b/scripts/asn1_compiler.c
@@ -568,6 +568,7 @@ int main(int argc, char **argv)
 	FILE *out, *hdr;
 	char *buffer, *p;
 	char *kbuild_verbose;
+	char const *hint;
 	int fd;
 
 	kbuild_verbose = getenv("KBUILD_VERBOSE");
@@ -588,7 +589,7 @@ int main(int argc, char **argv)
 	if (argc != 4) {
 		fprintf(stderr, "Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n",
 			argv[0]);
-		exit(2);
+		return 2;
 	}
 
 	filename = argv[1];
@@ -596,44 +597,39 @@ int main(int argc, char **argv)
 	headername = argv[3];
 
 	fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		perror(filename);
-		exit(1);
-	}
+	if (fd < 0)
+		goto report_file_failure;
 
-	if (fstat(fd, &st) < 0) {
-		perror(filename);
-		exit(1);
-	}
+	if (fstat(fd, &st) < 0)
+		goto report_file_failure;
 
 	buffer = malloc(st.st_size + 1);
 	if (!buffer) {
-		perror(NULL);
-		exit(1);
+		hint = NULL;
+		goto report_failure;
 	}
 
 	readlen = read(fd, buffer, st.st_size);
-	if (readlen < 0) {
-		perror(filename);
-		exit(1);
-	}
+	if (readlen < 0)
+		goto report_file_failure;
 
 	if (close(fd) < 0) {
-		perror(filename);
-		exit(1);
+report_file_failure:
+		hint = filename;
+		goto report_failure;
 	}
 
 	if (readlen != st.st_size) {
 		fprintf(stderr, "%s: Short read\n", filename);
-		exit(1);
+		return 1;
 	}
 
 	p = strrchr(argv[1], '/');
 	p = p ? p + 1 : argv[1];
 	grammar_name = strdup(p);
 	if (!p) {
-		perror(NULL);
-		exit(1);
+		hint = NULL;
+		goto report_failure;
 	}
 	p = strchr(grammar_name, '.');
 	if (p)
@@ -646,30 +642,32 @@ int main(int argc, char **argv)
 	dump_elements();
 
 	out = fopen(outputname, "w");
-	if (!out) {
-		perror(outputname);
-		exit(1);
-	}
+	if (!out)
+		goto report_output_failure;
 
 	hdr = fopen(headername, "w");
-	if (!hdr) {
-		perror(headername);
-		exit(1);
-	}
+	if (!hdr)
+		goto report_header_failure;
 
 	render(out, hdr);
 
-	if (fclose(out) < 0) {
-		perror(outputname);
-		exit(1);
+	if (fclose(hdr) < 0) {
+report_header_failure:
+		hint = headername;
+		goto report_failure;
 	}
 
-	if (fclose(hdr) < 0) {
-		perror(headername);
-		exit(1);
+	if (fclose(out) < 0) {
+report_output_failure:
+		hint = outputname;
+		goto report_failure;
 	}
 
 	return 0;
+
+report_failure:
+	perror(hint);
+	return 1;
 }
 
 enum compound {
-- 
2.15.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-11-10 12:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-10 12:45 [PATCH 0/4] ASN.1: Fine-tuning for three function implementations SF Markus Elfring
2017-11-10 12:46 ` [PATCH 1/4] ASN.1: Adjust two function calls together with a variable assignment SF Markus Elfring
2017-11-10 12:48 ` [PATCH 2/4] ASN.1: Improve exiting from parse_type() SF Markus Elfring
2017-11-10 12:49 ` [PATCH 3/4] ASN.1: Improve exiting from build_type_list() SF Markus Elfring
2017-11-10 12:50 ` [PATCH 4/4] ASN.1: Use common error handling code in main() SF Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).