Skip to content

Make sure to not try freeing an uninitialized pointer - #1463

Merged
flyingmutant merged 1 commit into
cmus:masterfrom
xant:fix_free_uninitialized_pointer
Nov 4, 2025
Merged

flyingmutant merged 1 commit into
cmus:masterfrom
xant:fix_free_uninitialized_pointer

Conversation

@xant

@xant xant commented Nov 1, 2025 •

Copy link
Copy Markdown
Contributor

While playing around with cmus and trying to load my Music archive I stumbled upon this bug ... reported as double free but actually trying to free an unitialized pointer.

Comment thread job.c

char **files_in_cue;
char **files_in_cue = NULL;
int n = cue_get_files(ents[i], &files_in_cue);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uninitialized pointer happens when cue_get_files returns -1, which happens when cue_from_file returns NULL, which happens if mmap_file fails or the cue parse returns an error.

The fix should be to add an if (n == -1) continue after the cue_get_files call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that also if the n is 0 files_in_cue would be null ? (I didn't follow xnew() implementation but I would assume so).

I can check against 'n' instead of checking for files_in_cue being NULL (which anyway is a better check to do here IMO ... because allocation could fail as well inside cue_get_files() and you would still try freeing an invalid address)

but maybe we should check for n being > 0 instead that != -1?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to call free on null.

The rest of the code tends to check against != -1. But, since there's not any point in doing the rest of the stuff if it's zero-length, wrapping the rest in an if > 1 makes sense.

Free should still be called if n == 0, but not if n == -1.

@xant xant Nov 1, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's true that it is safe to call free on null ... but that pointer was not set to null it was left uninitialized ;)

@xant
xant force-pushed the fix_free_uninitialized_pointer branch from 5f43f10 to 8bef306 Compare November 1, 2025 06:06
@xant
xant requested a review from pgaskin November 1, 2025 06:07

@pgaskin pgaskin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@flyingmutant
flyingmutant merged commit c81a0ba into cmus:master Nov 4, 2025
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
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.

3 participants