Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect generation of 'free' call #85

Open
brunexgeek opened this issue Oct 19, 2021 · 0 comments
Open

Incorrect generation of 'free' call #85

brunexgeek opened this issue Oct 19, 2021 · 0 comments
Labels

Comments

@brunexgeek
Copy link

I changed the example of #74 from this

function return_from_obj(){
  let obj = {key:1};
  return obj.key;
}

console.log(return_from_obj());

to

let temp;

function return_from_obj(){
  let obj = {key:1};
  temp = obj;
  return obj.key;
}

console.log(return_from_obj());

and the corresponding output C code is

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
typedef short int16_t;

struct temp_t {
    int16_t key;
};

static struct temp_t * temp;
int16_t return_from_obj()
{
    struct temp_t * obj;
    obj = malloc(sizeof(*obj));
    assert(obj != NULL);
    obj->key = 1;
    temp = obj;
    return obj->key;

}

int main(void) {
    printf("%d\n", return_from_obj());
    free(obj);

    return 0;
}

This C code has an error in free(obj), which should be free(temp). It appears the original variable name (obj in this case) is used to free the memory, even though the memory is referenced by another variable at that point.

I used the live demo for this test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants