Fixed LoadFile crashing on directory arguments.
authorWouter van Oortmerssen <wvo@google.com>
Thu, 26 May 2016 00:47:44 +0000 (17:47 -0700)
committerWouter van Oortmerssen <wvo@google.com>
Mon, 6 Jun 2016 18:34:31 +0000 (11:34 -0700)
Change-Id: I737f6fd0bb1302ab7cfa6ab8b06108e221ebb63c
Tested: on Linux.

include/flatbuffers/util.h
samples/monster_generated.h
src/util.cpp

index cfb211f..7bd7513 100644 (file)
 #include <winbase.h>
 #include <direct.h>
 #else
-#include <sys/stat.h>
 #include <limits.h>
 #endif
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "flatbuffers/flatbuffers.h"
 
@@ -124,6 +125,9 @@ FileExistsFunction SetFileExistsFunction(FileExistsFunction
 // Check if file "name" exists.
 bool FileExists(const char *name);
 
+// Check if "name" exists and it is also a directory.
+bool DirExists(const char *name);
+
 // Load file "name" into "buf" returning true if successful
 // false otherwise.  If "binary" is false data is read
 // using ifstream's text mode, otherwise data is read with
index cf45bbb..1a16126 100644 (file)
@@ -5,12 +5,13 @@
 
 #include "flatbuffers/flatbuffers.h"
 
-
 namespace MyGame {
 namespace Sample {
 
 struct Vec3;
+
 struct Monster;
+
 struct Weapon;
 
 enum Color {
@@ -55,8 +56,11 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
     : x_(flatbuffers::EndianScalar(_x)), y_(flatbuffers::EndianScalar(_y)), z_(flatbuffers::EndianScalar(_z)) { }
 
   float x() const { return flatbuffers::EndianScalar(x_); }
+  void mutate_x(float _x) { flatbuffers::WriteScalar(&x_, _x); }
   float y() const { return flatbuffers::EndianScalar(y_); }
+  void mutate_y(float _y) { flatbuffers::WriteScalar(&y_, _y); }
   float z() const { return flatbuffers::EndianScalar(z_); }
+  void mutate_z(float _z) { flatbuffers::WriteScalar(&z_, _z); }
 };
 STRUCT_END(Vec3, 12);
 
@@ -73,14 +77,23 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
     VT_EQUIPPED = 22
   };
   const Vec3 *pos() const { return GetStruct<const Vec3 *>(VT_POS); }
+  Vec3 *mutable_pos() { return GetStruct<Vec3 *>(VT_POS); }
   int16_t mana() const { return GetField<int16_t>(VT_MANA, 150); }
+  bool mutate_mana(int16_t _mana) { return SetField(VT_MANA, _mana); }
   int16_t hp() const { return GetField<int16_t>(VT_HP, 100); }
+  bool mutate_hp(int16_t _hp) { return SetField(VT_HP, _hp); }
   const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(VT_NAME); }
+  flatbuffers::String *mutable_name() { return GetPointer<flatbuffers::String *>(VT_NAME); }
   const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_INVENTORY); }
+  flatbuffers::Vector<uint8_t> *mutable_inventory() { return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_INVENTORY); }
   Color color() const { return static_cast<Color>(GetField<int8_t>(VT_COLOR, 2)); }
+  bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast<int8_t>(_color)); }
   const flatbuffers::Vector<flatbuffers::Offset<Weapon>> *weapons() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<Weapon>> *>(VT_WEAPONS); }
+  flatbuffers::Vector<flatbuffers::Offset<Weapon>> *mutable_weapons() { return GetPointer<flatbuffers::Vector<flatbuffers::Offset<Weapon>> *>(VT_WEAPONS); }
   Equipment equipped_type() const { return static_cast<Equipment>(GetField<uint8_t>(VT_EQUIPPED_TYPE, 0)); }
+  bool mutate_equipped_type(Equipment _equipped_type) { return SetField(VT_EQUIPPED_TYPE, static_cast<uint8_t>(_equipped_type)); }
   const void *equipped() const { return GetPointer<const void *>(VT_EQUIPPED); }
+  void *mutable_equipped() { return GetPointer<void *>(VT_EQUIPPED); }
   bool Verify(flatbuffers::Verifier &verifier) const {
     return VerifyTableStart(verifier) &&
            VerifyField<Vec3>(verifier, VT_POS) &&
@@ -150,7 +163,9 @@ struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
     VT_DAMAGE = 6
   };
   const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(VT_NAME); }
+  flatbuffers::String *mutable_name() { return GetPointer<flatbuffers::String *>(VT_NAME); }
   int16_t damage() const { return GetField<int16_t>(VT_DAMAGE, 0); }
+  bool mutate_damage(int16_t _damage) { return SetField(VT_DAMAGE, _damage); }
   bool Verify(flatbuffers::Verifier &verifier) const {
     return VerifyTableStart(verifier) &&
            VerifyField<flatbuffers::uoffset_t>(verifier, VT_NAME) &&
@@ -192,6 +207,8 @@ inline bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *union_o
 
 inline const MyGame::Sample::Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<MyGame::Sample::Monster>(buf); }
 
+inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot<Monster>(buf); }
+
 inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<MyGame::Sample::Monster>(); }
 
 inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<MyGame::Sample::Monster> root) { fbb.Finish(root); }
index 452e9ea..f4aecb5 100644 (file)
@@ -24,12 +24,14 @@ bool FileExistsRaw(const char *name) {
 }
 
 bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
+  if (DirExists(name)) return false;
   std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
   if (!ifs.is_open()) return false;
   if (binary) {
     // The fastest way to read a file into a string.
     ifs.seekg(0, std::ios::end);
-    (*buf).resize(static_cast<size_t>(ifs.tellg()));
+    auto size = ifs.tellg();
+    (*buf).resize(static_cast<size_t>(size));
     ifs.seekg(0, std::ios::beg);
     ifs.read(&(*buf)[0], (*buf).size());
   } else {
@@ -54,6 +56,19 @@ bool FileExists(const char *name) {
   return g_file_exists_function(name);
 }
 
+bool DirExists(const char *name) {
+  #ifdef _WIN32
+    #define flatbuffers_stat _stat
+    #define FLATBUFFERS_S_IFDIR _S_IFDIR
+  #else
+    #define flatbuffers_stat stat
+    #define FLATBUFFERS_S_IFDIR S_IFDIR
+  #endif
+  struct flatbuffers_stat file_info;
+  if (flatbuffers_stat(name, &file_info) != 0) return false;
+  return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
+}
+
 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
   LoadFileFunction previous_function = g_load_file_function;
   g_load_file_function = load_file_function ? load_file_function : LoadFileRaw;