Author Archives: Enrico

避免function argument implicit conversion的方式

在std::string中,如果我們想要assign char 例如 std::string s = ‘A’; 這樣是不行的 必須要 std::string s(1, ‘A’); std::string的constructor 參考 http://www.cplusplus.com/reference/string/string/string/ 假想我們是string的設計者,加一個 string(char c); 會如何呢? 這樣看起來似乎完美方便,但其實在使用上多出了一些意想不到的情況 例如 std::string s = 123.4; 這樣就會走 string(char c);這條路而可以compile過,理由是Floating-integral conversions An rvalue of a floating point type can be converted to … Continue reading

Posted in C++ Language | Leave a comment

bool conversion test

後記:以下主要是展示template處理explicit bool operator的檢查所用的技巧,但其實真正要做cast檢查這樣檢查太瑣碎也不全面,可以透過std::is_constructible和std::is_convertible來處理,留待之後的文章整理 在C++中如果我們要知道物件轉成bool的值,可以直接用cast operator將型別轉成bool 例如 (bool)10.0 => true 但是像是 這段代碼顯然是無法通過編譯的(invalid cast from type ‘X’ to type ‘bool’),因為X沒有定義bool轉型的函式 假如我們想要設計一個function GetBool 可以檢查物件是否可轉成bool ─ 如果可以轉型就返回轉型的bool value, 如果不能轉型就返回false 因為使用的型別事先未知,所以勢必要使用template,根據使用者呼叫的參數來決定回傳值。因此很直覺的可以想到應該可以這樣做: 但是這樣做有個問題 ─ 那就是前面提到的,有些類別型別不支援bool轉型(沒有實作bool operator) ,必須在compile time要分兩個function,一個是可以轉型的,一個是不能轉型的,可是要讓呼叫者自己分辨傳入的參數顯然不太方便,這時候可以透過template metaprogramming的一些技巧讓compiler在compile time自動推導處理 這邊展示的GetBool作法參考了 gcc libstdc++-v3的is_convertible的概念實作 ─ 透過helper class推導”type” … Continue reading

Posted in C++ Language | Leave a comment

software library documentation

整理 https://www.divio.com/blog/documentation/ 對於software documentation的觀點 該篇blog文主要解釋軟體文件需要具備的幾個部分(從他的文章內容來看,這邊的軟體比較接近library) tutorial guide (how-to guide) explanation reference 一個好的文件,需要區分這四個部分。主要原因在這四個部分所針對的對象不同,目的也不同。 tutorial的重點在new comer,第一次接觸這個軟體/library的人要如何入門。文章提到這常也是軟體是否成功的關鍵-將new comer變成user的關鍵。如果使用的人覺得學習障礙很高,很可能就不用了。. “A bad or missing tutorial will prevent your project from acquiring new users.” guide是topic based,針對一個topic透過一系列的步驟完成,在文章中他用cook recipe來形容 explanation比較像是背景說明,在一般library文件我們常會看到design rationale可以算在這類 reference主要在information細節整理,可以想成是dictionary 另外文章也說明 區分project documentation和software documentation。有很多文件是算在project documentation:例如change log等 … Continue reading

Posted in General | Leave a comment

js function test if it is called by new operator

在javascript function中,如果要強制使用new,可以參考以下nodejs cluster/worker.js的範例 他這邊利用檢查this是否是instanceof Worker,在一般情況下 https://www.w3schools.com/js/js_this.asp In a function, this refers to the global object. In a function, in strict mode, this is undefined. 但是在new operator下 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new The new keyword does the following things:1. Creates a blank, plain JavaScript object;2. Links (sets the constructor of) this object to … Continue reading

Posted in nodejs | Leave a comment

debug tips

