ASN.1: Fine-tuning for three function implementations
cover ASN.1: Fine-tuning for three function implementations
From: Markus Elfring <[email protected]>
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(-)
Signature · 1 line
2.15.0
PATCH 1/4 ASN.1: Adjust two function calls together with a variable assignment
From: Markus Elfring <[email protected]>
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.
--- scripts/asn1_compiler.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/asn1_compiler.c +4 -2
@@ -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 ); }
Signature · 1 line
2.15.0
PATCH 2/4 ASN.1: Improve exiting from parse_type()
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
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.
--- scripts/asn1_compiler.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
scripts/asn1_compiler.c +10 -9
@@ -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 ); }
Signature · 1 line
2.15.0
PATCH 3/4 ASN.1: Improve exiting from build_type_list()
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
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.
--- scripts/asn1_compiler.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
scripts/asn1_compiler.c +6 -5
@@ -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 ); }
Signature · 1 line
2.15.0
PATCH 4/4 ASN.1: Use common error handling code in main()
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
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.
--- scripts/asn1_compiler.c | 66 ++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 34 deletions(-)
scripts/asn1_compiler.c +32 -34
@@ -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 {
Signature · 1 line
2.15.0