Skip to content

api: Move close function in condition body #24277

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

Merged
merged 2 commits into from
Nov 7, 2024

Conversation

mi4r
Copy link

@mi4r mi4r commented Oct 15, 2024

The close is moved in the body of the error condition.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Does this PR introduce a user-facing change?

   None

The close is replaced in the body of the error condition.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Tigran Sogomonian <tsogomonian@astralinux.ru>
@github-actions github-actions bot added the kind/api-change Change to remote API; merits scrutiny label Oct 15, 2024
Copy link

Ephemeral COPR build failed. @containers/packit-build please check.

@mi4r mi4r changed the title api: Replace close function in condition body api: Move close function in condition body Oct 15, 2024
@baude baude added the No New Tests Allow PR to proceed without adding regression tests label Oct 15, 2024
@baude
Copy link
Member

baude commented Oct 15, 2024

approved not having tests

@baude
Copy link
Member

baude commented Oct 15, 2024

in looking at this PR, I saw the the error is not handled or logged. Then looking at just this file more broadly, sometimes it is and sometimes not. Some are deferred. If we have a consensus on how these should handled in the whole file, I would prefer that. I did not look close enough to verify that this is a possibility.

@mheon @Luap99 wdyt

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

I suggested this but it only makes sense if the error on tmpfile.Close() above source = tmpfile.Name() is checked and so that there was no errors during write.

Using defer in this context is not helpful because we want the tmpfile to be closed before we pass the file path to the import API.

@mheon
Copy link
Member

mheon commented Oct 15, 2024

I don't think there's value in logging the error from close. Maybe do an explicit _ = xyz.Close() so it's clear we are actually ignoring it.

@Luap99
Copy link
Member

Luap99 commented Oct 15, 2024

I don't think there's value in logging the error from close. Maybe do an explicit _ = xyz.Close() so it's clear we are actually ignoring it.

Read the close() man page, there absolute is a reason to check for errors (even when super unlikely)

A careful programmer will check the return value of close(), since it is quite possible that errors on a previous write(2) operation are reported only on the final close() that releases the open file description. Failing to
check the return value when closing a file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.

I know we ignore this pretty much elsewhere but here we are actually writing something potential large that my increase the odds of hitting such cases.

@baude baude added the 5.3 label Nov 4, 2024
@baude
Copy link
Member

baude commented Nov 4, 2024

This PR has been marked for 5.3 inclusion but it must be merged prior to Nov 5 for inclusion in 5.3 RC3. PRs not merged by that date are considered on a case by case basis for backporting.

@mi4r
Copy link
Author

mi4r commented Nov 7, 2024

Once again I would like to clarify what needs to be done. Do you want me to add if err = tmpfile.Close(); err != nill {log} or do you want me to fix this everywhere in the file, or would it be better to make this a separate pr and then accept this one without processing this error?

@Luap99
Copy link
Member

Luap99 commented Nov 7, 2024

I would want this

diff --git a/pkg/api/handlers/libpod/images.go b/pkg/api/handlers/libpod/images.go
index 6b5e5abb3..3e35a0dbb 100644
--- a/pkg/api/handlers/libpod/images.go
+++ b/pkg/api/handlers/libpod/images.go
@@ -410,7 +410,10 @@ func ImagesImport(w http.ResponseWriter, r *http.Request) {
                        return
                }
 
-               tmpfile.Close()
+               if err := tmpfile.Close(); err != nil {
+                       utils.Error(w, http.StatusInternalServerError, fmt.Errorf("unable to close tempfile: %w", err))
+                       return
+               }
                source = tmpfile.Name()
        }
 

ignoring the close error on the copy error path is fine but on the good path we should make sure we closed the file successfully to avoid io errors

Add error check during tmpfile close.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Tigran Sogomonian <tsogomonian@astralinux.ru>
@mheon
Copy link
Member

mheon commented Nov 7, 2024

LGTM

@rhatdan
Copy link
Member

rhatdan commented Nov 7, 2024

/approve
/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Nov 7, 2024
Copy link
Contributor

openshift-ci bot commented Nov 7, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mi4r, rhatdan

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 7, 2024
@rhatdan
Copy link
Member

rhatdan commented Nov 7, 2024

Thanks @mi4r

@openshift-merge-bot openshift-merge-bot bot merged commit f8ac02d into containers:main Nov 7, 2024
77 checks passed
@stale-locking-app stale-locking-app bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Feb 6, 2025
@stale-locking-app stale-locking-app bot locked as resolved and limited conversation to collaborators Feb 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
5.3 approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/api-change Change to remote API; merits scrutiny lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. No New Tests Allow PR to proceed without adding regression tests release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants