Git Product home page Git Product logo

darts-clone's People

Watchers

 avatar

darts-clone's Issues

save() bug

template <typename A, typename B, typename T, typename C>
int DoubleArrayImpl<A, B, T, C>::save(const char *file_name,
    const char *mode, std::size_t offset) const {
  if (size() == 0) {
    return -1;
  }

#ifdef _MSC_VER
  std::FILE *file;
  if (::fopen_s(&file, file_name, mode) != 0) {
    return -1;
  }
#else
  std::FILE *file = std::fopen(file_name, mode);
  if (file == NULL) {
    return -1;
  }
#endif

  //  you should add fseek
  if (std::fseek(file, offset, SEEK_SET) != 0) { 
    std::fclose(file);
    return -1;
  }

  if (std::fwrite(array_, unit_size(), size(), file) != size()) {
    std::fclose(file);
    return -1;
  }
  std::fclose(file);
  return 0;
}

Original issue reported on code.google.com by [email protected] on 26 Nov 2013 at 2:11

crashed after read a illegal binary file.

What steps will reproduce the problem?
A. darts-clone is crashed after read a illegal binary file.

What is the expected output? What do you see instead?
A. make sure the file is safe before loading it,and exit when the binary file 
is illegal.

What version of the product are you using? On what operating system?
A.
version:0.32
os:Red Hat Enterprise Linux AS release 4
gcc:gcc version 4.4.4 20100726

Please provide any additional information below.
1. example:
//some modified
template <typename A, typename B, typename T, typename C>
int DoubleArrayImpl<A, B, T, C>::save(const char *file_name,
    const char *mode, std::size_t) const {
  if (size() == 0) {
    return -1;
  }
#ifdef _MSC_VER
  std::FILE *file;
  if (::fopen_s(&file, file_name, mode) != 0) {
    return -1;
  }
#else
  std::FILE *file = std::fopen(file_name, mode);
  if (file == NULL) {
    return -1;
  }
#endif
  int a = size();
  if(std::fwrite(&a, sizeof(int), 1, file) != 1)
  {
      std::fclose(file);
      return -1;
  }
  if (std::fwrite(array_, unit_size(), size(), file) != size()) {
    std::fclose(file);
    return -1;
  }
  std::fclose(file);
  return 0;
}

template <typename A, typename B, typename T, typename C>
int DoubleArrayImpl<A, B, T, C>::open(const char *file_name,
    const char *mode, std::size_t offset, std::size_t size) {
#ifdef _MSC_VER
  std::FILE *file;
  if (::fopen_s(&file, file_name, mode) != 0) {
    return -1;
  }
#else
  std::FILE *file = std::fopen(file_name, mode);
  if (file == NULL) {
    return -1;
  }
#endif

  //check 1
  int a = 0;
  if(std::fread(&a, sizeof(int), 1, file) != 1)
  {
      std::fclose(file);
      return -1;
  }
  if (size == 0) {
    if (std::fseek(file, 0, SEEK_END) != 0) {
      std::fclose(file);
      return -1;
    }
    size = std::ftell(file) - sizeof(int) - offset;
  }

  //plus sizeof(int)
  if (std::fseek(file, offset+sizeof(int), SEEK_SET) != 0) {
    std::fclose(file);
    return -1;
  }

  size /= unit_size();

  //check 2
  if(size != (unsigned)a)
  {
      std::fclose(file);
      return -1;
  }

  unit_type *buf= NULL;

  buf = new (std::nothrow) unit_type[size];
  if(buf == NULL) {
      std::fclose(file);
      //fprintf(stderr, "faile to open\n");
      DARTS_THROW("failed to open double-array: std::bad_alloc");
      return -1;
  }

  if (std::fread(buf, unit_size(), size, file) != size) {
    std::fclose(file);
    delete[] buf;
    return -1;
  }
  std::fclose(file);

  clear();

  size_ = size;
  array_ = buf;
  buf_ = buf;
  return 0;
}

Original issue reported on code.google.com by [email protected] on 10 Dec 2012 at 7:15

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.