From 67207e20f3dc8b9e92e45560776b5b304f964cc1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 7 Nov 2020 07:56:08 -0500 Subject: Fixed segfaults for invalid module names --- Makefile | 7 +++++-- bible.cc | 8 ++++---- libbible.cc | 10 +++++++++- readme.md | 14 ++++---------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 61957d7..c10be48 100644 --- a/Makefile +++ b/Makefile @@ -5,17 +5,20 @@ LDFLAGS= SOURCES=libbible.cc mods.cc settings.cc OBJECTS=$(SOURCES:.cc=.o) LIBRARY=libbible.so +EXECUTABLE=bible ifeq ($(PREFIX),) PREFIX := /usr endif -all: $(SOURCES) $(LIBRARY) +all: $(SOURCES) $(LIBRARY) $(EXECUTABLE) -install: $(LIBRARY) +install: $(LIBRARY) $(EXECUTABLE) install -d $(DESTDIR)$(PREFIX)/lib/ install -m 644 $(LIBRARY) $(DESTDIR)$(PREFIX)/lib/ install -d $(DESTDIR)$(PREFIX)/include/ install -m 644 libbible.h $(DESTDIR)$(PREFIX)/include/ + install -d $(DESTDIR)$(PREFIX)/bin/ + install -m 644 bible $(DESTDIR)$(PREFIX)/bin/ test: $(OBJECTS) testLibbible.o $(CC) $(LDFLAGS) $(OBJECTS) testLibbible.o -o $@ `pkg-config $(LIBS) --libs` -lcppunit diff --git a/bible.cc b/bible.cc index b0e3ed3..3388364 100644 --- a/bible.cc +++ b/bible.cc @@ -19,7 +19,7 @@ void usage() { printf(" --list-installable= list bible versions available for download and install. Default lists for all languages.\n"); printf(" --install-network install module from the network where is LANG:NAME as provided by --list-installable\n"); printf(" --install-zip install module from a zip file\n"); - printf(" --remove-mod delete a mod from the system\n"); + printf(" --remove-module delete a module from the system\n"); printf("\n\nExamples:\n bible Gal 5:22-23\n"); printf(" bible John 3:16\n bible Romans 12\n bible Matt 5:3-7:27\n"); printf(" bible Genesis 1-3\n"); @@ -49,7 +49,7 @@ void setDefaultModule(string modname) { void listBooks(string modname) { map> mods = libbible::getModules(); if(mods.find(modname) == mods.end()) { - printf("ERROR: Module \"%s\" not installed!", modname.c_str()); + printf("ERROR: Module \"%s\" not installed!\n", modname.c_str()); } else { printf("Books in Module %s:\n", modname.c_str()); for(string book : mods[modname]) { @@ -113,7 +113,7 @@ int main(int argc, char* argv[]) { {"list-installable", optional_argument, 0, 0}, {"install-network", required_argument, 0, 0}, {"install-zip", required_argument, 0, 0}, - {"remove-mod", required_argument, 0, 0} + {"remove-module", required_argument, 0, 0} }; int opt, option_index; string modname; @@ -153,7 +153,7 @@ int main(int argc, char* argv[]) { installNetwork(string(optarg)); } else if(option == "install-zip") { installZip(string(optarg)); - } else if(option == "remove-mod") { + } else if(option == "remove-module") { removeMod(string(optarg)); } break; diff --git a/libbible.cc b/libbible.cc index 7b08174..29e8873 100644 --- a/libbible.cc +++ b/libbible.cc @@ -42,6 +42,10 @@ map> libbible::getModules() { vector libbible::getPassages(string modName, string book) { vector passages; SWModule *target = library.getModule(modName.c_str()); + if(target == nullptr) { + // Module doesn't exist + return passages; + } target->setKey((book + " " + "1").c_str()); VerseKey *key = (VerseKey *) target->getKey(); int maxChapter = key->getChapterMax(); @@ -124,13 +128,17 @@ void inferMissing(libbible::passage *pass, SWModule *target) { } vector libbible::getText(libbible::passage pass) { + vector texts; SWModule *target = library.getModule(pass.modName.c_str()); + if(target == nullptr) { + // Module doesn't exist + return texts; + } inferMissing(&pass, target); target->setKey((pass.book + " " + to_string(pass.chapterStart) + ":" + to_string(pass.verseStart)).c_str()); VerseKey *key = (VerseKey *) target->getKey(); - vector texts; bool endOfParagraph = false; diff --git a/readme.md b/readme.md index 21be739..7eca841 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ This library provides a simplified interface to the SWORD project. ##libbible.so -The library can be compiled into a sharable object file and installed to the system. This is the default make option: +The library can be compiled into a sharable object file and installed to the system: ```console make @@ -15,16 +15,10 @@ The full interface is available in libbible.h. ##bible -An example cli is included with libbible, which can be compiled using: +An example cli is included with libbible. It is designed to showcase the functionality of libbible: ```console -make bible -``` - -The interface is simplistic, and uses a subset of libbible's functionality: - -```console -./bible --help +bible --help Usage: bible [options] [reference] @@ -42,7 +36,7 @@ Options: --list-installable= list bible versions available for download and install. Default lists for all languages. --install-network install module from the network where is LANG:NAME as provided by --list-installable --install-zip install module from a zip file - --remove-mod delete a mod from the system + --remove-module delete a mod from the system Examples: -- cgit v1.2.3