Browse Source

add input validation for utilLoadImage()

master
cinnaboot 8 years ago
parent
commit
4110116a40
  1. 7
      src/mesh.cpp
  2. 28
      src/util.cpp
  3. 4
      src/util.h

7
src/mesh.cpp

@ -1,5 +1,4 @@
#include <cassert> #include <cassert>
#include <string>
#include <assimp/cimport.h> #include <assimp/cimport.h>
#include <assimp/scene.h> #include <assimp/scene.h>
@ -158,12 +157,10 @@ copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh)
if (has_tex && mat->GetTextureCount(aiTextureType_DIFFUSE) > 0) { if (has_tex && mat->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString file_name; aiString file_name;
if (AI_SUCCESS != mat->GetTexture(aiTextureType_DIFFUSE, 0, &file_name, NULL, NULL, NULL, NULL, NULL)) { if (AI_SUCCESS != mat->GetTexture(aiTextureType_DIFFUSE, 0, &file_name, NULL, NULL, NULL, NULL, NULL)) {
LOG(ERROR) << "Some Assimp-type-error\n"; LOG(ERROR) << "No diffuse texture from assimp\n";
} else { } else {
std::string file_path;
file_path.append(data_dir).append("/").append(file_name.C_Str());
LOG(INFO) << "Loading texture file: " << file_name.C_Str() << "\n"; LOG(INFO) << "Loading texture file: " << file_name.C_Str() << "\n";
mi->diffuse_texture = utilLoadImage(file_path.c_str()); mi->diffuse_texture = utilLoadImage(data_dir, file_name.C_Str());
mi->use_texture = true; mi->use_texture = true;
} }
} }

28
src/util.cpp

@ -13,6 +13,18 @@
const uint MAX_FILESIZE = 2 * 1024 * 1024; // 2MB const uint MAX_FILESIZE = 2 * 1024 * 1024; // 2MB
const uint MAX_STRING_LENGTH = 1024; const uint MAX_STRING_LENGTH = 1024;
const char*
utilBaseName(const char* path_str)
{
assert(std::strlen(path_str) < MAX_STRING_LENGTH);
const char* output = std::strrchr(path_str, '/');
if (output)
return output;
else
return path_str;
}
// TODO: don't use ftell() to get filesize // TODO: don't use ftell() to get filesize
// https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file // https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file
char * char *
@ -64,15 +76,21 @@ void utilSafeFree(const void* mem)
} }
util_image util_image
utilLoadImage(const char* filename) utilLoadImage(const char* base_dir, const char* filename)
{ {
LOG(INFO) << "Loading Image: " << filename << "\n"; const uint max_path = 256; // NOTE: util_image.file_path is char[256]
assert(std::strlen(filename) < 256); const char* base_name = utilBaseName(filename);
uint l = std::strlen(base_dir) + std::strlen(base_name) + 1; // NOTE: +1 for '/' char
assert(l < max_path);
char full_path[l];
std::memcpy(full_path, base_dir, std::strlen(base_dir) + 1);
std::strcat(std::strcat(full_path, "/"), base_name);
LOG(INFO) << "Loading Image: " << full_path << "\n";
util_image image; util_image image;
std::memcpy(image.file_path, filename, std::strlen(filename) + 1); std::memcpy(image.file_path, full_path, std::strlen(full_path) + 1);
stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(1);
image.pixels = stbi_load(filename, &image.w, &image.h, &image.num_channels, 0); image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0);
image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel
if (image.pixels == 0) { if (image.pixels == 0) {

4
src/util.h

@ -86,13 +86,15 @@ utilConvertColor(GLfloat buf[3], uint32 color)
buf[2] = (GLfloat) ((color >> 8) & 0xFF) / (GLfloat) 255; buf[2] = (GLfloat) ((color >> 8) & 0xFF) / (GLfloat) 255;
} }
const char* utilBaseName(const char* path_str);
// NOTE: Wrapper for calloc that will send error message on out of memory // NOTE: Wrapper for calloc that will send error message on out of memory
#define UTIL_ALLOC(len, type) (type *) utilLogAlloc((len), sizeof(type), __FILE__, __LINE__) #define UTIL_ALLOC(len, type) (type *) utilLogAlloc((len), sizeof(type), __FILE__, __LINE__)
void* utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line); void* utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line);
char* utilDumpTextFile(const char* filename); char* utilDumpTextFile(const char* filename);
util_image utilLoadImage(const char* filename); util_image utilLoadImage(const char* base_dir, const char* filename);
void utilFreeImage(util_image image); void utilFreeImage(util_image image);

Loading…
Cancel
Save