mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-08-15 22:45:56 +08:00
Hardened parsing and solved some issue found by fuzzer(issue 16).
This commit is contained in:
parent
9c0c665eb4
commit
2b0307f397
25
tiny_gltf.h
25
tiny_gltf.h
@ -944,6 +944,11 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
|
|||||||
// Looks reading directory, not a file.
|
// Looks reading directory, not a file.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sz == 0) {
|
||||||
|
// Invalid file size.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
std::vector<unsigned char> buf(sz);
|
std::vector<unsigned char> buf(sz);
|
||||||
|
|
||||||
f.seekg(0, f.beg);
|
f.seekg(0, f.beg);
|
||||||
@ -2273,6 +2278,14 @@ static bool ParseCamera(Camera *camera, std::string *err,
|
|||||||
bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
||||||
unsigned int length, const std::string &base_dir,
|
unsigned int length, const std::string &base_dir,
|
||||||
unsigned int check_sections) {
|
unsigned int check_sections) {
|
||||||
|
|
||||||
|
if (length < 4) {
|
||||||
|
if (err) {
|
||||||
|
(*err) = "JSON string too short.\n";
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
picojson::value v;
|
picojson::value v;
|
||||||
std::string perr = picojson::parse(v, str, str + length);
|
std::string perr = picojson::parse(v, str, str + length);
|
||||||
|
|
||||||
@ -2283,6 +2296,15 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!v.is<picojson::object>()) {
|
||||||
|
// root is not an object.
|
||||||
|
if (err) {
|
||||||
|
(*err) = "Root element is not a JSON object\n";
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// scene is not mandatory.
|
// scene is not mandatory.
|
||||||
// FIXME Maybe a better way to handle it than removing the code
|
// FIXME Maybe a better way to handle it than removing the code
|
||||||
|
|
||||||
@ -2601,6 +2623,9 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
|||||||
picojson::array::const_iterator itEnd(root.end());
|
picojson::array::const_iterator itEnd(root.end());
|
||||||
for (; it != itEnd; ++it) {
|
for (; it != itEnd; ++it) {
|
||||||
Sampler sampler;
|
Sampler sampler;
|
||||||
|
if (!(it->is<picojson::object>())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (!ParseSampler(&sampler, err, it->get<picojson::object>())) {
|
if (!ParseSampler(&sampler, err, it->get<picojson::object>())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user