From f03180d06a039d82b4e99ab28f61a64588eab0d4 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Sun, 9 Oct 2022 12:18:49 +0200 Subject: [PATCH] fuzzing: Simplify converters and used common defines for exit code. --- tests/fuzz/plain_json_proto_to_luks2.cc | 18 +++++++++--------- tests/fuzz/proto_to_luks2.cc | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/fuzz/plain_json_proto_to_luks2.cc b/tests/fuzz/plain_json_proto_to_luks2.cc index 2f718b08..3b664674 100644 --- a/tests/fuzz/plain_json_proto_to_luks2.cc +++ b/tests/fuzz/plain_json_proto_to_luks2.cc @@ -37,25 +37,27 @@ using namespace json_proto; int main(int argc, char *argv[]) { LUKS2_both_headers headers; LUKS2ProtoConverter converter; + int fd; std::string out_img_name; if (argc != 2) { std::cerr << "Usage: " << argv[0] << " \n"; - return 1; + return EXIT_FAILURE; } - int fd = open(argv[1], O_RDONLY); + fd = open(argv[1], O_RDONLY); if (fd < 0) { std::cerr << "Failed to open " << argv[1] << std::endl; - return 1; + return EXIT_FAILURE; } google::protobuf::io::FileInputStream fileInput(fd); if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) { std::cerr << "Failed to parse protobuf " << argv[1] << std::endl; - goto out; + close(fd); + return EXIT_FAILURE; } close(fd); @@ -65,13 +67,11 @@ int main(int argc, char *argv[]) { fd = open(out_img_name.c_str(), O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_TRUNC, 0644); if (fd < 0) { std::cerr << "Failed to open output file " << out_img_name << std::endl; - goto out; + return EXIT_FAILURE; } converter.set_write_headers_only(false); converter.convert(headers, fd); -out: - if (fd >= 0) - close(fd); - return 0; + close(fd); + return EXIT_SUCCESS; } diff --git a/tests/fuzz/proto_to_luks2.cc b/tests/fuzz/proto_to_luks2.cc index 9ea10306..3ac58053 100644 --- a/tests/fuzz/proto_to_luks2.cc +++ b/tests/fuzz/proto_to_luks2.cc @@ -37,25 +37,27 @@ using namespace LUKS2_proto; int main(int argc, char *argv[]) { LUKS2_both_headers headers; LUKS2ProtoConverter converter; + int fd; std::string out_img_name; if (argc != 2) { std::cerr << "Usage: " << argv[0] << " \n"; - return 1; + return EXIT_FAILURE; } - int fd = open(argv[1], O_RDONLY); + fd = open(argv[1], O_RDONLY); if (fd < 0) { std::cerr << "Failed to open " << argv[1] << std::endl; - return 1; + return EXIT_FAILURE; } google::protobuf::io::FileInputStream fileInput(fd); if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) { std::cerr << "Failed to parse protobuf " << argv[1] << std::endl; - goto out; + close(fd); + return EXIT_FAILURE; } close(fd); @@ -65,13 +67,11 @@ int main(int argc, char *argv[]) { fd = open(out_img_name.c_str(), O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_TRUNC, 0644); if (fd < 0) { std::cerr << "Failed to open output file " << out_img_name << std::endl; - goto out; + return EXIT_FAILURE; } converter.set_write_headers_only(false); converter.convert(headers, fd); -out: - if (fd >= 0) - close(fd); - return 0; + close(fd); + return EXIT_SUCCESS; }