From 509102d6025e46d65cf6a164e685bae1f698367f Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Wed, 16 Sep 2015 12:19:28 +0100 Subject: [PATCH] Check 'fread' return value in program generated with '--skeleton'. --- re2c/src/codegen/skeleton/generate_code.cc | 35 ++++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/re2c/src/codegen/skeleton/generate_code.cc b/re2c/src/codegen/skeleton/generate_code.cc index 9cc0e26d..352f0f3c 100644 --- a/re2c/src/codegen/skeleton/generate_code.cc +++ b/re2c/src/codegen/skeleton/generate_code.cc @@ -112,28 +112,39 @@ void Skeleton::emit_start (OutputFile & o, uint32_t maxfill) const o << "\n"; o << "\n" << "int lex_" << name << " ()"; o << "\n" << "{"; - o << "\n" << indString << "FILE * finput = fopen (\"" << o.file_name << "." << name << ".input" << "\", \"rb\");"; + o << "\n" << indString << "const char * finput_name = \"" << o.file_name << "." << name << ".input\";"; + o << "\n" << indString << "FILE * finput = fopen (finput_name, \"rb\");"; o << "\n" << indString << "if (!finput)"; o << "\n" << indString << "{"; - o << "\n" << indString << indString << "fprintf (stderr, \"cannot open file '%s'\\n\", \"" << o.file_name << "." << name << ".input\");"; + o << "\n" << indString << indString << "fprintf (stderr, \"cannot open file '%s'\\n\", finput_name);"; o << "\n" << indString << indString << "return 1;"; o << "\n" << indString << "}"; - o << "\n" << indString << "FILE * fkeys = fopen (\"" << o.file_name << "." << name << ".keys" << "\", \"rb\");"; - o << "\n" << indString << "if (!fkeys)"; - o << "\n" << indString << "{"; - o << "\n" << indString << indString << "fprintf (stderr, \"cannot open file '%s'\\n\", \"" << o.file_name << "." << name << ".keys\");"; - o << "\n" << indString << indString << "return 1;"; - o << "\n" << indString << "}"; - o << "\n"; o << "\n" << indString << "const size_t input_len = filesize (finput) / sizeof (YYCTYPE);"; o << "\n" << indString << "const size_t padding = " << maxfill << "; // YYMAXFILL"; o << "\n" << indString << "YYCTYPE * input = (YYCTYPE *) malloc ((input_len + padding) * sizeof (YYCTYPE));"; - o << "\n" << indString << "fread (input, sizeof (YYCTYPE), input_len, finput);"; + o << "\n" << indString << "if (fread (input, sizeof (YYCTYPE), input_len, finput) != input_len)"; + o << "\n" << indString << "{"; + o << "\n" << indString << indString << "fprintf (stderr, \"cannot read file '%s'\\n\", finput_name);"; + o << "\n" << indString << indString << "return 1;"; + o << "\n" << indString << "}"; + o << "\n" << indString << "fclose (finput);"; o << "\n" << indString << "memset (input + input_len, 0, padding * sizeof (YYCTYPE));"; o << "\n"; + o << "\n" << indString << "const char * fkeys_name = \"" << o.file_name << "." << name << ".keys\";"; + o << "\n" << indString << "FILE * fkeys = fopen (fkeys_name, \"rb\");"; + o << "\n" << indString << "if (!fkeys)"; + o << "\n" << indString << "{"; + o << "\n" << indString << indString << "fprintf (stderr, \"cannot open file '%s'\\n\", fkeys_name);"; + o << "\n" << indString << indString << "return 1;"; + o << "\n" << indString << "}"; o << "\n" << indString << "const size_t keys_size = filesize (fkeys);"; o << "\n" << indString << "YYKEYTYPE * keys = (YYKEYTYPE *) malloc (keys_size);"; - o << "\n" << indString << "fread (keys, 1, keys_size, fkeys);"; + o << "\n" << indString << "if (fread (keys, 1, keys_size, fkeys) != keys_size)"; + o << "\n" << indString << "{"; + o << "\n" << indString << indString << "fprintf (stderr, \"cannot read file '%s'\\n\", fkeys_name);"; + o << "\n" << indString << indString << "return 1;"; + o << "\n" << indString << "}"; + o << "\n" << indString << "fclose (fkeys);"; o << "\n" << indString << "const size_t keys_count = keys_size / (3 * sizeof (YYKEYTYPE));"; o << "\n"; o << "\n" << indString << "const YYCTYPE * cursor = input;"; @@ -175,8 +186,6 @@ void Skeleton::emit_end (OutputFile & o) const o << "\n" << indString << "}"; o << "\n" << indString << "free (input);"; o << "\n" << indString << "free (keys);"; - o << "\n" << indString << "fclose (finput);"; - o << "\n" << indString << "fclose (fkeys);"; o << "\n"; o << "\n" << indString << "return status;"; o << "\n" << "}"; -- 2.40.0