fuzzing: Simplify converters and used common defines for exit code.

This commit is contained in:
Milan Broz
2022-10-09 12:18:49 +02:00
parent 39b94ae530
commit f03180d06a
2 changed files with 18 additions and 18 deletions

View File

@@ -37,25 +37,27 @@ using namespace json_proto;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
LUKS2_both_headers headers; LUKS2_both_headers headers;
LUKS2ProtoConverter converter; LUKS2ProtoConverter converter;
int fd;
std::string out_img_name; std::string out_img_name;
if (argc != 2) { if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <LUKS2 proto>\n"; std::cerr << "Usage: " << argv[0] << " <LUKS2 proto>\n";
return 1; return EXIT_FAILURE;
} }
int fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
if (fd < 0) { if (fd < 0) {
std::cerr << "Failed to open " << argv[1] << std::endl; std::cerr << "Failed to open " << argv[1] << std::endl;
return 1; return EXIT_FAILURE;
} }
google::protobuf::io::FileInputStream fileInput(fd); google::protobuf::io::FileInputStream fileInput(fd);
if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) { if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) {
std::cerr << "Failed to parse protobuf " << argv[1] << std::endl; std::cerr << "Failed to parse protobuf " << argv[1] << std::endl;
goto out; close(fd);
return EXIT_FAILURE;
} }
close(fd); 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); fd = open(out_img_name.c_str(), O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_TRUNC, 0644);
if (fd < 0) { if (fd < 0) {
std::cerr << "Failed to open output file " << out_img_name << std::endl; std::cerr << "Failed to open output file " << out_img_name << std::endl;
goto out; return EXIT_FAILURE;
} }
converter.set_write_headers_only(false); converter.set_write_headers_only(false);
converter.convert(headers, fd); converter.convert(headers, fd);
out: close(fd);
if (fd >= 0) return EXIT_SUCCESS;
close(fd);
return 0;
} }

View File

@@ -37,25 +37,27 @@ using namespace LUKS2_proto;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
LUKS2_both_headers headers; LUKS2_both_headers headers;
LUKS2ProtoConverter converter; LUKS2ProtoConverter converter;
int fd;
std::string out_img_name; std::string out_img_name;
if (argc != 2) { if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <LUKS2 proto>\n"; std::cerr << "Usage: " << argv[0] << " <LUKS2 proto>\n";
return 1; return EXIT_FAILURE;
} }
int fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
if (fd < 0) { if (fd < 0) {
std::cerr << "Failed to open " << argv[1] << std::endl; std::cerr << "Failed to open " << argv[1] << std::endl;
return 1; return EXIT_FAILURE;
} }
google::protobuf::io::FileInputStream fileInput(fd); google::protobuf::io::FileInputStream fileInput(fd);
if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) { if (!google::protobuf::TextFormat::Parse(&fileInput, &headers)) {
std::cerr << "Failed to parse protobuf " << argv[1] << std::endl; std::cerr << "Failed to parse protobuf " << argv[1] << std::endl;
goto out; close(fd);
return EXIT_FAILURE;
} }
close(fd); 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); fd = open(out_img_name.c_str(), O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_TRUNC, 0644);
if (fd < 0) { if (fd < 0) {
std::cerr << "Failed to open output file " << out_img_name << std::endl; std::cerr << "Failed to open output file " << out_img_name << std::endl;
goto out; return EXIT_FAILURE;
} }
converter.set_write_headers_only(false); converter.set_write_headers_only(false);
converter.convert(headers, fd); converter.convert(headers, fd);
out: close(fd);
if (fd >= 0) return EXIT_SUCCESS;
close(fd);
return 0;
} }