Thanks to visit codestin.com
Credit goes to github.com

Skip to content

astfd.c: Avoid calling fclose with NULL argument.#901

Merged
asterisk-org-access-app[bot] merged 1 commit into
asterisk:masterfrom
InterLinked1:master-issue-900
Sep 25, 2024
Merged

astfd.c: Avoid calling fclose with NULL argument.#901
asterisk-org-access-app[bot] merged 1 commit into
asterisk:masterfrom
InterLinked1:master-issue-900

Conversation

@InterLinked1

Copy link
Copy Markdown
Contributor

Don't pass through a NULL argument to fclose, which is undefined behavior, and instead return -1 and set errno appropriately. This avoids a compiler warning with gcc 13.2.0.

Resolves: #900

@InterLinked1

Copy link
Copy Markdown
Contributor Author

cherry-pick-to: 18
cherry-pick-to: 20
cherry-pick-to: 21
cherry-pick-to: 22

jcolp
jcolp previously approved these changes Sep 25, 2024
@gtjoseph gtjoseph added the cherry-pick-test Trigger dry run of cherry-picks label Sep 25, 2024
@github-actions github-actions Bot added cherry-pick-testing-in-progress Cherry-Pick tests in progress and removed cherry-pick-test Trigger dry run of cherry-picks labels Sep 25, 2024
@gtjoseph

Copy link
Copy Markdown
Member

@InterLinked1 I was trying to determine if we should apply this patch to the certified branches but I can't seem to reproduce the issue. I'm using Ubuntu22 with gcc 13.2.0 and have tried with and without devmode and with and without COMPILE_DOUBLE. Can you reproduce the issue with a make NOISY_BUILD=yes and paste the full commands for [CCp] and [CCi]? I'd like to compare with what I'm seeing.

@seanbright seanbright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now that I look at both the POSIX docs and fclose(3) on Linux ("The behaviour of fclose() is undefined if the stream parameter is an illegal pointer"), setting errno here is probably not technically correct. I think returning -1 is fine though.

@github-actions github-actions Bot added cherry-pick-checks-passed Cherry-Pick checks passed and removed cherry-pick-testing-in-progress Cherry-Pick tests in progress labels Sep 25, 2024
@InterLinked1

InterLinked1 commented Sep 25, 2024

Copy link
Copy Markdown
Contributor Author

@InterLinked1 I was trying to determine if we should apply this patch to the certified branches but I can't seem to reproduce the issue. I'm using Ubuntu22 with gcc 13.2.0 and have tried with and without devmode and with and without COMPILE_DOUBLE. Can you reproduce the issue with a make NOISY_BUILD=yes and paste the full commands for [CCp] and [CCi]? I'd like to compare with what I'm seeing.

I believe I noticed this on an Ubuntu 24.04 build with gcc 13.2.0, but not Ubuntu 22.04 or any other distro. Builds were in devmode with DONT_OPTIMIZE as well (oh, and DEBUG_FD_LEAKS too, obviously). I think this might have happened only in a GitHub runner and not other Ubuntu images but I don't remember for sure now.

@InterLinked1

Copy link
Copy Markdown
Contributor Author

Now that I look at both the POSIX docs and fclose(3) on Linux ("The behaviour of fclose() is undefined if the stream parameter is an illegal pointer"), setting errno here is probably not technically correct. I think returning -1 is fine though.

Yeah, it's undefined behavior, but is there a reason it's better not to set errno here anyways? I was thinking there should be some way to indicate what the issue is, but both an assertion and even a log message seemed too harsh. It's only if we compile with DEBUG_FD_LEAKS, which aims to provide us more info about bad management of file descriptors, so setting errno seems like it would help with that goal... in theory.
In practice, nobody ever checks if fclose returns nonzero, so I suppose bothering to set errno doesn't help much anyways.

@seanbright

Copy link
Copy Markdown
Contributor

Yeah, it's undefined behavior, but is there a reason it's better not to set errno here anyways?

Sorry, I could have been more clear before. I don't necessarily object to errno being set, but EBADF has a specific meaning:

The file descriptor underlying stream is not valid.

I don't feel strongly either way, so if you'd prefer to keep it as-is that's fine.

@InterLinked1

InterLinked1 commented Sep 25, 2024

Copy link
Copy Markdown
Contributor Author

@InterLinked1 I was trying to determine if we should apply this patch to the certified branches but I can't seem to reproduce the issue. I'm using Ubuntu22 with gcc 13.2.0 and have tried with and without devmode and with and without COMPILE_DOUBLE. Can you reproduce the issue with a make NOISY_BUILD=yes and paste the full commands for [CCp] and [CCi]? I'd like to compare with what I'm seeing.

Actually, after thinking about this, I don't think the version of gcc matters. It has to do with when the nonnull attribute would've been added, which looks like glibc 2.38: bminor/glibc@71d9e0f

I'm guessing your Ubuntu system has something older, even 24.04 seems to come with 2.36 by default. My guess is glibc got upgraded on that system and the newer header file then triggered the build failure.

@InterLinked1

Copy link
Copy Markdown
Contributor Author

Yeah, it's undefined behavior, but is there a reason it's better not to set errno here anyways?

Sorry, I could have been more clear before. I don't necessarily object to errno being set, but EBADF has a specific meaning:

The file descriptor underlying stream is not valid.

I don't feel strongly either way, so if you'd prefer to keep it as-is that's fine.

Okay, thanks for clarifying... I guess "file descriptor underlying stream is not valid" is different from "stream itself is not valid".

I'll see if there's a more appropriate value to use or remove it otherwise.

Comment thread main/astfd.c Outdated
#undef fclose
int __ast_fdleak_fclose(FILE *ptr)
{
int __ast_fdleak_fclose(FILE *ptr) {

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.

Not sure why this happened, but I'm working on it

@gtjoseph

Copy link
Copy Markdown
Member

It's only if we compile with DEBUG_FD_LEAKS

Ah, that's what I was missing. Since it only happens with that flag we'll skip the certified branches.

Don't pass through a NULL argument to fclose, which is undefined
behavior, and instead return -1 and set errno appropriately. This
also avoids a compiler warning with glibc 2.38 and newer, as glibc
commit 71d9e0fe766a3c22a730995b9d024960970670af
added the nonnull attribute to this argument.

Resolves: asterisk#900
@gtjoseph gtjoseph added the cherry-pick-test Trigger dry run of cherry-picks label Sep 25, 2024
@github-actions github-actions Bot added cherry-pick-testing-in-progress Cherry-Pick tests in progress cherry-pick-checks-failed Cherry-Pick checks failed and removed cherry-pick-test Trigger dry run of cherry-picks cherry-pick-testing-in-progress Cherry-Pick tests in progress labels Sep 25, 2024
@asterisk-org-access-app asterisk-org-access-app Bot merged commit a988506 into asterisk:master Sep 25, 2024
@github-actions

Copy link
Copy Markdown

Successfully merged to branch master and cherry-picked to ["18","20","21","22"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: astfd.c: NULL pointer passed to fclose with nonnull attribute causes compilation failure

4 participants