Giter Site home page Giter Site logo

Comments (9)

memononen avatar memononen commented on July 17, 2024

Excellent catch! Sorry for the trouble finding it. I changed all allocators to follow same pattern in the hopes that copy-pastering will not catch a bad habit in the future. Just to be sure, can you please review the changes and let me know if there is something silly with it.

from nanovg.

cforfang avatar cforfang commented on July 17, 2024

Just thinking out loud for a bit, I was wondering if the use of realloc might cause some issues?

The glnvg__alloc*s all use it like this:

gl->paths = (struct GLNVGpath*)realloc(gl->paths, sizeof(struct GLNVGpath) * gl->cpaths);

... which if realloc fails all the old data is leaked and you're probably looking at a segfault in the near future?

Similar issues in nvg_add* and nvg__appendCommands where you do check for NULL but still leak the old data -- and by not clearing e.g. ncommands/ccommands I guess the code is likely to try to dereference the NULL later though I haven't looked at it closely.

I guess realloc is pretty unlikely to fail on modern system, at least for what its used for here, but still might be something to consider. :)

from nanovg.

memononen avatar memononen commented on July 17, 2024

You're right. So pattern like would be better:

struct GLNVGpath* paths;
paths = (struct GLNVGpath*)realloc(gl->paths, sizeof(struct GLNVGpath) * gl->cpaths);
if (paths == NULL) fail;
gl->paths = paths;

I think the code should try to fail as close to the problem as possible, even if it is a segfault. So that code definitely needs a little improvements.

from nanovg.

cforfang avatar cforfang commented on July 17, 2024

Yeah. What you could maybe do is (at least for the gl-backend) change glnvg__alloc* to somehow indicate allocation-failure, and then handle this in glnvg__render* by allocating everything up front (with glnvg__allocCall last) and either silently dropping the draw or reporting it through an error-callback or something. Not sure how complex you want to make this though: :)

from nanovg.

memononen avatar memononen commented on July 17, 2024

Take a look at 7f55dec I hope caught all cases.

from nanovg.

cforfang avatar cforfang commented on July 17, 2024

The only thing that jumps out at me is that you probably shouldn't update the capacity before you know the realloc succeeds. Will run into problems the second time the function is called. :) Looks good otherwise!

from nanovg.

memononen avatar memononen commented on July 17, 2024

One more time... 02ca24a
Remote pair programming ftw! :)

from nanovg.

cforfang avatar cforfang commented on July 17, 2024

Indeed. :) Seems, compiles, and runs fine for me.

from nanovg.

guidodehaan avatar guidodehaan commented on July 17, 2024

Realloc changes look ok. And running without problems now.

from nanovg.

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.