最近在協助debug一個crash的問題,紀錄一下debug的思路。 它的現象是當發soap https request時,在ssl handshake階段就出問題(出現有問題的封包內容或是程式crash) crash問題比較麻煩的是有時候發生,有時候正常,但一般情況,如果能夠經常重現,比較能夠快一點界定問題。常見的C/C++程式crash出現是在multithread lock問題導致資料的內部結構不一致或毀損。 crash在openssl內部,觀察comp_methods的值,發現內容怪怪的 ssl compression相關的資訊是在初始化就會做完的,第一個合理的猜測:在初始化的時候,可能有多thread同時做ssl init,導致global object的內部結構race condition,所以先在SSL_library_init裡面插printf看看。 這個版本使用的openssl是1.0.2,參考 https://www.openssl.org/docs/man1.0.2/man3/SSL_library_init.html 發現有多個thread呼叫此init函式多次,理論上init只需要一次就好,因此先確定是哪些地方直接或間接呼叫到。這邊用thread來找,當然在環境許可下,也可以直接設breakpoint看callstack 透過callstack先找到解決呼叫多次的問題, 發現ssl init多次是有個lib內部多次呼叫curl_global_init (當然也多次呼叫了curl_global_cleanup) https://curl.haxx.se/libcurl/c/curl_global_init.html 但事實上SSL_library_init被設計成可以呼叫多次(只是不能reentrant,或是thread同時呼叫),參考: https://libwebsockets.org/pipermail/libwebsockets/2016-May/002366.html 所以以上的狀況看起來不是crash問題的原因 SSL_library_init的開始和結尾地方加上printf,觀察thread id,發現其實 SSL_library_init 執行沒有overlap,也就是造成內部結構的問題不在這 前面提到有lib多次呼叫curl global cleanup看起來很可疑,curl_global_cleanup應該只被呼叫一次,而且是在程式最後結束時。因此第二個合理的猜測,應該是在cleanup處將openssl內部global object destroy。 果然看到在curl global cleanup裡面呼叫到了SSL_COMP_free_compression_methods,因此前面提到有個lib多次呼叫 curl_global_cleanup ,導致其他地方要使openssl function時因為compression methods內容有問題而crash。 … Continue reading

Posted in Tips | Leave a comment

Linux – (dash)開頭檔名的刪除

產生一個 “-v” 的檔名 echo hello > -v 或是 touch — -v 註 touch -v 不行 -v會被認為是option 列印內容 cat — -v cat -v 不行,剛好cat -v 是指–show-nonprinting 刪除 rm — -v rm -v不行 -v會被認為是option — 的功能主要是告知 option scanning可以結束,在一般unix程式,option的解析是透過getopt()來處理,在getopt() 單獨的 “–” 參數 代表後面不再當成option。當然,這也只適用於程式是用getopt()來解析arguments的情況。 … Continue reading

Posted in System Administration | Leave a comment

CentOS sshd log

sshd log是透過syslog來處理 ,參考 /etc/ssh/sshd_config設定 實際上記錄到哪一個檔案可查看rsyslog config /etc/rsyslog.conf 上面說明的是 *.info或是更高權限的log記錄到 /var/log/messages,但是mail, authpriv, cron當成no priority。 authpriv log到 /var/log/secure,所以sshd log在CentOS是記錄到 /var/log/secure 參考: https://www.rsyslog.com/doc/v8-stable/configuration/filters.html#selectors The keyword none stands for no priority of the given facility.

Posted in System Administration | Leave a comment

更新git

CentOS 7自帶的git 版本是1.8.3,如果要更新可以從source build https://github.com/git/git/releases make configure ./configure –prefix=/usr/localmakemake install 安裝完會裝至 /usr/bin/local, 但是如果直接打git –version發現還是舊版的,檢查一下env PATH 看起來搜索路徑是先從 /usr/local/bin沒錯 會發生這個原因主要是 bash cache了git路徑,避免每次使用都要重新在PATH搜索,此時只要rehash就好 另外需注意build環境若沒有curl dev files,build完git會無法使用https

Posted in System Administration | Leave a comment

CentOS 7 disable IPv6

nmtui工具可以設定是否要啟用ipv6,但是關掉後發現dmesg還是一直出現router advertisement failed to add default router的訊息,代表網卡的ipv6還是有在運作(發送router solicitation) 查看 /etc/sysconfig/network-scripts  下網卡的設定 IPV6INIT=no 但是透過 ip addr show dev enp1s0 還是可以看到ipv6的設定 參考上面說明: It is not helpful to add IPV6INIT=no parameter to interfaces that need to disable IPv6. Link local ipv6 can still be seen … Continue reading

Posted in System Administration | Leave a comment

exception safety

整理一下David Abrahams提出的exception safety幾個exception handling level 這在設計function時抱持這個概念非常重要, 因為對使用function的使用者來說, 常常在使用C++的function時,會擔心function失敗時的行為。 使用者角度處理exception handling時,不確定該function到底可能會丟出哪些exception,而造成困擾。 因為可能發生的exception不單是看呼叫的function本身, 該function內部呼叫的函式有可能也會產生exception。 如果真的不確定function可能會丟出哪些exception,最好的方式就是將exception往外傳。 當exception往外傳時,代表function流程中斷了,因此這時候就需要思考function exception safety gurantee。 他的概念有點類似資料庫transaction integrity,當transcation failure, 資料庫內容的一致性要達到什麼程度。例如ACID裡的C – consistency類似exception safety的Basic exception safety(invariants在function前後保持一致) Consistency ensures that a transaction can only bring the database from one valid state … Continue reading

Posted in C++ idioms | Leave a comment