Skip to content

Add null check before closing file pointer - #887

Open
hgqxjj wants to merge 1 commit into
pnggroup:libpng18from
hgqxjj:add-null-check
Open

Add null check before closing file pointer#887
hgqxjj wants to merge 1 commit into
pnggroup:libpng18from
hgqxjj:add-null-check

Conversation

@hgqxjj

@hgqxjj hgqxjj commented Jun 30, 2026

Copy link
Copy Markdown

Please review this change. Thanks!

Description:

Avoid calling fclose() with a NULL file pointer.

fopen() may fail and return NULL, so check ip before calling fclose(ip).

@jbowler

jbowler commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Why?

@hgqxjj

hgqxjj commented Aug 17, 2026

Copy link
Copy Markdown
Author

Hi @jbowler, Because fopen() may fail and return NULL. In that case, calling fclose(ip) would effectively call fclose(NULL), which is undefined behavior and may cause a crash. The ip != NULL check ensures we only close a successfully opened file.

@jbowler

jbowler commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Your pull request says:

No description provided.

That's why I asked, "Why?"

@hgqxjj

hgqxjj commented Aug 18, 2026

Copy link
Copy Markdown
Author

Thanks, I misunderstood your question. I've added the rationale to the PR description.

@jbowler

jbowler commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Reasonable; the prior code would give a user who passed in a bogus file name a nasty rejoinder and stop listening to what he said (the failure is in a loop over the arguments).

Neither is satisfactory of course but the suggested change is reasonable.

@hgqxjj

hgqxjj commented Aug 20, 2026

Copy link
Copy Markdown
Author

Thanks for the review and for the feedback!

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