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

Fix #539 「Texのファイルで落ちます。」 #553

Merged
merged 7 commits into from Oct 17, 2018

Conversation

ds14050
Copy link
Contributor

@ds14050 ds14050 commented Oct 15, 2018

Fix #539 「Texのファイルで落ちます。」

  • Fix: トピックの階層が連続しない場合に、トピックが抜けたり、未初期化変数にアクセスして落ちないように。
  • Fix: バッファ長より長いトピックタイトルで落ちないように。
  • Fix: 3桁以上になる連番で落ちないように。
  • Chg: 連番なしのトピックがあるときでも階層が正しくなるように。

詳細は Issue #539 を参照してください。

* Fix: トピックの階層が連続しない場合に、トピックが抜けたり、未初期化変数にアクセスして落ちないように。
* Fix: バッファ長より長いトピックタイトルで落ちないように。
* Fix: 3桁以上になる連番で落ちないように。
* Chg: 連番なしのトピックがあるときでも階層が正しくなるように。
Copy link
Member

@m-tmatma m-tmatma left a comment

Choose a reason for hiding this comment

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

パラメータやクラスの役割の説明コメントが欲しい

@m-tmatma m-tmatma added this to the next release milestone Oct 15, 2018
@m-tmatma m-tmatma added the 🐛bug🦋 ■バグ修正(Something isn't working) label Oct 15, 2018
@m-tmatma
Copy link
Member

テストデータをこの PR に貼り付けていただけますか?

@ds14050
Copy link
Contributor Author

ds14050 commented Oct 16, 2018

例外データは issue #539 の KENCHjp さん投稿のものを使いました。

正常データは同じ issue の #539 (comment) で投稿したマクロで作成しました。マクロが出力した正常データの例を、コメントとして javascript ファイルに含めています。

修正した同マクロで出力した、完全にランダムな例外データをここにアップロードします。random_topic.tex.txt


// '\' の後ろから、'{' を目印にタグとタイトルを見つける。
pTag = ++p;
p = (p = wmemchr(p, L'{', pLineEnd - p)) ? p : pLineEnd;
Copy link
Member

Choose a reason for hiding this comment

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

一行に詰め込みすぎです。

Copy link
Contributor

Choose a reason for hiding this comment

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

c#でよく見る書き方の移植版だと思えばそれほど複雑でもないような。

中括弧を探して見つかった位置を取得、見つからなければ行末位置を取得、というたけの処理なんで。

@beru さんあたりからツッコミ入るだろうと思って静観しておりました。

複雑さに関しては問題ないと思ってます。

たぶんこれ、バラすとかえって分かりづらくなる気がします。何がしたいか読み取りにくくなると思います。今だったら説明されれば分かるコードだと思うんです。

Copy link
Contributor Author

@ds14050 ds14050 Oct 16, 2018

Choose a reason for hiding this comment

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

wmemchr の戻り値に不満があるんです。NULL では使いにくい。STL ならこういう場合には end に相当する値を返してきます。

軽量言語なら ||or を使うところですし、gcc なら条件演算子の真の場合の値を省いた ?: を使って途中の代入が省けるので、まだ読みやすくなるんですが。

Copy link
Contributor

Choose a reason for hiding this comment

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

こう書くのはどうでしょうか?

while(p<pLineEnd&&*p!='{')++p;

Copy link
Contributor Author

@ds14050 ds14050 Oct 16, 2018

Choose a reason for hiding this comment

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

それは元の書き方です。手続きではなく、目的を書くために wmemchr を使いました。不本意な wmemchr の戻り値に加えて、|| 演算子が使えなかったのも誤算でしたが。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

元のコードが wcsstr を使っていたことでなんとなく <wchar.h> 縛りを課していましたが、異論百出で無理があったようです。STL の戻り値なら素直に書けるのですから、std::find で書き直しました。

Copy link
Contributor

Choose a reason for hiding this comment

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

std::find は目的に合致していて良いですね。C++アレルギーをこじらせてしまっているせいか自分には思い付きませんでした。


// トピック文字列を作成する(2)。タイトルをバッファに埋め込む。
const ptrdiff_t copyLen = t_min(szTopic + _countof(szTopic) - 1 - pTopicEnd, pTitleEnd - pTitle);
pTopicEnd = wmemcpy(pTopicEnd, pTitle, copyLen) + copyLen;
Copy link
Member

Choose a reason for hiding this comment

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

https://msdn.microsoft.com/ja-jp/library/dswaw1wk.aspx
によると wmemcpy dest の値 を返す、とあります。
単に pTopicEnd += copyLen;
とした方が簡潔になると思いますが、なぜこうしているのですか?

Copy link
Member

Choose a reason for hiding this comment

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

wmemcpy(pTopicEnd, pTitle, copyLen); とは別に分けて書くという意味です。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

書き込みと書き込みポインタの更新はアトミックな処理であって欲しいということです。せめて見た目上だけでも。

wmemcpy の戻り値に不満があります。書き込んだ後の位置であるか、書き込んだ長さを返してくれたら使いやすいんですが、dst そのものを返してきます。

Copy link
Member

Choose a reason for hiding this comment

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

書き込みと書き込みポインタの更新はアトミックな処理であって欲しいということです。せめて見た目上だけでも。

このように書いても特に何か変わることはなく、
単に、wmemcpy の戻り値の仕様なんだっけ?
第一引数を返すのか、pTopicEnd を直接更新すればいいじゃん、ということになるだけで
特にメリットはないように思います。

関数が処理したサイズとかを利用するのなら意味あるのですが、
この場合は単に読みにくくなるだけだと思います。

Copy link
Contributor

Choose a reason for hiding this comment

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

微妙ですが、ここは「記述を分ける」
に1票です。

書き込みと書き込みポインタの更新をアトミックにしたい意図はわかるんです。ioの場合はそちらが普通だとも思います。ただここはメモリ書き込みなので、Ioのように失敗の考慮が要らず、実際の処理はアトミックではないと思います。

既存コードはどうか?で行くと
行を分けてmemcpyの戻り値は捨ててるコードが多い気がします。

あと、個人的には分けた方が読みやすい気がします。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不本意だという wmemcpy の戻り値を知らなければ理解できないうえに読みにくいとあれば、拘る理由はありませんでした。分けます。気づくのが遅れて煩わせてしまいました。:pensive:

void CDocOutline::MakeTopicList_tex(CFuncInfoArr* pcFuncInfoArr)

/** アウトライン解析の補助クラス */
template<int Size>
Copy link
Member

Choose a reason for hiding this comment

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

この Size は何のサイズですか? タグの階層ですか?

Copy link
Member

Choose a reason for hiding this comment

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

TagHierarchy の要素数? 何のサイズなのかが変数名から自明になるようにしてほしいです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MakeTagProcessor を使えば意識する必要はありませんし、コンストラクタを見ればわかりますので、わかってください、としか。重要ではないのでそっけない名前を付けています。

とは、一度書きかけましたが、思ったより Size - 1 が頻出するわけではなかったので、HierarchyMax 変数と統合して HierarchyCount としておきました。

Copy link
Member

Choose a reason for hiding this comment

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

MakeTagProcessor を使えば意識する必要はありませんし、コンストラクタを見ればわかりますので、わかってください、としか。重要ではないのでそっけない名前を付けています。

コメントで説明するのでもいいのですが、
単体で見たときに、これ何? みたいなのは避けてほしいです。

Copy link
Contributor Author

@ds14050 ds14050 Oct 17, 2018

Choose a reason for hiding this comment

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

Size はそれのみで存在するわけではなく、コンストラクタのパラメータによって定義されるものですので、説明的な名前を付けることに意味がないと考えています。変数名を付ける者の意図も使途もなく、ただ定義によって決まるものなので、それ以外の意味がありません。

Size が使われる場所において読みやすくなるのはたしか。予め -1 した使いやすい HierarchyMax 変数を使うことで、Size の使用は控えていました。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TagHierarchy のサイズだというのは決まっているのだから、何を言っているのかと、自分で読んで思いました。

やはり、重要ではない、無視してよい、使われないものに、長い名前は似つかわしくないということです。

@ds14050 ds14050 force-pushed the FixTeXOutline branch 2 times, most recently from 5dd8ed0 to b66c740 Compare October 17, 2018 06:50
Copy link
Contributor

@beru beru left a comment

Choose a reason for hiding this comment

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

#539 (comment) に貼られているテキストのアウトライン解析をすると落ちる不具合が解消されているので PR としては問題無いと思います。

欲を言うとユニットテストが追加されていると良いと思いますが、ブーメランになるので独り言にしておきます。。ブツブツ…。

@ds14050 ds14050 merged commit d9e119f into sakura-editor:master Oct 17, 2018
HoppingTappy pushed a commit to HoppingTappy/sakura that referenced this pull request Jun 11, 2019
* Fix: トピックの階層が連続しない場合に、トピックが抜けたり、未初期化変数にアクセスして落ちないように。
* Fix: バッファ長より長いトピックタイトルで落ちないように。
* Fix: 3桁以上になる連番で落ちないように。
* Chg: 連番なしのトピックがあるときでも階層が正しくなるように。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛bug🦋 ■バグ修正(Something isn't working)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants