check fread calls

This commit is contained in:
Evgeniy Dushistov
2013-07-07 23:29:09 +00:00
parent 5f8d2cb174
commit 8298a578b0
4 changed files with 57 additions and 41 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -55,8 +55,7 @@ namespace glib {
typedef ResourceWrapper<gchar *, gchar *, free_str_array> 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);
}

View File

@@ -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; i<sametypesequence_len-1; i++) {
@@ -258,7 +269,7 @@ gchar* DictBase::GetWordData(guint32 idxitem_offset, guint32 idxitem_size)
}
}
//calculate the last item 's size.
sec_size = idxitem_size - (p2-origin_data);
sec_size = idxitem_size - (p2-get_impl(origin_data));
*p1=sametypesequence[sametypesequence_len-1];
p1+=sizeof(gchar);
switch (sametypesequence[sametypesequence_len-1]) {
@@ -290,13 +301,13 @@ gchar* DictBase::GetWordData(guint32 idxitem_offset, guint32 idxitem_size)
}
break;
}
g_free(origin_data);
set_uint32(data, data_size);
} else {
data = (gchar *)g_malloc(idxitem_size + sizeof(guint32));
if (dictfile)
fread(data+sizeof(guint32), idxitem_size, 1, dictfile);
else
if (dictfile) {
const size_t nitems = fread(data+sizeof(guint32), idxitem_size, 1, dictfile);
THROW_IF_ERROR(nitems == 1);
} else
dictdzfile->read(data+sizeof(guint32), idxitem_offset, idxitem_size);
set_uint32(data, idxitem_size+sizeof(guint32));
}
@@ -318,9 +329,10 @@ bool DictBase::SearchData(std::vector<std::string> &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;
@@ -505,9 +517,10 @@ namespace {
{
fseek(idxfile, wordoffset[page_idx], SEEK_SET);
guint32 page_size = wordoffset[page_idx + 1] - wordoffset[page_idx];
fread(wordentry_buf,
const size_t nitems = fread(wordentry_buf,
std::min(sizeof(wordentry_buf), static_cast<size_t>(page_size)),
1, idxfile);
THROW_IF_ERROR(nitems == 1);
//TODO: check returned values, deal with word entry that strlen>255.
return wordentry_buf;
}
@@ -633,14 +646,16 @@ namespace {
{
gulong nentr = ENTR_PER_PAGE;
if (page_idx == glong(wordoffset.size()-2))
if ((nentr=wordcount%ENTR_PER_PAGE)==0)
if ((nentr = (wordcount % ENTR_PER_PAGE)) == 0)
nentr = ENTR_PER_PAGE;
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);
}