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

SonarQubeの静的解析で検出されたCFontAutoDeleterのバグを修正する #1799

Conversation

berryzplus
Copy link
Contributor

PR の目的

SonarCloudにバグだと言われているコードを修正し、見た目の品質を上げます。

カテゴリ

  • リファクタリング

PR の背景

SonarCloudにより以下の不具合が検出されています。
https://sonarcloud.io/project/issues?id=sakura-editor_sakura&issues=AWnxcxv5O9B58BDk-CU-&open=AWnxcxv5O9B58BDk-CU-

utif/window.h で定義されているクラス CFontAutoDeleter のコピーコンストラクタ・コピー代入演算子が欠落しているよ(≒コピー時に何か追加の処理が要るとすると処理が漏れるよ)という指摘です。

PR のメリット

  • SonarCloudにバグと検出される残念なコードが1つ消えます。

PR のデメリット (トレードオフとかあれば)

仕様・動作説明

アプリの仕様・機能に変更はありません。

CFontAutoDeleterの潜在不具合を修正します。

CFontAutoDeleterは似非スマートポインタです。
スマートポインタなので、インスタンス破棄時にフォントオブジェクトを削除しています。

コピーコンストラクタが実装されていなかったため、
インスタンスをコピーした場合に削除済みフォントオブジェクトを再度削除しようとする危険がありました。
削除済みのフォントハンドルをDeleteObjectした場合の結果は保証されないです。

対策として、コピーコンストラクタとコピー代入演算子を実装して、問題が起きないようにします。

同時に、GetFontメソッドを追加してクラスの単体テストが書けるようにします。
同時に、まったく利用されていない無駄なメンバ変数を削除します。

PR の影響範囲

コンボボックスを表示する各種ダイアログで設定したフォントを保持するために使用する似非スマートポインタに関する変更です。

テスト内容

このPRは、修正部分のテストを含んでいます。

テスト1

手順

関連 issue, PR

参考資料

CFontAutoDeleterは似非スマートポインタ。
コピーコンストラクタが未実装で、削除済みフォントを何度も削除しようとしてWindows GDIにダメージを与える危険があったのを修正する。
*/
TEST( CFontAutoDeleter, test )
{
CFontAutoDeleter deletor;
Copy link
Member

Choose a reason for hiding this comment

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

deleter? deletor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

どっちでしょう?(笑

Copy link
Member

@kengoide kengoide Feb 12, 2022

Choose a reason for hiding this comment

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

英語としては deleter です😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://wikidiff.com/deleter/deletor
https://english.stackexchange.com/questions/60660/deleter-or-deletor

deleterが名詞で、deletorは動詞だそうです。
ここの文脈は名詞だからerのが良さそうです。
いまやってる対応が終わったら直しときます。

#include <windowsx.h>
#include <Shlwapi.h>

#include <cstdlib>
Copy link
Member

Choose a reason for hiding this comment

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

不要なヘッダをインクルードしているように見えます。

  • tchar.h
  • shlwapi.h
  • cstdlib

細かい点ですので修正してほしいという要求ではありません。

@AppVeyorBot
Copy link

@AppVeyorBot
Copy link

@berryzplus
Copy link
Contributor Author

これを入れたらvisual studioのIDEでSonarCloudに表示されるのとほぼ同じ警告が表示されるようになりました。
https://www.sonarlint.org/visualstudio

@sonarcloud
Copy link

sonarcloud bot commented Feb 12, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@AppVeyorBot
Copy link

@berryzplus
Copy link
Contributor Author

@berryzplus berryzplus marked this pull request as ready for review February 12, 2022 18:44
Copy link
Member

@kengoide kengoide left a comment

Choose a reason for hiding this comment

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

問題ないように思います。

@berryzplus
Copy link
Contributor Author

レビューありがとうございます。マージしてしまいます。
何か不都合あれば別PRで対処していきたいと思います。

@berryzplus berryzplus merged commit 25c5e47 into sakura-editor:master Feb 13, 2022
@berryzplus berryzplus deleted the feature/refactoring_of_CFontAutoDeleter branch February 13, 2022 03:54
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.

None yet

3 participants