Fix json string crash

This commit is contained in:
Leith Bade
2014-11-18 23:34:46 +11:00
parent 900f6e87fa
commit e52cf05ac3
3 changed files with 10 additions and 7 deletions

View File

@@ -53,7 +53,7 @@ void AssetRequestBaton::run(uv_async_t *async) {
int error = 0;
ptr->apk = zip_open(mbgl::android::apk_path.c_str(), 0, &error);
if (ptr->apk == nullptr || ptr->canceled || !ptr->request) {
if ((ptr->apk == nullptr) || ptr->canceled || !ptr->request) {
// Opening the APK failed or was canceled. There isn't much left we can do.
const int message_size = zip_error_to_str(nullptr, 0, error, errno);
const std::unique_ptr<char[]> message = boost::make_unique<char[]>(message_size);
@@ -65,7 +65,7 @@ void AssetRequestBaton::run(uv_async_t *async) {
std::string apk_file_path = "assets/" + ptr->path;
ptr->apk_file = zip_fopen(ptr->apk, apk_file_path.c_str(), ZIP_FL_NOCASE);
if (ptr->apk_file == nullptr || ptr->canceled || !ptr->request) {
if ((ptr->apk_file == nullptr) || ptr->canceled || !ptr->request) {
// Opening the asset failed or was canceled. We already have an open file handle
// though, which we'll have to close.
zip_error_get(ptr->apk, &error, nullptr);
@@ -77,7 +77,7 @@ void AssetRequestBaton::run(uv_async_t *async) {
}
struct zip_stat stat;
if (zip_stat(ptr->apk, apk_file_path.c_str(), ZIP_FL_NOCASE, &stat) != 0 ||
if ((zip_stat(ptr->apk, apk_file_path.c_str(), ZIP_FL_NOCASE, &stat) != 0) ||
ptr->canceled || !ptr->request) {
// Stating failed or was canceled. We already have an open file handle
// though, which we'll have to close.
@@ -90,9 +90,9 @@ void AssetRequestBaton::run(uv_async_t *async) {
return;
}
const std::unique_ptr<char[]> body = boost::make_unique<char[]>(stat.size);
const std::unique_ptr<char[]> data = boost::make_unique<char[]>(stat.size);
if (zip_fread(ptr->apk_file, reinterpret_cast<void *>(body.get()), stat.size) == -1 ||
if (static_cast<zip_uint64_t>(zip_fread(ptr->apk_file, reinterpret_cast<void *>(data.get()), stat.size)) != stat.size ||
ptr->canceled || !ptr->request) {
// Reading failed or was canceled. We already have an open file handle
// though, which we'll have to close.
@@ -108,7 +108,8 @@ void AssetRequestBaton::run(uv_async_t *async) {
if (ptr->request) {
ptr->request->response = std::unique_ptr<Response>(new Response);
ptr->request->response->code = 200;
ptr->request->response->data = body.get();
std::string body(data.get(), stat.size);
ptr->request->response->data = body;
ptr->request->notify();
}

View File

@@ -448,7 +448,7 @@ static CURLcode sslctx_function(CURL */*curl*/, void *sslctx, void */*parm*/) {
const std::unique_ptr<char[]> pem = boost::make_unique<char[]>(stat.size);
if (zip_fread(apk_file, reinterpret_cast<void *>(pem.get()), stat.size) == -1) {
if (static_cast<zip_uint64_t>(zip_fread(apk_file, reinterpret_cast<void *>(pem.get()), stat.size)) != stat.size) {
zip_fclose(apk_file);
apk_file = nullptr;
zip_close(apk);

View File

@@ -8,6 +8,7 @@
#include <mbgl/util/error.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/platform/log.hpp>
#include <csscolorparser/csscolorparser.hpp>
#include <rapidjson/document.h>
@@ -91,6 +92,7 @@ void Style::loadJSON(const uint8_t *const data) {
rapidjson::Document doc;
doc.Parse<0>((const char *const)data);
if (doc.HasParseError()) {
Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", doc.GetErrorOffset(), doc.GetParseError());
throw error::style_parse(doc.GetErrorOffset(), doc.GetParseError());
}