Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1510956

[PATCH 01/10] scripts/basic/bin2c: Complete error handling in main()

From SF Markus Elfring <elfring@users.sourceforge.net>
Newsgroups linux.kernel
Subject [PATCH 01/10] scripts/basic/bin2c: Complete error handling in main()
Date 2016-10-28 10:40 +0200
Message-ID <sxaRI-5SO-49@gated-at.bofh.it> (permalink)
References <sxaRH-5SO-5@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 27 Oct 2016 16:15:04 +0200

Return values were not checked from five calls of the function "printf".

This issue was detected also by using the Coccinelle software.


* Add a bit of exception handling there.

* Optimise this function implementation a bit.

  - Replace two output calls with the functions "fputs" and "puts".

  - Use the preincrement operator for the variable "total".

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

diff --git a/scripts/basic/bin2c.c b/scripts/basic/bin2c.c
index c3d7eef..c6c8860 100644
--- a/scripts/basic/bin2c.c
+++ b/scripts/basic/bin2c.c
@@ -8,29 +8,35 @@
  */
 
 #include <stdio.h>
+#include <errno.h>
 
 int main(int argc, char *argv[])
 {
 	int ch, total = 0;
 
 	if (argc > 1)
-		printf("const char %s[] %s=\n",
-			argv[1], argc > 2 ? argv[2] : "");
+		if (printf("const char %s[] %s=\n",
+			   argv[1], argc > 2 ? argv[2] : "") < 16)
+			return errno;
 
 	do {
-		printf("\t\"");
+		if (fputs("\t\"", stdout) < 0)
+			return errno;
+
 		while ((ch = getchar()) != EOF) {
-			total++;
-			printf("\\x%02x", ch);
-			if (total % 16 == 0)
+			if (printf("\\x%02x", ch) < 4)
+				return errno;
+			if (++total % 16 == 0)
 				break;
 		}
-		printf("\"\n");
+
+		if (puts("\"") < 0)
+			return errno;
 	} while (ch != EOF);
 
 	if (argc > 1)
-		printf("\t;\n\n#include <linux/types.h>\n\nconst size_t %s_size = %d;\n",
-		       argv[1], total);
-
+		if (printf("\t;\n\n#include <linux/types.h>\n\nconst size_t %s_size = %d;\n",
+			   argv[1], total) < 54)
+			return errno;
 	return 0;
 }
-- 
2.10.1

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 00/10] scripts/basic: Fine-tuning for seven function  implementations SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 08/10] scripts/basic/fixdep: Complete error handling in  print_config() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 06/10] scripts/basic/fixdep: Complete error handling in  do_config_file() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 05/10] scripts/basic/fixdep: Complete error handling in  parse_dep_file() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 07/10] scripts/basic/fixdep: Fix error log output in  do_config_file() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 03/10] scripts/basic/fixdep: Use the symbol "MAP_FAILED" in  print_deps() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
  [PATCH 01/10] scripts/basic/bin2c: Complete error handling in main() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:40 +0200
    Re: [PATCH 01/10] scripts/basic/bin2c: Complete error handling in main() Jim Davis <jim.epost@gmail.com> - 2016-10-29 00:40 +0200
    Re: [PATCH 01/10] scripts/basic/bin2c: Complete error handling in main() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-11-02 16:50 +0100
      Re: scripts/basic/bin2c: Complete error handling in main() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-11-02 18:50 +0100
        Re: scripts/basic/bin2c: Complete error handling in main() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-11-02 19:30 +0100
          Re: scripts/basic/bin2c: Complete error handling in main() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-11-02 19:50 +0100
            Re: scripts/basic/bin2c: Complete error handling in main() Michal Marek <mmarek@suse.com> - 2016-11-03 16:50 +0100
              Re: scripts/basic/bin2c: Complete error handling in main() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-11-03 20:50 +0100
                Re: scripts/basic/bin2c: Complete error handling in main() Michal Marek <mmarek@suse.com> - 2016-11-04 13:20 +0100
                Re: scripts/basic/bin2c: Complete error handling in main() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-11-04 14:50 +0100
  [PATCH 09/10] scripts/basic/fixdep: Complete error handling in  print_cmdline() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:50 +0200
    Re: [PATCH 09/10] scripts/basic/fixdep: Complete error handling in print_cmdline() Jim Davis <jim.epost@gmail.com> - 2016-10-29 01:50 +0200
      Re: scripts/basic/fixdep: Complete error handling in print_cmdline() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-30 16:20 +0100
      Re: [PATCH 09/10] scripts/basic/fixdep: Complete error handling in print_cmdline() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-11-02 16:40 +0100
        Re: scripts/basic/fixdep: Complete error handling in print_cmdline() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-11-02 18:40 +0100
          Re: scripts/basic/fixdep: Complete error handling in print_cmdline() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-11-02 19:40 +0100
            Re: scripts/basic/fixdep: Complete error handling in print_cmdline() Michal Marek <mmarek@suse.com> - 2016-11-03 17:00 +0100
  [PATCH 10/10] scripts/basic/fixdep: Combine two fprintf() calls into  one fputs() call in usage() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-10-28 10:50 +0200

csiph-web