Giter VIP home page Giter VIP logo

Comments (3)

XavilPergis avatar XavilPergis commented on June 28, 2024

So I've been looking over this and adding in a few checks

  • Functions that return arrays just need a check to see if malloc failed, returning nullptr if it did
  • Functions that return 2D arrays need the same check for the top-level array, as well as checks for each sub-arrays. If an allocation of a sub-array fails, either all the entries before the current one can be freed and nullptr returned, or that entry set to nullptr. Freeing everything and returning nullptr seems to assume less about the caller, though, and I think this is the safest behavior.

Things get a little bit hairy with the ConvertToC functions with checks, because then a struct could be partially initialized without knowing so. Example:

static void ConvertToC(const EntryInfo& in, NT_EntryInfo* out) {
  // Returning early here possibly leaves `out` completely uninitialized,
  // but the caller doesn't know
  if (!out) return false;
  out->entry = in.entry;
  // out->name could be uninitialized here, but we don't know
  // and can't propagate that error up to the caller
  ConvertToC(in.name, &out->name);
  out->type = in.type;
  out->flags = in.flags;
  out->last_change = in.last_change;
}

Since ConvertToC is a private interface, it is possible to propagate error information, but then you have the same problem when you get to the public interfaces; NT_GetEntryValue, for example, has no way to tell the caller that there was an allocation error other than leaving the value marked as unassigned. Doing that, though, would cause ambiguity between an OOM error and a nonexistent entry.

At the very least, the undefined behavior should be eliminated, and this could be done by just aborting the process if an alloc failed.

from ntcore.

PeterJohnson avatar PeterJohnson commented on June 28, 2024

Frankly, out of memory errors are seldom recoverable. Simply aborting on allocation failure is probably the right answer.

from ntcore.

XavilPergis avatar XavilPergis commented on June 28, 2024

Yeah, that seems like the most consistent thing to do. I'll submit a PR in a bit.

from ntcore.

Related Issues (20)

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.