fix remaning std::fread usage - check return value

This commit is contained in:
artemp 2017-01-13 14:57:15 +01:00
parent a220bda05d
commit a108068cf6
7 changed files with 24 additions and 19 deletions

View file

@ -97,7 +97,10 @@ mapnik::feature_ptr csv_featureset::next()
std::fseek(file_.get(), file_offset, SEEK_SET); std::fseek(file_.get(), file_offset, SEEK_SET);
std::vector<char> record; std::vector<char> record;
record.resize(size); record.resize(size);
std::fread(record.data(), size, 1, file_.get()); if (std::fread(record.data(), size, 1, file_.get()) != 1)
{
return mapnik::feature_ptr();
}
auto const* start = record.data(); auto const* start = record.data();
auto const* end = start + record.size(); auto const* end = start + record.size();
#endif #endif

View file

@ -119,7 +119,10 @@ mapnik::feature_ptr csv_index_featureset::next()
std::fseek(file_.get(), pos.first, SEEK_SET); std::fseek(file_.get(), pos.first, SEEK_SET);
std::vector<char> record; std::vector<char> record;
record.resize(pos.second); record.resize(pos.second);
std::fread(record.data(), pos.second, 1, file_.get()); if (std::fread(record.data(), pos.second, 1, file_.get()) != 1)
{
return mapnik::feature_ptr();
}
auto const* start = record.data(); auto const* start = record.data();
auto const* end = start + record.size(); auto const* end = start + record.size();
#endif #endif

View file

@ -155,9 +155,9 @@ geojson_datasource::geojson_datasource(parameters const& params)
std::string file_buffer; std::string file_buffer;
file_buffer.resize(file.size()); file_buffer.resize(file.size());
std::fread(&file_buffer[0], file.size(), 1, file.get()); auto count = std::fread(&file_buffer[0], file.size(), 1, file.get());
char const* start = file_buffer.c_str(); char const* start = file_buffer.c_str();
char const* end = start + file_buffer.length(); char const* end = (count == 1) ? start + file_buffer.length() : start;
#else #else
boost::optional<mapnik::mapped_region_ptr> mapped_region = boost::optional<mapnik::mapped_region_ptr> mapped_region =
mapnik::mapped_memory_cache::instance().find(filename_, false); mapnik::mapped_memory_cache::instance().find(filename_, false);
@ -226,9 +226,9 @@ void geojson_datasource::initialise_disk_index(std::string const& filename)
std::fseek(file.get(), pos.first, SEEK_SET); std::fseek(file.get(), pos.first, SEEK_SET);
std::vector<char> record; std::vector<char> record;
record.resize(pos.second); record.resize(pos.second);
std::fread(record.data(), pos.second, 1, file.get()); auto count = std::fread(record.data(), pos.second, 1, file.get());
auto const* start = record.data(); auto const* start = record.data();
auto const* end = start + record.size(); auto const* end = (count == 1) ? start + record.size() : start;
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1));
try try
{ {
@ -471,9 +471,9 @@ boost::optional<mapnik::datasource_geometry_t> geojson_datasource::get_geometry_
std::fseek(file.get(), pos.first, SEEK_SET); std::fseek(file.get(), pos.first, SEEK_SET);
std::vector<char> record; std::vector<char> record;
record.resize(pos.second); record.resize(pos.second);
std::fread(record.data(), pos.second, 1, file.get()); auto count = std::fread(record.data(), pos.second, 1, file.get());
auto const* start = record.data(); auto const* start = record.data();
auto const* end = start + record.size(); auto const* end = (count == 1) ? start + record.size() : start;
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature
try try
{ {
@ -533,11 +533,10 @@ boost::optional<mapnik::datasource_geometry_t> geojson_datasource::get_geometry_
std::fseek(file.get(), file_offset, SEEK_SET); std::fseek(file.get(), file_offset, SEEK_SET);
std::vector<char> json; std::vector<char> json;
json.resize(size); json.resize(size);
std::fread(json.data(), size, 1, file.get()); auto count_objects = std::fread(json.data(), size, 1, file.get());
using chr_iterator_type = char const*; using chr_iterator_type = char const*;
chr_iterator_type start2 = json.data(); chr_iterator_type start2 = json.data();
chr_iterator_type end2 = start2 + json.size(); chr_iterator_type end2 = (count_objects == 1) ? start2 + json.size() : start2;
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp feature
try try

View file

@ -87,9 +87,9 @@ mapnik::feature_ptr geojson_index_featureset::next()
std::fseek(file_.get(), pos.first, SEEK_SET); std::fseek(file_.get(), pos.first, SEEK_SET);
std::vector<char> record; std::vector<char> record;
record.resize(pos.second); record.resize(pos.second);
std::fread(record.data(), pos.second, 1, file_.get()); auto count = std::fread(record.data(), pos.second, 1, file_.get());
auto const* start = record.data(); auto const* start = record.data();
auto const* end = start + record.size(); auto const* end = (count == 1) ? start + record.size() : start;
#endif #endif
static const mapnik::transcoder tr("utf8"); static const mapnik::transcoder tr("utf8");
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_, feature_id_++)); mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_, feature_id_++));

View file

@ -61,10 +61,10 @@ mapnik::feature_ptr geojson_memory_index_featureset::next()
std::fseek(file_.get(), file_offset, SEEK_SET); std::fseek(file_.get(), file_offset, SEEK_SET);
std::vector<char> json; std::vector<char> json;
json.resize(size); json.resize(size);
std::fread(json.data(), size, 1, file_.get()); auto count = std::fread(json.data(), size, 1, file_.get());
using chr_iterator_type = char const*; using chr_iterator_type = char const*;
chr_iterator_type start = json.data(); chr_iterator_type start = json.data();
chr_iterator_type end = start + json.size(); chr_iterator_type end = (count == 1) ? start + json.size() : start;
static const mapnik::transcoder tr("utf8"); static const mapnik::transcoder tr("utf8");
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_, feature_id_++)); mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_, feature_id_++));
mapnik::json::parse_feature(start, end, *feature, tr); // throw on failure mapnik::json::parse_feature(start, end, *feature, tr); // throw on failure

View file

@ -171,8 +171,8 @@ topojson_datasource::topojson_datasource(parameters const& params)
} }
std::string file_buffer; std::string file_buffer;
file_buffer.resize(file.size()); file_buffer.resize(file.size());
std::fread(&file_buffer[0], file.size(), 1, file.get()); auto count = std::fread(&file_buffer[0], file.size(), 1, file.get());
parse_topojson(file_buffer.c_str()); if (count == 1) parse_topojson(file_buffer.c_str());
} }
} }

View file

@ -252,9 +252,9 @@ std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & b
} }
std::string file_buffer; std::string file_buffer;
file_buffer.resize(file.size()); file_buffer.resize(file.size());
std::fread(&file_buffer[0], file.size(), 1, file.get()); auto count = std::fread(&file_buffer[0], file.size(), 1, file.get());
base_iterator_type start = file_buffer.c_str(); base_iterator_type start = file_buffer.c_str();
base_iterator_type end = start + file_buffer.length(); base_iterator_type end = (count == 1) ? start + file_buffer.length() : start;
#endif #endif
base_iterator_type itr = start; // make a copy to preserve `start` iterator state base_iterator_type itr = start; // make a copy to preserve `start` iterator state
try try