Avoid out-of-bounds access when checking file extensions.
Include the '.' in the file extension check. PiperOrigin-RevId: 474866478 Change-Id: I809ae975e4c8b00f17278735975a7d07e0748643
This commit is contained in:
committed by
Copybara-Service
parent
68718e76b1
commit
f556d4d94f
@@ -160,12 +160,12 @@ void mjCMesh::Compile(const mjVFS* vfs) {
|
||||
}
|
||||
|
||||
// load STL, OBJ or MSH
|
||||
string ext = file.substr(file.size()-3, 3);
|
||||
if (!strcasecmp(ext.c_str(), "stl")) {
|
||||
string ext = mjuu_getext(file);
|
||||
if (!strcasecmp(ext.c_str(), ".stl")) {
|
||||
LoadSTL(vfs);
|
||||
} else if (!strcasecmp(ext.c_str(), "obj")) {
|
||||
} else if (!strcasecmp(ext.c_str(), ".obj")) {
|
||||
LoadOBJ(vfs);
|
||||
} else if (!strcasecmp(ext.c_str(), "msh")) {
|
||||
} else if (!strcasecmp(ext.c_str(), ".msh")) {
|
||||
LoadMSH(vfs);
|
||||
} else {
|
||||
throw mjCError(this, "Unknown mesh file type: %s", file.c_str());
|
||||
@@ -1621,8 +1621,8 @@ void mjCSkin::Compile(const mjVFS* vfs) {
|
||||
}
|
||||
|
||||
// load SKN
|
||||
string ext = file.substr(file.size()-3, 3);
|
||||
if (!strcasecmp(ext.c_str(), "skn")) {
|
||||
string ext = mjuu_getext(file);
|
||||
if (!strcasecmp(ext.c_str(), ".skn")) {
|
||||
LoadSKN(vfs);
|
||||
} else {
|
||||
throw mjCError(this, "Unknown skin file type: %s", file.c_str());
|
||||
|
||||
@@ -1879,7 +1879,8 @@ void mjCHField::Compile(const mjVFS* vfs) {
|
||||
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir, file);
|
||||
|
||||
// load depending on format
|
||||
if (!strcasecmp(filename.substr(filename.length()-4, 5).c_str(), ".png")) {
|
||||
string ext = mjuu_getext(filename);
|
||||
if (!strcasecmp(ext.c_str(), ".png")) {
|
||||
LoadPNG(filename, vfs);
|
||||
} else {
|
||||
LoadCustom(filename, vfs);
|
||||
@@ -2301,7 +2302,8 @@ void mjCTexture::LoadFlip(string filename, const mjVFS* vfs,
|
||||
std::vector<unsigned char>& image,
|
||||
unsigned int& w, unsigned int& h) {
|
||||
// dispatch to PNG or Custom loaded
|
||||
if (!strcasecmp(filename.substr(filename.length()-4, 5).c_str(), ".png")) {
|
||||
string ext = mjuu_getext(filename);
|
||||
if (!strcasecmp(ext.c_str(), ".png")) {
|
||||
LoadPNG(filename, vfs, image, w, h);
|
||||
} else {
|
||||
LoadCustom(filename, vfs, image, w, h);
|
||||
|
||||
+10
-2
@@ -19,6 +19,7 @@
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <mujoco/mjtnum.h>
|
||||
#include "engine/engine_macro.h"
|
||||
@@ -488,9 +489,16 @@ string mjuu_stripext(string filename) {
|
||||
}
|
||||
|
||||
// return name without extension
|
||||
else {
|
||||
return filename.substr(0, end);
|
||||
return filename.substr(0, end);
|
||||
}
|
||||
|
||||
string mjuu_getext(std::string_view filename) {
|
||||
size_t dot = filename.find_last_of('.');
|
||||
|
||||
if (dot==string::npos) {
|
||||
return "";
|
||||
}
|
||||
return string(filename.substr(dot, filename.size() - dot));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define MUJOCO_SRC_USER_USER_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
extern const double mjNAN; // used to mark undefined fields
|
||||
@@ -129,6 +130,9 @@ std::string mjuu_strippath(std::string filename);
|
||||
// strip extension from filename
|
||||
std::string mjuu_stripext(std::string filename);
|
||||
|
||||
// get the extension of a filename
|
||||
std::string mjuu_getext(std::string_view filename);
|
||||
|
||||
// check if path is absolute
|
||||
bool mjuu_isabspath(std::string path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user