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

[pkg] fix deferring unsafe method "Close" on type "*os.File" #3548

Merged
merged 11 commits into from
Jul 20, 2022

Conversation

huof6829
Copy link
Contributor

Description

fix gosec error: Deferring unsafe method "Close" on type "*os.File"

Fixes #3532

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@huof6829 huof6829 requested a review from a team as a code owner July 14, 2022 08:52
@codecov
Copy link

codecov bot commented Jul 14, 2022

Codecov Report

Merging #3548 (4c8b2e9) into master (a20e489) will decrease coverage by 1.37%.
The diff coverage is 45.01%.

❗ Current head 4c8b2e9 differs from pull request most recent head fd151a7. Consider uploading reports for the commit fd151a7 to get more accurate results

@@            Coverage Diff             @@
##           master    #3548      +/-   ##
==========================================
- Coverage   75.43%   74.06%   -1.38%     
==========================================
  Files         247      253       +6     
  Lines       22845    23327     +482     
==========================================
+ Hits        17233    17277      +44     
- Misses       4685     5124     +439     
+ Partials      927      926       -1     
Impacted Files Coverage Δ
action/protocol/staking/read_state.go 15.38% <0.00%> (ø)
api/grpcserver.go 79.49% <ø> (+0.55%) ⬆️
blockchain/blockchain.go 0.89% <0.00%> (ø)
blockchain/pubsubmanager.go 0.00% <0.00%> (ø)
config/config.go 94.06% <ø> (+9.31%) ⬆️
consensus/scheme/rolldpos/rolldposctx.go 73.47% <0.00%> (-0.18%) ⬇️
ioctl/newcmd/alias/alias.go 0.00% <0.00%> (ø)
ioctl/newcmd/bc/bc.go 37.50% <0.00%> (+0.46%) ⬆️
ioctl/util/util.go 40.00% <0.00%> (ø)
ioctl/newcmd/action/action.go 20.62% <20.62%> (ø)
... and 46 more

if err != nil {
return err
}
defer f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

can you check why security scan considers f.Close() as "unsafe method"? does it mean the error is not checked? like you need to:

defer func() {
    return f.Close()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

error is skipped by defer f.Close(). it's unsafe. error should return.
defer func() error { return f.Close() }
or return f.Close() at the end of processing is both ok

Copy link
Member

Choose a reason for hiding this comment

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

i see, so should check/catch the error inside defer

if return f.Close() at the end, also need to handle f.Close() in L42 and L45, so defer is better

Copy link
Member

@Liuhaai Liuhaai Jul 15, 2022

Choose a reason for hiding this comment

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

although current code is acceptable, you could find a new way to improve it

Copy link
Contributor Author

@huof6829 huof6829 Jul 18, 2022

Choose a reason for hiding this comment

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

ok. modified

}
if err = f.Close(); err != nil {
log.S().Errorf("crashlog: close heap profile error: %v", err)
return
Copy link
Member

Choose a reason for hiding this comment

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

also move to defer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

ioctl/doc/doc.go Outdated
defer f.Close()

defer func() error {
return f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

this just run f.Close() and return from defer, it does not return the actual error, need to assign the error inside defer, like

defer func() {
     err = f.Close()
}

and return err at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

modified

@Liuhaai Liuhaai merged commit e209d4e into iotexproject:master Jul 20, 2022
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jul 20, 2022
…task/set-config

* 'task/set-config' of github.com:pocockn/iotex-core:
  [test] Disable workingset cache in the benchmark test (iotexproject#3558)
  [pkg] fix  deferring unsafe method "Close" on type "*os.File" (iotexproject#3548)
  [action] Refactor handleTransfer() (iotexproject#3557)
  Add MinVersion in tls.Config (iotexproject#3562)
  [ioctl] Modify file permission as 0600 (iotexproject#3563)
  [httputil] add ReadHeaderTimeout (iotexproject#3550)
  [staking] unexport namespace (iotexproject#3551)
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
@huof6829 huof6829 deleted the fix_gosec_recovery branch August 4, 2022 14:03
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jun 3, 2023
* upstream/master: (45 commits)
  Task: Get config cmd (iotexproject#3552)
  [ioctl] fix  Errors unhandled (iotexproject#3567)
  fix dir permission and file inclusion (iotexproject#3566)
  [test] Disable workingset cache in the benchmark test (iotexproject#3558)
  [pkg] fix  deferring unsafe method "Close" on type "*os.File" (iotexproject#3548)
  [action] Refactor handleTransfer() (iotexproject#3557)
  Add MinVersion in tls.Config (iotexproject#3562)
  [ioctl] Modify file permission as 0600 (iotexproject#3563)
  [httputil] add ReadHeaderTimeout (iotexproject#3550)
  [staking] unexport namespace (iotexproject#3551)
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
  [ioctl] Incorrect conversion between integer types (iotexproject#3522)
  [action] fix incorrect conversion between integer types (iotexproject#3545)
  [test] fix TestLoadBlockchainfromDB (iotexproject#3521)
  ...
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.

[ioctl] deferring unsafe method "Close" on type "*os.File"
5 participants