kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
146 stars 46 forks source link

[BUG] libkubernetes frees the memory that is passed in #14

Closed ityuhui closed 4 years ago

ityuhui commented 4 years ago

v1_volume_mount_create uses the address of "mount_path", "mount_propagation","name","sub_path","sub_path_expr" directly,

v1_volume_mount_t *v1_volume_mount_create(
    char *mount_path,
    char *mount_propagation,
    char *name,
    int read_only,
    char *sub_path,
    char *sub_path_expr
    ) {
    v1_volume_mount_t *v1_volume_mount_local_var = malloc(sizeof(v1_volume_mount_t));
    if (!v1_volume_mount_local_var) {
        return NULL;
    }
    v1_volume_mount_local_var->mount_path = mount_path;
    v1_volume_mount_local_var->mount_propagation = mount_propagation;
    v1_volume_mount_local_var->name = name;
    v1_volume_mount_local_var->read_only = read_only;
    v1_volume_mount_local_var->sub_path = sub_path;
    v1_volume_mount_local_var->sub_path_expr = sub_path_expr;

    return v1_volume_mount_local_var;
}

but v1_volume_mount_free will free the memory when cleaning up:

void v1_volume_mount_free(v1_volume_mount_t *v1_volume_mount) {
    if(NULL == v1_volume_mount){
        return ;
    }
    listEntry_t *listEntry;
    free(v1_volume_mount->mount_path);
    free(v1_volume_mount->mount_propagation);
    free(v1_volume_mount->name);
    free(v1_volume_mount->sub_path);
    free(v1_volume_mount->sub_path_expr);
    free(v1_volume_mount);
}

So it will introduce a coredump when

    v1_volume_mount_t *volmou = v1_volume_mount_create("/test", NULL, "test", 0, NULL, NULL);
    v1_volume_mount_free(volmou);
ityuhui commented 4 years ago

By now, v1_*_create is only for internal use (call by v1_*_parseFromJSON), not for the library user.

So, the right usage is:

    v1_volume_mount_t *volmou = calloc(1, sizeof(v1_volume_mount_t));
    volmou->mount_path = strdup("/test");
    volmou->name = strdup("test");
...
    v1_volume_mount_free(volmou);

This is a not a defect. Just a design. So close this issue.