From ea9226df0bd7e820d1dbcad839265f1d5c4a8ff7 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 7 Jun 2024 21:42:53 -0400 Subject: prevent null dereference on bad search term --- booki.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'booki.c') diff --git a/booki.c b/booki.c index 5b884d9..c780c44 100644 --- a/booki.c +++ b/booki.c @@ -765,6 +765,11 @@ struct search_opt parse_search_options(int argc, char* argv[]) { edit = true; break; case '?': + if (!argv[optind] || argv[optind][0] == '-') { + printf("%s requires an operand\n", argv[optind-1]); + opt_out.count = -1; + return opt_out; + } // optind points at the argument (one past the option) opt_out.opts[count] = argv[optind-1] + 2; // '--example' -> 'example' opt_out.args[count] = argv[optind]; @@ -786,6 +791,9 @@ struct search_opt parse_search_options(int argc, char* argv[]) { void search(int argc, char* argv[], char* booki_file) { struct search_opt search_opts = parse_search_options(argc, argv); + if (search_opts.count == -1) { + return; + } // get the books array char* data = load_file(booki_file); -- cgit v1.2.3