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

Fix memory leaks found by ASAN #6

Merged
merged 1 commit into from
May 2, 2024
Merged

Conversation

NeKzor
Copy link
Member

@NeKzor NeKzor commented Sep 21, 2023

Follow-up of #5

Summary

1.) The switch case statement on SAR_DATA_ENTITY_INPUT_SLOT has a fall-through here:

mdp/src/demo.c

Lines 155 to 158 in 4a580f6

case SAR_DATA_ENTITY_INPUT_SLOT:
out->slot = data[0];
++data;
case SAR_DATA_ENTITY_INPUT:

but this case is missing in _sar_data_free:

mdp/src/demo.c

Lines 30 to 37 in 4a580f6

break;
case SAR_DATA_ENTITY_INPUT:
free(data.entity_input.targetname);
free(data.entity_input.classname);
free(data.entity_input.inputname);
free(data.entity_input.parameter);
break;

2.) Missing call to closedir after opendir:

mdp/src/main.c

Lines 301 to 325 in 4a580f6

DIR *d = opendir(DEMO_DIR);
if (d) {
size_t demo_dir_len = strlen(DEMO_DIR);
struct dirent *ent;
bool is_first = true;
while ((ent = readdir(d))) {
if (!strcmp(ent->d_name, ".")) continue;
if (!strcmp(ent->d_name, "..")) continue;
if (!is_first) {
fputs("\n", g_outfile);
}
is_first = false;
char *path = malloc(demo_dir_len + strlen(ent->d_name) + 2);
strcpy(path, DEMO_DIR);
strcat(path, "/");
strcat(path, ent->d_name);
_g_detected_timescale = false;
run_demo(path);
free(path);
}

3.) strdup allocated header fields are not freed:

mdp/src/demo.c

Lines 546 to 550 in 4a580f6

struct demo_hdr hdr = {
.server_name = strdup((char *)(hdr_buf + 16)),
.client_name = strdup((char *)(hdr_buf + 276)),
.map_name = strdup((char *)(hdr_buf + 536)),
.game_directory = strdup((char *)(hdr_buf + 796)),

mdp/src/demo.c

Lines 84 to 91 in 4a580f6

void demo_free(struct demo *demo) {
if (!demo) return;
for (size_t i = 0; i < demo->nmsgs; ++i) {
_msg_free(demo->msgs[i]);
}
free(demo->msgs);
free(demo);
}

@pektezol pektezol requested a review from mlugg September 22, 2023 05:16
@ThisAMJ ThisAMJ merged commit 6a3a2ea into p2sr:master May 2, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants