contributed by < 93i7xo2
>
Source: lab-0
qtest
執行過程中的錯誤
qtest
再於命令提示列輸入 help
命令,會使 開啟 Address Sanitizer 觸發錯誤,應予以排除qtest
實作,撰寫獨立的終端機命令解譯器 (可建立新的 GitHub repository)qtest
實作的記憶體錯誤,並透過 Massif 視覺化 "simulation" 過程中的記憶體使用量,需要設計對應的實驗qtest
中實作 coroutine,並提供新的命令 web
,提供 web 伺服器功能,注意: web 伺服器運作過程中,qtest
仍可接受其他命令
FORK_COUNT
變更為 0
,並以 coroutine 取代原本的 fork 系統呼叫qtest.c
實作q_new
為了實現 時間複雜度,加上 count
、 last_element
紀錄。
q_free
q_insert_head
依據 CERN Common vulnerabilities guide for C programmers 的建議使用 strncpy
,以 strlen
取的字串大小作為 strncpy
參數。由於 strncpy
並不會加上 null character,需自行補上或以 calloc
替代 malloc
。
q_insert_tail
一開始實作 q_insert_tail
忽視掉佇列可能是空的可能性,透過 valgrind 發現無效寫入
於是加入空佇列的判斷
最終實作如下
q_remove_head
q_size
q_reverse
q_sort
qsort
,分割 list 時使用 slow/faster pointer 的方法qtest.c
使用 make valgrind
或是 valgrind ./qtest -f traces/trace-XX-ops.cmd
會出現 memory leak 的訊息:
trace 後發現這段配置的記憶體應該是經由 linenoise
掛上 exit handlers 並於程式正常結束時清除
按照 run_console
現有寫法,只有在 interactive mode 下才會呼叫到 linenoise
並在 runconsole
前設定 linenoise
想到的解法有兩個,在 qtest
結束前自行呼叫 linenoiseAtExit
,或是將 linenoiseHistoryLoad
搬移到接下來要執行 linenoise
的前面,確保記憶體一定被釋放,例如原始專案範例 example.c。
選擇用前者,畢竟在 non-interactive mode 去載入 linenoise
的設定就是一件匪夷所思的事情,修改後如下:
natsort 的行為是先以數字做排序,其次是字母,有別於 strcmp
逐一字元比較;數字的界定是以左右字母或特殊字元(-
)做區隔,不計中間的空格或 0。
測試腳本提供數種 testdata,然而實際用到的只有 test-dates、test-fractions、test-words。
來看 natsort 的程式碼,strnatcasecmp.c
提供兩個比較函式
基於考量採用 strnatcasecmp
當作比較函數,並在 qtest.c
引入 strnatcmp.h
、strnatcmp.h
,同時在console.c
的simulation
後方新增一個 option - natsort,用以切換 natsort/normal sort mode。
可惜在 console 輸入時是以空白做運算元分隔,下面字詞插入時會被誤判,故不列入測試。
run-test.bash
有這麼一段
根據 3.5.6 Process Substitution 的說明,diff
必須要將 <()
內的程式輸出讀進來,作用等同