Giter Site home page Giter Site logo

Comments (3)

fralken avatar fralken commented on August 9, 2024

Hello,
the tmporig pointer has moved in this snippet

    while (count--) {
        ins = strstr(tmporig, rep);
        len_front = ins - tmporig;
        tmp = strncpy(tmp, tmporig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        tmporig += len_front + len_rep; // move to next "end of rep"
    }

hence the free(tmporig); doesn't work since tmporig doesn't point to the start of the buffer.
Since the buffer pointed by orig parameter is not modified inside the function, there is no need to duplicate it.
So, instead of doing

    char *tmporig = malloc(strlen(orig) + 1); // Copy of orig that we work with
    tmporig = strcpy(tmporig, orig);

it should be enough doing instead

    char *tmporig = orig;

so there is no need to perform a free() in the end, and no memory leak is created.

from pacparser.

manugarg avatar manugarg commented on August 9, 2024

Good catch, @fralken. My C-foo is pretty rusty now, but we don't really need to even create a copy of orig, right? We could simply use orig; it won't modify the underlying string

from pacparser.

fralken avatar fralken commented on August 9, 2024

Indeed you are right @manugarg since orig itself is a copy of the pointer to the input string.

from pacparser.

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.