From 8298a578b03b128987edfd304b6a2c9c010b48ee Mon Sep 17 00:00:00 2001 From: Evgeniy Dushistov Date: Sun, 7 Jul 2013 23:29:09 +0000 Subject: [PATCH] check fread calls --- src/libwrapper.cpp | 1 - src/readline.hpp | 2 +- src/sdcv.cpp | 6 ++- src/stardict_lib.cpp | 89 ++++++++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/libwrapper.cpp b/src/libwrapper.cpp index 8cc0559..dd66ce8 100644 --- a/src/libwrapper.cpp +++ b/src/libwrapper.cpp @@ -42,7 +42,6 @@ static const char ESC_GREEN[] = "\033[0;32m"; static const char *SEARCH_TERM_VISFMT = ESC_BOLD; static const char *NAME_OF_DICT_VISFMT = ESC_BLUE; static const char *TRANSCRIPTION_VISFMT = ESC_BOLD; -static const char *DEF_COLOR_VISFMT = ESC_BLUE; static const char *EXAMPLE_VISFMT = ESC_LIGHT_GRAY; static const char *KREF_VISFMT = ESC_BOLD; static const char *ABR_VISFMT = ESC_GREEN; diff --git a/src/readline.hpp b/src/readline.hpp index 2911859..4f46b46 100644 --- a/src/readline.hpp +++ b/src/readline.hpp @@ -6,7 +6,7 @@ class IReadLine { public: virtual ~IReadLine() {} virtual bool read(const std::string &banner, std::string& line) = 0; - virtual void add_to_history(const std::string& phrase) {} + virtual void add_to_history(const std::string&) {} }; extern std::string sdcv_readline; diff --git a/src/sdcv.cpp b/src/sdcv.cpp index fdf1f7f..fa88632 100644 --- a/src/sdcv.cpp +++ b/src/sdcv.cpp @@ -55,8 +55,7 @@ namespace glib { typedef ResourceWrapper StrArr; } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) try { setlocale(LC_ALL, ""); #if ENABLE_NLS bindtextdomain("sdcv", @@ -197,4 +196,7 @@ int main(int argc, char *argv[]) fprintf(stderr, _("There are no words/phrases to translate.\n")); return EXIT_SUCCESS; +} catch (const std::exception& ex) { + fprintf(stderr, "Internal error: %s\n", ex.what()); + exit(EXIT_FAILURE); } diff --git a/src/stardict_lib.cpp b/src/stardict_lib.cpp index 5a9d8fc..02feed2 100644 --- a/src/stardict_lib.cpp +++ b/src/stardict_lib.cpp @@ -16,6 +16,16 @@ #include "stardict_lib.hpp" + +#define TO_STR2(xstr) #xstr +#define TO_STR1(xstr) TO_STR2(xstr) + +#define THROW_IF_ERROR(expr) do { \ + assert((expr)); \ + if (!(expr)) \ + throw std::runtime_error(#expr " not true at " __FILE__ ": " TO_STR1(__LINE__)); \ + } while (false) + // Notice: read src/tools/DICTFILE_FORMAT for the dictionary // file's format information! @@ -182,12 +192,13 @@ gchar* DictBase::GetWordData(guint32 idxitem_offset, guint32 idxitem_size) gchar *data; if (!sametypesequence.empty()) { - gchar *origin_data = (gchar *)g_malloc(idxitem_size); + glib::CharStr origin_data((gchar *)g_malloc(idxitem_size)); - if (dictfile) - fread(origin_data, idxitem_size, 1, dictfile); - else - dictdzfile->read(origin_data, idxitem_offset, idxitem_size); + if (dictfile) { + const size_t nitems = fread(get_impl(origin_data), idxitem_size, 1, dictfile); + THROW_IF_ERROR(nitems == 1); + } else + dictdzfile->read(get_impl(origin_data), idxitem_offset, idxitem_size); guint32 data_size; gint sametypesequence_len = sametypesequence.length(); @@ -218,7 +229,7 @@ gchar* DictBase::GetWordData(guint32 idxitem_offset, guint32 idxitem_size) data = (gchar *)g_malloc(data_size); gchar *p1,*p2; p1 = data + sizeof(guint32); - p2 = origin_data; + p2 = get_impl(origin_data); guint32 sec_size; //copy the head items. for (int i=0; iread(data+sizeof(guint32), idxitem_offset, idxitem_size); set_uint32(data, idxitem_size+sizeof(guint32)); } @@ -318,9 +329,10 @@ bool DictBase::SearchData(std::vector &SearchWords, guint32 idxitem if (dictfile) fseek(dictfile, idxitem_offset, SEEK_SET); - if (dictfile) - fread(origin_data, idxitem_size, 1, dictfile); - else + if (dictfile) { + const size_t nitems = fread(origin_data, idxitem_size, 1, dictfile); + THROW_IF_ERROR(nitems == 1); + } else dictdzfile->read(origin_data, idxitem_offset, idxitem_size); gchar *p = origin_data; guint32 sec_size; @@ -504,10 +516,11 @@ namespace { inline const gchar *OffsetIndex::read_first_on_page_key(glong page_idx) { fseek(idxfile, wordoffset[page_idx], SEEK_SET); - guint32 page_size=wordoffset[page_idx+1]-wordoffset[page_idx]; - fread(wordentry_buf, - std::min(sizeof(wordentry_buf), static_cast(page_size)), - 1, idxfile); + guint32 page_size = wordoffset[page_idx + 1] - wordoffset[page_idx]; + const size_t nitems = fread(wordentry_buf, + std::min(sizeof(wordentry_buf), static_cast(page_size)), + 1, idxfile); + THROW_IF_ERROR(nitems == 1); //TODO: check returned values, deal with word entry that strlen>255. return wordentry_buf; } @@ -631,16 +644,18 @@ namespace { inline gulong OffsetIndex::load_page(glong page_idx) { - gulong nentr=ENTR_PER_PAGE; - if (page_idx==glong(wordoffset.size()-2)) - if ((nentr=wordcount%ENTR_PER_PAGE)==0) - nentr=ENTR_PER_PAGE; + gulong nentr = ENTR_PER_PAGE; + if (page_idx == glong(wordoffset.size()-2)) + if ((nentr = (wordcount % ENTR_PER_PAGE)) == 0) + nentr = ENTR_PER_PAGE; - if (page_idx!=page.idx) { + if (page_idx != page.idx) { page_data.resize(wordoffset[page_idx+1]-wordoffset[page_idx]); fseek(idxfile, wordoffset[page_idx], SEEK_SET); - fread(&page_data[0], 1, page_data.size(), idxfile); + const size_t nitems = fread(&page_data[0], 1, page_data.size(), idxfile); + THROW_IF_ERROR(nitems == page_data.size()); + page.fill(&page_data[0], nentr, page_idx); } @@ -691,29 +706,29 @@ namespace { idx = iThisIndex; } if (!bFound) { - gulong netr=load_page(idx); - iFrom=1; // Needn't search the first word anymore. - iTo=netr-1; - iThisIndex=0; - while (iFrom<=iTo) { - iThisIndex=(iFrom+iTo)/2; + gulong netr = load_page(idx); + iFrom = 1; // Needn't search the first word anymore. + iTo = netr-1; + iThisIndex = 0; + while (iFrom <= iTo) { + iThisIndex = (iFrom + iTo) / 2; cmpint = stardict_strcmp(str, page.entries[iThisIndex].keystr); - if (cmpint>0) - iFrom=iThisIndex+1; - else if (cmpint<0) - iTo=iThisIndex-1; + if (cmpint > 0) + iFrom = iThisIndex+1; + else if (cmpint < 0) + iTo = iThisIndex-1; else { - bFound=true; + bFound = true; break; } } - idx*=ENTR_PER_PAGE; + idx *= ENTR_PER_PAGE; if (!bFound) idx += iFrom; //next else idx += iThisIndex; } else { - idx*=ENTR_PER_PAGE; + idx *= ENTR_PER_PAGE; } return bFound; }